* modules/*: sanitization of the modules description strings.
[vlc.git] / 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.16 2003/03/30 18:14:39 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble:
26  *
27  * This plugin will use YUV overlay if supported, using overlay will result in
28  * the best video quality (hardware interpolation when rescaling the picture)
29  * and the fastest display as it requires less processing.
30  *
31  * If YUV overlay is not supported this plugin will use RGB offscreen video
32  * surfaces that will be blitted onto the primary surface (display) to
33  * effectively display the pictures. This fallback method also enables us to
34  * display video in window mode.
35  *
36  *****************************************************************************/
37 #include <errno.h>                                                 /* ENOMEM */
38 #include <stdlib.h>                                                /* free() */
39 #include <string.h>                                            /* strerror() */
40
41 #include <vlc/vlc.h>
42 #include <vlc/intf.h>
43 #include <vlc/vout.h>
44
45 #include <windows.h>
46 #include <ddraw.h>
47
48 #include "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 int  OpenVideo  ( vlc_object_t * );
65 static void CloseVideo ( vlc_object_t * );
66
67 static int  Init      ( vout_thread_t * );
68 static void End       ( vout_thread_t * );
69 static int  Manage    ( vout_thread_t * );
70 static void Display   ( vout_thread_t *, picture_t * );
71
72 static int  NewPictureVec  ( vout_thread_t *, picture_t *, int );
73 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
74 static int  UpdatePictureStruct( vout_thread_t *, picture_t *, int );
75
76 static int  DirectXInitDDraw      ( vout_thread_t *p_vout );
77 static void DirectXCloseDDraw     ( vout_thread_t *p_vout );
78 static int  DirectXCreateDisplay  ( vout_thread_t *p_vout );
79 static void DirectXCloseDisplay   ( vout_thread_t *p_vout );
80 static int  DirectXCreateSurface  ( vout_thread_t *p_vout,
81                                     LPDIRECTDRAWSURFACE2 *, int, int, int );
82 static void DirectXCloseSurface   ( vout_thread_t *p_vout,
83                                     LPDIRECTDRAWSURFACE2 );
84 static int  DirectXCreateClipper  ( vout_thread_t *p_vout );
85 static void DirectXGetDDrawCaps   ( vout_thread_t *p_vout );
86 static int  DirectXLockSurface    ( vout_thread_t *p_vout, picture_t *p_pic );
87 static int  DirectXUnlockSurface  ( vout_thread_t *p_vout, picture_t *p_pic );
88
89 /*****************************************************************************
90  * Module descriptor
91  *****************************************************************************/
92 #define ON_TOP_TEXT N_("always on top")
93 #define ON_TOP_LONGTEXT N_("place the directx window on top of other windows")
94 #define HW_YUV_TEXT N_("use hardware YUV->RGB conversions")
95 #define HW_YUV_LONGTEXT N_( \
96     "Try to use hardware acceleration for YUV->RGB conversions. " \
97     "This option doesn't have any effect when using overlays." )
98 #define SYSMEM_TEXT N_("use video buffers in system memory")
99 #define SYSMEM_LONGTEXT N_( \
100     "Create video buffers in system memory instead of video memory. This " \
101     "isn't recommended as usually using video memory allows to benefit from " \
102     "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
103     "This option doesn't have any effect when using overlays." )
104 #define TRIPLEBUF_TEXT N_("use triple buffering for overlays")
105 #define TRIPLEBUF_LONGTEXT N_( \
106     "Try to use triple bufferring when using YUV overlays. That results in " \
107     "much better video quality (no flickering)." )
108
109 vlc_module_begin();
110     add_category_hint( N_("Video"), NULL, VLC_FALSE );
111     add_bool( "directx-on-top", 0, NULL, ON_TOP_TEXT, ON_TOP_LONGTEXT, VLC_FALSE );
112     add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT, VLC_TRUE );
113     add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT, VLC_TRUE );
114     add_bool( "directx-3buffering", 1, NULL, TRIPLEBUF_TEXT, TRIPLEBUF_LONGTEXT, VLC_TRUE );
115     set_description( _("DirectX video output") );
116     set_capability( "video output", 100 );
117     add_shortcut( "directx" );
118     set_callbacks( OpenVideo, CloseVideo );
119 vlc_module_end();
120
121 #if 0 /* FIXME */
122     /* check if we registered a window class because we need to
123      * unregister it */
124     WNDCLASS wndclass;
125     if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
126         UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
127 #endif
128
129 /*****************************************************************************
130  * OpenVideo: allocate DirectX video thread output method
131  *****************************************************************************
132  * This function allocates and initialize the DirectX vout method.
133  *****************************************************************************/
134 static int OpenVideo( vlc_object_t *p_this )
135 {
136     vout_thread_t * p_vout = (vout_thread_t *)p_this;
137     vlc_value_t val;
138
139     /* Allocate structure */
140     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
141     if( p_vout->p_sys == NULL )
142     {
143         msg_Err( p_vout, "out of memory" );
144         return VLC_ENOMEM;
145     }
146
147     /* Initialisations */
148     p_vout->pf_init = Init;
149     p_vout->pf_end = End;
150     p_vout->pf_manage = Manage;
151     p_vout->pf_render = NULL;
152     p_vout->pf_display = Display;
153
154     p_vout->p_sys->p_ddobject = NULL;
155     p_vout->p_sys->p_display = NULL;
156     p_vout->p_sys->p_current_surface = NULL;
157     p_vout->p_sys->p_clipper = NULL;
158     p_vout->p_sys->hbrush = NULL;
159     p_vout->p_sys->hwnd = NULL;
160     p_vout->p_sys->hparent = NULL;
161     p_vout->p_sys->i_changes = 0;
162     p_vout->p_sys->b_caps_overlay_clipping = 0;
163     SetRectEmpty( &p_vout->p_sys->rect_display );
164     p_vout->p_sys->b_using_overlay = config_GetInt( p_vout, "overlay" );
165     p_vout->p_sys->b_use_sysmem = config_GetInt( p_vout, "directx-use-sysmem");
166     p_vout->p_sys->b_hw_yuv = config_GetInt( p_vout, "directx-hw-yuv" );
167     p_vout->p_sys->b_3buf_overlay = config_GetInt( p_vout, "directx-3buffering" );
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     /* Our surface can be lost so be sure to check this
529      * and restore it if need be */
530     if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
531         == DDERR_SURFACELOST )
532     {
533         if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
534             p_vout->p_sys->b_using_overlay )
535             DirectXUpdateOverlay( p_vout );
536     }
537
538     if( !p_vout->p_sys->b_using_overlay )
539     {
540         DDBLTFX  ddbltfx;
541
542         /* We ask for the "NOTEARING" option */
543         memset( &ddbltfx, 0, sizeof(DDBLTFX) );
544         ddbltfx.dwSize = sizeof(DDBLTFX);
545         ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
546
547         /* Blit video surface to display */
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         if( dxresult != DD_OK )
554         {
555             msg_Warn( p_vout, "could not blit surface (error %i)", dxresult );
556             return;
557         }
558
559     }
560     else /* using overlay */
561     {
562         /* Flip the overlay buffers if we are using back buffers */
563         if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
564         {
565             return;
566         }
567
568         dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
569                                              NULL, DDFLIP_WAIT );
570         if( dxresult != DD_OK )
571         {
572             msg_Warn( p_vout, "could not flip overlay (error %i)", dxresult );
573         }
574
575         /* set currently displayed pic */
576         p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
577
578         /* Lock surface to get all the required info */
579         if( DirectXLockSurface( p_vout, p_pic ) )
580         {
581             /* AAARRGG */
582             msg_Warn( p_vout, "cannot lock surface" );
583             return;
584         }
585         DirectXUnlockSurface( p_vout, p_pic );
586     }
587 }
588
589
590 /* following functions are local */
591
592 /*****************************************************************************
593  * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
594  *****************************************************************************
595  * This function initialise and allocate resources for DirectDraw.
596  *****************************************************************************/
597 static int DirectXInitDDraw( vout_thread_t *p_vout )
598 {
599     HRESULT    dxresult;
600     HRESULT    (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
601     LPDIRECTDRAW  p_ddobject;
602
603     msg_Dbg( p_vout, "DirectXInitDDraw" );
604
605     /* load direct draw DLL */
606     p_vout->p_sys->hddraw_dll = LoadLibrary("DDRAW.DLL");
607     if( p_vout->p_sys->hddraw_dll == NULL )
608     {
609         msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
610         goto error;
611     }
612
613     OurDirectDrawCreate =
614       (void *)GetProcAddress(p_vout->p_sys->hddraw_dll, "DirectDrawCreate");
615     if ( OurDirectDrawCreate == NULL )
616     {
617         msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
618         goto error;
619     }
620
621     /* Initialize DirectDraw now */
622     dxresult = OurDirectDrawCreate( NULL, &p_ddobject, NULL );
623     if( dxresult != DD_OK )
624     {
625         msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
626         goto error;
627     }
628
629     /* Get the IDirectDraw2 interface */
630     dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
631                                         (LPVOID *)&p_vout->p_sys->p_ddobject );
632     /* Release the unused interface */
633     IDirectDraw_Release( p_ddobject );
634     if( dxresult != DD_OK )
635     {
636         msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
637         goto error;
638     }
639
640     /* Set DirectDraw Cooperative level, ie what control we want over Windows
641      * display */
642     dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
643                                            p_vout->p_sys->hwnd, DDSCL_NORMAL );
644     if( dxresult != DD_OK )
645     {
646         msg_Err( p_vout, "cannot set direct draw cooperative level" );
647         goto error;
648     }
649
650     /* Probe the capabilities of the hardware */
651     DirectXGetDDrawCaps( p_vout );
652
653     msg_Dbg( p_vout, "End DirectXInitDDraw" );
654     return VLC_SUCCESS;
655
656  error:
657     if( p_vout->p_sys->p_ddobject )
658         IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
659     if( p_vout->p_sys->hddraw_dll )
660         FreeLibrary( p_vout->p_sys->hddraw_dll );
661     p_vout->p_sys->hddraw_dll = NULL;
662     p_vout->p_sys->p_ddobject = NULL;
663     return VLC_EGENERIC;
664 }
665
666 /*****************************************************************************
667  * DirectXCreateDisplay: create the DirectDraw display.
668  *****************************************************************************
669  * Create and initialize display according to preferences specified in the vout
670  * thread fields.
671  *****************************************************************************/
672 static int DirectXCreateDisplay( vout_thread_t *p_vout )
673 {
674     HRESULT              dxresult;
675     DDSURFACEDESC        ddsd;
676     LPDIRECTDRAWSURFACE  p_display;
677     DDPIXELFORMAT   pixel_format;
678
679     msg_Dbg( p_vout, "DirectXCreateDisplay" );
680
681     /* Now get the primary surface. This surface is what you actually see
682      * on your screen */
683     memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
684     ddsd.dwSize = sizeof(DDSURFACEDESC);
685     ddsd.dwFlags = DDSD_CAPS;
686     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
687
688     dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
689                                            &ddsd,
690                                            &p_display, NULL );
691     if( dxresult != DD_OK )
692     {
693         msg_Err( p_vout, "cannot get primary surface (error %i)", dxresult );
694         return VLC_EGENERIC;
695     }
696
697     dxresult = IDirectDrawSurface_QueryInterface( p_display,
698                                          &IID_IDirectDrawSurface2,
699                                          (LPVOID *)&p_vout->p_sys->p_display );
700     /* Release the old interface */
701     IDirectDrawSurface_Release( p_display );
702     if ( dxresult != DD_OK )
703     {
704         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
705                          "(error %i)", dxresult );
706         return VLC_EGENERIC;
707     }
708
709     /* The clipper will be used only in non-overlay mode */
710     DirectXCreateClipper( p_vout );
711
712
713 #if 1
714     /* compute the colorkey pixel value from the RGB value we've got */
715     memset( &pixel_format, 0, sizeof( DDPIXELFORMAT ));
716     pixel_format.dwSize = sizeof( DDPIXELFORMAT );
717     dxresult = IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
718                                                    &pixel_format );
719     if( dxresult != DD_OK )
720     {
721         msg_Warn( p_vout, "DirectXUpdateOverlay GetPixelFormat failed "
722                           "(error %i)", dxresult );
723     }
724     p_vout->p_sys->i_colorkey = (DWORD)((( p_vout->p_sys->i_rgb_colorkey
725                                            * pixel_format.dwRBitMask) / 255)
726                                         & pixel_format.dwRBitMask );
727 #endif
728
729     return VLC_SUCCESS;
730 }
731
732
733 /*****************************************************************************
734  * DirectXCreateClipper: Create a clipper that will be used when blitting the
735  *                       RGB surface to the main display.
736  *****************************************************************************
737  * This clipper prevents us to modify by mistake anything on the screen
738  * which doesn't belong to our window. For example when a part of our video
739  * window is hidden by another window.
740  *****************************************************************************/
741 static int DirectXCreateClipper( vout_thread_t *p_vout )
742 {
743     HRESULT dxresult;
744
745     msg_Dbg( p_vout, "DirectXCreateClipper" );
746
747     /* Create the clipper */
748     dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
749                                            &p_vout->p_sys->p_clipper, NULL );
750     if( dxresult != DD_OK )
751     {
752         msg_Warn( p_vout, "cannot create clipper (error %i)", dxresult );
753         goto error;
754     }
755
756     /* Associate the clipper to the window */
757     dxresult = IDirectDrawClipper_SetHWnd(p_vout->p_sys->p_clipper, 0,
758                                           p_vout->p_sys->hwnd);
759     if( dxresult != DD_OK )
760     {
761         msg_Warn( p_vout, "cannot attach clipper to window (error %i)",
762                           dxresult );
763         goto error;
764     }
765
766     /* associate the clipper with the surface */
767     dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
768                                              p_vout->p_sys->p_clipper);
769     if( dxresult != DD_OK )
770     {
771         msg_Warn( p_vout, "cannot attach clipper to surface (error %i)",
772                           dxresult );
773         goto error;
774     }
775
776     return VLC_SUCCESS;
777
778  error:
779     if( p_vout->p_sys->p_clipper )
780     {
781         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
782     }
783     p_vout->p_sys->p_clipper = NULL;
784     return VLC_EGENERIC;
785 }
786
787 /*****************************************************************************
788  * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
789  *****************************************************************************
790  * The best method of display is with an YUV overlay because the YUV->RGB
791  * conversion is done in hardware.
792  * You can also create a plain RGB surface.
793  * ( Maybe we could also try an RGB overlay surface, which could have hardware
794  * scaling and which would also be faster in window mode because you don't
795  * need to do any blitting to the main display...)
796  *****************************************************************************/
797 static int DirectXCreateSurface( vout_thread_t *p_vout,
798                                  LPDIRECTDRAWSURFACE2 *pp_surface_final,
799                                  int i_chroma, int b_overlay,
800                                  int i_backbuffers )
801 {
802     HRESULT dxresult;
803     LPDIRECTDRAWSURFACE p_surface;
804     DDSURFACEDESC ddsd;
805
806     /* Create the video surface */
807     if( b_overlay )
808     {
809         /* Now try to create the YUV overlay surface.
810          * This overlay will be displayed on top of the primary surface.
811          * A color key is used to determine whether or not the overlay will be
812          * displayed, ie the overlay will be displayed in place of the primary
813          * surface wherever the primary surface will have this color.
814          * The video window has been created with a background of this color so
815          * the overlay will be only displayed on top of this window */
816
817         memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
818         ddsd.dwSize = sizeof(DDSURFACEDESC);
819         ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
820         ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
821         ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
822         ddsd.dwFlags = DDSD_CAPS |
823                        DDSD_HEIGHT |
824                        DDSD_WIDTH |
825                        DDSD_PIXELFORMAT;
826         ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
827         ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY |
828                               DDSCAPS_VIDEOMEMORY;
829         ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
830                                 : 0 );
831         ddsd.dwHeight = p_vout->render.i_height;
832         ddsd.dwWidth = p_vout->render.i_width;
833         ddsd.dwBackBufferCount = i_backbuffers;
834
835         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
836                                                &ddsd,
837                                                &p_surface, NULL );
838         if( dxresult != DD_OK )
839         {
840             *pp_surface_final = NULL;
841             return VLC_EGENERIC;
842         }
843     }
844
845     if( !b_overlay )
846     {
847         vlc_bool_t b_rgb_surface =
848             ( i_chroma == VLC_FOURCC('R','G','B','2') )
849           || ( i_chroma == VLC_FOURCC('R','V','1','5') )
850            || ( i_chroma == VLC_FOURCC('R','V','1','6') )
851             || ( i_chroma == VLC_FOURCC('R','V','2','4') )
852              || ( i_chroma == VLC_FOURCC('R','V','3','2') );
853
854         memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
855         ddsd.dwSize = sizeof(DDSURFACEDESC);
856         ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
857         ddsd.dwFlags = DDSD_HEIGHT |
858                        DDSD_WIDTH |
859                        DDSD_CAPS;
860         ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
861         ddsd.dwHeight = p_vout->render.i_height;
862         ddsd.dwWidth = p_vout->render.i_width;
863
864         if( p_vout->p_sys->b_use_sysmem )
865             ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
866         else
867             ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
868
869         if( !b_rgb_surface )
870         {
871             ddsd.dwFlags |= DDSD_PIXELFORMAT;
872             ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
873             ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
874         }
875
876         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
877                                                &ddsd,
878                                                &p_surface, NULL );
879         if( dxresult != DD_OK )
880         {
881             *pp_surface_final = NULL;
882             return VLC_EGENERIC;
883         }
884     }
885
886     /* Now that the surface is created, try to get a newer DirectX interface */
887     dxresult = IDirectDrawSurface_QueryInterface( p_surface,
888                                      &IID_IDirectDrawSurface2,
889                                      (LPVOID *)pp_surface_final );
890     IDirectDrawSurface_Release( p_surface );    /* Release the old interface */
891     if ( dxresult != DD_OK )
892     {
893         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
894                          "(error %i)", dxresult );
895         *pp_surface_final = NULL;
896         return VLC_EGENERIC;
897     }
898
899     return VLC_SUCCESS;
900 }
901
902 /*****************************************************************************
903  * DirectXUpdateOverlay: Move or resize overlay surface on video display.
904  *****************************************************************************
905  * This function is used to move or resize an overlay surface on the screen.
906  * Ususally the overlay is moved by the user and thus, by a move or resize
907  * event (in Manage).
908  *****************************************************************************/
909 void DirectXUpdateOverlay( vout_thread_t *p_vout )
910 {
911     DDOVERLAYFX     ddofx;
912     DWORD           dwFlags;
913     HRESULT         dxresult;
914
915     if( p_vout->p_sys->p_current_surface == NULL ||
916         !p_vout->p_sys->b_using_overlay )
917         return;
918
919     /* The new window dimensions should already have been computed by the
920      * caller of this function */
921
922     /* Position and show the overlay */
923     memset(&ddofx, 0, sizeof(DDOVERLAYFX));
924     ddofx.dwSize = sizeof(DDOVERLAYFX);
925     ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
926     ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
927
928     dwFlags = DDOVER_SHOW;
929     if( !p_vout->p_sys->b_caps_overlay_clipping )
930         dwFlags |= DDOVER_KEYDESTOVERRIDE;
931
932     dxresult = IDirectDrawSurface2_UpdateOverlay(
933                                          p_vout->p_sys->p_current_surface,
934                                          &p_vout->p_sys->rect_src_clipped,
935                                          p_vout->p_sys->p_display,
936                                          &p_vout->p_sys->rect_dest_clipped,
937                                          dwFlags, &ddofx );
938     if(dxresult != DD_OK)
939     {
940         msg_Warn( p_vout,
941                   "DirectXUpdateOverlay cannot move or resize overlay" );
942     }
943 }
944
945 /*****************************************************************************
946  * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
947  *****************************************************************************
948  * This function returns all resources allocated by DirectXInitDDraw.
949  *****************************************************************************/
950 static void DirectXCloseDDraw( vout_thread_t *p_vout )
951 {
952     msg_Dbg( p_vout, "DirectXCloseDDraw" );
953     if( p_vout->p_sys->p_ddobject != NULL )
954     {
955         IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
956         p_vout->p_sys->p_ddobject = NULL;
957     }
958
959     if( p_vout->p_sys->hddraw_dll != NULL )
960     {
961         FreeLibrary( p_vout->p_sys->hddraw_dll );
962         p_vout->p_sys->hddraw_dll = NULL;
963     }
964 }
965
966 /*****************************************************************************
967  * DirectXCloseDisplay: close and reset the DirectX display device
968  *****************************************************************************
969  * This function returns all resources allocated by DirectXCreateDisplay.
970  *****************************************************************************/
971 static void DirectXCloseDisplay( vout_thread_t *p_vout )
972 {
973     msg_Dbg( p_vout, "DirectXCloseDisplay" );
974
975     if( p_vout->p_sys->p_clipper != NULL )
976     {
977         msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
978         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
979         p_vout->p_sys->p_clipper = NULL;
980     }
981
982     if( p_vout->p_sys->p_display != NULL )
983     {
984         msg_Dbg( p_vout, "DirectXCloseDisplay display" );
985         IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
986         p_vout->p_sys->p_display = NULL;
987     }
988 }
989
990 /*****************************************************************************
991  * DirectXCloseSurface: close the YUV overlay or RGB surface.
992  *****************************************************************************
993  * This function returns all resources allocated for the surface.
994  *****************************************************************************/
995 static void DirectXCloseSurface( vout_thread_t *p_vout,
996                                  LPDIRECTDRAWSURFACE2 p_surface )
997 {
998     msg_Dbg( p_vout, "DirectXCloseSurface" );
999     if( p_surface != NULL )
1000     {
1001         IDirectDrawSurface2_Release( p_surface );
1002     }
1003 }
1004
1005 /*****************************************************************************
1006  * NewPictureVec: allocate a vector of identical pictures
1007  *****************************************************************************
1008  * Returns 0 on success, -1 otherwise
1009  *****************************************************************************/
1010 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1011                           int i_num_pics )
1012 {
1013     int i;
1014     int i_ret = VLC_SUCCESS;
1015     LPDIRECTDRAWSURFACE2 p_surface;
1016
1017     msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1018              p_vout->p_sys->b_using_overlay ? "yes" : "no",
1019              (char *)&p_vout->output.i_chroma );
1020
1021     I_OUTPUTPICTURES = 0;
1022
1023     /* First we try to use an YUV overlay surface.
1024      * The overlay surface that we create won't be used to decode directly
1025      * into it because accessing video memory directly is way to slow (remember
1026      * that pictures are decoded macroblock per macroblock). Instead the video
1027      * will be decoded in picture buffers in system memory which will then be
1028      * memcpy() to the overlay surface. */
1029     if( p_vout->p_sys->b_using_overlay )
1030     {
1031         /* Triple buffering rocks! it doesn't have any processing overhead
1032          * (you don't have to wait for the vsync) and provides for a very nice
1033          * video quality (no tearing). */
1034         if( p_vout->p_sys->b_3buf_overlay )
1035             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1036                                           p_vout->output.i_chroma,
1037                                           p_vout->p_sys->b_using_overlay,
1038                                           2 /* number of backbuffers */ );
1039
1040         if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1041         {
1042             /* Try to reduce the number of backbuffers */
1043             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1044                                           p_vout->output.i_chroma,
1045                                           p_vout->p_sys->b_using_overlay,
1046                                           0 /* number of backbuffers */ );
1047         }
1048
1049         if( i_ret == VLC_SUCCESS )
1050         {
1051             DDSCAPS dds_caps;
1052             picture_t front_pic;
1053             picture_sys_t front_pic_sys;
1054             front_pic.p_sys = &front_pic_sys;
1055
1056             /* Allocate internal structure */
1057             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1058             if( p_pic[0].p_sys == NULL )
1059             {
1060                 DirectXCloseSurface( p_vout, p_surface );
1061                 return VLC_ENOMEM;
1062             }
1063
1064             /* set front buffer */
1065             p_pic[0].p_sys->p_front_surface = p_surface;
1066
1067             /* Get the back buffer */
1068             memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1069             dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1070             if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1071                                                 p_surface, &dds_caps,
1072                                                 &p_pic[0].p_sys->p_surface ) )
1073             {
1074                 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1075                 /* front buffer is the same as back buffer */
1076                 p_pic[0].p_sys->p_surface = p_surface;
1077             }
1078
1079
1080             p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1081                 p_pic[0].p_sys->p_front_surface;
1082
1083             /* Reset the front buffer memory */
1084             if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1085             {
1086                 int i,j;
1087                 for( i = 0; i < front_pic.i_planes; i++ )
1088                     for( j = 0; j < front_pic.p[i].i_lines; j++)
1089                         memset( front_pic.p[i].p_pixels + j *
1090                                 front_pic.p[i].i_pitch, 127,
1091                                 front_pic.p[i].i_visible_pitch );
1092
1093                 DirectXUnlockSurface( p_vout, &front_pic );
1094             }
1095
1096             DirectXUpdateOverlay( p_vout );
1097             I_OUTPUTPICTURES = 1;
1098             msg_Dbg( p_vout, "YUV overlay created successfully" );
1099         }
1100     }
1101
1102     /* As we can't have an overlay, we'll try to create a plain offscreen
1103      * surface. This surface will reside in video memory because there's a
1104      * better chance then that we'll be able to use some kind of hardware
1105      * acceleration like rescaling, blitting or YUV->RGB conversions.
1106      * We then only need to blit this surface onto the main display when we
1107      * want to display it */
1108     if( !p_vout->p_sys->b_using_overlay )
1109     {
1110         if( p_vout->p_sys->b_hw_yuv )
1111         {
1112             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1113                                           p_vout->output.i_chroma,
1114                                           0 /* no overlay */,
1115                                           0 /* no back buffers */ );
1116         }
1117
1118         if( i_ret || !p_vout->p_sys->b_hw_yuv )
1119         {
1120             /* Our last choice is to use a plain RGB surface */
1121             DDPIXELFORMAT ddpfPixelFormat;
1122
1123             ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1124             IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1125                                                 &ddpfPixelFormat );
1126
1127             if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1128             {
1129                 switch( ddpfPixelFormat.dwRGBBitCount )
1130                 {
1131                 case 8: /* FIXME: set the palette */
1132                     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
1133                     break;
1134                 case 15:
1135                     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
1136                     break;
1137                 case 16:
1138                     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
1139                     break;
1140                 case 24:
1141                     p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
1142                     break;
1143                 case 32:
1144                     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
1145                     break;
1146                 default:
1147                     msg_Err( p_vout, "unknown screen depth" );
1148                     return VLC_EGENERIC;
1149                 }
1150                 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1151                 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1152                 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1153             }
1154
1155             p_vout->p_sys->b_hw_yuv = 0;
1156
1157             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1158                                           p_vout->output.i_chroma,
1159                                           0 /* no overlay */,
1160                                           0 /* no back buffers */ );
1161
1162             if( i_ret && !p_vout->p_sys->b_use_sysmem )
1163             {
1164                 /* Give it a last try with b_use_sysmem enabled */
1165                 p_vout->p_sys->b_use_sysmem = 1;
1166
1167                 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1168                                               p_vout->output.i_chroma,
1169                                               0 /* no overlay */,
1170                                               0 /* no back buffers */ );
1171             }
1172         }
1173
1174         if( i_ret == VLC_SUCCESS )
1175         {
1176             /* Allocate internal structure */
1177             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1178             if( p_pic[0].p_sys == NULL )
1179             {
1180                 DirectXCloseSurface( p_vout, p_surface );
1181                 return VLC_ENOMEM;
1182             }
1183
1184             p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1185                 = p_surface;
1186
1187             I_OUTPUTPICTURES = 1;
1188
1189             msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1190                      (char *)&p_vout->output.i_chroma );
1191         }
1192     }
1193
1194
1195     /* Now that we've got all our direct-buffers, we can finish filling in the
1196      * picture_t structures */
1197     for( i = 0; i < I_OUTPUTPICTURES; i++ )
1198     {
1199         p_pic[i].i_status = DESTROYED_PICTURE;
1200         p_pic[i].i_type   = DIRECT_PICTURE;
1201         p_pic[i].pf_lock  = DirectXLockSurface;
1202         p_pic[i].pf_unlock = DirectXUnlockSurface;
1203         PP_OUTPUTPICTURE[i] = &p_pic[i];
1204
1205         if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1206         {
1207             /* AAARRGG */
1208             FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1209             I_OUTPUTPICTURES = 0;
1210             msg_Err( p_vout, "cannot lock surface" );
1211             return VLC_EGENERIC;
1212         }
1213         DirectXUnlockSurface( p_vout, &p_pic[i] );
1214     }
1215
1216     msg_Dbg( p_vout, "End NewPictureVec (%s)",
1217              I_OUTPUTPICTURES ? "succeeded" : "failed" );
1218
1219     return VLC_SUCCESS;
1220 }
1221
1222 /*****************************************************************************
1223  * FreePicture: destroy a picture vector allocated with NewPictureVec
1224  *****************************************************************************
1225  *
1226  *****************************************************************************/
1227 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1228                             int i_num_pics )
1229 {
1230     int i;
1231
1232     for( i = 0; i < i_num_pics; i++ )
1233     {
1234         DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1235
1236         for( i = 0; i < i_num_pics; i++ )
1237         {
1238             free( p_pic[i].p_sys );
1239         }
1240     }
1241 }
1242
1243 /*****************************************************************************
1244  * UpdatePictureStruct: updates the internal data in the picture_t structure
1245  *****************************************************************************
1246  * This will setup stuff for use by the video_output thread
1247  *****************************************************************************/
1248 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1249                                 int i_chroma )
1250 {
1251     switch( p_vout->output.i_chroma )
1252     {
1253         case VLC_FOURCC('R','G','B','2'):
1254         case VLC_FOURCC('R','V','1','5'):
1255         case VLC_FOURCC('R','V','1','6'):
1256         case VLC_FOURCC('R','V','2','4'):
1257         case VLC_FOURCC('R','V','3','2'):
1258             p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1259             p_pic->p->i_lines = p_vout->output.i_height;
1260             p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1261             switch( p_vout->output.i_chroma )
1262             {
1263                 case VLC_FOURCC('R','G','B','2'):
1264                     p_pic->p->i_pixel_pitch = 1;
1265                     break;
1266                 case VLC_FOURCC('R','V','1','5'):
1267                 case VLC_FOURCC('R','V','1','6'):
1268                     p_pic->p->i_pixel_pitch = 2;
1269                     break;
1270                 case VLC_FOURCC('R','V','2','4'):
1271                 case VLC_FOURCC('R','V','3','2'):
1272                     p_pic->p->i_pixel_pitch = 4;
1273                     break;
1274                 default:
1275                     return VLC_EGENERIC;
1276             }
1277             p_pic->p->i_visible_pitch = p_vout->output.i_width *
1278               p_pic->p->i_pixel_pitch;
1279             p_pic->i_planes = 1;
1280             break;
1281
1282         case VLC_FOURCC('Y','V','1','2'):
1283
1284             p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1285             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1286             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1287             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1288             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1289               p_pic->p[Y_PLANE].i_pixel_pitch;
1290
1291             p_pic->V_PIXELS =  p_pic->Y_PIXELS
1292               + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1293             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1294             p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1295             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1296             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1297               p_pic->p[V_PLANE].i_pixel_pitch;
1298
1299             p_pic->U_PIXELS = p_pic->V_PIXELS
1300               + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1301             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1302             p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1303             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1304             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1305               p_pic->p[U_PLANE].i_pixel_pitch;
1306
1307             p_pic->i_planes = 3;
1308             break;
1309
1310         case VLC_FOURCC('I','Y','U','V'):
1311
1312             p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1313             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1314             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1315             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1316             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1317               p_pic->p[Y_PLANE].i_pixel_pitch;
1318
1319             p_pic->U_PIXELS = p_pic->Y_PIXELS
1320               + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1321             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1322             p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1323             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1324             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1325               p_pic->p[U_PLANE].i_pixel_pitch;
1326
1327             p_pic->V_PIXELS =  p_pic->U_PIXELS
1328               + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1329             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1330             p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1331             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1332             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1333               p_pic->p[V_PLANE].i_pixel_pitch;
1334
1335             p_pic->i_planes = 3;
1336             break;
1337
1338         case VLC_FOURCC('Y','U','Y','2'):
1339
1340             p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1341             p_pic->p->i_lines = p_vout->output.i_height;
1342             p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1343             p_pic->p->i_pixel_pitch = 2;
1344             p_pic->p->i_visible_pitch = p_vout->output.i_width *
1345               p_pic->p->i_pixel_pitch;
1346
1347             p_pic->i_planes = 1;
1348             break;
1349
1350         default:
1351             /* Unknown chroma, tell the guy to get lost */
1352             msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1353                      p_vout->output.i_chroma,
1354                      (char*)&p_vout->output.i_chroma );
1355             return VLC_EGENERIC;
1356     }
1357
1358     return VLC_SUCCESS;
1359 }
1360
1361 /*****************************************************************************
1362  * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1363  *****************************************************************************
1364  * It is nice to know which features are supported by the hardware so we can
1365  * find ways to optimize our rendering.
1366  *****************************************************************************/
1367 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1368 {
1369     DDCAPS ddcaps;
1370     HRESULT dxresult;
1371
1372     /* This is just an indication of whether or not we'll support overlay,
1373      * but with this test we don't know if we support YUV overlay */
1374     memset( &ddcaps, 0, sizeof( DDCAPS ));
1375     ddcaps.dwSize = sizeof(DDCAPS);
1376     dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1377                                      &ddcaps, NULL );
1378     if(dxresult != DD_OK )
1379     {
1380         msg_Warn( p_vout, "cannot get caps" );
1381     }
1382     else
1383     {
1384         BOOL bHasOverlay, bHasOverlayFourCC, bCanClipOverlay,
1385              bHasColorKey, bCanStretch;
1386
1387         /* Determine if the hardware supports overlay surfaces */
1388         bHasOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAY) ==
1389                        DDCAPS_OVERLAY) ? TRUE : FALSE;
1390         /* Determine if the hardware supports overlay surfaces */
1391         bHasOverlayFourCC = ((ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ==
1392                        DDCAPS_OVERLAYFOURCC) ? TRUE : FALSE;
1393         /* Determine if the hardware supports overlay surfaces */
1394         bCanClipOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAYCANTCLIP) ==
1395                        0 ) ? TRUE : FALSE;
1396         /* Determine if the hardware supports colorkeying */
1397         bHasColorKey = ((ddcaps.dwCaps & DDCAPS_COLORKEY) ==
1398                         DDCAPS_COLORKEY) ? TRUE : FALSE;
1399         /* Determine if the hardware supports scaling of the overlay surface */
1400         bCanStretch = ((ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ==
1401                        DDCAPS_OVERLAYSTRETCH) ? TRUE : FALSE;
1402         msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1403                          "can_clip_overlay=%i colorkey=%i stretch=%i",
1404                          bHasOverlay, bHasOverlayFourCC, bCanClipOverlay,
1405                          bHasColorKey, bCanStretch );
1406
1407         /* Overlay clipping support is interesting for us as it means we can
1408          * get rid of the colorkey alltogether */
1409         p_vout->p_sys->b_caps_overlay_clipping = bCanClipOverlay;
1410
1411     }
1412 }
1413
1414 /*****************************************************************************
1415  * DirectXLockSurface: Lock surface and get picture data pointer
1416  *****************************************************************************
1417  * This function locks a surface and get the surface descriptor which amongst
1418  * other things has the pointer to the picture data.
1419  *****************************************************************************/
1420 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1421 {
1422     HRESULT dxresult;
1423
1424     /* Lock the surface to get a valid pointer to the picture buffer */
1425     memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1426     p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1427     dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1428                                          NULL, &p_pic->p_sys->ddsd,
1429                                          DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1430                                          NULL );
1431     if( dxresult != DD_OK )
1432     {
1433         if( dxresult == DDERR_INVALIDPARAMS )
1434         {
1435             /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1436              * in an invalid params error */
1437             dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1438                                              &p_pic->p_sys->ddsd,
1439                                              DDLOCK_WAIT, NULL);
1440         }
1441         if( dxresult == DDERR_SURFACELOST )
1442         {
1443             /* Your surface can be lost so be sure
1444              * to check this and restore it if needed */
1445
1446             /* When using overlays with back-buffers, we need to restore
1447              * the front buffer so the back-buffers get restored as well. */
1448             if( p_vout->p_sys->b_using_overlay  )
1449                 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1450             else
1451                 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1452
1453             dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1454                                                  &p_pic->p_sys->ddsd,
1455                                                  DDLOCK_WAIT, NULL);
1456             if( dxresult == DDERR_SURFACELOST )
1457                 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1458         }
1459         if( dxresult != DD_OK )
1460         {
1461             return VLC_EGENERIC;
1462         }
1463     }
1464
1465     /* Now we have a pointer to the surface memory, we can update our picture
1466      * structure. */
1467     if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma )
1468         != VLC_SUCCESS )
1469     {
1470         DirectXUnlockSurface( p_vout, p_pic );
1471         return VLC_EGENERIC;
1472     }
1473     else
1474         return VLC_SUCCESS;      
1475 }
1476
1477 /*****************************************************************************
1478  * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1479  *****************************************************************************/
1480 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1481 {
1482     /* Unlock the Surface */
1483     if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1484         return VLC_SUCCESS;
1485     else
1486         return VLC_EGENERIC;
1487 }