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