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