]> git.sesse.net Git - vlc/blob - modules/video_output/msw/directx.c
vout_directx: fix memleak.
[vlc] / modules / video_output / msw / directx.c
1 /*****************************************************************************
2  * directx.c: Windows DirectDraw video output
3  *****************************************************************************
4  * Copyright (C) 2001-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble:
26  *
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.
30  *
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.
35  *
36  *****************************************************************************/
37
38 #include <errno.h>                                                 /* ENOMEM */
39
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43
44 #include <vlc_common.h>
45 #include <vlc_plugin.h>
46 #include <vlc_vout.h>
47 #include <vlc_playlist.h>   /* needed for wallpaper */
48
49 #include <ddraw.h>
50 #include <commctrl.h>       /* ListView_(Get|Set)* */
51
52 #ifndef UNDER_CE
53 #   include <multimon.h>
54 #endif
55 #undef GetSystemMetrics
56
57 #ifndef MONITOR_DEFAULTTONEAREST
58 #   define MONITOR_DEFAULTTONEAREST 2
59 #endif
60
61 #include "vout.h"
62
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  *****************************************************************************/
69 struct picture_sys_t
70 {
71     LPDIRECTDRAWSURFACE2 p_surface;
72     LPDIRECTDRAWSURFACE2 p_front_surface;
73     DDSURFACEDESC        ddsd;
74 };
75
76 /*****************************************************************************
77  * DirectDraw GUIDs.
78  * Defining them here allows us to get rid of the dxguid library during
79  * the linking stage.
80  *****************************************************************************/
81 #include <initguid.h>
82 #undef GUID_EXT
83 #define GUID_EXT
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 );
86
87 /*****************************************************************************
88  * Local prototypes.
89  *****************************************************************************/
90 static int  OpenVideo  ( vlc_object_t * );
91 static void CloseVideo ( vlc_object_t * );
92
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 * );
99
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 * );
103
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 );
116
117 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *i_color );
118
119 void SwitchWallpaperMode( vout_thread_t *, bool );
120
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 * );
126
127 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
128                                  LPTSTR psz_drivername, VOID* p_context,
129                                  HMONITOR hmon );
130                                  
131 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
132                                   LPTSTR psz_drivername, VOID* p_context,
133                                   HMONITOR hmon );
134                                   
135 /*****************************************************************************
136  * Module descriptor
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." )
142
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." )
149
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)." )
154
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\"." )
160
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." )
166
167 static const char *const ppsz_dev[] = { "" };
168 static const char *const ppsz_dev_text[] = { N_("Default") };
169
170 vlc_module_begin ()
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,
175               true )
176     add_bool( "directx-use-sysmem", false, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT,
177               true )
178     add_bool( "directx-3buffering", true, NULL, TRIPLEBUF_TEXT,
179               TRIPLEBUF_LONGTEXT, true )
180
181     add_string( "directx-device", "", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
182                 true )
183         change_string_list( ppsz_dev, ppsz_dev_text, FindDevicesCallback )
184         change_action_add( FindDevicesCallback, N_("Refresh list") )
185
186     add_bool( "directx-wallpaper", false, NULL, WALLPAPER_TEXT, WALLPAPER_LONGTEXT,
187               true )
188
189     set_description( N_("DirectX (DirectDraw) video output") )
190     set_capability( "video output", 100 )
191     add_shortcut( "directx" )
192     set_callbacks( OpenVideo, CloseVideo )
193
194     /* FIXME: Hack to avoid unregistering our window class */
195     linked_with_a_crap_library_which_uses_atexit ()
196 vlc_module_end ()
197
198 #if 0 /* FIXME */
199     /* check if we registered a window class because we need to
200      * unregister it */
201     WNDCLASS wndclass;
202     if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
203         UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
204 #endif
205
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 )
212 {
213     vout_thread_t * p_vout = (vout_thread_t *)p_this;
214     vlc_value_t val;
215     HMODULE huser32;
216
217     /* Allocate structure */
218     p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
219     if( p_vout->p_sys == NULL )
220         return VLC_ENOMEM;
221     vlc_mutex_init( &p_vout->p_sys->lock );
222
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;
230
231     if( CommonInit( p_vout ) )
232         goto error;
233
234     /* */
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;
240
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") ) ) )
247     {
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") );
252     }
253
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 );
259
260     /* Initialise DirectDraw */
261     if( DirectXInitDDraw( p_vout ) )
262     {
263         msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
264         goto error;
265     }
266
267     /* Create the directx display */
268     if( DirectXCreateDisplay( p_vout ) )
269     {
270         msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
271         goto error;
272     }
273
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" );
281
282     return VLC_SUCCESS;
283
284  error:
285     CloseVideo( VLC_OBJECT(p_vout) );
286     return VLC_EGENERIC;
287 }
288
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 )
296 {
297     int i_chroma_backup;
298
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" );
304
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 )
310     {
311         msg_Err( p_vout, "cannot initialize DirectDraw" );
312         return VLC_EGENERIC;
313     }
314
315     /* Create the directx display */
316     if( !p_vout->p_sys->p_display &&
317         DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
318     {
319         msg_Err( p_vout, "cannot initialize DirectDraw" );
320         return VLC_EGENERIC;
321     }
322
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 );
331
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 */
336
337     /* Choose the chroma we will try first. */
338     switch( p_vout->render.i_chroma )
339     {
340         case VLC_CODEC_YUYV:
341             p_vout->output.i_chroma = VLC_CODEC_YUYV;
342             break;
343         case VLC_CODEC_UYVY:
344             p_vout->output.i_chroma = VLC_CODEC_UYVY;
345             break;
346         case VLC_CODEC_YVYU:
347             p_vout->output.i_chroma = VLC_CODEC_YVYU;
348             break;
349         case VLC_CODEC_I420:
350             p_vout->output.i_chroma = VLC_CODEC_I420;
351             break;
352         default:
353             msg_Dbg( p_vout, "use default chroma YV12 for render " \
354                              "chroma (%4.4s)",
355                              (char *)&p_vout->render.i_chroma);
356             p_vout->output.i_chroma = VLC_CODEC_YV12;
357             break;
358     }
359
360     NewPictureVec( p_vout, p_vout->p_picture );
361
362     i_chroma_backup = p_vout->output.i_chroma;
363
364     if( !I_OUTPUTPICTURES )
365     {
366         /* hmmm, it didn't work! Let's try commonly supported chromas */
367         if( p_vout->output.i_chroma != VLC_CODEC_I420 )
368         {
369             p_vout->output.i_chroma = VLC_CODEC_YV12;
370             NewPictureVec( p_vout, p_vout->p_picture );
371         }
372         if( !I_OUTPUTPICTURES )
373         {
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 );
377         }
378     }
379
380     if( !I_OUTPUTPICTURES )
381     {
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 );
387     }
388
389     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
390
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)";
397     else
398         psz_fallback = VOUT_TITLE " (software RGB DirectX output)";
399     EventThreadUpdateTitle( p_vout->p_sys->p_event, psz_fallback );
400
401     return VLC_SUCCESS;
402 }
403
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 )
411 {
412     FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
413
414     DirectXCloseDisplay( p_vout );
415     DirectXCloseDDraw( p_vout );
416
417     return;
418 }
419
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 )
426 {
427     vout_thread_t * p_vout = (vout_thread_t *)p_this;
428
429     /* Make sure the wallpaper is restored */
430     var_DelCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
431     SwitchWallpaperMode( p_vout, false );
432
433     CommonClean( p_vout );
434
435     vlc_mutex_destroy( &p_vout->p_sys->lock );
436     free( p_vout->p_sys );
437 }
438
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 )
446 {
447     CommonManage( p_vout );
448
449     /*
450      * Position Change
451      */
452     if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
453     {
454         p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
455
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 ) )
461         {
462             /* This will force the vout core to recreate the picture buffers */
463             p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
464         }
465     }
466
467     if( p_vout->p_sys->i_changes & DX_WALLPAPER_CHANGE )
468     {
469         SwitchWallpaperMode( p_vout, !p_vout->p_sys->b_wallpaper );
470         p_vout->p_sys->i_changes &= ~DX_WALLPAPER_CHANGE;
471         DirectDrawUpdateOverlay( p_vout );
472     }
473
474     return VLC_SUCCESS;
475 }
476
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 )
484 {
485     HRESULT dxresult;
486
487     if( (p_vout->p_sys->p_display == NULL) )
488     {
489         msg_Warn( p_vout, "no display!" );
490         return;
491     }
492
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 )
497     {
498         if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
499             p_vout->p_sys->b_using_overlay )
500             DirectDrawUpdateOverlay( p_vout );
501     }
502
503     if( !p_vout->p_sys->b_using_overlay )
504     {
505         DDBLTFX  ddbltfx;
506
507         /* We ask for the "NOTEARING" option */
508         memset( &ddbltfx, 0, sizeof(DDBLTFX) );
509         ddbltfx.dwSize = sizeof(DDBLTFX);
510         ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
511
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 )
519         {
520             msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
521             return;
522         }
523     }
524     else /* using overlay */
525     {
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 )
528         {
529             return;
530         }
531
532         dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
533                                              NULL, DDFLIP_WAIT );
534         if( dxresult != DD_OK )
535         {
536             msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
537         }
538
539         /* set currently displayed pic */
540         p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
541
542         /* Lock surface to get all the required info */
543         if( DirectXLockSurface( p_vout, p_pic ) )
544         {
545             /* AAARRGG */
546             msg_Warn( p_vout, "cannot lock surface" );
547             return;
548         }
549         DirectXUnlockSurface( p_vout, p_pic );
550     }
551 }
552
553 /*
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
556 ** display.
557 */
558
559 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
560 {
561     /* get initial picture rendered on overlay surface */
562     Display(p_vout, p_pic);
563
564     IDirectDraw_WaitForVerticalBlank(p_vout->p_sys->p_ddobject,
565             DDWAITVB_BLOCKBEGIN, NULL);
566
567     if( p_vout->p_sys->b_using_overlay )
568     {
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 );
572     }
573     /*
574     ** Video window is initially hidden, show it now since we got a
575     ** picture to show.
576     */
577     SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
578         SWP_ASYNCWINDOWPOS|
579         SWP_FRAMECHANGED|
580         SWP_SHOWWINDOW|
581         SWP_NOMOVE|
582         SWP_NOSIZE|
583         SWP_NOZORDER );
584
585     /* use and restores proper display function for further pictures */
586     p_vout->pf_display = Display;
587 }
588
589 /* following functions are local */
590
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,
599                                  HMONITOR hmon )
600 {
601     vout_thread_t *p_vout = (vout_thread_t *)p_context;
602     vlc_value_t device;
603
604     msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
605
606     if( hmon )
607     {
608         var_Get( p_vout, "directx-device", &device );
609
610         if( ( !device.psz_string || !*device.psz_string ) &&
611             hmon == p_vout->p_sys->hmonitor )
612         {
613             free( device.psz_string );
614         }
615         else if( strcmp( psz_drivername, device.psz_string ) == 0 )
616         {
617             MONITORINFO monitor_info;
618             monitor_info.cbSize = sizeof( MONITORINFO );
619
620             if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
621             {
622                 RECT rect;
623
624                 /* Move window to the right screen */
625                 GetWindowRect( p_vout->p_sys->hwnd, &rect );
626                 if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
627                 {
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 );
635                 }
636             }
637
638             p_vout->p_sys->hmonitor = hmon;
639             free( device.psz_string );
640         }
641         else
642         {
643             free( device.psz_string );
644             return TRUE; /* Keep enumerating */
645         }
646
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) );
651     }
652
653     return TRUE; /* Keep enumerating */
654 }
655
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 )
662 {
663     HRESULT dxresult;
664     HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
665     HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
666                                                 DWORD );
667     LPDIRECTDRAW p_ddobject;
668
669     msg_Dbg( p_vout, "DirectXInitDDraw" );
670
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 )
674     {
675         msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
676         goto error;
677     }
678
679     OurDirectDrawCreate =
680       (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
681                               _T("DirectDrawCreate") );
682     if( OurDirectDrawCreate == NULL )
683     {
684         msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
685         goto error;
686     }
687
688     OurDirectDrawEnumerateEx =
689       (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
690                               _T("DirectDrawEnumerateExW") );
691
692     if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
693     {
694         vlc_value_t device;
695
696         var_Get( p_vout, "directx-device", &device );
697         if( device.psz_string )
698         {
699             msg_Dbg( p_vout, "directx-device: %s", device.psz_string );
700             free( device.psz_string );
701         }
702
703         p_vout->p_sys->hmonitor =
704             p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
705                                               MONITOR_DEFAULTTONEAREST );
706
707         /* Enumerate displays */
708         OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
709                                   DDENUM_ATTACHEDSECONDARYDEVICES );
710     }
711
712     /* Initialize DirectDraw now */
713     dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
714                                     &p_ddobject, NULL );
715     if( dxresult != DD_OK )
716     {
717         msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
718         goto error;
719     }
720
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 )
727     {
728         msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
729         goto error;
730     }
731
732     /* Set DirectDraw Cooperative level, ie what control we want over Windows
733      * display */
734     dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
735                                                  NULL, DDSCL_NORMAL );
736     if( dxresult != DD_OK )
737     {
738         msg_Err( p_vout, "cannot set direct draw cooperative level" );
739         goto error;
740     }
741
742     /* Get the size of the current display device */
743     if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
744     {
745         MONITORINFO monitor_info;
746         monitor_info.cbSize = sizeof( MONITORINFO );
747         p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
748                                        &monitor_info );
749         p_vout->p_sys->rect_display = monitor_info.rcMonitor;
750     }
751     else
752     {
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);
757     }
758
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 );
764
765     /* Probe the capabilities of the hardware */
766     DirectXGetDDrawCaps( p_vout );
767
768     msg_Dbg( p_vout, "End DirectXInitDDraw" );
769     return VLC_SUCCESS;
770
771  error:
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;
778     return VLC_EGENERIC;
779 }
780
781 /*****************************************************************************
782  * DirectXCreateDisplay: create the DirectDraw display.
783  *****************************************************************************
784  * Create and initialize display according to preferences specified in the vout
785  * thread fields.
786  *****************************************************************************/
787 static int DirectXCreateDisplay( vout_thread_t *p_vout )
788 {
789     HRESULT              dxresult;
790     DDSURFACEDESC        ddsd;
791     LPDIRECTDRAWSURFACE  p_display;
792
793     msg_Dbg( p_vout, "DirectXCreateDisplay" );
794
795     /* Now get the primary surface. This surface is what you actually see
796      * on your screen */
797     memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
798     ddsd.dwSize = sizeof(DDSURFACEDESC);
799     ddsd.dwFlags = DDSD_CAPS;
800     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
801
802     dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
803                                            &ddsd, &p_display, NULL );
804     if( dxresult != DD_OK )
805     {
806         msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
807         return VLC_EGENERIC;
808     }
809
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 )
816     {
817         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
818                          "(error %li)", dxresult );
819         return VLC_EGENERIC;
820     }
821
822     /* The clipper will be used only in non-overlay mode */
823     DirectXCreateClipper( p_vout );
824
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 );
829
830     UpdateRects( p_vout, true );
831
832     return VLC_SUCCESS;
833 }
834
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 )
844 {
845     HRESULT dxresult;
846
847     msg_Dbg( p_vout, "DirectXCreateClipper" );
848
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 )
853     {
854         msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
855         goto error;
856     }
857
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 )
862     {
863         msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
864                           dxresult );
865         goto error;
866     }
867
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 )
872     {
873         msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
874                           dxresult );
875         goto error;
876     }
877
878     return VLC_SUCCESS;
879
880  error:
881     if( p_vout->p_sys->p_clipper )
882     {
883         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
884     }
885     p_vout->p_sys->p_clipper = NULL;
886     return VLC_EGENERIC;
887 }
888
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,
902                                  int i_backbuffers )
903 {
904     HRESULT dxresult;
905     LPDIRECTDRAWSURFACE p_surface;
906     DDSURFACEDESC ddsd;
907
908     /* Create the video surface */
909     if( b_overlay )
910     {
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 */
918
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
928                                 : 0 );
929         ddsd.dwHeight = p_vout->render.i_height;
930         ddsd.dwWidth = p_vout->render.i_width;
931         ddsd.dwBackBufferCount = i_backbuffers;
932
933         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
934                                                &ddsd, &p_surface, NULL );
935         if( dxresult != DD_OK )
936         {
937             *pp_surface_final = NULL;
938             return VLC_EGENERIC;
939         }
940     }
941
942     if( !b_overlay )
943     {
944         bool b_rgb_surface =
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 );
950
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;
958
959         if( p_vout->p_sys->b_use_sysmem )
960             ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
961         else
962             ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
963
964         if( !b_rgb_surface )
965         {
966             ddsd.dwFlags |= DDSD_PIXELFORMAT;
967             ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
968             ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
969         }
970
971         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
972                                                &ddsd, &p_surface, NULL );
973         if( dxresult != DD_OK )
974         {
975             *pp_surface_final = NULL;
976             return VLC_EGENERIC;
977         }
978     }
979
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 )
986     {
987         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
988                          "(error %li)", dxresult );
989         *pp_surface_final = NULL;
990         return VLC_EGENERIC;
991     }
992
993     if( b_overlay )
994     {
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 )
999         {
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;
1004         }
1005     }
1006
1007     return VLC_SUCCESS;
1008 }
1009
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 )
1018 {
1019     DDOVERLAYFX     ddofx;
1020     DWORD           dwFlags;
1021     HRESULT         dxresult;
1022     RECT            rect_src = p_vout->p_sys->rect_src_clipped;
1023     RECT            rect_dest = p_vout->p_sys->rect_dest_clipped;
1024
1025     if( !p_vout->p_sys->b_using_overlay ) return VLC_EGENERIC;
1026
1027     if( p_vout->p_sys->b_wallpaper )
1028     {
1029         unsigned i_x, i_y, i_width, i_height;
1030
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;
1035
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 );
1039
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;
1044     }
1045
1046     vlc_mutex_lock( &p_vout->p_sys->lock );
1047     if( p_vout->p_sys->p_current_surface == NULL )
1048     {
1049         vlc_mutex_unlock( &p_vout->p_sys->lock );
1050         return VLC_EGENERIC;
1051     }
1052
1053     /* The new window dimensions should already have been computed by the
1054      * caller of this function */
1055
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;
1061
1062     dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1063
1064     dxresult = IDirectDrawSurface2_UpdateOverlay(
1065                    p_vout->p_sys->p_current_surface,
1066                    &rect_src, p_vout->p_sys->p_display, &rect_dest,
1067                    dwFlags, &ddofx );
1068
1069     vlc_mutex_unlock( &p_vout->p_sys->lock );
1070
1071     if(dxresult != DD_OK)
1072     {
1073         msg_Warn( p_vout, "DirectDrawUpdateOverlay cannot move/resize overlay" );
1074         return VLC_EGENERIC;
1075     }
1076
1077     return VLC_SUCCESS;
1078 }
1079
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 )
1086 {
1087     msg_Dbg( p_vout, "DirectXCloseDDraw" );
1088     if( p_vout->p_sys->p_ddobject != NULL )
1089     {
1090         IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1091         p_vout->p_sys->p_ddobject = NULL;
1092     }
1093
1094     if( p_vout->p_sys->hddraw_dll != NULL )
1095     {
1096         FreeLibrary( p_vout->p_sys->hddraw_dll );
1097         p_vout->p_sys->hddraw_dll = NULL;
1098     }
1099
1100     free( p_vout->p_sys->p_display_driver );
1101     p_vout->p_sys->p_display_driver = NULL;
1102
1103     p_vout->p_sys->hmonitor = NULL;
1104 }
1105
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 )
1112 {
1113     msg_Dbg( p_vout, "DirectXCloseDisplay" );
1114
1115     if( p_vout->p_sys->p_clipper != NULL )
1116     {
1117         msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1118         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1119         p_vout->p_sys->p_clipper = NULL;
1120     }
1121
1122     if( p_vout->p_sys->p_display != NULL )
1123     {
1124         msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1125         IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1126         p_vout->p_sys->p_display = NULL;
1127     }
1128 }
1129
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 )
1137 {
1138     msg_Dbg( p_vout, "DirectXCloseSurface" );
1139     if( p_surface != NULL )
1140     {
1141         IDirectDrawSurface2_Release( p_surface );
1142     }
1143 }
1144
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 )
1152 {
1153     int i;
1154     int i_ret = VLC_SUCCESS;
1155     LPDIRECTDRAWSURFACE2 p_surface;
1156
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 );
1160
1161     I_OUTPUTPICTURES = 0;
1162
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 )
1170     {
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 */ );
1179
1180         if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1181         {
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 */ );
1187         }
1188
1189         if( i_ret == VLC_SUCCESS )
1190         {
1191             DDSCAPS dds_caps;
1192             picture_t front_pic;
1193             picture_sys_t front_pic_sys;
1194             front_pic.p_sys = &front_pic_sys;
1195
1196             /* Allocate internal structure */
1197             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1198             if( p_pic[0].p_sys == NULL )
1199             {
1200                 DirectXCloseSurface( p_vout, p_surface );
1201                 return VLC_ENOMEM;
1202             }
1203
1204             /* set front buffer */
1205             p_pic[0].p_sys->p_front_surface = p_surface;
1206
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 ) )
1213             {
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;
1217             }
1218
1219
1220             p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1221                 p_pic[0].p_sys->p_front_surface;
1222
1223             /* Reset the front buffer memory */
1224             if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1225             {
1226                 int i,j;
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 );
1232
1233                 DirectXUnlockSurface( p_vout, &front_pic );
1234             }
1235
1236             DirectDrawUpdateOverlay( p_vout );
1237             I_OUTPUTPICTURES = 1;
1238             msg_Dbg( p_vout, "YUV overlay created successfully" );
1239         }
1240     }
1241
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 )
1249     {
1250         if( p_vout->p_sys->b_hw_yuv )
1251         {
1252             DWORD i_codes;
1253             DWORD *pi_codes;
1254             bool b_result = false;
1255
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 )
1261             {
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 )
1265                 {
1266                     for( i = 0; i < (int)i_codes; i++ )
1267                     {
1268                         if( p_vout->output.i_chroma == pi_codes[i] )
1269                         {
1270                             b_result = true;
1271                             break;
1272                         }
1273                     }
1274                 }
1275                 free( pi_codes );
1276             }
1277
1278             if( b_result )
1279                 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1280                                               p_vout->output.i_chroma,
1281                                               0 /* no overlay */,
1282                                               0 /* no back buffers */ );
1283             else
1284                 p_vout->p_sys->b_hw_yuv = false;
1285         }
1286
1287         if( i_ret || !p_vout->p_sys->b_hw_yuv )
1288         {
1289             /* Our last choice is to use a plain RGB surface */
1290             DDPIXELFORMAT ddpfPixelFormat;
1291
1292             ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1293             IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1294                                                 &ddpfPixelFormat );
1295
1296             if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1297             {
1298                 switch( ddpfPixelFormat.dwRGBBitCount )
1299                 {
1300                 case 8:
1301                     p_vout->output.i_chroma = VLC_CODEC_RGB8;
1302                     p_vout->output.pf_setpalette = SetPalette;
1303                     break;
1304                 case 15:
1305                     p_vout->output.i_chroma = VLC_CODEC_RGB15;
1306                     break;
1307                 case 16:
1308                     p_vout->output.i_chroma = VLC_CODEC_RGB16;
1309                     break;
1310                 case 24:
1311                     p_vout->output.i_chroma = VLC_CODEC_RGB24;
1312                     break;
1313                 case 32:
1314                     p_vout->output.i_chroma = VLC_CODEC_RGB32;
1315                     break;
1316                 default:
1317                     msg_Err( p_vout, "unknown screen depth" );
1318                     return VLC_EGENERIC;
1319                 }
1320                 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1321                 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1322                 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1323             }
1324
1325             p_vout->p_sys->b_hw_yuv = 0;
1326
1327             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1328                                           p_vout->output.i_chroma,
1329                                           0 /* no overlay */,
1330                                           0 /* no back buffers */ );
1331
1332             if( i_ret && !p_vout->p_sys->b_use_sysmem )
1333             {
1334                 /* Give it a last try with b_use_sysmem enabled */
1335                 p_vout->p_sys->b_use_sysmem = 1;
1336
1337                 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1338                                               p_vout->output.i_chroma,
1339                                               0 /* no overlay */,
1340                                               0 /* no back buffers */ );
1341             }
1342         }
1343
1344         if( i_ret == VLC_SUCCESS )
1345         {
1346             /* Allocate internal structure */
1347             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1348             if( p_pic[0].p_sys == NULL )
1349             {
1350                 DirectXCloseSurface( p_vout, p_surface );
1351                 return VLC_ENOMEM;
1352             }
1353
1354             p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1355                 = p_surface;
1356
1357             I_OUTPUTPICTURES = 1;
1358
1359             msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1360                      (char *)&p_vout->output.i_chroma );
1361         }
1362     }
1363
1364
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++ )
1368     {
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];
1375
1376         if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1377         {
1378             /* AAARRGG */
1379             FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1380             I_OUTPUTPICTURES = 0;
1381             msg_Err( p_vout, "cannot lock surface" );
1382             return VLC_EGENERIC;
1383         }
1384         DirectXUnlockSurface( p_vout, &p_pic[i] );
1385     }
1386
1387     msg_Dbg( p_vout, "End NewPictureVec (%s)",
1388              I_OUTPUTPICTURES ? "succeeded" : "failed" );
1389
1390     return VLC_SUCCESS;
1391 }
1392
1393 /*****************************************************************************
1394  * FreePicture: destroy a picture vector allocated with NewPictureVec
1395  *****************************************************************************
1396  *
1397  *****************************************************************************/
1398 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1399                             int i_num_pics )
1400 {
1401     int i;
1402
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 );
1406
1407     for( i = 0; i < i_num_pics; i++ )
1408     {
1409         DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1410
1411         for( i = 0; i < i_num_pics; i++ )
1412         {
1413             free( p_pic[i].p_sys );
1414         }
1415     }
1416 }
1417
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 )
1424 {
1425     switch( p_vout->output.i_chroma )
1426     {
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 )
1437             {
1438                 case VLC_CODEC_RGB8:
1439                     p_pic->p->i_pixel_pitch = 1;
1440                     break;
1441                 case VLC_CODEC_RGB15:
1442                 case VLC_CODEC_RGB16:
1443                     p_pic->p->i_pixel_pitch = 2;
1444                     break;
1445                 case VLC_CODEC_RGB24:
1446                     p_pic->p->i_pixel_pitch = 3;
1447                     break;
1448                 case VLC_CODEC_RGB32:
1449                     p_pic->p->i_pixel_pitch = 4;
1450                     break;
1451                 default:
1452                     return VLC_EGENERIC;
1453             }
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;
1457             break;
1458
1459         case VLC_CODEC_YV12:
1460
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;
1465
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;
1473
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;
1482
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;
1491
1492             p_pic->i_planes = 3;
1493             break;
1494
1495         case VLC_CODEC_I420:
1496
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;
1504
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;
1513
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;
1522
1523             p_pic->i_planes = 3;
1524             break;
1525
1526         case VLC_CODEC_UYVY:
1527         case VLC_CODEC_YUYV:
1528
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;
1536
1537             p_pic->i_planes = 1;
1538             break;
1539
1540         default:
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;
1546     }
1547
1548     return VLC_SUCCESS;
1549 }
1550
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 )
1558 {
1559     DDCAPS ddcaps;
1560     HRESULT dxresult;
1561
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,
1567                                      &ddcaps, NULL );
1568     if(dxresult != DD_OK )
1569     {
1570         msg_Warn( p_vout, "cannot get caps" );
1571     }
1572     else
1573     {
1574         bool bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1575              bHasColorKey, bCanStretch, bCanBltFourcc,
1576              bAlignBoundarySrc, bAlignBoundaryDest,
1577              bAlignSizeSrc, bAlignSizeDest;
1578
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;
1599
1600         msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1601                          "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1602                          "bltfourcc=%i",
1603                          bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1604                          bHasColorKey, bCanStretch, bCanBltFourcc );
1605
1606         if( bAlignBoundarySrc || bAlignBoundaryDest ||
1607             bAlignSizeSrc || bAlignSizeDest )
1608         {
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;
1617
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 );
1625         }
1626
1627         /* Don't ask for troubles */
1628         if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1629     }
1630 }
1631
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 )
1639 {
1640     HRESULT dxresult;
1641
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,
1648                                          NULL );
1649     if( dxresult != DD_OK )
1650     {
1651         if( dxresult == DDERR_INVALIDPARAMS )
1652         {
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,
1657                                              DDLOCK_WAIT, NULL);
1658         }
1659         if( dxresult == DDERR_SURFACELOST )
1660         {
1661             /* Your surface can be lost so be sure
1662              * to check this and restore it if needed */
1663
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 );
1668             else
1669                 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1670
1671             dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1672                                                  &p_pic->p_sys->ddsd,
1673                                                  DDLOCK_WAIT, NULL);
1674 #ifndef NDEBUG
1675             if( dxresult == DDERR_SURFACELOST )
1676                 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1677 #endif
1678         }
1679         if( dxresult != DD_OK )
1680         {
1681             return VLC_EGENERIC;
1682         }
1683     }
1684
1685     /* Now we have a pointer to the surface memory, we can update our picture
1686      * structure. */
1687     if( UpdatePictureStruct( p_vout, p_pic )
1688         != VLC_SUCCESS )
1689     {
1690         DirectXUnlockSurface( p_vout, p_pic );
1691         return VLC_EGENERIC;
1692     }
1693     else
1694         return VLC_SUCCESS;
1695 }
1696
1697 /*****************************************************************************
1698  * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1699  *****************************************************************************/
1700 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1701 {
1702     VLC_UNUSED( p_vout );
1703
1704     /* Unlock the Surface */
1705     if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1706         return VLC_SUCCESS;
1707     else
1708         return VLC_EGENERIC;
1709 }
1710
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 )
1715 {
1716     DDSURFACEDESC ddsd;
1717     HRESULT dxresult;
1718     COLORREF i_rgb = 0;
1719     uint32_t i_pixel_backup;
1720     HDC hdc;
1721
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;
1726
1727     i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1728
1729     switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1730     {
1731     case 4:
1732         *(uint8_t *)ddsd.lpSurface = *pi_color | (*pi_color << 4);
1733         break;
1734     case 8:
1735         *(uint8_t *)ddsd.lpSurface = *pi_color;
1736         break;
1737     case 15:
1738     case 16:
1739         *(uint16_t *)ddsd.lpSurface = *pi_color;
1740         break;
1741     case 24:
1742         /* Seems to be problematic so we'll just put black as the colorkey */
1743         *pi_color = 0;
1744     default:
1745         *(uint32_t *)ddsd.lpSurface = *pi_color;
1746         break;
1747     }
1748
1749     IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1750
1751     if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1752     {
1753         i_rgb = GetPixel( hdc, 0, 0 );
1754         IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1755     }
1756
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;
1761
1762     *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1763
1764     IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1765
1766     return i_rgb;
1767 }
1768
1769 /*****************************************************************************
1770  * A few toolbox functions
1771  *****************************************************************************/
1772 void SwitchWallpaperMode( vout_thread_t *p_vout, bool b_on )
1773 {
1774     HWND hwnd;
1775
1776     if( p_vout->p_sys->b_wallpaper == b_on ) return; /* Nothing to do */
1777
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 );
1781     if( !hwnd )
1782     {
1783         msg_Warn( p_vout, "couldn't find \"SysListView32\" window, "
1784                   "wallpaper mode not supported" );
1785         return;
1786     }
1787
1788     p_vout->p_sys->b_wallpaper = b_on;
1789
1790     msg_Dbg( p_vout, "wallpaper mode %s", b_on ? "enabled" : "disabled" );
1791
1792     if( p_vout->p_sys->b_wallpaper )
1793     {
1794         p_vout->p_sys->color_bkg = ListView_GetBkColor( hwnd );
1795         p_vout->p_sys->color_bkgtxt = ListView_GetTextBkColor( hwnd );
1796
1797         ListView_SetBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1798         ListView_SetTextBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1799     }
1800     else if( hwnd )
1801     {
1802         ListView_SetBkColor( hwnd, p_vout->p_sys->color_bkg );
1803         ListView_SetTextBkColor( hwnd, p_vout->p_sys->color_bkgtxt );
1804     }
1805
1806     /* Update desktop */
1807     InvalidateRect( hwnd, NULL, TRUE );
1808     UpdateWindow( hwnd );
1809 }
1810
1811 /*****************************************************************************
1812  * config variable callback
1813  *****************************************************************************/
1814 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
1815                                   LPTSTR psz_drivername, VOID* p_context,
1816                                   HMONITOR hmon )
1817 {
1818     VLC_UNUSED( p_guid ); VLC_UNUSED( psz_desc ); VLC_UNUSED( hmon );
1819
1820     module_config_t *p_item = (module_config_t *)p_context;
1821
1822     p_item->ppsz_list =
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 *) );
1828
1829     p_item->ppsz_list[p_item->i_list] = strdup( psz_drivername );
1830     p_item->ppsz_list_text[p_item->i_list] = NULL;
1831     p_item->i_list++;
1832     p_item->ppsz_list[p_item->i_list] = NULL;
1833     p_item->ppsz_list_text[p_item->i_list] = NULL;
1834
1835     return TRUE; /* Keep enumerating */
1836 }
1837
1838 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1839                                vlc_value_t newval, vlc_value_t oldval, void *d)
1840 {
1841     VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( d );
1842
1843     HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
1844                                                 DWORD );
1845     HINSTANCE hddraw_dll;
1846
1847     module_config_t *p_item;
1848     int i;
1849
1850     p_item = config_FindConfig( p_this, psz_name );
1851     if( !p_item ) return VLC_SUCCESS;
1852
1853     /* Clear-up the current list */
1854     if( p_item->i_list )
1855     {
1856         /* Keep the first entry */
1857         for( i = 1; i < p_item->i_list; i++ )
1858         {
1859             free( p_item->ppsz_list[i] );
1860             free( p_item->ppsz_list_text[i] );
1861         }
1862         /* TODO: Remove when no more needed */
1863         p_item->ppsz_list[i] = NULL;
1864         p_item->ppsz_list_text[i] = NULL;
1865     }
1866     p_item->i_list = 1;
1867
1868     /* Load direct draw DLL */
1869     hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
1870     if( hddraw_dll == NULL ) return VLC_SUCCESS;
1871
1872     OurDirectDrawEnumerateEx =
1873       (void *)GetProcAddress( hddraw_dll, _T("DirectDrawEnumerateExW") );
1874
1875     if( OurDirectDrawEnumerateEx )
1876     {
1877         /* Enumerate displays */
1878         OurDirectDrawEnumerateEx( DirectXEnumCallback2, p_item,
1879                                   DDENUM_ATTACHEDSECONDARYDEVICES );
1880     }
1881
1882     FreeLibrary( hddraw_dll );
1883
1884     /* Signal change to the interface */
1885     p_item->b_dirty = true;
1886
1887     return VLC_SUCCESS;
1888 }
1889
1890 static int WallpaperCallback( vlc_object_t *p_this, char const *psz_cmd,
1891                               vlc_value_t oldval, vlc_value_t newval,
1892                               void *p_data )
1893 {
1894     VLC_UNUSED( psz_cmd ); VLC_UNUSED( oldval ); VLC_UNUSED( p_data );
1895     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1896
1897     if( (newval.b_bool && !p_vout->p_sys->b_wallpaper) ||
1898         (!newval.b_bool && p_vout->p_sys->b_wallpaper) )
1899     {
1900         playlist_t *p_playlist = pl_Hold( p_vout );
1901
1902         if( p_playlist )
1903         {
1904             /* Modify playlist as well because the vout might have to be
1905              * restarted */
1906             var_Create( p_playlist, "directx-wallpaper", VLC_VAR_BOOL );
1907             var_Set( p_playlist, "directx-wallpaper", newval );
1908             pl_Release( p_vout );
1909         }
1910
1911         p_vout->p_sys->i_changes |= DX_WALLPAPER_CHANGE;
1912     }
1913
1914     return VLC_SUCCESS;
1915 }
1916
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 )
1922 {
1923     VLC_UNUSED( red ); VLC_UNUSED( green );VLC_UNUSED( blue );
1924     msg_Err( p_vout, "FIXME: SetPalette unimplemented" );
1925 }