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