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