]> git.sesse.net Git - vlc/blob - modules/video_output/directx/directx.c
4146b012bcb2b5664724f06910b0978c94261c79
[vlc] / modules / video_output / directx / directx.c
1 /*****************************************************************************
2  * vout.c: Windows DirectX video output display method
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: directx.c,v 1.28 2003/12/13 14:01:51 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 #include <errno.h>                                                 /* ENOMEM */
38 #include <stdlib.h>                                                /* free() */
39 #include <string.h>                                            /* strerror() */
40
41 #include <vlc/vlc.h>
42 #include <vlc/intf.h>
43 #include <vlc/vout.h>
44
45 #include <windows.h>
46 #include <ddraw.h>
47
48 #include <multimon.h>
49 #undef GetSystemMetrics
50
51 #ifndef MONITOR_DEFAULTTONEAREST
52 #   define MONITOR_DEFAULTTONEAREST 2
53 #endif
54
55 #include "vout.h"
56
57 /*****************************************************************************
58  * DirectDraw GUIDs.
59  * Defining them here allows us to get rid of the dxguid library during
60  * the linking stage.
61  *****************************************************************************/
62 #include <initguid.h>
63 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
64 DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
65
66 /*****************************************************************************
67  * Local prototypes.
68  *****************************************************************************/
69 static int  OpenVideo  ( vlc_object_t * );
70 static void CloseVideo ( vlc_object_t * );
71
72 static int  Init      ( vout_thread_t * );
73 static void End       ( vout_thread_t * );
74 static int  Manage    ( vout_thread_t * );
75 static void Display   ( vout_thread_t *, picture_t * );
76
77 static int  NewPictureVec  ( vout_thread_t *, picture_t *, int );
78 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
79 static int  UpdatePictureStruct( vout_thread_t *, picture_t *, int );
80
81 static int  DirectXInitDDraw      ( vout_thread_t *p_vout );
82 static void DirectXCloseDDraw     ( vout_thread_t *p_vout );
83 static int  DirectXCreateDisplay  ( vout_thread_t *p_vout );
84 static void DirectXCloseDisplay   ( vout_thread_t *p_vout );
85 static int  DirectXCreateSurface  ( vout_thread_t *p_vout,
86                                     LPDIRECTDRAWSURFACE2 *, int, int, int );
87 static void DirectXCloseSurface   ( vout_thread_t *p_vout,
88                                     LPDIRECTDRAWSURFACE2 );
89 static int  DirectXCreateClipper  ( vout_thread_t *p_vout );
90 static void DirectXGetDDrawCaps   ( vout_thread_t *p_vout );
91 static int  DirectXLockSurface    ( vout_thread_t *p_vout, picture_t *p_pic );
92 static int  DirectXUnlockSurface  ( vout_thread_t *p_vout, picture_t *p_pic );
93
94 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t i_color );
95
96 /* Object variables callbacks */
97 static int OnTopCallback( vlc_object_t *, char const *,
98                           vlc_value_t, vlc_value_t, void * );
99
100 /*****************************************************************************
101  * Module descriptor
102  *****************************************************************************/
103 #define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
104 #define HW_YUV_LONGTEXT N_( \
105     "Try to use hardware acceleration for YUV->RGB conversions. " \
106     "This option doesn't have any effect when using overlays." )
107 #define SYSMEM_TEXT N_("Use video buffers in system memory")
108 #define SYSMEM_LONGTEXT N_( \
109     "Create video buffers in system memory instead of video memory. This " \
110     "isn't recommended as usually using video memory allows to benefit from " \
111     "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
112     "This option doesn't have any effect when using overlays." )
113 #define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
114 #define TRIPLEBUF_LONGTEXT N_( \
115     "Try to use triple bufferring when using YUV overlays. That results in " \
116     "much better video quality (no flickering)." )
117
118 vlc_module_begin();
119     add_category_hint( N_("Video"), NULL, VLC_FALSE );
120     add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT, VLC_TRUE );
121     add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT, VLC_TRUE );
122     add_bool( "directx-3buffering", 1, NULL, TRIPLEBUF_TEXT, TRIPLEBUF_LONGTEXT, VLC_TRUE );
123     set_description( _("DirectX video output") );
124     set_capability( "video output", 100 );
125     add_shortcut( "directx" );
126     set_callbacks( OpenVideo, CloseVideo );
127 vlc_module_end();
128
129 #if 0 /* FIXME */
130     /* check if we registered a window class because we need to
131      * unregister it */
132     WNDCLASS wndclass;
133     if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
134         UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
135 #endif
136
137 /*****************************************************************************
138  * OpenVideo: allocate DirectX video thread output method
139  *****************************************************************************
140  * This function allocates and initialize the DirectX vout method.
141  *****************************************************************************/
142 static int OpenVideo( vlc_object_t *p_this )
143 {
144     vout_thread_t * p_vout = (vout_thread_t *)p_this;
145     vlc_value_t val, text;
146     HMODULE huser32;
147
148     /* Allocate structure */
149     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
150     if( p_vout->p_sys == NULL )
151     {
152         msg_Err( p_vout, "out of memory" );
153         return VLC_ENOMEM;
154     }
155
156     /* Initialisations */
157     p_vout->pf_init = Init;
158     p_vout->pf_end = End;
159     p_vout->pf_manage = Manage;
160     p_vout->pf_render = NULL;
161     p_vout->pf_display = Display;
162
163     p_vout->p_sys->p_ddobject = NULL;
164     p_vout->p_sys->p_display = NULL;
165     p_vout->p_sys->p_current_surface = NULL;
166     p_vout->p_sys->p_clipper = NULL;
167     p_vout->p_sys->hwnd = NULL;
168     p_vout->p_sys->hparent = NULL;
169     p_vout->p_sys->i_changes = 0;
170
171     /* Multimonitor stuff */
172     p_vout->p_sys->hmonitor = NULL;
173     p_vout->p_sys->p_display_driver = NULL;
174     p_vout->p_sys->MonitorFromWindow = NULL;
175     p_vout->p_sys->GetMonitorInfo = NULL;
176     if( (huser32 = GetModuleHandle( "USER32" ) ) )
177     {
178         p_vout->p_sys->MonitorFromWindow =
179             GetProcAddress( huser32, "MonitorFromWindow" );
180         p_vout->p_sys->GetMonitorInfo =
181             GetProcAddress( huser32, "GetMonitorInfoA" );
182     }
183
184     var_Create( p_vout, "overlay", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
185     var_Create( p_vout, "directx-use-sysmem", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
186     var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
187     var_Create( p_vout, "directx-3buffering", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
188
189     p_vout->p_sys->b_cursor_hidden = 0;
190     p_vout->p_sys->i_lastmoved = mdate();
191
192     /* Set main window's size */
193     p_vout->p_sys->i_window_width = p_vout->i_window_width;
194     p_vout->p_sys->i_window_height = p_vout->i_window_height;
195
196     /* Create the DirectXEventThread, this thread is created by us to isolate
197      * the Win32 PeekMessage function calls. We want to do this because
198      * Windows can stay blocked inside this call for a long time, and when
199      * this happens it thus blocks vlc's video_output thread.
200      * DirectXEventThread will take care of the creation of the video
201      * window (because PeekMessage has to be called from the same thread which
202      * created the window). */
203     msg_Dbg( p_vout, "creating DirectXEventThread" );
204     p_vout->p_sys->p_event =
205         vlc_object_create( p_vout, sizeof(event_thread_t) );
206     p_vout->p_sys->p_event->p_vout = p_vout;
207     if( vlc_thread_create( p_vout->p_sys->p_event, "DirectX Events Thread",
208                            DirectXEventThread, 0, 1 ) )
209     {
210         msg_Err( p_vout, "cannot create DirectXEventThread" );
211         vlc_object_destroy( p_vout->p_sys->p_event );
212         p_vout->p_sys->p_event = NULL;
213         goto error;
214     }
215
216     if( p_vout->p_sys->p_event->b_error )
217     {
218         msg_Err( p_vout, "DirectXEventThread failed" );
219         goto error;
220     }
221
222     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
223
224     msg_Dbg( p_vout, "DirectXEventThread running" );
225
226     /* Initialise DirectDraw */
227     if( DirectXInitDDraw( p_vout ) )
228     {
229         msg_Err( p_vout, "cannot initialize DirectDraw" );
230         goto error;
231     }
232
233     /* Create the directx display */
234     if( DirectXCreateDisplay( p_vout ) )
235     {
236         msg_Err( p_vout, "cannot initialize DirectDraw" );
237         goto error;
238     }
239
240     /* Add a variable to indicate if the window should be on top of others */
241     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
242     text.psz_string = _("Always on top");
243     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
244     var_Get( p_vout, "video-on-top", &val );
245     p_vout->p_sys->b_on_top_change = val.b_bool; 
246     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
247
248     return VLC_SUCCESS;
249
250  error:
251     CloseVideo( VLC_OBJECT(p_vout) );
252     return VLC_EGENERIC;
253 }
254
255 /*****************************************************************************
256  * Init: initialize DirectX video thread output method
257  *****************************************************************************
258  * This function create the directx surfaces needed by the output thread.
259  * It is called at the beginning of the thread.
260  *****************************************************************************/
261 static int Init( vout_thread_t *p_vout )
262 {
263     int i_chroma_backup;
264     vlc_value_t val;
265
266     /* Get a few default parameters */
267     var_Get( p_vout, "overlay", &val );
268     p_vout->p_sys->b_using_overlay = val.b_bool;
269     var_Get( p_vout, "directx-use-sysmem", &val );
270     p_vout->p_sys->b_use_sysmem = val.b_bool;
271     var_Get( p_vout, "directx-hw-yuv", &val );
272     p_vout->p_sys->b_hw_yuv = val.b_bool;
273     var_Get( p_vout, "directx-3buffering", &val );
274     p_vout->p_sys->b_3buf_overlay = val.b_bool;
275
276     /* Initialise DirectDraw if not already done.
277      * We do this here because on multi-monitor systems we may have to
278      * re-create the directdraw surfaces. */
279     if( !p_vout->p_sys->p_ddobject &&
280         DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
281     {
282         msg_Err( p_vout, "cannot initialize DirectDraw" );
283         return VLC_EGENERIC;
284     }
285
286     /* Create the directx display */
287     if( !p_vout->p_sys->p_display &&
288         DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
289     {
290         msg_Err( p_vout, "cannot initialize DirectDraw" );
291         return VLC_EGENERIC;
292     }
293
294     /* Initialize the output structure.
295      * Since DirectDraw can do rescaling for us, stick to the default
296      * coordinates and aspect. */
297     p_vout->output.i_width  = p_vout->render.i_width;
298     p_vout->output.i_height = p_vout->render.i_height;
299     p_vout->output.i_aspect = p_vout->render.i_aspect;
300
301 #define MAX_DIRECTBUFFERS 1
302     /* Right now we use only 1 directbuffer because we don't want the
303      * video decoder to decode directly into direct buffers as they are
304      * created into video memory and video memory is _really_ slow */
305
306     /* Choose the chroma we will try first. */
307     switch( p_vout->render.i_chroma )
308     {
309         case VLC_FOURCC('Y','U','Y','2'):
310         case VLC_FOURCC('Y','U','N','V'):
311             p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
312             break;
313         case VLC_FOURCC('U','Y','V','Y'):
314         case VLC_FOURCC('U','Y','N','V'):
315         case VLC_FOURCC('Y','4','2','2'):
316             p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
317             break;
318         case VLC_FOURCC('Y','V','Y','U'):
319             p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
320             break;
321         default:
322             p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
323             break;
324     }
325
326     NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
327
328     i_chroma_backup = p_vout->output.i_chroma;
329
330     if( !I_OUTPUTPICTURES )
331     {
332         /* hmmm, it didn't work! Let's try commonly supported chromas */
333         if( p_vout->output.i_chroma != VLC_FOURCC('Y','V','1','2') )
334         {
335             p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
336             NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
337         }
338         if( !I_OUTPUTPICTURES )
339         {
340             /* hmmm, it still didn't work! Let's try another one */
341             p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
342             NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
343         }
344     }
345
346     if( !I_OUTPUTPICTURES )
347     {
348         /* If it still didn't work then don't try to use an overlay */
349         p_vout->output.i_chroma = i_chroma_backup;
350         p_vout->p_sys->b_using_overlay = 0;
351         NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
352     }
353
354     /* Change the window title bar text */
355     if( p_vout->p_sys->hparent )
356         ; /* Do nothing */
357     else if( p_vout->p_sys->b_using_overlay )
358         SetWindowText( p_vout->p_sys->hwnd,
359                        VOUT_TITLE " (hardware YUV overlay DirectX output)" );
360     else if( p_vout->p_sys->b_hw_yuv )
361         SetWindowText( p_vout->p_sys->hwnd,
362                        VOUT_TITLE " (hardware YUV DirectX output)" );
363     else SetWindowText( p_vout->p_sys->hwnd,
364                         VOUT_TITLE " (software RGB DirectX output)" );
365
366     return VLC_SUCCESS;
367 }
368
369 /*****************************************************************************
370  * End: terminate Sys video thread output method
371  *****************************************************************************
372  * Terminate an output method created by Create.
373  * It is called at the end of the thread.
374  *****************************************************************************/
375 static void End( vout_thread_t *p_vout )
376 {
377     FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
378
379     DirectXCloseDisplay( p_vout );
380     DirectXCloseDDraw( p_vout );
381
382     return;
383 }
384
385 /*****************************************************************************
386  * CloseVideo: destroy Sys video thread output method
387  *****************************************************************************
388  * Terminate an output method created by Create
389  *****************************************************************************/
390 static void CloseVideo( vlc_object_t *p_this )
391 {
392     vout_thread_t * p_vout = (vout_thread_t *)p_this;
393
394     msg_Dbg( p_vout, "CloseVideo" );
395
396     var_Destroy( p_vout, "video-on-top" );
397
398     if( p_vout->p_sys->p_event )
399     {
400         vlc_object_detach( p_vout->p_sys->p_event );
401
402         /* Kill DirectXEventThread */
403         p_vout->p_sys->p_event->b_die = VLC_TRUE;
404
405         /* we need to be sure DirectXEventThread won't stay stuck in
406          * GetMessage, so we send a fake message */
407         if( p_vout->p_sys->hwnd )
408         {
409             PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
410         }
411
412         vlc_thread_join( p_vout->p_sys->p_event );
413         vlc_object_destroy( p_vout->p_sys->p_event );
414     }
415
416     if( p_vout->p_sys )
417     {
418         free( p_vout->p_sys );
419         p_vout->p_sys = NULL;
420     }
421 }
422
423 /*****************************************************************************
424  * Manage: handle Sys events
425  *****************************************************************************
426  * This function should be called regularly by the video output thread.
427  * It returns a non null value if an error occured.
428  *****************************************************************************/
429 static int Manage( vout_thread_t *p_vout )
430 {
431     WINDOWPLACEMENT window_placement;
432
433     /* If we do not control our window, we check for geometry changes
434      * ourselves because the parent might not send us its events. */
435     if( p_vout->p_sys->hparent )
436     {
437         DirectXUpdateRects( p_vout, VLC_FALSE );
438     }
439
440     /*
441      * Position Change
442      */
443     if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
444     {
445         if( p_vout->p_sys->b_using_overlay )
446             DirectXUpdateOverlay( p_vout );
447
448         /* Check if we are still on the same monitor */
449         if( p_vout->p_sys->MonitorFromWindow &&
450             p_vout->p_sys->hmonitor !=
451                 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
452                                                   MONITOR_DEFAULTTONEAREST ) )
453         {
454             /* This will force the vout core to recreate the picture buffers */
455             p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
456         }
457
458         p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
459     }
460
461     /* We used to call the Win32 PeekMessage function here to read the window
462      * messages. But since window can stay blocked into this function for a
463      * long time (for example when you move your window on the screen), I
464      * decided to isolate PeekMessage in another thread. */
465
466     /*
467      * Fullscreen change
468      */
469     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
470         || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
471     {
472         vlc_value_t val;
473
474         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
475
476         /* We need to switch between Maximized and Normal sized window */
477         window_placement.length = sizeof(WINDOWPLACEMENT);
478         GetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
479         if( p_vout->b_fullscreen )
480         {
481             /* Maximized window */
482             window_placement.showCmd = SW_SHOWMAXIMIZED;
483             /* Change window style, no borders and no title bar */
484             SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, WS_CLIPCHILDREN );
485
486         }
487         else
488         {
489             /* Normal window */
490             window_placement.showCmd = SW_SHOWNORMAL;
491             /* Change window style, borders and title bar */
492             SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, WS_CLIPCHILDREN |
493                            WS_OVERLAPPEDWINDOW | WS_SIZEBOX | WS_VISIBLE );
494         }
495
496         SetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
497
498         /* Update the object variable and trigger callback */
499         val.b_bool = p_vout->b_fullscreen;
500         var_Set( p_vout, "fullscreen", val );
501
502         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
503         p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
504     }
505
506     /*
507      * Pointer change
508      */
509     if( (!p_vout->p_sys->b_cursor_hidden) &&
510         ( (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 ) )
511     {
512         /* Hide the mouse automatically */
513         if( p_vout->p_sys->hwnd != p_vout->p_sys->hparent )
514         {
515             p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
516             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
517         }
518     }
519
520     /*
521      * "Always on top" status change
522      */
523     if( p_vout->p_sys->b_on_top_change )
524     {
525         vlc_value_t val;
526         HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
527
528         var_Get( p_vout, "video-on-top", &val );
529  
530         /* Set the window on top if necessary */
531         if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
532                            & WS_EX_TOPMOST ) )
533         {
534             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
535                            MF_BYCOMMAND | MFS_CHECKED );
536             SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
537                           SWP_NOSIZE | SWP_NOMOVE );
538         }
539         else
540         /* The window shouldn't be on top */
541         if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
542                            & WS_EX_TOPMOST ) )
543         {
544             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
545                            MF_BYCOMMAND | MFS_UNCHECKED );
546             SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
547                           SWP_NOSIZE | SWP_NOMOVE );
548         }
549
550         p_vout->p_sys->b_on_top_change = VLC_FALSE;
551     }
552
553     /* Check if the event thread is still running */
554     if( p_vout->p_sys->p_event->b_die )
555     {
556         return VLC_EGENERIC; /* exit */
557     }
558
559     return VLC_SUCCESS;
560 }
561
562 /*****************************************************************************
563  * Display: displays previously rendered output
564  *****************************************************************************
565  * This function sends the currently rendered image to the display, wait until
566  * it is displayed and switch the two rendering buffers, preparing next frame.
567  *****************************************************************************/
568 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
569 {
570     HRESULT dxresult;
571
572     if( (p_vout->p_sys->p_display == NULL) )
573     {
574         msg_Warn( p_vout, "no display!!" );
575         return;
576     }
577
578     /* Our surface can be lost so be sure to check this
579      * and restore it if need be */
580     if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
581         == DDERR_SURFACELOST )
582     {
583         if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
584             p_vout->p_sys->b_using_overlay )
585             DirectXUpdateOverlay( p_vout );
586     }
587
588     if( !p_vout->p_sys->b_using_overlay )
589     {
590         DDBLTFX  ddbltfx;
591
592         /* We ask for the "NOTEARING" option */
593         memset( &ddbltfx, 0, sizeof(DDBLTFX) );
594         ddbltfx.dwSize = sizeof(DDBLTFX);
595         ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
596
597         /* Blit video surface to display */
598         dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
599                                             &p_vout->p_sys->rect_dest_clipped,
600                                             p_pic->p_sys->p_surface,
601                                             &p_vout->p_sys->rect_src_clipped,
602                                             DDBLT_ASYNC, &ddbltfx );
603         if( dxresult != DD_OK )
604         {
605             msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
606             return;
607         }
608
609     }
610     else /* using overlay */
611     {
612         /* Flip the overlay buffers if we are using back buffers */
613         if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
614         {
615             return;
616         }
617
618         dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
619                                              NULL, DDFLIP_WAIT );
620         if( dxresult != DD_OK )
621         {
622             msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
623         }
624
625         /* set currently displayed pic */
626         p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
627
628         /* Lock surface to get all the required info */
629         if( DirectXLockSurface( p_vout, p_pic ) )
630         {
631             /* AAARRGG */
632             msg_Warn( p_vout, "cannot lock surface" );
633             return;
634         }
635         DirectXUnlockSurface( p_vout, p_pic );
636     }
637 }
638
639 /* following functions are local */
640
641 /*****************************************************************************
642  * DirectXEnumCallback: Device enumeration
643  *****************************************************************************
644  * This callback function is called by DirectDraw once for each 
645  * available DirectDraw device.
646  *****************************************************************************/
647 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
648                                  LPTSTR psz_drivername, VOID* p_context,
649                                  HMONITOR hmon )
650 {
651     vout_thread_t *p_vout = (vout_thread_t *)p_context;
652     msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
653
654     if( hmon && hmon == p_vout->p_sys->hmonitor )
655     {
656         msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
657         p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
658         if( p_vout->p_sys->p_display_driver )
659             memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
660     }
661
662     return TRUE; /* Keep enumerating */
663 }
664
665 /*****************************************************************************
666  * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
667  *****************************************************************************
668  * This function initialise and allocate resources for DirectDraw.
669  *****************************************************************************/
670 static int DirectXInitDDraw( vout_thread_t *p_vout )
671 {
672     HRESULT dxresult;
673     HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
674     HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
675                                                 DWORD );
676     LPDIRECTDRAW p_ddobject;
677
678     msg_Dbg( p_vout, "DirectXInitDDraw" );
679
680     /* Load direct draw DLL */
681     p_vout->p_sys->hddraw_dll = LoadLibrary("DDRAW.DLL");
682     if( p_vout->p_sys->hddraw_dll == NULL )
683     {
684         msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
685         goto error;
686     }
687
688     OurDirectDrawCreate =
689       (void *)GetProcAddress(p_vout->p_sys->hddraw_dll, "DirectDrawCreate");
690     if( OurDirectDrawCreate == NULL )
691     {
692         msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
693         goto error;
694     }
695
696     OurDirectDrawEnumerateEx =
697       (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
698                               "DirectDrawEnumerateExA" );
699
700     if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
701     {
702         p_vout->p_sys->hmonitor =
703             p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
704                                               MONITOR_DEFAULTTONEAREST );
705
706         /* Enumerate displays */
707         OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout, 
708                                   DDENUM_ATTACHEDSECONDARYDEVICES );
709     }
710
711     /* Initialize DirectDraw now */
712     dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
713                                     &p_ddobject, NULL );
714     if( dxresult != DD_OK )
715     {
716         msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
717         goto error;
718     }
719
720     /* Get the IDirectDraw2 interface */
721     dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
722                                         (LPVOID *)&p_vout->p_sys->p_ddobject );
723     /* Release the unused interface */
724     IDirectDraw_Release( p_ddobject );
725     if( dxresult != DD_OK )
726     {
727         msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
728         goto error;
729     }
730
731     /* Set DirectDraw Cooperative level, ie what control we want over Windows
732      * display */
733     dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
734                                                  NULL, DDSCL_NORMAL );
735     if( dxresult != DD_OK )
736     {
737         msg_Err( p_vout, "cannot set direct draw cooperative level" );
738         goto error;
739     }
740
741     /* Get the size of the current display device */
742     if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
743     {
744         MONITORINFO monitor_info;
745         monitor_info.cbSize = sizeof( MONITORINFO );
746         p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
747                                        &monitor_info );
748         p_vout->p_sys->rect_display = monitor_info.rcMonitor;
749     }
750     else
751     {
752         p_vout->p_sys->rect_display.left = 0;
753         p_vout->p_sys->rect_display.top = 0;
754         p_vout->p_sys->rect_display.right  = GetSystemMetrics(SM_CXSCREEN);
755         p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
756     }
757
758     msg_Dbg( p_vout, "screen dimensions %ix%i",
759                       p_vout->p_sys->rect_display.right,
760                       p_vout->p_sys->rect_display.bottom );
761
762     /* Probe the capabilities of the hardware */
763     DirectXGetDDrawCaps( p_vout );
764
765     msg_Dbg( p_vout, "End DirectXInitDDraw" );
766     return VLC_SUCCESS;
767
768  error:
769     if( p_vout->p_sys->p_ddobject )
770         IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
771     if( p_vout->p_sys->hddraw_dll )
772         FreeLibrary( p_vout->p_sys->hddraw_dll );
773     p_vout->p_sys->hddraw_dll = NULL;
774     p_vout->p_sys->p_ddobject = NULL;
775     return VLC_EGENERIC;
776 }
777
778 /*****************************************************************************
779  * DirectXCreateDisplay: create the DirectDraw display.
780  *****************************************************************************
781  * Create and initialize display according to preferences specified in the vout
782  * thread fields.
783  *****************************************************************************/
784 static int DirectXCreateDisplay( vout_thread_t *p_vout )
785 {
786     HRESULT              dxresult;
787     DDSURFACEDESC        ddsd;
788     LPDIRECTDRAWSURFACE  p_display;
789
790     msg_Dbg( p_vout, "DirectXCreateDisplay" );
791
792     /* Now get the primary surface. This surface is what you actually see
793      * on your screen */
794     memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
795     ddsd.dwSize = sizeof(DDSURFACEDESC);
796     ddsd.dwFlags = DDSD_CAPS;
797     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
798
799     dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
800                                            &ddsd, &p_display, NULL );
801     if( dxresult != DD_OK )
802     {
803         msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
804         return VLC_EGENERIC;
805     }
806
807     dxresult = IDirectDrawSurface_QueryInterface( p_display,
808                                          &IID_IDirectDrawSurface2,
809                                          (LPVOID *)&p_vout->p_sys->p_display );
810     /* Release the old interface */
811     IDirectDrawSurface_Release( p_display );
812     if ( dxresult != DD_OK )
813     {
814         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
815                          "(error %li)", dxresult );
816         return VLC_EGENERIC;
817     }
818
819     /* The clipper will be used only in non-overlay mode */
820     DirectXCreateClipper( p_vout );
821
822     /* Make sure the colorkey will be painted */
823     p_vout->p_sys->i_colorkey = 1;
824     p_vout->p_sys->i_rgb_colorkey =
825         DirectXFindColorkey( p_vout, p_vout->p_sys->i_colorkey );
826
827     /* Create the actual brush */
828     SetClassLong( p_vout->p_sys->hvideownd, GCL_HBRBACKGROUND,
829                   (LONG)CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey ) );
830     InvalidateRect( p_vout->p_sys->hvideownd, NULL, TRUE );
831     DirectXUpdateRects( p_vout, VLC_TRUE );
832
833     return VLC_SUCCESS;
834 }
835
836 /*****************************************************************************
837  * DirectXCreateClipper: Create a clipper that will be used when blitting the
838  *                       RGB surface to the main display.
839  *****************************************************************************
840  * This clipper prevents us to modify by mistake anything on the screen
841  * which doesn't belong to our window. For example when a part of our video
842  * window is hidden by another window.
843  *****************************************************************************/
844 static int DirectXCreateClipper( vout_thread_t *p_vout )
845 {
846     HRESULT dxresult;
847
848     msg_Dbg( p_vout, "DirectXCreateClipper" );
849
850     /* Create the clipper */
851     dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
852                                            &p_vout->p_sys->p_clipper, NULL );
853     if( dxresult != DD_OK )
854     {
855         msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
856         goto error;
857     }
858
859     /* Associate the clipper to the window */
860     dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
861                                            p_vout->p_sys->hvideownd );
862     if( dxresult != DD_OK )
863     {
864         msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
865                           dxresult );
866         goto error;
867     }
868
869     /* associate the clipper with the surface */
870     dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
871                                              p_vout->p_sys->p_clipper);
872     if( dxresult != DD_OK )
873     {
874         msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
875                           dxresult );
876         goto error;
877     }
878
879     return VLC_SUCCESS;
880
881  error:
882     if( p_vout->p_sys->p_clipper )
883     {
884         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
885     }
886     p_vout->p_sys->p_clipper = NULL;
887     return VLC_EGENERIC;
888 }
889
890 /*****************************************************************************
891  * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
892  *****************************************************************************
893  * The best method of display is with an YUV overlay because the YUV->RGB
894  * conversion is done in hardware.
895  * You can also create a plain RGB surface.
896  * ( Maybe we could also try an RGB overlay surface, which could have hardware
897  * scaling and which would also be faster in window mode because you don't
898  * need to do any blitting to the main display...)
899  *****************************************************************************/
900 static int DirectXCreateSurface( vout_thread_t *p_vout,
901                                  LPDIRECTDRAWSURFACE2 *pp_surface_final,
902                                  int i_chroma, int b_overlay,
903                                  int i_backbuffers )
904 {
905     HRESULT dxresult;
906     LPDIRECTDRAWSURFACE p_surface;
907     DDSURFACEDESC ddsd;
908
909     /* Create the video surface */
910     if( b_overlay )
911     {
912         /* Now try to create the YUV overlay surface.
913          * This overlay will be displayed on top of the primary surface.
914          * A color key is used to determine whether or not the overlay will be
915          * displayed, ie the overlay will be displayed in place of the primary
916          * surface wherever the primary surface will have this color.
917          * The video window has been created with a background of this color so
918          * the overlay will be only displayed on top of this window */
919
920         memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
921         ddsd.dwSize = sizeof(DDSURFACEDESC);
922         ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
923         ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
924         ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
925         ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
926         ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
927         ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
928         ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
929                                 : 0 );
930         ddsd.dwHeight = p_vout->render.i_height;
931         ddsd.dwWidth = p_vout->render.i_width;
932         ddsd.dwBackBufferCount = i_backbuffers;
933
934         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
935                                                &ddsd, &p_surface, NULL );
936         if( dxresult != DD_OK )
937         {
938             *pp_surface_final = NULL;
939             return VLC_EGENERIC;
940         }
941     }
942
943     if( !b_overlay )
944     {
945         vlc_bool_t b_rgb_surface =
946             ( i_chroma == VLC_FOURCC('R','G','B','2') )
947           || ( i_chroma == VLC_FOURCC('R','V','1','5') )
948            || ( i_chroma == VLC_FOURCC('R','V','1','6') )
949             || ( i_chroma == VLC_FOURCC('R','V','2','4') )
950              || ( i_chroma == VLC_FOURCC('R','V','3','2') );
951
952         memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
953         ddsd.dwSize = sizeof(DDSURFACEDESC);
954         ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
955         ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
956         ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
957         ddsd.dwHeight = p_vout->render.i_height;
958         ddsd.dwWidth = p_vout->render.i_width;
959
960         if( p_vout->p_sys->b_use_sysmem )
961             ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
962         else
963             ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
964
965         if( !b_rgb_surface )
966         {
967             ddsd.dwFlags |= DDSD_PIXELFORMAT;
968             ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
969             ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
970         }
971
972         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
973                                                &ddsd, &p_surface, NULL );
974         if( dxresult != DD_OK )
975         {
976             *pp_surface_final = NULL;
977             return VLC_EGENERIC;
978         }
979     }
980
981     /* Now that the surface is created, try to get a newer DirectX interface */
982     dxresult = IDirectDrawSurface_QueryInterface( p_surface,
983                                      &IID_IDirectDrawSurface2,
984                                      (LPVOID *)pp_surface_final );
985     IDirectDrawSurface_Release( p_surface );    /* Release the old interface */
986     if ( dxresult != DD_OK )
987     {
988         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
989                          "(error %li)", dxresult );
990         *pp_surface_final = NULL;
991         return VLC_EGENERIC;
992     }
993
994     if( b_overlay )
995     {
996         /* Check the overlay is useable as some graphics cards allow creating
997          * several overlays but only one can be used at one time. */
998         p_vout->p_sys->p_current_surface = *pp_surface_final;
999         if( DirectXUpdateOverlay( p_vout ) != VLC_SUCCESS )
1000         {
1001             IDirectDrawSurface2_Release( *pp_surface_final );
1002             *pp_surface_final = NULL;
1003             msg_Err( p_vout, "overlay unuseable (might already be in use)" );
1004             return VLC_EGENERIC;
1005         }
1006     }
1007
1008     return VLC_SUCCESS;
1009 }
1010
1011 /*****************************************************************************
1012  * DirectXUpdateOverlay: Move or resize overlay surface on video display.
1013  *****************************************************************************
1014  * This function is used to move or resize an overlay surface on the screen.
1015  * Ususally the overlay is moved by the user and thus, by a move or resize
1016  * event (in Manage).
1017  *****************************************************************************/
1018 int DirectXUpdateOverlay( vout_thread_t *p_vout )
1019 {
1020     DDOVERLAYFX     ddofx;
1021     DWORD           dwFlags;
1022     HRESULT         dxresult;
1023
1024     if( p_vout->p_sys->p_current_surface == NULL ||
1025         !p_vout->p_sys->b_using_overlay )
1026         return VLC_EGENERIC;
1027
1028     /* The new window dimensions should already have been computed by the
1029      * caller of this function */
1030
1031     /* Position and show the overlay */
1032     memset(&ddofx, 0, sizeof(DDOVERLAYFX));
1033     ddofx.dwSize = sizeof(DDOVERLAYFX);
1034     ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
1035     ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
1036
1037     dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1038
1039     dxresult = IDirectDrawSurface2_UpdateOverlay(
1040                                          p_vout->p_sys->p_current_surface,
1041                                          &p_vout->p_sys->rect_src_clipped,
1042                                          p_vout->p_sys->p_display,
1043                                          &p_vout->p_sys->rect_dest_clipped,
1044                                          dwFlags, &ddofx );
1045     if(dxresult != DD_OK)
1046     {
1047         msg_Warn( p_vout,
1048                   "DirectXUpdateOverlay cannot move or resize overlay" );
1049         return VLC_EGENERIC;
1050     }
1051
1052     return VLC_SUCCESS;
1053 }
1054
1055 /*****************************************************************************
1056  * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
1057  *****************************************************************************
1058  * This function returns all resources allocated by DirectXInitDDraw.
1059  *****************************************************************************/
1060 static void DirectXCloseDDraw( vout_thread_t *p_vout )
1061 {
1062     msg_Dbg( p_vout, "DirectXCloseDDraw" );
1063     if( p_vout->p_sys->p_ddobject != NULL )
1064     {
1065         IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1066         p_vout->p_sys->p_ddobject = NULL;
1067     }
1068
1069     if( p_vout->p_sys->hddraw_dll != NULL )
1070     {
1071         FreeLibrary( p_vout->p_sys->hddraw_dll );
1072         p_vout->p_sys->hddraw_dll = NULL;
1073     }
1074
1075     if( p_vout->p_sys->p_display_driver != NULL )
1076     {
1077         free( p_vout->p_sys->p_display_driver );
1078         p_vout->p_sys->p_display_driver = NULL;
1079     }
1080
1081     p_vout->p_sys->hmonitor = NULL;
1082 }
1083
1084 /*****************************************************************************
1085  * DirectXCloseDisplay: close and reset the DirectX display device
1086  *****************************************************************************
1087  * This function returns all resources allocated by DirectXCreateDisplay.
1088  *****************************************************************************/
1089 static void DirectXCloseDisplay( vout_thread_t *p_vout )
1090 {
1091     msg_Dbg( p_vout, "DirectXCloseDisplay" );
1092
1093     if( p_vout->p_sys->p_clipper != NULL )
1094     {
1095         msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1096         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1097         p_vout->p_sys->p_clipper = NULL;
1098     }
1099
1100     if( p_vout->p_sys->p_display != NULL )
1101     {
1102         msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1103         IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1104         p_vout->p_sys->p_display = NULL;
1105     }
1106 }
1107
1108 /*****************************************************************************
1109  * DirectXCloseSurface: close the YUV overlay or RGB surface.
1110  *****************************************************************************
1111  * This function returns all resources allocated for the surface.
1112  *****************************************************************************/
1113 static void DirectXCloseSurface( vout_thread_t *p_vout,
1114                                  LPDIRECTDRAWSURFACE2 p_surface )
1115 {
1116     msg_Dbg( p_vout, "DirectXCloseSurface" );
1117     if( p_surface != NULL )
1118     {
1119         IDirectDrawSurface2_Release( p_surface );
1120     }
1121 }
1122
1123 /*****************************************************************************
1124  * NewPictureVec: allocate a vector of identical pictures
1125  *****************************************************************************
1126  * Returns 0 on success, -1 otherwise
1127  *****************************************************************************/
1128 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1129                           int i_num_pics )
1130 {
1131     int i;
1132     int i_ret = VLC_SUCCESS;
1133     LPDIRECTDRAWSURFACE2 p_surface;
1134
1135     msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1136              p_vout->p_sys->b_using_overlay ? "yes" : "no",
1137              (char *)&p_vout->output.i_chroma );
1138
1139     I_OUTPUTPICTURES = 0;
1140
1141     /* First we try to use an YUV overlay surface.
1142      * The overlay surface that we create won't be used to decode directly
1143      * into it because accessing video memory directly is way to slow (remember
1144      * that pictures are decoded macroblock per macroblock). Instead the video
1145      * will be decoded in picture buffers in system memory which will then be
1146      * memcpy() to the overlay surface. */
1147     if( p_vout->p_sys->b_using_overlay )
1148     {
1149         /* Triple buffering rocks! it doesn't have any processing overhead
1150          * (you don't have to wait for the vsync) and provides for a very nice
1151          * video quality (no tearing). */
1152         if( p_vout->p_sys->b_3buf_overlay )
1153             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1154                                           p_vout->output.i_chroma,
1155                                           p_vout->p_sys->b_using_overlay,
1156                                           2 /* number of backbuffers */ );
1157
1158         if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1159         {
1160             /* Try to reduce the number of backbuffers */
1161             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1162                                           p_vout->output.i_chroma,
1163                                           p_vout->p_sys->b_using_overlay,
1164                                           0 /* number of backbuffers */ );
1165         }
1166
1167         if( i_ret == VLC_SUCCESS )
1168         {
1169             DDSCAPS dds_caps;
1170             picture_t front_pic;
1171             picture_sys_t front_pic_sys;
1172             front_pic.p_sys = &front_pic_sys;
1173
1174             /* Allocate internal structure */
1175             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1176             if( p_pic[0].p_sys == NULL )
1177             {
1178                 DirectXCloseSurface( p_vout, p_surface );
1179                 return VLC_ENOMEM;
1180             }
1181
1182             /* set front buffer */
1183             p_pic[0].p_sys->p_front_surface = p_surface;
1184
1185             /* Get the back buffer */
1186             memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1187             dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1188             if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1189                                                 p_surface, &dds_caps,
1190                                                 &p_pic[0].p_sys->p_surface ) )
1191             {
1192                 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1193                 /* front buffer is the same as back buffer */
1194                 p_pic[0].p_sys->p_surface = p_surface;
1195             }
1196
1197
1198             p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1199                 p_pic[0].p_sys->p_front_surface;
1200
1201             /* Reset the front buffer memory */
1202             if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1203             {
1204                 int i,j;
1205                 for( i = 0; i < front_pic.i_planes; i++ )
1206                     for( j = 0; j < front_pic.p[i].i_lines; j++)
1207                         memset( front_pic.p[i].p_pixels + j *
1208                                 front_pic.p[i].i_pitch, 127,
1209                                 front_pic.p[i].i_visible_pitch );
1210
1211                 DirectXUnlockSurface( p_vout, &front_pic );
1212             }
1213
1214             DirectXUpdateOverlay( p_vout );
1215             I_OUTPUTPICTURES = 1;
1216             msg_Dbg( p_vout, "YUV overlay created successfully" );
1217         }
1218     }
1219
1220     /* As we can't have an overlay, we'll try to create a plain offscreen
1221      * surface. This surface will reside in video memory because there's a
1222      * better chance then that we'll be able to use some kind of hardware
1223      * acceleration like rescaling, blitting or YUV->RGB conversions.
1224      * We then only need to blit this surface onto the main display when we
1225      * want to display it */
1226     if( !p_vout->p_sys->b_using_overlay )
1227     {
1228         if( p_vout->p_sys->b_hw_yuv )
1229         {
1230             DWORD i_codes;
1231             DWORD *pi_codes;
1232             vlc_bool_t b_result = VLC_FALSE;
1233
1234             /* Check if the chroma is supported first. This is required
1235              * because a few buggy drivers don't mind creating the surface
1236              * even if they don't know about the chroma. */
1237             if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1238                                              &i_codes, NULL ) == DD_OK )
1239             {
1240                 pi_codes = malloc( i_codes * sizeof(DWORD) );
1241                 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1242                     p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
1243                 {
1244                     for( i = 0; i < (int)i_codes; i++ )
1245                     {
1246                         if( p_vout->output.i_chroma == pi_codes[i] )
1247                         {
1248                             b_result = VLC_TRUE;
1249                             break;
1250                         }
1251                     }
1252                 }
1253             }
1254
1255             if( b_result )
1256                 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1257                                               p_vout->output.i_chroma,
1258                                               0 /* no overlay */,
1259                                               0 /* no back buffers */ );
1260             else
1261                 p_vout->p_sys->b_hw_yuv = VLC_FALSE;
1262         }
1263
1264         if( i_ret || !p_vout->p_sys->b_hw_yuv )
1265         {
1266             /* Our last choice is to use a plain RGB surface */
1267             DDPIXELFORMAT ddpfPixelFormat;
1268
1269             ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1270             IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1271                                                 &ddpfPixelFormat );
1272
1273             if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1274             {
1275                 switch( ddpfPixelFormat.dwRGBBitCount )
1276                 {
1277                 case 8: /* FIXME: set the palette */
1278                     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
1279                     break;
1280                 case 15:
1281                     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
1282                     break;
1283                 case 16:
1284                     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
1285                     break;
1286                 case 24:
1287                     p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
1288                     break;
1289                 case 32:
1290                     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
1291                     break;
1292                 default:
1293                     msg_Err( p_vout, "unknown screen depth" );
1294                     return VLC_EGENERIC;
1295                 }
1296                 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1297                 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1298                 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1299             }
1300
1301             p_vout->p_sys->b_hw_yuv = 0;
1302
1303             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1304                                           p_vout->output.i_chroma,
1305                                           0 /* no overlay */,
1306                                           0 /* no back buffers */ );
1307
1308             if( i_ret && !p_vout->p_sys->b_use_sysmem )
1309             {
1310                 /* Give it a last try with b_use_sysmem enabled */
1311                 p_vout->p_sys->b_use_sysmem = 1;
1312
1313                 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1314                                               p_vout->output.i_chroma,
1315                                               0 /* no overlay */,
1316                                               0 /* no back buffers */ );
1317             }
1318         }
1319
1320         if( i_ret == VLC_SUCCESS )
1321         {
1322             /* Allocate internal structure */
1323             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1324             if( p_pic[0].p_sys == NULL )
1325             {
1326                 DirectXCloseSurface( p_vout, p_surface );
1327                 return VLC_ENOMEM;
1328             }
1329
1330             p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1331                 = p_surface;
1332
1333             I_OUTPUTPICTURES = 1;
1334
1335             msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1336                      (char *)&p_vout->output.i_chroma );
1337         }
1338     }
1339
1340
1341     /* Now that we've got all our direct-buffers, we can finish filling in the
1342      * picture_t structures */
1343     for( i = 0; i < I_OUTPUTPICTURES; i++ )
1344     {
1345         p_pic[i].i_status = DESTROYED_PICTURE;
1346         p_pic[i].i_type   = DIRECT_PICTURE;
1347         p_pic[i].pf_lock  = DirectXLockSurface;
1348         p_pic[i].pf_unlock = DirectXUnlockSurface;
1349         PP_OUTPUTPICTURE[i] = &p_pic[i];
1350
1351         if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1352         {
1353             /* AAARRGG */
1354             FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1355             I_OUTPUTPICTURES = 0;
1356             msg_Err( p_vout, "cannot lock surface" );
1357             return VLC_EGENERIC;
1358         }
1359         DirectXUnlockSurface( p_vout, &p_pic[i] );
1360     }
1361
1362     msg_Dbg( p_vout, "End NewPictureVec (%s)",
1363              I_OUTPUTPICTURES ? "succeeded" : "failed" );
1364
1365     return VLC_SUCCESS;
1366 }
1367
1368 /*****************************************************************************
1369  * FreePicture: destroy a picture vector allocated with NewPictureVec
1370  *****************************************************************************
1371  *
1372  *****************************************************************************/
1373 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1374                             int i_num_pics )
1375 {
1376     int i;
1377
1378     for( i = 0; i < i_num_pics; i++ )
1379     {
1380         DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1381
1382         for( i = 0; i < i_num_pics; i++ )
1383         {
1384             free( p_pic[i].p_sys );
1385         }
1386     }
1387
1388     p_vout->p_sys->p_current_surface = 0;
1389 }
1390
1391 /*****************************************************************************
1392  * UpdatePictureStruct: updates the internal data in the picture_t structure
1393  *****************************************************************************
1394  * This will setup stuff for use by the video_output thread
1395  *****************************************************************************/
1396 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1397                                 int i_chroma )
1398 {
1399     switch( p_vout->output.i_chroma )
1400     {
1401         case VLC_FOURCC('R','G','B','2'):
1402         case VLC_FOURCC('R','V','1','5'):
1403         case VLC_FOURCC('R','V','1','6'):
1404         case VLC_FOURCC('R','V','2','4'):
1405         case VLC_FOURCC('R','V','3','2'):
1406             p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1407             p_pic->p->i_lines = p_vout->output.i_height;
1408             p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1409             switch( p_vout->output.i_chroma )
1410             {
1411                 case VLC_FOURCC('R','G','B','2'):
1412                     p_pic->p->i_pixel_pitch = 1;
1413                     break;
1414                 case VLC_FOURCC('R','V','1','5'):
1415                 case VLC_FOURCC('R','V','1','6'):
1416                     p_pic->p->i_pixel_pitch = 2;
1417                     break;
1418                 case VLC_FOURCC('R','V','2','4'):
1419                     p_pic->p->i_pixel_pitch = 3;
1420                     break;
1421                 case VLC_FOURCC('R','V','3','2'):
1422                     p_pic->p->i_pixel_pitch = 4;
1423                     break;
1424                 default:
1425                     return VLC_EGENERIC;
1426             }
1427             p_pic->p->i_visible_pitch = p_vout->output.i_width *
1428               p_pic->p->i_pixel_pitch;
1429             p_pic->i_planes = 1;
1430             break;
1431
1432         case VLC_FOURCC('Y','V','1','2'):
1433
1434             p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1435             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1436             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1437             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1438             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1439               p_pic->p[Y_PLANE].i_pixel_pitch;
1440
1441             p_pic->V_PIXELS =  p_pic->Y_PIXELS
1442               + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1443             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1444             p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1445             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1446             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1447               p_pic->p[V_PLANE].i_pixel_pitch;
1448
1449             p_pic->U_PIXELS = p_pic->V_PIXELS
1450               + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1451             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1452             p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1453             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1454             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1455               p_pic->p[U_PLANE].i_pixel_pitch;
1456
1457             p_pic->i_planes = 3;
1458             break;
1459
1460         case VLC_FOURCC('I','Y','U','V'):
1461
1462             p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1463             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1464             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1465             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1466             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1467               p_pic->p[Y_PLANE].i_pixel_pitch;
1468
1469             p_pic->U_PIXELS = p_pic->Y_PIXELS
1470               + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1471             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1472             p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1473             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1474             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1475               p_pic->p[U_PLANE].i_pixel_pitch;
1476
1477             p_pic->V_PIXELS =  p_pic->U_PIXELS
1478               + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1479             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1480             p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1481             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1482             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1483               p_pic->p[V_PLANE].i_pixel_pitch;
1484
1485             p_pic->i_planes = 3;
1486             break;
1487
1488         case VLC_FOURCC('Y','U','Y','2'):
1489
1490             p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1491             p_pic->p->i_lines = p_vout->output.i_height;
1492             p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1493             p_pic->p->i_pixel_pitch = 2;
1494             p_pic->p->i_visible_pitch = p_vout->output.i_width *
1495               p_pic->p->i_pixel_pitch;
1496
1497             p_pic->i_planes = 1;
1498             break;
1499
1500         default:
1501             /* Unknown chroma, tell the guy to get lost */
1502             msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1503                      p_vout->output.i_chroma,
1504                      (char*)&p_vout->output.i_chroma );
1505             return VLC_EGENERIC;
1506     }
1507
1508     return VLC_SUCCESS;
1509 }
1510
1511 /*****************************************************************************
1512  * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1513  *****************************************************************************
1514  * It is nice to know which features are supported by the hardware so we can
1515  * find ways to optimize our rendering.
1516  *****************************************************************************/
1517 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1518 {
1519     DDCAPS ddcaps;
1520     HRESULT dxresult;
1521
1522     /* This is just an indication of whether or not we'll support overlay,
1523      * but with this test we don't know if we support YUV overlay */
1524     memset( &ddcaps, 0, sizeof( DDCAPS ));
1525     ddcaps.dwSize = sizeof(DDCAPS);
1526     dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1527                                      &ddcaps, NULL );
1528     if(dxresult != DD_OK )
1529     {
1530         msg_Warn( p_vout, "cannot get caps" );
1531     }
1532     else
1533     {
1534         BOOL bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1535              bHasColorKey, bCanStretch, bCanBltFourcc;
1536
1537         /* Determine if the hardware supports overlay surfaces */
1538         bHasOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAY) ==
1539                        DDCAPS_OVERLAY) ? TRUE : FALSE;
1540         /* Determine if the hardware supports overlay surfaces */
1541         bHasOverlayFourCC = ((ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ==
1542                        DDCAPS_OVERLAYFOURCC) ? TRUE : FALSE;
1543         /* Determine if the hardware supports overlay deinterlacing */
1544         bCanDeinterlace = ((ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ==
1545                        0 ) ? TRUE : FALSE;
1546         /* Determine if the hardware supports colorkeying */
1547         bHasColorKey = ((ddcaps.dwCaps & DDCAPS_COLORKEY) ==
1548                         DDCAPS_COLORKEY) ? TRUE : FALSE;
1549         /* Determine if the hardware supports scaling of the overlay surface */
1550         bCanStretch = ((ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ==
1551                        DDCAPS_OVERLAYSTRETCH) ? TRUE : FALSE;
1552         /* Determine if the hardware supports color conversion during a blit */
1553         bCanBltFourcc = ((ddcaps.dwCaps & DDCAPS_BLTFOURCC ) ==
1554                         DDCAPS_BLTFOURCC) ? TRUE : FALSE;
1555
1556         msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1557                          "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1558                          "bltfourcc=%i",
1559                          bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1560                          bHasColorKey, bCanStretch, bCanBltFourcc );
1561
1562         /* Don't ask for troubles */
1563         if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE; 
1564     }
1565 }
1566
1567 /*****************************************************************************
1568  * DirectXLockSurface: Lock surface and get picture data pointer
1569  *****************************************************************************
1570  * This function locks a surface and get the surface descriptor which amongst
1571  * other things has the pointer to the picture data.
1572  *****************************************************************************/
1573 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1574 {
1575     HRESULT dxresult;
1576
1577     /* Lock the surface to get a valid pointer to the picture buffer */
1578     memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1579     p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1580     dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1581                                          NULL, &p_pic->p_sys->ddsd,
1582                                          DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1583                                          NULL );
1584     if( dxresult != DD_OK )
1585     {
1586         if( dxresult == DDERR_INVALIDPARAMS )
1587         {
1588             /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1589              * in an invalid params error */
1590             dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1591                                              &p_pic->p_sys->ddsd,
1592                                              DDLOCK_WAIT, NULL);
1593         }
1594         if( dxresult == DDERR_SURFACELOST )
1595         {
1596             /* Your surface can be lost so be sure
1597              * to check this and restore it if needed */
1598
1599             /* When using overlays with back-buffers, we need to restore
1600              * the front buffer so the back-buffers get restored as well. */
1601             if( p_vout->p_sys->b_using_overlay  )
1602                 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1603             else
1604                 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1605
1606             dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1607                                                  &p_pic->p_sys->ddsd,
1608                                                  DDLOCK_WAIT, NULL);
1609             if( dxresult == DDERR_SURFACELOST )
1610                 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1611         }
1612         if( dxresult != DD_OK )
1613         {
1614             return VLC_EGENERIC;
1615         }
1616     }
1617
1618     /* Now we have a pointer to the surface memory, we can update our picture
1619      * structure. */
1620     if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma )
1621         != VLC_SUCCESS )
1622     {
1623         DirectXUnlockSurface( p_vout, p_pic );
1624         return VLC_EGENERIC;
1625     }
1626     else
1627         return VLC_SUCCESS;      
1628 }
1629
1630 /*****************************************************************************
1631  * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1632  *****************************************************************************/
1633 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1634 {
1635     /* Unlock the Surface */
1636     if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1637         return VLC_SUCCESS;
1638     else
1639         return VLC_EGENERIC;
1640 }
1641
1642 /*****************************************************************************
1643  * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
1644  *****************************************************************************/
1645 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t i_color )
1646 {
1647     DDSURFACEDESC ddsd;
1648     HRESULT dxresult;
1649     COLORREF i_rgb = 0;
1650     uint32_t i_pixel_backup;
1651     HDC hdc;
1652
1653     ddsd.dwSize = sizeof(ddsd);
1654     dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1655                                          &ddsd, DDLOCK_WAIT, NULL );
1656     if( dxresult != DD_OK ) return 0;
1657
1658     i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1659
1660     switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1661     {
1662     case 4:
1663         *(uint8_t *)ddsd.lpSurface = 0x11;
1664         break;
1665     case 8:
1666         *(uint8_t *)ddsd.lpSurface = 0x01;
1667         break;
1668     case 16:
1669         *(uint16_t *)ddsd.lpSurface = 0x01;
1670         break;
1671     case 24:
1672         *(uint32_t *)ddsd.lpSurface = 0x0100;
1673         break;
1674     case 32:
1675         *(uint32_t *)ddsd.lpSurface = 0x01;
1676         break;
1677     }
1678
1679     IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1680
1681     if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1682     {
1683         i_rgb = GetPixel( hdc, 0, 0 );
1684         IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1685     }
1686
1687     ddsd.dwSize = sizeof(ddsd);
1688     dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1689                                          &ddsd, DDLOCK_WAIT, NULL );
1690     if( dxresult != DD_OK ) return i_rgb;
1691
1692     *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1693
1694     IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1695
1696     return i_rgb;
1697 }
1698
1699 /*****************************************************************************
1700  * object variables callbacks: a bunch of object variables are used by the
1701  * interfaces to interact with the vout.
1702  *****************************************************************************/
1703 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1704                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1705 {
1706     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1707     p_vout->p_sys->b_on_top_change = VLC_TRUE;
1708     return VLC_SUCCESS;
1709 }