]> git.sesse.net Git - vlc/blob - modules/video_output/directx/directx.c
- fixed a compilation issue with C compilers
[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.11 2003/02/01 09:40:50 babal 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     HWND hwnd;
381     HMENU hMenu;
382     vlc_value_t val;
383
384     /* If we do not control our window, we check for geometry changes
385      * ourselves because the parent might not send us its events. */
386     if( p_vout->p_sys->hparent )
387     {
388         DirectXUpdateRects( p_vout, VLC_FALSE );
389     }
390
391     /* We used to call the Win32 PeekMessage function here to read the window
392      * messages. But since window can stay blocked into this function for a
393      * long time (for example when you move your window on the screen), I
394      * decided to isolate PeekMessage in another thread. */
395
396     /*
397      * Scale Change
398      */
399     if( p_vout->i_changes & VOUT_SCALE_CHANGE
400          || p_vout->p_sys->i_changes & VOUT_SCALE_CHANGE )
401     {
402         msg_Dbg( p_vout, "scale change" );
403         if( !p_vout->p_sys->b_using_overlay )
404             InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
405         else
406             DirectXUpdateOverlay( p_vout );
407         p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
408         p_vout->p_sys->i_changes &= ~VOUT_SCALE_CHANGE;
409     }
410
411     /*
412      * Size Change
413      */
414     if( p_vout->i_changes & VOUT_SIZE_CHANGE
415         || p_vout->p_sys->i_changes & VOUT_SIZE_CHANGE )
416     {
417         msg_Dbg( p_vout, "size change" );
418         if( !p_vout->p_sys->b_using_overlay )
419             InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
420         else
421             DirectXUpdateOverlay( p_vout );
422         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
423         p_vout->p_sys->i_changes &= ~VOUT_SIZE_CHANGE;
424     }
425
426     /*
427      * Fullscreen change
428      */
429     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
430         || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
431     {
432         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
433
434         /* We need to switch between Maximized and Normal sized window */
435         window_placement.length = sizeof(WINDOWPLACEMENT);
436         GetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
437         if( p_vout->b_fullscreen )
438         {
439             /* Maximized window */
440             window_placement.showCmd = SW_SHOWMAXIMIZED;
441             /* Change window style, no borders and no title bar */
442             SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, 0 );
443
444         }
445         else
446         {
447             /* Normal window */
448             window_placement.showCmd = SW_SHOWNORMAL;
449             /* Change window style, borders and title bar */
450             SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
451                            WS_OVERLAPPEDWINDOW | WS_SIZEBOX | WS_VISIBLE );
452         }
453
454         SetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
455
456         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
457         p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
458     }
459
460     /*
461      * Pointer change
462      */
463     if( (!p_vout->p_sys->b_cursor_hidden) &&
464         ( (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 ) )
465     {
466         /* Hide the mouse automatically */
467         if( p_vout->p_sys->hwnd != p_vout->p_sys->hparent )
468         {
469             p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
470             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
471         }
472     }
473
474     /*
475      * "Always on top" status change
476      */
477     hwnd = p_vout->p_sys->hwnd;
478     hMenu = GetSystemMenu( hwnd , FALSE );
479     var_Get( p_vout, "directx-on-top", &val );
480     if( val.b_bool )
481     {
482         /* Set the window on top if necessary */
483         if( !( GetWindowLong( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST ) )
484         {
485             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
486                            MF_BYCOMMAND | MFS_CHECKED );
487             SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
488                            SWP_NOSIZE | SWP_NOMOVE );
489         }
490     }
491     else
492     {
493         /* The window shouldn't be on top */
494         if( GetWindowLong( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST )
495         {
496             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
497                            MF_BYCOMMAND | MFS_UNCHECKED );
498             SetWindowPos( hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
499                           SWP_NOSIZE | SWP_NOMOVE );
500         }
501     }
502
503     /* Check if the event thread is still running */
504     if( p_vout->p_sys->p_event->b_die )
505     {
506         return VLC_EGENERIC; /* exit */
507     }
508
509     return VLC_SUCCESS;
510 }
511
512 /*****************************************************************************
513  * Display: displays previously rendered output
514  *****************************************************************************
515  * This function sends the currently rendered image to the display, wait until
516  * it is displayed and switch the two rendering buffers, preparing next frame.
517  *****************************************************************************/
518 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
519 {
520     HRESULT dxresult;
521
522     if( (p_vout->p_sys->p_display == NULL) )
523     {
524         msg_Warn( p_vout, "no display!!" );
525         return;
526     }
527
528     if( !p_vout->p_sys->b_using_overlay )
529     {
530         DDBLTFX  ddbltfx;
531
532         /* We ask for the "NOTEARING" option */
533         memset( &ddbltfx, 0, sizeof(DDBLTFX) );
534         ddbltfx.dwSize = sizeof(DDBLTFX);
535         ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
536
537         /* Blit video surface to display */
538         dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
539                                             &p_vout->p_sys->rect_dest_clipped,
540                                             p_pic->p_sys->p_surface,
541                                             &p_vout->p_sys->rect_src_clipped,
542                                             DDBLT_ASYNC, &ddbltfx );
543         if ( dxresult == DDERR_SURFACELOST )
544         {
545             /* Our surface can be lost so be sure
546              * to check this and restore it if needed */
547             IDirectDrawSurface2_Restore( p_vout->p_sys->p_display );
548
549             /* Now that the surface has been restored try to display again */
550             dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
551                                            &p_vout->p_sys->rect_dest_clipped,
552                                            p_pic->p_sys->p_surface,
553                                            &p_vout->p_sys->rect_src_clipped,
554                                            DDBLT_ASYNC, &ddbltfx );
555         }
556
557         if( dxresult != DD_OK )
558         {
559             msg_Warn( p_vout, "could not blit surface (error %i)", dxresult );
560             return;
561         }
562
563     }
564     else /* using overlay */
565     {
566         /* Flip the overlay buffers if we are using back buffers */
567         if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
568         {
569             return;
570         }
571
572         dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
573                                              NULL, DDFLIP_WAIT );
574         if ( dxresult == DDERR_SURFACELOST )
575         {
576             /* Our surface can be lost so be sure
577              * to check this and restore it if needed */
578             IDirectDrawSurface2_Restore( p_vout->p_sys->p_display );
579             IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
580
581             /* Now that the surface has been restored try to display again */
582             dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
583                                                  NULL, DDFLIP_WAIT );
584             DirectXUpdateOverlay( p_vout );
585         }
586
587         if( dxresult != DD_OK )
588         {
589             msg_Warn( p_vout, "could not flip overlay (error %i)", dxresult );
590         }
591
592         if( DirectXGetSurfaceDesc( p_vout, p_pic ) )
593         {
594             /* AAARRGG */
595             msg_Err( p_vout, "cannot get surface desc" );
596             return;
597         }
598
599         if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma ) )
600         {
601             /* AAARRGG */
602             msg_Err( p_vout, "invalid pic chroma" );
603             return;
604         }
605
606         /* set currently displayed pic */
607         p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
608     }
609 }
610
611
612 /* following functions are local */
613
614 /*****************************************************************************
615  * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
616  *****************************************************************************
617  * This function initialise and allocate resources for DirectDraw.
618  *****************************************************************************/
619 static int DirectXInitDDraw( vout_thread_t *p_vout )
620 {
621     HRESULT    dxresult;
622     HRESULT    (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
623     LPDIRECTDRAW  p_ddobject;
624
625     msg_Dbg( p_vout, "DirectXInitDDraw" );
626
627     /* load direct draw DLL */
628     p_vout->p_sys->hddraw_dll = LoadLibrary("DDRAW.DLL");
629     if( p_vout->p_sys->hddraw_dll == NULL )
630     {
631         msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
632         goto error;
633     }
634
635     OurDirectDrawCreate =
636       (void *)GetProcAddress(p_vout->p_sys->hddraw_dll, "DirectDrawCreate");
637     if ( OurDirectDrawCreate == NULL )
638     {
639         msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
640         goto error;
641     }
642
643     /* Initialize DirectDraw now */
644     dxresult = OurDirectDrawCreate( NULL, &p_ddobject, NULL );
645     if( dxresult != DD_OK )
646     {
647         msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
648         goto error;
649     }
650
651     /* Get the IDirectDraw2 interface */
652     dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
653                                         (LPVOID *)&p_vout->p_sys->p_ddobject );
654     /* Release the unused interface */
655     IDirectDraw_Release( p_ddobject );
656     if( dxresult != DD_OK )
657     {
658         msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
659         goto error;
660     }
661
662     /* Set DirectDraw Cooperative level, ie what control we want over Windows
663      * display */
664     dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
665                                            p_vout->p_sys->hwnd, DDSCL_NORMAL );
666     if( dxresult != DD_OK )
667     {
668         msg_Err( p_vout, "cannot set direct draw cooperative level" );
669         goto error;
670     }
671
672     /* Probe the capabilities of the hardware */
673     DirectXGetDDrawCaps( p_vout );
674
675     msg_Dbg( p_vout, "End DirectXInitDDraw" );
676     return VLC_SUCCESS;
677
678  error:
679     if( p_vout->p_sys->p_ddobject )
680         IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
681     if( p_vout->p_sys->hddraw_dll )
682         FreeLibrary( p_vout->p_sys->hddraw_dll );
683     p_vout->p_sys->hddraw_dll = NULL;
684     p_vout->p_sys->p_ddobject = NULL;
685     return VLC_EGENERIC;
686 }
687
688 /*****************************************************************************
689  * DirectXCreateDisplay: create the DirectDraw display.
690  *****************************************************************************
691  * Create and initialize display according to preferences specified in the vout
692  * thread fields.
693  *****************************************************************************/
694 static int DirectXCreateDisplay( vout_thread_t *p_vout )
695 {
696     HRESULT              dxresult;
697     DDSURFACEDESC        ddsd;
698     LPDIRECTDRAWSURFACE  p_display;
699     DDPIXELFORMAT   pixel_format;
700
701     msg_Dbg( p_vout, "DirectXCreateDisplay" );
702
703     /* Now get the primary surface. This surface is what you actually see
704      * on your screen */
705     memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
706     ddsd.dwSize = sizeof(DDSURFACEDESC);
707     ddsd.dwFlags = DDSD_CAPS;
708     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
709
710     dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
711                                            &ddsd,
712                                            &p_display, NULL );
713     if( dxresult != DD_OK )
714     {
715         msg_Err( p_vout, "cannot get primary surface (error %i)", dxresult );
716         return VLC_EGENERIC;
717     }
718
719     dxresult = IDirectDrawSurface_QueryInterface( p_display,
720                                          &IID_IDirectDrawSurface2,
721                                          (LPVOID *)&p_vout->p_sys->p_display );
722     /* Release the old interface */
723     IDirectDrawSurface_Release( p_display );
724     if ( dxresult != DD_OK )
725     {
726         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
727                          "(error %i)", dxresult );
728         return VLC_EGENERIC;
729     }
730
731     /* The clipper will be used only in non-overlay mode */
732     DirectXCreateClipper( p_vout );
733
734
735 #if 1
736     /* compute the colorkey pixel value from the RGB value we've got */
737     memset( &pixel_format, 0, sizeof( DDPIXELFORMAT ));
738     pixel_format.dwSize = sizeof( DDPIXELFORMAT );
739     dxresult = IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
740                                                    &pixel_format );
741     if( dxresult != DD_OK )
742     {
743         msg_Warn( p_vout, "DirectXUpdateOverlay GetPixelFormat failed "
744                           "(error %i)", dxresult );
745     }
746     p_vout->p_sys->i_colorkey = (DWORD)((( p_vout->p_sys->i_rgb_colorkey
747                                            * pixel_format.dwRBitMask) / 255)
748                                         & pixel_format.dwRBitMask );
749 #endif
750
751     return VLC_SUCCESS;
752 }
753
754
755 /*****************************************************************************
756  * DirectXCreateClipper: Create a clipper that will be used when blitting the
757  *                       RGB surface to the main display.
758  *****************************************************************************
759  * This clipper prevents us to modify by mistake anything on the screen
760  * which doesn't belong to our window. For example when a part of our video
761  * window is hidden by another window.
762  *****************************************************************************/
763 static int DirectXCreateClipper( vout_thread_t *p_vout )
764 {
765     HRESULT dxresult;
766
767     msg_Dbg( p_vout, "DirectXCreateClipper" );
768
769     /* Create the clipper */
770     dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
771                                            &p_vout->p_sys->p_clipper, NULL );
772     if( dxresult != DD_OK )
773     {
774         msg_Warn( p_vout, "cannot create clipper (error %i)", dxresult );
775         goto error;
776     }
777
778     /* Associate the clipper to the window */
779     dxresult = IDirectDrawClipper_SetHWnd(p_vout->p_sys->p_clipper, 0,
780                                           p_vout->p_sys->hwnd);
781     if( dxresult != DD_OK )
782     {
783         msg_Warn( p_vout, "cannot attach clipper to window (error %i)",
784                           dxresult );
785         goto error;
786     }
787
788     /* associate the clipper with the surface */
789     dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
790                                              p_vout->p_sys->p_clipper);
791     if( dxresult != DD_OK )
792     {
793         msg_Warn( p_vout, "cannot attach clipper to surface (error %i)",
794                           dxresult );
795         goto error;
796     }
797
798     return VLC_SUCCESS;
799
800  error:
801     if( p_vout->p_sys->p_clipper )
802     {
803         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
804     }
805     p_vout->p_sys->p_clipper = NULL;
806     return VLC_EGENERIC;
807 }
808
809 /*****************************************************************************
810  * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
811  *****************************************************************************
812  * The best method of display is with an YUV overlay because the YUV->RGB
813  * conversion is done in hardware.
814  * You can also create a plain RGB surface.
815  * ( Maybe we could also try an RGB overlay surface, which could have hardware
816  * scaling and which would also be faster in window mode because you don't
817  * need to do any blitting to the main display...)
818  *****************************************************************************/
819 static int DirectXCreateSurface( vout_thread_t *p_vout,
820                                  LPDIRECTDRAWSURFACE2 *pp_surface_final,
821                                  int i_chroma, int b_overlay,
822                                  int i_backbuffers )
823 {
824     HRESULT dxresult;
825     LPDIRECTDRAWSURFACE p_surface;
826     DDSURFACEDESC ddsd;
827
828     /* Create the video surface */
829     if( b_overlay )
830     {
831         /* Now try to create the YUV overlay surface.
832          * This overlay will be displayed on top of the primary surface.
833          * A color key is used to determine whether or not the overlay will be
834          * displayed, ie the overlay will be displayed in place of the primary
835          * surface wherever the primary surface will have this color.
836          * The video window has been created with a background of this color so
837          * the overlay will be only displayed on top of this window */
838
839         memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
840         ddsd.dwSize = sizeof(DDSURFACEDESC);
841         ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
842         ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
843         ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
844         ddsd.dwFlags = DDSD_CAPS |
845                        DDSD_HEIGHT |
846                        DDSD_WIDTH |
847                        DDSD_PIXELFORMAT;
848         ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
849         ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY |
850                               DDSCAPS_VIDEOMEMORY;
851         ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
852                                 : 0 );
853         ddsd.dwHeight = p_vout->render.i_height;
854         ddsd.dwWidth = p_vout->render.i_width;
855         ddsd.dwBackBufferCount = i_backbuffers;
856
857         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
858                                                &ddsd,
859                                                &p_surface, NULL );
860         if( dxresult != DD_OK )
861         {
862             *pp_surface_final = NULL;
863             return VLC_EGENERIC;
864         }
865     }
866
867     if( !b_overlay )
868     {
869         vlc_bool_t b_rgb_surface =
870             ( i_chroma == VLC_FOURCC('R','G','B','2') )
871           || ( i_chroma == VLC_FOURCC('R','V','1','5') )
872            || ( i_chroma == VLC_FOURCC('R','V','1','6') )
873             || ( i_chroma == VLC_FOURCC('R','V','2','4') )
874              || ( i_chroma == VLC_FOURCC('R','V','3','2') );
875
876         memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
877         ddsd.dwSize = sizeof(DDSURFACEDESC);
878         ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
879         ddsd.dwFlags = DDSD_HEIGHT |
880                        DDSD_WIDTH |
881                        DDSD_CAPS;
882         ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
883         ddsd.dwHeight = p_vout->render.i_height;
884         ddsd.dwWidth = p_vout->render.i_width;
885
886         if( p_vout->p_sys->b_use_sysmem )
887             ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
888         else
889             ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
890
891         if( !b_rgb_surface )
892         {
893             ddsd.dwFlags |= DDSD_PIXELFORMAT;
894             ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
895             ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
896         }
897
898         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
899                                                &ddsd,
900                                                &p_surface, NULL );
901         if( dxresult != DD_OK )
902         {
903             *pp_surface_final = NULL;
904             return VLC_EGENERIC;
905         }
906     }
907
908     /* Now that the surface is created, try to get a newer DirectX interface */
909     dxresult = IDirectDrawSurface_QueryInterface( p_surface,
910                                      &IID_IDirectDrawSurface2,
911                                      (LPVOID *)pp_surface_final );
912     IDirectDrawSurface_Release( p_surface );    /* Release the old interface */
913     if ( dxresult != DD_OK )
914     {
915         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
916                          "(error %i)", dxresult );
917         *pp_surface_final = NULL;
918         return VLC_EGENERIC;
919     }
920
921     return VLC_SUCCESS;
922 }
923
924 /*****************************************************************************
925  * DirectXUpdateOverlay: Move or resize overlay surface on video display.
926  *****************************************************************************
927  * This function is used to move or resize an overlay surface on the screen.
928  * Ususally the overlay is moved by the user and thus, by a move or resize
929  * event (in Manage).
930  *****************************************************************************/
931 void DirectXUpdateOverlay( vout_thread_t *p_vout )
932 {
933     DDOVERLAYFX     ddofx;
934     DWORD           dwFlags;
935     HRESULT         dxresult;
936
937     if( p_vout->p_sys->p_current_surface == NULL ||
938         !p_vout->p_sys->b_using_overlay )
939         return;
940
941     /* The new window dimensions should already have been computed by the
942      * caller of this function */
943
944     /* Position and show the overlay */
945     memset(&ddofx, 0, sizeof(DDOVERLAYFX));
946     ddofx.dwSize = sizeof(DDOVERLAYFX);
947     ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
948     ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
949
950     dwFlags = DDOVER_SHOW;
951     if( !p_vout->p_sys->b_caps_overlay_clipping )
952         dwFlags |= DDOVER_KEYDESTOVERRIDE;
953
954     dxresult = IDirectDrawSurface2_UpdateOverlay(
955                                          p_vout->p_sys->p_current_surface,
956                                          &p_vout->p_sys->rect_src_clipped,
957                                          p_vout->p_sys->p_display,
958                                          &p_vout->p_sys->rect_dest_clipped,
959                                          dwFlags,
960                                          &ddofx );
961     if(dxresult != DD_OK)
962     {
963         msg_Warn( p_vout,
964                   "DirectXUpdateOverlay cannot move or resize overlay" );
965     }
966
967 }
968
969 /*****************************************************************************
970  * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
971  *****************************************************************************
972  * This function returns all resources allocated by DirectXInitDDraw.
973  *****************************************************************************/
974 static void DirectXCloseDDraw( vout_thread_t *p_vout )
975 {
976     msg_Dbg( p_vout, "DirectXCloseDDraw" );
977     if( p_vout->p_sys->p_ddobject != NULL )
978     {
979         IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
980         p_vout->p_sys->p_ddobject = NULL;
981     }
982
983     if( p_vout->p_sys->hddraw_dll != NULL )
984     {
985         FreeLibrary( p_vout->p_sys->hddraw_dll );
986         p_vout->p_sys->hddraw_dll = NULL;
987     }
988 }
989
990 /*****************************************************************************
991  * DirectXCloseDisplay: close and reset the DirectX display device
992  *****************************************************************************
993  * This function returns all resources allocated by DirectXCreateDisplay.
994  *****************************************************************************/
995 static void DirectXCloseDisplay( vout_thread_t *p_vout )
996 {
997     msg_Dbg( p_vout, "DirectXCloseDisplay" );
998
999     if( p_vout->p_sys->p_clipper != NULL )
1000     {
1001         msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1002         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1003         p_vout->p_sys->p_clipper = NULL;
1004     }
1005
1006     if( p_vout->p_sys->p_display != NULL )
1007     {
1008         msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1009         IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1010         p_vout->p_sys->p_display = NULL;
1011     }
1012 }
1013
1014 /*****************************************************************************
1015  * DirectXCloseSurface: close the YUV overlay or RGB surface.
1016  *****************************************************************************
1017  * This function returns all resources allocated for the surface.
1018  *****************************************************************************/
1019 static void DirectXCloseSurface( vout_thread_t *p_vout,
1020                                  LPDIRECTDRAWSURFACE2 p_surface )
1021 {
1022     msg_Dbg( p_vout, "DirectXCloseSurface" );
1023     if( p_surface != NULL )
1024     {
1025         IDirectDrawSurface2_Release( p_surface );
1026     }
1027 }
1028
1029 /*****************************************************************************
1030  * NewPictureVec: allocate a vector of identical pictures
1031  *****************************************************************************
1032  * Returns 0 on success, -1 otherwise
1033  *****************************************************************************/
1034 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1035                           int i_num_pics )
1036 {
1037     int i;
1038     int i_ret = VLC_SUCCESS;
1039     LPDIRECTDRAWSURFACE2 p_surface;
1040
1041     msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1042              p_vout->p_sys->b_using_overlay ? "yes" : "no",
1043              (char *)&p_vout->output.i_chroma );
1044
1045     I_OUTPUTPICTURES = 0;
1046
1047     /* First we try to use an YUV overlay surface.
1048      * The overlay surface that we create won't be used to decode directly
1049      * into it because accessing video memory directly is way to slow (remember
1050      * that pictures are decoded macroblock per macroblock). Instead the video
1051      * will be decoded in picture buffers in system memory which will then be
1052      * memcpy() to the overlay surface. */
1053     if( p_vout->p_sys->b_using_overlay )
1054     {
1055         /* Triple buffering rocks! it doesn't have any processing overhead
1056          * (you don't have to wait for the vsync) and provides for a very nice
1057          * video quality (no tearing). */
1058
1059         i_ret = DirectXCreateSurface( p_vout, &p_surface,
1060                                       p_vout->output.i_chroma,
1061                                       p_vout->p_sys->b_using_overlay,
1062                                       2 /* number of backbuffers */ );
1063
1064         if( i_ret != VLC_SUCCESS )
1065         {
1066             /* Try to reduce the number of backbuffers */
1067             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1068                                           p_vout->output.i_chroma,
1069                                           p_vout->p_sys->b_using_overlay,
1070                                           0 /* number of backbuffers */ );
1071         }
1072
1073         if( i_ret == VLC_SUCCESS )
1074         {
1075             DDSCAPS dds_caps;
1076             picture_t front_pic;
1077             picture_sys_t front_pic_sys;
1078             front_pic.p_sys = &front_pic_sys;
1079
1080             /* Allocate internal structure */
1081             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1082             if( p_pic[0].p_sys == NULL )
1083             {
1084                 DirectXCloseSurface( p_vout, p_surface );
1085                 return VLC_ENOMEM;
1086             }
1087
1088             /* set front buffer */
1089             p_pic[0].p_sys->p_front_surface = p_surface;
1090
1091             /* Get the back buffer */
1092             memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1093             dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1094             if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1095                                                 p_surface, &dds_caps,
1096                                                 &p_pic[0].p_sys->p_surface ) )
1097             {
1098                 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1099                 /* front buffer is the same as back buffer */
1100                 p_pic[0].p_sys->p_surface = p_surface;
1101             }
1102
1103
1104             p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1105                 p_pic[0].p_sys->p_front_surface;
1106
1107             /* Reset the front buffer memory */
1108             if( !DirectXGetSurfaceDesc( p_vout, &front_pic ) &&
1109                 !UpdatePictureStruct( p_vout, &front_pic,
1110                                       p_vout->output.i_chroma ) )
1111             {
1112                 int i,j;
1113                 for( i = 0; i < front_pic.i_planes; i++ )
1114                     for( j = 0; j < front_pic.p[i].i_lines; j++)
1115                         memset( front_pic.p[i].p_pixels + j *
1116                                 front_pic.p[i].i_pitch, 127,
1117                                 front_pic.p[i].i_visible_pitch );
1118             }
1119
1120             DirectXUpdateOverlay( p_vout );
1121             I_OUTPUTPICTURES = 1;
1122             msg_Dbg( p_vout, "YUV overlay created successfully" );
1123         }
1124     }
1125
1126     /* As we can't have an overlay, we'll try to create a plain offscreen
1127      * surface. This surface will reside in video memory because there's a
1128      * better chance then that we'll be able to use some kind of hardware
1129      * acceleration like rescaling, blitting or YUV->RGB conversions.
1130      * We then only need to blit this surface onto the main display when we
1131      * want to display it */
1132     if( !p_vout->p_sys->b_using_overlay )
1133     {
1134         if( p_vout->p_sys->b_hw_yuv )
1135         {
1136             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1137                                           p_vout->output.i_chroma,
1138                                           0 /* no overlay */,
1139                                           0 /* no back buffers */ );
1140         }
1141
1142         if( i_ret || !p_vout->p_sys->b_hw_yuv )
1143         {
1144             /* Our last choice is to use a plain RGB surface */
1145             DDPIXELFORMAT ddpfPixelFormat;
1146
1147             ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1148             IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1149                                                 &ddpfPixelFormat );
1150
1151             if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1152             {
1153                 switch( ddpfPixelFormat.dwRGBBitCount )
1154                 {
1155                 case 8: /* FIXME: set the palette */
1156                     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
1157                     break;
1158                 case 15:
1159                     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
1160                     break;
1161                 case 16:
1162                     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
1163                     break;
1164                 case 24:
1165                     p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
1166                     break;
1167                 case 32:
1168                     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
1169                     break;
1170                 default:
1171                     msg_Err( p_vout, "unknown screen depth" );
1172                     return VLC_EGENERIC;
1173                 }
1174                 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1175                 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1176                 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1177             }
1178
1179             p_vout->p_sys->b_hw_yuv = 0;
1180
1181             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1182                                           p_vout->output.i_chroma,
1183                                           0 /* no overlay */,
1184                                           0 /* no back buffers */ );
1185
1186             if( i_ret && !p_vout->p_sys->b_use_sysmem )
1187             {
1188                 /* Give it a last try with b_use_sysmem enabled */
1189                 p_vout->p_sys->b_use_sysmem = 1;
1190
1191                 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1192                                               p_vout->output.i_chroma,
1193                                               0 /* no overlay */,
1194                                               0 /* no back buffers */ );
1195             }
1196         }
1197
1198         if( i_ret == VLC_SUCCESS )
1199         {
1200             /* Allocate internal structure */
1201             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1202             if( p_pic[0].p_sys == NULL )
1203             {
1204                 DirectXCloseSurface( p_vout, p_surface );
1205                 return VLC_ENOMEM;
1206             }
1207             p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1208                 = p_surface;
1209
1210             I_OUTPUTPICTURES = 1;
1211
1212             msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1213                      (char *)&p_vout->output.i_chroma );
1214         }
1215     }
1216
1217
1218     /* Now that we've got all our direct-buffers, we can finish filling in the
1219      * picture_t structures */
1220     for( i = 0; i < I_OUTPUTPICTURES; i++ )
1221     {
1222         p_pic[i].i_status = DESTROYED_PICTURE;
1223         p_pic[i].i_type   = DIRECT_PICTURE;
1224         PP_OUTPUTPICTURE[i] = &p_pic[i];
1225
1226         if( DirectXGetSurfaceDesc( p_vout, &p_pic[i] ) )
1227         {
1228             /* AAARRGG */
1229             FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1230             I_OUTPUTPICTURES = 0;
1231             return VLC_EGENERIC;
1232         }
1233
1234         if( UpdatePictureStruct(p_vout, &p_pic[i], p_vout->output.i_chroma) )
1235         {
1236             /* Unknown chroma, tell the guy to get lost */
1237             msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1238                      p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1239             FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1240             I_OUTPUTPICTURES = 0;
1241             return VLC_EGENERIC;
1242         }
1243     }
1244
1245     msg_Dbg( p_vout, "End NewPictureVec (%s)",
1246              I_OUTPUTPICTURES ? "succeeded" : "failed" );
1247
1248     return VLC_SUCCESS;
1249 }
1250
1251 /*****************************************************************************
1252  * FreePicture: destroy a picture vector allocated with NewPictureVec
1253  *****************************************************************************
1254  *
1255  *****************************************************************************/
1256 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1257                             int i_num_pics )
1258 {
1259     int i;
1260
1261     for( i = 0; i < i_num_pics; i++ )
1262     {
1263         DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1264
1265         for( i = 0; i < i_num_pics; i++ )
1266         {
1267             free( p_pic[i].p_sys );
1268         }
1269     }
1270 }
1271
1272 /*****************************************************************************
1273  * UpdatePictureStruct: updates the internal data in the picture_t structure
1274  *****************************************************************************
1275  * This will setup stuff for use by the video_output thread
1276  *****************************************************************************/
1277 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1278                                 int i_chroma )
1279 {
1280     switch( p_vout->output.i_chroma )
1281     {
1282         case VLC_FOURCC('R','G','B','2'):
1283         case VLC_FOURCC('R','V','1','5'):
1284         case VLC_FOURCC('R','V','1','6'):
1285         case VLC_FOURCC('R','V','2','4'):
1286         case VLC_FOURCC('R','V','3','2'):
1287             p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1288             p_pic->p->i_lines = p_vout->output.i_height;
1289             p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1290             switch( p_vout->output.i_chroma )
1291             {
1292                 case VLC_FOURCC('R','G','B','2'):
1293                     p_pic->p->i_pixel_pitch = 1;
1294                     break;
1295                 case VLC_FOURCC('R','V','1','5'):
1296                 case VLC_FOURCC('R','V','1','6'):
1297                     p_pic->p->i_pixel_pitch = 2;
1298                     break;
1299                 case VLC_FOURCC('R','V','2','4'):
1300                 case VLC_FOURCC('R','V','3','2'):
1301                     p_pic->p->i_pixel_pitch = 4;
1302                     break;
1303                 default:
1304                     return VLC_EGENERIC;
1305             }
1306             p_pic->p->i_visible_pitch = p_vout->output.i_width *
1307               p_pic->p->i_pixel_pitch;
1308             p_pic->i_planes = 1;
1309             break;
1310
1311         case VLC_FOURCC('Y','V','1','2'):
1312
1313             p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1314             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1315             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1316             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1317             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1318               p_pic->p[Y_PLANE].i_pixel_pitch;
1319
1320             p_pic->V_PIXELS =  p_pic->Y_PIXELS
1321               + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1322             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1323             p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1324             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1325             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1326               p_pic->p[V_PLANE].i_pixel_pitch;
1327
1328             p_pic->U_PIXELS = p_pic->V_PIXELS
1329               + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1330             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1331             p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1332             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1333             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1334               p_pic->p[U_PLANE].i_pixel_pitch;
1335
1336             p_pic->i_planes = 3;
1337             break;
1338
1339         case VLC_FOURCC('I','Y','U','V'):
1340
1341             p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1342             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1343             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1344             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1345             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1346               p_pic->p[Y_PLANE].i_pixel_pitch;
1347
1348             p_pic->U_PIXELS = p_pic->Y_PIXELS
1349               + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1350             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1351             p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1352             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1353             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1354               p_pic->p[U_PLANE].i_pixel_pitch;
1355
1356             p_pic->V_PIXELS =  p_pic->U_PIXELS
1357               + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1358             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1359             p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1360             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1361             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1362               p_pic->p[V_PLANE].i_pixel_pitch;
1363
1364             p_pic->i_planes = 3;
1365             break;
1366
1367         case VLC_FOURCC('Y','U','Y','2'):
1368
1369             p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1370             p_pic->p->i_lines = p_vout->output.i_height;
1371             p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1372             p_pic->p->i_pixel_pitch = 2;
1373             p_pic->p->i_visible_pitch = p_vout->output.i_width *
1374               p_pic->p->i_pixel_pitch;
1375
1376             p_pic->i_planes = 1;
1377             break;
1378
1379         default:
1380             /* Not supported */
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;
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         msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1429                          "can_clip_overlay=%i colorkey=%i stretch=%i",
1430                          bHasOverlay, bHasOverlayFourCC, bCanClipOverlay,
1431                          bHasColorKey, bCanStretch );
1432
1433         /* Overlay clipping support is interesting for us as it means we can
1434          * get rid of the colorkey alltogether */
1435         p_vout->p_sys->b_caps_overlay_clipping = bCanClipOverlay;
1436
1437     }
1438 }
1439
1440 /*****************************************************************************
1441  * DirectXGetSurfaceDesc: Get some more information about the surface
1442  *****************************************************************************
1443  * This function get and stores the surface descriptor which among things
1444  * has the pointer to the picture data.
1445  *****************************************************************************/
1446 static int DirectXGetSurfaceDesc( vout_thread_t *p_vout, picture_t *p_pic )
1447 {
1448     HRESULT dxresult;
1449
1450     /* Lock the surface to get a valid pointer to the picture buffer */
1451     memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1452     p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1453     dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1454                                          NULL, &p_pic->p_sys->ddsd,
1455                                          DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1456                                          NULL );
1457     if( dxresult != DD_OK )
1458     {
1459         if( dxresult == DDERR_INVALIDPARAMS )
1460         {
1461             /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1462              * in an invalid params error */
1463             dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1464                                              &p_pic->p_sys->ddsd,
1465                                              DDLOCK_WAIT, NULL);
1466         }
1467         if( dxresult == DDERR_SURFACELOST )
1468         {
1469             /* Your surface can be lost so be sure
1470              * to check this and restore it if needed */
1471             dxresult = IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1472             dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1473                                              &p_pic->p_sys->ddsd,
1474                                              DDLOCK_WAIT, NULL);
1475         }
1476         if( dxresult != DD_OK )
1477         {
1478             msg_Err( p_vout, "DirectXGetSurfaceDesc cannot lock surface" );
1479             return VLC_EGENERIC;
1480         }
1481     }
1482
1483     /* Unlock the Surface */
1484     dxresult = IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL );
1485
1486     return VLC_SUCCESS;
1487 }