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