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