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