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