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