]> git.sesse.net Git - vlc/blob - modules/video_output/msw/direct3d.c
* modules/video_output/msw/direct3d.c: typo adpater -> adapter
[vlc] / modules / video_output / msw / direct3d.c
1 /*****************************************************************************
2  * direct3d.c: Windows Direct3D video output module
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  *$Id$
6  *
7  * Authors: Damien Fouilleul <damienf@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble:
26  *
27  * This plugin will use YUV surface if supported, using YUV will result in
28  * the best video quality (hardware filering 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.
34  *
35  *****************************************************************************/
36 #include <errno.h>                                                 /* ENOMEM */
37 #include <stdlib.h>                                                /* free() */
38 #include <string.h>                                            /* strerror() */
39
40 #include <vlc/vlc.h>
41 #include <vlc_interface.h>
42 #include <vlc_playlist.h>
43 #include <vlc_vout.h>
44
45 #include <windows.h>
46 #include <d3d9.h>
47
48 #include "vout.h"
49
50 /*****************************************************************************
51  * Local prototypes.
52  *****************************************************************************/
53 static int  OpenVideo  ( vlc_object_t * );
54 static void CloseVideo ( vlc_object_t * );
55
56 static int  Init      ( vout_thread_t * );
57 static void End       ( vout_thread_t * );
58 static int  Manage    ( vout_thread_t * );
59 static void Display   ( vout_thread_t *, picture_t * );
60 static void FirstDisplay( vout_thread_t *, picture_t * );
61
62 static int Direct3DVoutCreate     ( vout_thread_t * );
63 static void Direct3DVoutRelease   ( vout_thread_t * );
64
65 static int  Direct3DVoutOpen      ( vout_thread_t * );
66 static void Direct3DVoutClose     ( vout_thread_t * );
67
68 static int Direct3DVoutResetDevice( vout_thread_t * );
69
70 static int Direct3DVoutCreatePictures   ( vout_thread_t *, size_t );
71 static void Direct3DVoutReleasePictures ( vout_thread_t * );
72
73 static int Direct3DVoutLockSurface  ( vout_thread_t *, picture_t * );
74 static int Direct3DVoutUnlockSurface( vout_thread_t *, picture_t * );
75
76 static int Direct3DVoutCreateScene      ( vout_thread_t * );
77 static void Direct3DVoutReleaseScene    ( vout_thread_t * );
78 static void Direct3DVoutRenderScene     ( vout_thread_t *, picture_t * );
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83
84 static vlc_bool_t _got_vista_or_above;
85
86 static int get_capability_for_osversion(void)
87 {
88     OSVERSIONINFO winVer;
89     winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
90
91     if( GetVersionEx(&winVer) )
92     {
93         if( winVer.dwMajorVersion > 5 )
94         {
95             /* Windows Vista or above, make this module the default */
96             _got_vista_or_above = VLC_TRUE;
97             return 150;
98         }
99     }
100     /* Windows XP or lower, make sure this module isn't the default */
101     _got_vista_or_above = VLC_FALSE;
102     return 50;
103 }
104
105 vlc_module_begin();
106     set_shortname( "Direct3D" );
107     set_category( CAT_VIDEO );
108     set_subcategory( SUBCAT_VIDEO_VOUT );
109     set_description( _("DirectX 3D video output") );
110     set_capability( "video output", get_capability_for_osversion() );
111     add_shortcut( "direct3d" );
112     set_callbacks( OpenVideo, CloseVideo );
113
114     /* FIXME: Hack to avoid unregistering our window class */
115     linked_with_a_crap_library_which_uses_atexit( );
116 vlc_module_end();
117
118 #if 0 /* FIXME */
119     /* check if we registered a window class because we need to
120      * unregister it */
121     WNDCLASS wndclass;
122     if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
123         UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
124 #endif
125
126 /*****************************************************************************
127  * CUSTOMVERTEX: 
128  *****************************************************************************
129  *****************************************************************************/
130 typedef struct 
131 {
132     FLOAT       x,y,z;      // vertex untransformed position 
133     FLOAT       rhw;        // eye distance
134     D3DCOLOR    diffuse;    // diffuse color
135     FLOAT       tu, tv;     // texture relative coordinates
136 } CUSTOMVERTEX;
137
138 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
139
140 /*****************************************************************************
141  * OpenVideo: allocate Vout video thread output method
142  *****************************************************************************
143  * This function allocates and initialize the Direct3D vout method.
144  *****************************************************************************/
145 static int OpenVideo( vlc_object_t *p_this )
146 {
147     vout_thread_t * p_vout = (vout_thread_t *)p_this;
148     vlc_value_t val;
149
150     /* Allocate structure */
151     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
152     if( p_vout->p_sys == NULL )
153     {
154         msg_Err( p_vout, "out of memory" );
155         return VLC_ENOMEM;
156     }
157     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
158
159     if( VLC_SUCCESS != Direct3DVoutCreate( p_vout ) )
160     {
161         msg_Err( p_vout, "Direct3D could not be initialized !");
162         goto error;
163     }
164
165     /* Initialisations */
166     p_vout->pf_init = Init;
167     p_vout->pf_end = End;
168     p_vout->pf_manage = Manage;
169     p_vout->pf_render = Direct3DVoutRenderScene;
170     p_vout->pf_display = FirstDisplay;
171
172     p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
173     p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
174     p_vout->p_sys->i_changes = 0;
175     vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
176     SetRectEmpty( &p_vout->p_sys->rect_display );
177     SetRectEmpty( &p_vout->p_sys->rect_parent );
178
179     var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
180     var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
181
182     p_vout->p_sys->b_cursor_hidden = 0;
183     p_vout->p_sys->i_lastmoved = mdate();
184
185     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
186     var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
187
188     /* Set main window's size */
189     p_vout->p_sys->i_window_width = p_vout->i_window_width;
190     p_vout->p_sys->i_window_height = p_vout->i_window_height;
191
192     /* Create the Vout EventThread, this thread is created by us to isolate
193      * the Win32 PeekMessage function calls. We want to do this because
194      * Windows can stay blocked inside this call for a long time, and when
195      * this happens it thus blocks vlc's video_output thread.
196      * Vout EventThread will take care of the creation of the video
197      * window (because PeekMessage has to be called from the same thread which
198      * created the window). */
199     msg_Dbg( p_vout, "creating Vout EventThread" );
200     p_vout->p_sys->p_event =
201         vlc_object_create( p_vout, sizeof(event_thread_t) );
202     p_vout->p_sys->p_event->p_vout = p_vout;
203     if( vlc_thread_create( p_vout->p_sys->p_event, "Vout Events Thread",
204                            E_(EventThread), 0, 1 ) )
205     {
206         msg_Err( p_vout, "cannot create Vout EventThread" );
207         vlc_object_destroy( p_vout->p_sys->p_event );
208         p_vout->p_sys->p_event = NULL;
209         goto error;
210     }
211
212     if( p_vout->p_sys->p_event->b_error )
213     {
214         msg_Err( p_vout, "Vout EventThread failed" );
215         goto error;
216     }
217
218     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
219
220     msg_Dbg( p_vout, "Vout EventThread running" );
221
222     /* Variable to indicate if the window should be on top of others */
223     /* Trigger a callback right now */
224     var_Get( p_vout, "video-on-top", &val );
225     var_Set( p_vout, "video-on-top", val );
226
227     /* disable screensaver by temporarily changing system settings */
228     p_vout->p_sys->i_spi_lowpowertimeout = 0;
229     p_vout->p_sys->i_spi_powerofftimeout = 0;
230     p_vout->p_sys->i_spi_screensavetimeout = 0;
231     var_Get( p_vout, "disable-screensaver", &val);
232     if( val.b_bool ) {
233         msg_Dbg(p_vout, "disabling screen saver");
234         SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
235             0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
236         if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
237             SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
238         }
239         SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
240             &(p_vout->p_sys->i_spi_powerofftimeout), 0);
241         if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
242             SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
243         }
244         SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
245             &(p_vout->p_sys->i_spi_screensavetimeout), 0);
246         if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
247             SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
248         }
249     }
250     return VLC_SUCCESS;
251
252  error:
253     CloseVideo( VLC_OBJECT(p_vout) );
254     return VLC_EGENERIC;
255 }
256
257 /*****************************************************************************
258  * CloseVideo: destroy Sys video thread output method
259  *****************************************************************************
260  * Terminate an output method created by Create
261  *****************************************************************************/
262 static void CloseVideo( vlc_object_t *p_this )
263 {
264     vout_thread_t * p_vout = (vout_thread_t *)p_this;
265
266     Direct3DVoutRelease( p_vout );
267
268     if( p_vout->p_sys->p_event )
269     {
270         vlc_object_detach( p_vout->p_sys->p_event );
271
272         /* Kill Vout EventThread */
273         vlc_object_kill( p_vout->p_sys->p_event );
274
275         /* we need to be sure Vout EventThread won't stay stuck in
276          * GetMessage, so we send a fake message */
277         if( p_vout->p_sys->hwnd )
278         {
279             PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
280         }
281
282         vlc_thread_join( p_vout->p_sys->p_event );
283         vlc_object_destroy( p_vout->p_sys->p_event );
284     }
285
286     vlc_mutex_destroy( &p_vout->p_sys->lock );
287
288     /* restore screensaver system settings */
289     if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
290         SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
291             p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
292     }
293     if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
294         SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
295             p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
296     }
297     if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
298         SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
299             p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
300     }
301
302     if( p_vout->p_sys )
303     {
304         free( p_vout->p_sys );
305         p_vout->p_sys = NULL;
306     }
307 }
308
309 /*****************************************************************************
310  * Init: initialize Direct3D video thread output method
311  *****************************************************************************/
312 static int Init( vout_thread_t *p_vout )
313 {
314     int i_ret;
315     vlc_value_t val;
316
317     var_Get( p_vout, "directx-hw-yuv", &val );
318     p_vout->p_sys->b_hw_yuv = val.b_bool;
319
320     /* Initialise Direct3D */
321     if( VLC_SUCCESS != Direct3DVoutOpen( p_vout ) )
322     {
323         msg_Err( p_vout, "cannot initialize Direct3D" );
324         return VLC_EGENERIC;
325     }
326
327     /* Initialize the output structure.
328      * Since Direct3D can do rescaling for us, stick to the default
329      * coordinates and aspect. */
330     p_vout->output.i_width  = p_vout->render.i_width;
331     p_vout->output.i_height = p_vout->render.i_height;
332     p_vout->output.i_aspect = p_vout->render.i_aspect;
333     p_vout->fmt_out = p_vout->fmt_in;
334     E_(UpdateRects)( p_vout, VLC_TRUE );
335
336     /*  create picture pool */
337     p_vout->output.i_chroma = 0;
338     i_ret = Direct3DVoutCreatePictures(p_vout, 1);
339     if( VLC_SUCCESS != i_ret )
340     {
341         msg_Err(p_vout, "Direct3D picture pool initialization failed !");
342         return i_ret;
343     }
344
345     /* create scene */
346     i_ret = Direct3DVoutCreateScene(p_vout);
347     if( VLC_SUCCESS != i_ret )
348     {
349         msg_Err(p_vout, "Direct3D scene initialization failed !");
350         Direct3DVoutReleasePictures(p_vout);
351         return i_ret;
352     }
353
354     /* Change the window title bar text */
355     PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
356
357     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
358     return VLC_SUCCESS;
359 }
360
361 /*****************************************************************************
362  * End: terminate Sys video thread output method
363  *****************************************************************************
364  * Terminate an output method created by Create.
365  * It is called at the end of the thread.
366  *****************************************************************************/
367 static void End( vout_thread_t *p_vout )
368 {
369     Direct3DVoutReleaseScene(p_vout);
370     Direct3DVoutReleasePictures(p_vout);
371     Direct3DVoutClose( p_vout );
372 }
373
374 /*****************************************************************************
375  * Manage: handle Sys events
376  *****************************************************************************
377  * This function should be called regularly by the video output thread.
378  * It returns a non null value if an error occurred.
379  *****************************************************************************/
380 static int Manage( vout_thread_t *p_vout )
381 {
382     /* If we do not control our window, we check for geometry changes
383      * ourselves because the parent might not send us its events. */
384     vlc_mutex_lock( &p_vout->p_sys->lock );
385     if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
386     {
387         RECT rect_parent;
388         POINT point;
389
390         vlc_mutex_unlock( &p_vout->p_sys->lock );
391
392         GetClientRect( p_vout->p_sys->hparent, &rect_parent );
393         point.x = point.y = 0;
394         ClientToScreen( p_vout->p_sys->hparent, &point );
395         OffsetRect( &rect_parent, point.x, point.y );
396
397         if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
398         {
399             p_vout->p_sys->rect_parent = rect_parent;
400
401             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
402                           rect_parent.right - rect_parent.left,
403                           rect_parent.bottom - rect_parent.top,
404                           SWP_NOZORDER );
405         }
406     }
407     else
408     {
409         vlc_mutex_unlock( &p_vout->p_sys->lock );
410     }
411
412     /*
413      * Position Change
414      */
415     if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
416     {
417 #if 0 /* need that when bicubic filter is available */
418         RECT rect;
419         UINT width, height;
420
421         GetClientRect(p_vout->p_sys->hvideownd, &rect);
422         width  = rect.right-rect.left;
423         height = rect.bottom-rect.top;
424
425         if( (width != p_vout->p_sys->d3dpp.BackBufferWidth)
426          || (height != p_vout->p_sys->d3dpp.BackBufferHeight) )
427         {
428             msg_Dbg(p_vout, "resizing device back buffers to (%lux%lu)", width, height);
429             // need to reset D3D device to resize back buffer
430             if( VLC_SUCCESS != Direct3DVoutResetDevice(p_vout, width, height) )
431                 return VLC_EGENERIC;
432         }
433 #endif
434         p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
435     }
436
437     /* Check for cropping / aspect changes */
438     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
439         p_vout->i_changes & VOUT_ASPECT_CHANGE )
440     {
441         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
442         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
443
444         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
445         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
446         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
447         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
448         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
449         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
450         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
451         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
452         E_(UpdateRects)( p_vout, VLC_TRUE );
453     }
454
455     /* We used to call the Win32 PeekMessage function here to read the window
456      * messages. But since window can stay blocked into this function for a
457      * long time (for example when you move your window on the screen), I
458      * decided to isolate PeekMessage in another thread. */
459
460     /*
461      * Fullscreen change
462      */
463     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
464         || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
465     {
466         Win32ToggleFullscreen( p_vout );
467
468         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
469         p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
470     }
471
472     /*
473      * Pointer change
474      */
475     if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
476         (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 )
477     {
478         POINT point;
479         HWND hwnd;
480
481         /* Hide the cursor only if it is inside our window */
482         GetCursorPos( &point );
483         hwnd = WindowFromPoint(point);
484         if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
485         {
486             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
487         }
488         else
489         {
490             p_vout->p_sys->i_lastmoved = mdate();
491         }
492     }
493
494     /*
495      * "Always on top" status change
496      */
497     if( p_vout->p_sys->b_on_top_change )
498     {
499         vlc_value_t val;
500         HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
501
502         var_Get( p_vout, "video-on-top", &val );
503
504         /* Set the window on top if necessary */
505         if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
506                            & WS_EX_TOPMOST ) )
507         {
508             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
509                            MF_BYCOMMAND | MFS_CHECKED );
510             SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
511                           SWP_NOSIZE | SWP_NOMOVE );
512         }
513         else
514         /* The window shouldn't be on top */
515         if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
516                            & WS_EX_TOPMOST ) )
517         {
518             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
519                            MF_BYCOMMAND | MFS_UNCHECKED );
520             SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
521                           SWP_NOSIZE | SWP_NOMOVE );
522         }
523
524         p_vout->p_sys->b_on_top_change = VLC_FALSE;
525     }
526
527     /* Check if the event thread is still running */
528     if( p_vout->p_sys->p_event->b_die )
529     {
530         return VLC_EGENERIC; /* exit */
531     }
532
533     return VLC_SUCCESS;
534 }
535
536 /*****************************************************************************
537  * Display: displays previously rendered output
538  *****************************************************************************
539  * This function sends the currently rendered image to the display, wait until
540  * it is displayed and switch the two rendering buffers, preparing next frame.
541  *****************************************************************************/
542 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
543 {
544     LPDIRECT3DDEVICE9       p_d3ddev = p_vout->p_sys->p_d3ddev;
545     // Present the back buffer contents to the display
546     // stretching and filtering happens here
547     HRESULT hr = IDirect3DDevice9_Present(p_d3ddev,
548                     &(p_vout->p_sys->rect_src_clipped),
549                     NULL, NULL, NULL);
550     if( FAILED(hr) )
551         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
552 }
553
554 /*
555 ** this function is only used once when the first picture is received
556 ** this function will show the video window once a picture is ready
557 */
558
559 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
560 {
561     /* get initial picture presented through D3D */
562     Display(p_vout, p_pic);
563
564     /*
565     ** Video window is initially hidden, show it now since we got a 
566     ** picture to show.
567     */
568     SetWindowPos( p_vout->p_sys->hvideownd, 0, 0, 0, 0, 0, 
569         SWP_ASYNCWINDOWPOS|
570         SWP_FRAMECHANGED|
571         SWP_SHOWWINDOW|
572         SWP_NOMOVE|
573         SWP_NOSIZE|
574         SWP_NOZORDER );
575
576     /* use and restores proper display function for further pictures */
577     p_vout->pf_display = Display;
578 }
579
580 /*****************************************************************************
581  * DirectD3DVoutCreate: Initialize and instance of Direct3D9 
582  *****************************************************************************
583  * This function initialize Direct3D and analyze available resources from
584  * default adapter.
585  *****************************************************************************/
586 static int Direct3DVoutCreate( vout_thread_t *p_vout )
587 {
588     HRESULT hr;
589     LPDIRECT3D9 p_d3dobj;
590     D3DCAPS9 d3dCaps;
591
592     LPDIRECT3D9 (WINAPI *OurDirect3DCreate9)(UINT SDKVersion);
593
594     p_vout->p_sys->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
595     if( NULL == p_vout->p_sys->hd3d9_dll )
596     {
597         msg_Warn( p_vout, "cannot load d3d9.dll, aborting" );
598         return VLC_EGENERIC;
599     }
600
601     OurDirect3DCreate9 =
602       (void *)GetProcAddress( p_vout->p_sys->hd3d9_dll,
603                               TEXT("Direct3DCreate9") );
604     if( OurDirect3DCreate9 == NULL )
605     {
606         msg_Err( p_vout, "Cannot locate reference to Direct3DCreate9 ABI in DLL" );
607         return VLC_EGENERIC;
608     }
609
610     /* Create the D3D object. */
611     p_d3dobj = OurDirect3DCreate9( D3D_SDK_VERSION );
612     if( NULL == p_d3dobj )
613     {
614        msg_Err( p_vout, "Could not create Direct3D9 instance.");
615        return VLC_EGENERIC;
616     }
617     p_vout->p_sys->p_d3dobj = p_d3dobj;
618
619     /*
620     ** Get device capabilities
621     */
622     ZeroMemory(&d3dCaps, sizeof(d3dCaps));
623     hr = IDirect3D9_GetDeviceCaps(p_d3dobj, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps);
624     if( FAILED(hr) )
625     {
626        msg_Err( p_vout, "Could not read adapter capabilities. (hr=0x%lX)", hr);
627        return VLC_EGENERIC;
628     }
629     /* TODO: need to test device capabilities and select the right render function */
630
631     return VLC_SUCCESS;
632 }
633
634 /*****************************************************************************
635  * DirectD3DVoutRelease: release an instance of Direct3D9
636  *****************************************************************************/
637
638 static void Direct3DVoutRelease( vout_thread_t *p_vout )
639 {
640     if( p_vout->p_sys->p_d3dobj )
641     {
642        IDirect3D9_Release(p_vout->p_sys->p_d3dobj);
643        p_vout->p_sys->p_d3dobj = NULL;
644     }
645     if( NULL != p_vout->p_sys->hd3d9_dll )
646     {
647         FreeLibrary(p_vout->p_sys->hd3d9_dll);
648         p_vout->p_sys->hd3d9_dll = NULL;
649     }
650 }
651
652 static int Direct3DFillPresentationParameters(vout_thread_t *p_vout, D3DPRESENT_PARAMETERS *d3dpp)
653 {
654     LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
655     D3DDISPLAYMODE d3ddm;
656     HRESULT hr;
657
658     /*
659     ** Get the current desktop display mode, so we can set up a back
660     ** buffer of the same format
661     */
662     hr = IDirect3D9_GetAdapterDisplayMode(p_d3dobj, D3DADAPTER_DEFAULT, &d3ddm );
663     if( FAILED(hr))
664     {
665        msg_Err( p_vout, "Could not read adapter display mode. (hr=0x%lX)", hr);
666        return VLC_EGENERIC;
667     }
668
669     /* keep a copy of current desktop format */
670     p_vout->p_sys->bbFormat = d3ddm.Format;
671
672     /* Set up the structure used to create the D3DDevice. */
673     ZeroMemory( d3dpp, sizeof(D3DPRESENT_PARAMETERS) );
674     d3dpp->Flags                  = D3DPRESENTFLAG_VIDEO;
675     d3dpp->Windowed               = TRUE;
676     d3dpp->hDeviceWindow          = p_vout->p_sys->hvideownd;
677     d3dpp->BackBufferWidth        = p_vout->output.i_width;
678     d3dpp->BackBufferHeight       = p_vout->output.i_height;
679     d3dpp->SwapEffect             = D3DSWAPEFFECT_COPY;
680     d3dpp->MultiSampleType        = D3DMULTISAMPLE_NONE;
681     d3dpp->PresentationInterval   = D3DPRESENT_INTERVAL_DEFAULT;
682     d3dpp->BackBufferFormat       = d3ddm.Format;
683     d3dpp->BackBufferCount        = 1;
684     d3dpp->EnableAutoDepthStencil = FALSE;
685
686     return VLC_SUCCESS;
687 }
688
689 /*****************************************************************************
690  * DirectD3DVoutOpen: Takes care of all the Direct3D9 initialisations
691  *****************************************************************************
692  * This function creates Direct3D device
693  * this must be called from the vout thread for performance reason, as
694  * all Direct3D Device APIs are used in a non multithread safe environment
695  *****************************************************************************/
696 static int Direct3DVoutOpen( vout_thread_t *p_vout )
697 {
698     LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
699     LPDIRECT3DDEVICE9 p_d3ddev;
700     D3DPRESENT_PARAMETERS d3dpp;
701     HRESULT hr;
702
703     if( VLC_SUCCESS != Direct3DFillPresentationParameters(p_vout, &d3dpp) )
704         return VLC_EGENERIC;
705
706     // Create the D3DDevice
707     hr = IDirect3D9_CreateDevice(p_d3dobj, D3DADAPTER_DEFAULT,
708                                  D3DDEVTYPE_HAL, p_vout->p_sys->hvideownd,
709                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING|
710                                  D3DCREATE_MULTITHREADED,
711                                  &d3dpp, &p_d3ddev );
712     if( FAILED(hr) )                                      
713     {
714        msg_Err(p_vout, "Could not create the D3D device! (hr=0x%lX)", hr);
715        return VLC_EGENERIC;
716     }
717     p_vout->p_sys->p_d3ddev = p_d3ddev;
718
719     msg_Dbg( p_vout, "Direct3D device adapter successfully initialized" );
720     return VLC_SUCCESS;
721 }
722
723 /*****************************************************************************
724  * DirectD3DClose: release the Direct3D9 device 
725  *****************************************************************************/
726 static void Direct3DVoutClose( vout_thread_t *p_vout )
727 {
728     if( p_vout->p_sys->p_d3ddev )
729     {
730        IDirect3DDevice9_Release(p_vout->p_sys->p_d3ddev);
731        p_vout->p_sys->p_d3ddev = NULL;
732     }
733        
734     p_vout->p_sys->hmonitor = NULL;
735 }
736
737 /*****************************************************************************
738  * DirectD3DClose: reset the Direct3D9 device 
739  *****************************************************************************
740  * All resources must be deallocated before the reset occur, they will be
741  * realllocated once the reset has been performed successfully
742  *****************************************************************************/
743 static int Direct3DVoutResetDevice( vout_thread_t *p_vout )
744 {
745     LPDIRECT3DDEVICE9       p_d3ddev = p_vout->p_sys->p_d3ddev;
746     D3DPRESENT_PARAMETERS   d3dpp;
747     HRESULT hr;
748
749     if( VLC_SUCCESS != Direct3DFillPresentationParameters(p_vout, &d3dpp) )
750         return VLC_EGENERIC;
751
752     // release all D3D objects
753     Direct3DVoutReleaseScene( p_vout );
754     Direct3DVoutReleasePictures( p_vout );
755
756     hr = IDirect3DDevice9_Reset(p_d3ddev, &d3dpp);
757     if( SUCCEEDED(hr) )
758     {
759         // re-create them
760         if( (VLC_SUCCESS != Direct3DVoutCreatePictures(p_vout, 1))
761          || (VLC_SUCCESS != Direct3DVoutCreateScene(p_vout)) )
762         {
763             msg_Dbg(p_vout, "%s failed !", __FUNCTION__);
764             return VLC_EGENERIC;
765         }
766     }
767     else {
768         msg_Err(p_vout, "%s failed ! (hr=%08lX)", __FUNCTION__, hr);
769         return VLC_EGENERIC;
770     }
771     return VLC_SUCCESS;
772 }
773
774 static D3DFORMAT Direct3DVoutSelectFormat( vout_thread_t *p_vout, D3DFORMAT target,
775     const D3DFORMAT *formats, size_t count)
776 {
777     LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
778     size_t c;
779
780     for( c=0; c<count; ++c )
781     {
782         HRESULT hr;
783         D3DFORMAT format = formats[c];
784         /* test whether device can create a surface of that format */
785         hr = IDirect3D9_CheckDeviceFormat(p_d3dobj, D3DADAPTER_DEFAULT,
786                 D3DDEVTYPE_HAL, target, 0, D3DRTYPE_SURFACE, format);
787         if( SUCCEEDED(hr) )
788         {
789             /* test whether device can perform color-conversion
790             ** from that format to target format
791             */
792             hr = IDirect3D9_CheckDeviceFormatConversion(p_d3dobj,
793                     D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
794                     format, target);
795         }
796         if( SUCCEEDED(hr) )
797         {
798             // found a compatible format
799             switch( format )
800             {
801                 case D3DFMT_UYVY:
802                     msg_Dbg( p_vout, "selected surface pixel format is UYVY");
803                     break;
804                 case D3DFMT_YUY2:
805                     msg_Dbg( p_vout, "selected surface pixel format is YUY2");
806                     break;
807                 case D3DFMT_X8R8G8B8:
808                     msg_Dbg( p_vout, "selected surface pixel format is X8R8G8B8");
809                     break;
810                 case D3DFMT_A8R8G8B8:
811                     msg_Dbg( p_vout, "selected surface pixel format is A8R8G8B8");
812                     break;
813                 case D3DFMT_R8G8B8:
814                     msg_Dbg( p_vout, "selected surface pixel format is R8G8B8");
815                     break;
816                 case D3DFMT_R5G6B5:
817                     msg_Dbg( p_vout, "selected surface pixel format is R5G6B5");
818                     break;
819                 case D3DFMT_X1R5G5B5:
820                     msg_Dbg( p_vout, "selected surface pixel format is X1R5G5B5");
821                     break;
822                 default:
823                     msg_Dbg( p_vout, "selected surface pixel format is 0x%0X", format);
824                     break;
825             }
826             return format;
827         }
828         else if( D3DERR_NOTAVAILABLE != hr )
829         {
830             msg_Err( p_vout, "Could not query adapter supported formats. (hr=0x%lX)", hr);
831             break;
832         }
833     }
834     return D3DFMT_UNKNOWN;
835 }
836
837 static D3DFORMAT Direct3DVoutFindFormat(vout_thread_t *p_vout, int i_chroma, D3DFORMAT target)
838 {
839     //if( p_vout->p_sys->b_hw_yuv && ! _got_vista_or_above )
840     if( p_vout->p_sys->b_hw_yuv )
841     {
842         /* it sounds like vista does not support YUV surfaces at all */
843         switch( i_chroma )
844         {
845             case VLC_FOURCC('U','Y','V','Y'):
846             case VLC_FOURCC('U','Y','N','V'):
847             case VLC_FOURCC('Y','4','2','2'):
848             {
849                 static const D3DFORMAT formats[] =
850                     { D3DFMT_UYVY, D3DFMT_YUY2, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
851                 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
852             }
853             case VLC_FOURCC('I','4','2','0'):
854             case VLC_FOURCC('I','4','2','2'):
855             case VLC_FOURCC('Y','V','1','2'):
856             {
857                 /* typically 3D textures don't support planar format
858                 ** fallback to packed version and use CPU for the conversion
859                 */
860                 static const D3DFORMAT formats[] = 
861                     { D3DFMT_YUY2, D3DFMT_UYVY, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
862                 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
863             }
864             case VLC_FOURCC('Y','U','Y','2'):
865             case VLC_FOURCC('Y','U','N','V'):
866             {
867                 static const D3DFORMAT formats[] = 
868                     { D3DFMT_YUY2, D3DFMT_UYVY, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
869                 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
870             }
871         }
872     }
873
874     switch( i_chroma )
875     {
876         case VLC_FOURCC('R', 'V', '1', '5'):
877         {
878             static const D3DFORMAT formats[] = 
879                 { D3DFMT_X1R5G5B5 };
880             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
881         }
882         case VLC_FOURCC('R', 'V', '1', '6'):
883         {
884             static const D3DFORMAT formats[] = 
885                 { D3DFMT_R5G6B5 };
886             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
887         }
888         case VLC_FOURCC('R', 'V', '2', '4'):
889         {
890             static const D3DFORMAT formats[] = 
891                 { D3DFMT_R8G8B8, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8 };
892             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
893         }
894         case VLC_FOURCC('R', 'V', '3', '2'):
895         {
896             static const D3DFORMAT formats[] = 
897                 { D3DFMT_A8R8G8B8, D3DFMT_X8R8G8B8 };
898             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
899         }
900         default:
901         {
902             /* use display default format */
903             LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
904             D3DDISPLAYMODE d3ddm;
905
906             HRESULT hr = IDirect3D9_GetAdapterDisplayMode(p_d3dobj, D3DADAPTER_DEFAULT, &d3ddm );
907             if( SUCCEEDED(hr))
908             {
909                 /*
910                 ** some professional cards could use some advanced pixel format as default, 
911                 ** make sure we stick with chromas that we can handle internally
912                 */
913                 switch( d3ddm.Format )
914                 {
915                     case D3DFMT_R8G8B8:
916                     case D3DFMT_X8R8G8B8:
917                     case D3DFMT_A8R8G8B8:
918                     case D3DFMT_R5G6B5:
919                     case D3DFMT_X1R5G5B5:
920                         msg_Dbg( p_vout, "defaulting to adapter pixel format");
921                         return Direct3DVoutSelectFormat(p_vout, target, &d3ddm.Format, 1);
922                     default:
923                     {
924                         /* if we fall here, that probably means that we need to render some YUV format */
925                         static const D3DFORMAT formats[] = 
926                             { D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
927                         msg_Dbg( p_vout, "defaulting to built-in pixel format");
928                         return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
929                     }
930                 }
931             }
932         }
933     }
934     return D3DFMT_UNKNOWN;
935 }
936
937 static int Direct3DVoutSetOutputFormat(vout_thread_t *p_vout, D3DFORMAT format)
938 {
939     switch( format )
940     {
941         case D3DFMT_YUY2:
942             p_vout->output.i_chroma = VLC_FOURCC('Y', 'U', 'Y', '2');
943             break;
944         case D3DFMT_UYVY:
945             p_vout->output.i_chroma = VLC_FOURCC('U', 'Y', 'V', 'Y');
946             break;
947         case D3DFMT_R8G8B8:
948             p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '2', '4');
949             p_vout->output.i_rmask = 0xff0000;
950             p_vout->output.i_gmask = 0x00ff00;
951             p_vout->output.i_bmask = 0x0000ff;
952             break;
953         case D3DFMT_X8R8G8B8:
954         case D3DFMT_A8R8G8B8:
955             p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '3', '2');
956             p_vout->output.i_rmask = 0x00ff0000;
957             p_vout->output.i_gmask = 0x0000ff00;
958             p_vout->output.i_bmask = 0x000000ff;
959             break;
960         case D3DFMT_R5G6B5:
961             p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '1', '6');
962             p_vout->output.i_rmask = (0x1fL)<<11;
963             p_vout->output.i_gmask = (0x3fL)<<5;
964             p_vout->output.i_bmask = (0x1fL)<<0;
965             break;
966         case D3DFMT_X1R5G5B5:
967             p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '1', '5');
968             p_vout->output.i_rmask = (0x1fL)<<10;
969             p_vout->output.i_gmask = (0x1fL)<<5;
970             p_vout->output.i_bmask = (0x1fL)<<0;
971             break;
972         default:
973             return VLC_EGENERIC;
974     }
975     return VLC_SUCCESS;
976 }
977
978 /*****************************************************************************
979  * Direct3DVoutCreatePictures: allocate a vector of identical pictures
980  *****************************************************************************
981  * Each picture has an associated offscreen surface in video memory
982  * depending on hardware capabilities the picture chroma will be as close
983  * as possible to the orginal render chroma to reduce CPU conversion overhead
984  * and delegate this work to video card GPU
985  *****************************************************************************/
986 static int Direct3DVoutCreatePictures( vout_thread_t *p_vout, size_t i_num_pics )
987 {
988     LPDIRECT3DDEVICE9       p_d3ddev  = p_vout->p_sys->p_d3ddev;
989     D3DFORMAT               format;
990     HRESULT hr;
991     size_t c;
992     // if vout is already running, use current chroma, otherwise choose from upstream 
993     int i_chroma = p_vout->output.i_chroma ? : p_vout->render.i_chroma;
994
995     I_OUTPUTPICTURES = 0;
996
997     /*
998     ** find the appropriate D3DFORMAT for the render chroma, the format will be the closest to
999     ** the requested chroma which is usable by the hardware in an offscreen surface, as they
1000     ** typically support more formats than textures
1001     */ 
1002     format = Direct3DVoutFindFormat(p_vout, i_chroma, p_vout->p_sys->bbFormat);
1003     if( VLC_SUCCESS != Direct3DVoutSetOutputFormat(p_vout, format) )
1004     {
1005         msg_Err(p_vout, "surface pixel format is not supported.");
1006         return VLC_EGENERIC;
1007     }
1008
1009     for( c=0; c<i_num_pics; )
1010     {
1011
1012         LPDIRECT3DSURFACE9 p_d3dsurf;
1013         picture_t *p_pic = p_vout->p_picture+c; 
1014
1015         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(p_d3ddev, 
1016                 p_vout->render.i_width,
1017                 p_vout->render.i_height,
1018                 format,
1019                 D3DPOOL_DEFAULT,
1020                 &p_d3dsurf,
1021                 NULL);
1022         if( FAILED(hr) )
1023         {
1024             msg_Err(p_vout, "Failed to create picture surface. (hr=0x%lx)", hr);
1025             Direct3DVoutReleasePictures(p_vout);
1026             return VLC_EGENERIC;
1027         }
1028
1029         /* fill surface with black color */
1030         IDirect3DDevice9_ColorFill(p_d3ddev, p_d3dsurf, NULL, D3DCOLOR_ARGB(0xFF, 0, 0, 0) );
1031         
1032         /* assign surface to internal structure */
1033         p_pic->p_sys = (void *)p_d3dsurf;
1034
1035         /* Now that we've got our direct-buffer, we can finish filling in the
1036          * picture_t structures */
1037         switch( p_vout->output.i_chroma )
1038         {
1039             case VLC_FOURCC('R','G','B','2'):
1040                 p_pic->p->i_lines = p_vout->output.i_height;
1041                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1042                 p_pic->p->i_pixel_pitch = 1;
1043                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1044                     p_pic->p->i_pixel_pitch;
1045                 p_pic->i_planes = 1;
1046             break;
1047             case VLC_FOURCC('R','V','1','5'):
1048             case VLC_FOURCC('R','V','1','6'):
1049                 p_pic->p->i_lines = p_vout->output.i_height;
1050                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1051                 p_pic->p->i_pixel_pitch = 2;
1052                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1053                     p_pic->p->i_pixel_pitch;
1054                 p_pic->i_planes = 1;
1055             break;
1056             case VLC_FOURCC('R','V','2','4'):
1057                 p_pic->p->i_lines = p_vout->output.i_height;
1058                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1059                 p_pic->p->i_pixel_pitch = 3;
1060                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1061                     p_pic->p->i_pixel_pitch;
1062                 p_pic->i_planes = 1;
1063             break;
1064             case VLC_FOURCC('R','V','3','2'):
1065                 p_pic->p->i_lines = p_vout->output.i_height;
1066                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1067                 p_pic->p->i_pixel_pitch = 4;
1068                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1069                     p_pic->p->i_pixel_pitch;
1070                 p_pic->i_planes = 1;
1071                 break;
1072             case VLC_FOURCC('U','Y','V','Y'):
1073             case VLC_FOURCC('Y','U','Y','2'):
1074                 p_pic->p->i_lines = p_vout->output.i_height;
1075                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1076                 p_pic->p->i_pixel_pitch = 2;
1077                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1078                     p_pic->p->i_pixel_pitch;
1079                 p_pic->i_planes = 1;
1080                 break;
1081             default:
1082                 Direct3DVoutReleasePictures(p_vout);
1083                 return VLC_EGENERIC;
1084         }
1085         p_pic->i_status = DESTROYED_PICTURE;
1086         p_pic->i_type   = DIRECT_PICTURE;
1087         p_pic->b_slow   = VLC_TRUE;
1088         p_pic->pf_lock  = Direct3DVoutLockSurface;
1089         p_pic->pf_unlock = Direct3DVoutUnlockSurface;
1090         PP_OUTPUTPICTURE[c] = p_pic;
1091
1092         I_OUTPUTPICTURES = ++c;
1093     }
1094
1095     msg_Dbg( p_vout, "%u Direct3D pictures created successfully", c );
1096
1097     return VLC_SUCCESS;
1098 }
1099
1100 /*****************************************************************************
1101  * Direct3DVoutReleasePictures: destroy a picture vector 
1102  *****************************************************************************
1103  * release all video resources used for pictures
1104  *****************************************************************************/
1105 static void Direct3DVoutReleasePictures( vout_thread_t *p_vout)
1106 {
1107     size_t i_num_pics = I_OUTPUTPICTURES;
1108     size_t c;
1109     for( c=0; c<i_num_pics; ++c )
1110     {
1111         picture_t *p_pic = p_vout->p_picture+c; 
1112         if( p_pic->p_sys )
1113         {
1114             LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1115
1116             p_pic->p_sys = NULL;
1117
1118             if( p_d3dsurf )
1119             {
1120                 IDirect3DSurface9_Release(p_d3dsurf);
1121             }
1122         }
1123     }
1124     msg_Dbg( p_vout, "%u Direct3D pictures released.", c);
1125
1126     I_OUTPUTPICTURES = 0;
1127 }
1128
1129 /*****************************************************************************
1130  * Direct3DVoutLockSurface: Lock surface and get picture data pointer
1131  *****************************************************************************
1132  * This function locks a surface and get the surface descriptor which amongst
1133  * other things has the pointer to the picture data.
1134  *****************************************************************************/
1135 static int Direct3DVoutLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1136 {
1137     HRESULT hr;
1138     D3DLOCKED_RECT d3drect;
1139     LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1140
1141     if( NULL == p_d3dsurf )
1142         return VLC_EGENERIC;
1143
1144     /* Lock the surface to get a valid pointer to the picture buffer */
1145     hr = IDirect3DSurface9_LockRect(p_d3dsurf, &d3drect, NULL, 0);
1146     if( FAILED(hr) )
1147     {
1148         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1149         return VLC_EGENERIC;
1150     }
1151
1152     /* fill in buffer info in first plane */
1153     p_pic->p->p_pixels = d3drect.pBits;
1154     p_pic->p->i_pitch  = d3drect.Pitch;
1155
1156     return VLC_SUCCESS;
1157 }
1158
1159 /*****************************************************************************
1160  * Direct3DVoutUnlockSurface: Unlock a surface locked by Direct3DLockSurface().
1161  *****************************************************************************/
1162 static int Direct3DVoutUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1163 {
1164     HRESULT hr;
1165     LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1166
1167     if( NULL == p_d3dsurf )
1168         return VLC_EGENERIC;
1169
1170     /* Unlock the Surface */
1171     hr = IDirect3DSurface9_UnlockRect(p_d3dsurf);
1172     if( FAILED(hr) )
1173     {
1174         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1175         return VLC_EGENERIC;
1176     }
1177     return VLC_SUCCESS;
1178 }
1179
1180 /*****************************************************************************
1181  * Direct3DVoutCreateScene: allocate and initialize a 3D scene
1182  *****************************************************************************
1183  * for advanced blending/filtering a texture needs be used in a 3D scene.
1184  *****************************************************************************/
1185
1186 static int Direct3DVoutCreateScene( vout_thread_t *p_vout )
1187 {
1188     LPDIRECT3DDEVICE9       p_d3ddev  = p_vout->p_sys->p_d3ddev;
1189     LPDIRECT3DTEXTURE9      p_d3dtex;
1190     LPDIRECT3DVERTEXBUFFER9 p_d3dvtc;
1191
1192     HRESULT hr;
1193
1194     /*
1195     ** Create a texture for use when rendering a scene
1196     ** for performance reason, texture format is identical to backbuffer
1197     ** which would usually be a RGB format
1198     */
1199     hr = IDirect3DDevice9_CreateTexture(p_d3ddev, 
1200             p_vout->render.i_width,
1201             p_vout->render.i_height,
1202             1,
1203             D3DUSAGE_RENDERTARGET, 
1204             p_vout->p_sys->bbFormat,
1205             D3DPOOL_DEFAULT,
1206             &p_d3dtex, 
1207             NULL);
1208     if( FAILED(hr))
1209     {
1210         msg_Err(p_vout, "Failed to create texture. (hr=0x%lx)", hr);
1211         return VLC_EGENERIC;
1212     }
1213
1214     /*
1215     ** Create a vertex buffer for use when rendering scene
1216     */
1217     hr = IDirect3DDevice9_CreateVertexBuffer(p_d3ddev,
1218             sizeof(CUSTOMVERTEX)*4,
1219             D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,
1220             D3DFVF_CUSTOMVERTEX,
1221             D3DPOOL_DEFAULT,
1222             &p_d3dvtc,
1223             NULL);
1224     if( FAILED(hr) )
1225     {
1226         msg_Err(p_vout, "Failed to create vertex buffer. (hr=0x%lx)", hr);
1227             IDirect3DTexture9_Release(p_d3dtex);
1228         return VLC_EGENERIC;
1229     }
1230
1231     p_vout->p_sys->p_d3dtex = p_d3dtex;
1232     p_vout->p_sys->p_d3dvtc = p_d3dvtc;
1233
1234     // Texture coordinates outside the range [0.0, 1.0] are set 
1235     // to the texture color at 0.0 or 1.0, respectively.
1236     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
1237     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
1238
1239     // Set linear filtering quality
1240     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
1241     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
1242
1243     // set maximum ambient light
1244     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_AMBIENT, D3DCOLOR_XRGB(255,255,255));
1245
1246     // Turn off culling
1247     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_CULLMODE, D3DCULL_NONE);
1248
1249     // Turn off the zbuffer
1250     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ZENABLE, D3DZB_FALSE);
1251
1252     // Turn off lights
1253     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_LIGHTING, FALSE);
1254
1255     // Enable dithering
1256     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DITHERENABLE, TRUE);
1257
1258     // disable stencil
1259     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_STENCILENABLE, FALSE);
1260
1261     // manage blending
1262     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHABLENDENABLE, TRUE);
1263     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
1264     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
1265     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHATESTENABLE,TRUE);
1266     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAREF, 0x10);
1267     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAFUNC,D3DCMP_GREATER);
1268
1269     // Set texture states
1270     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLOROP,D3DTOP_MODULATE);
1271     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
1272     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG2,D3DTA_DIFFUSE);
1273
1274     // turn off alpha operation
1275     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
1276
1277     msg_Dbg( p_vout, "Direct3D scene created successfully");
1278
1279     return VLC_SUCCESS;
1280 }
1281
1282 /*****************************************************************************
1283  * Direct3DVoutReleaseScene
1284  *****************************************************************************/
1285 static void Direct3DVoutReleaseScene( vout_thread_t *p_vout )
1286 {
1287     LPDIRECT3DTEXTURE9      p_d3dtex = p_vout->p_sys->p_d3dtex;
1288     LPDIRECT3DVERTEXBUFFER9 p_d3dvtc = p_vout->p_sys->p_d3dvtc;
1289
1290     if( p_d3dvtc )
1291     {
1292         IDirect3DVertexBuffer9_Release(p_d3dvtc);
1293         p_vout->p_sys->p_d3dvtc = NULL;
1294     }
1295
1296     if( p_d3dtex )
1297     {
1298         IDirect3DTexture9_Release(p_d3dtex);
1299         p_vout->p_sys->p_d3dtex = NULL;
1300     }
1301     msg_Dbg( p_vout, "Direct3D scene released successfully");
1302 }
1303
1304 /*****************************************************************************
1305  * Render: copy picture surface into a texture and render into a scene
1306  *****************************************************************************
1307  * This function is intented for higher end 3D cards, with pixel shader support
1308  * and at least 64 MB of video RAM.
1309  *****************************************************************************/
1310 static void Direct3DVoutRenderScene( vout_thread_t *p_vout, picture_t *p_pic )
1311 {
1312     LPDIRECT3DDEVICE9       p_d3ddev  = p_vout->p_sys->p_d3ddev;
1313     LPDIRECT3DTEXTURE9      p_d3dtex;
1314     LPDIRECT3DVERTEXBUFFER9 p_d3dvtc;
1315     LPDIRECT3DSURFACE9      p_d3dsrc, p_d3ddest;
1316     CUSTOMVERTEX            *p_vertices;
1317     HRESULT hr;
1318     float f_width, f_height;
1319
1320     // check if device is still available    
1321     hr = IDirect3DDevice9_TestCooperativeLevel(p_d3ddev);
1322     if( FAILED(hr) )
1323     {
1324         if( (D3DERR_DEVICENOTRESET != hr)
1325          || (VLC_SUCCESS != Direct3DVoutResetDevice(p_vout)) )
1326         {
1327             // device is not usable at present (lost device, out of video mem ?)
1328             return;
1329         }
1330     }
1331     p_d3dtex  = p_vout->p_sys->p_d3dtex;
1332     p_d3dvtc  = p_vout->p_sys->p_d3dvtc;
1333
1334     /* Clear the backbuffer and the zbuffer */
1335     hr = IDirect3DDevice9_Clear( p_d3ddev, 0, NULL, D3DCLEAR_TARGET,
1336                               D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0 );
1337     if( FAILED(hr) )
1338     {
1339         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1340         return;
1341     }
1342
1343     /*  retrieve picture surface */
1344     p_d3dsrc = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1345     if( NULL == p_d3dsrc )
1346     {
1347         msg_Dbg( p_vout, "no surface to render ?");
1348         return;
1349     }
1350
1351     /* retrieve texture top-level surface */
1352     hr = IDirect3DTexture9_GetSurfaceLevel(p_d3dtex, 0, &p_d3ddest);
1353     if( FAILED(hr) )
1354     {
1355         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1356         return;
1357     }
1358
1359     /* Copy picture surface into texture surface, color space conversion happens here */
1360     hr = IDirect3DDevice9_StretchRect(p_d3ddev, p_d3dsrc, NULL, p_d3ddest, NULL, D3DTEXF_NONE);
1361     IDirect3DSurface9_Release(p_d3ddest);
1362     if( FAILED(hr) )
1363     {
1364         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1365         return;
1366     }
1367
1368     /* Update the vertex buffer */
1369     hr = IDirect3DVertexBuffer9_Lock(p_d3dvtc, 0, 0, (VOID **)(&p_vertices), D3DLOCK_DISCARD);
1370     if( FAILED(hr) )
1371     {
1372         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1373         return;
1374     }
1375
1376     /* Setup vertices */
1377     f_width  = (float)(p_vout->output.i_width);
1378     f_height = (float)(p_vout->output.i_height);
1379
1380     p_vertices[0].x       = 0.0f;       // left
1381     p_vertices[0].y       = 0.0f;       // top
1382     p_vertices[0].z       = 0.0f;
1383     p_vertices[0].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1384     p_vertices[0].rhw     = 1.0f;
1385     p_vertices[0].tu      = 0.0f;
1386     p_vertices[0].tv      = 0.0f;
1387  
1388     p_vertices[1].x       = f_width;    // right
1389     p_vertices[1].y       = 0.0f;       // top
1390     p_vertices[1].z       = 0.0f;
1391     p_vertices[1].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1392     p_vertices[1].rhw     = 1.0f;
1393     p_vertices[1].tu      = 1.0f;
1394     p_vertices[1].tv      = 0.0f;
1395  
1396     p_vertices[2].x       = f_width;    // right
1397     p_vertices[2].y       = f_height;   // bottom
1398     p_vertices[2].z       = 0.0f;
1399     p_vertices[2].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1400     p_vertices[2].rhw     = 1.0f;
1401     p_vertices[2].tu      = 1.0f;
1402     p_vertices[2].tv      = 1.0f;
1403  
1404     p_vertices[3].x       = 0.0f;       // left
1405     p_vertices[3].y       = f_height;   // bottom
1406     p_vertices[3].z       = 0.0f;
1407     p_vertices[3].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1408     p_vertices[3].rhw     = 1.0f;
1409     p_vertices[3].tu      = 0.0f;
1410     p_vertices[3].tv      = 1.0f;
1411  
1412     hr= IDirect3DVertexBuffer9_Unlock(p_d3dvtc);
1413     if( FAILED(hr) )
1414     {
1415         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1416         return;
1417     }
1418
1419     // Begin the scene
1420     hr = IDirect3DDevice9_BeginScene(p_d3ddev);
1421     if( FAILED(hr) )
1422     {
1423         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1424         return;
1425     }
1426
1427     // Setup our texture. Using textures introduces the texture stage states,
1428     // which govern how textures get blended together (in the case of multiple
1429     // textures) and lighting information. In this case, we are modulating
1430     // (blending) our texture with the diffuse color of the vertices.
1431     hr = IDirect3DDevice9_SetTexture(p_d3ddev, 0, (LPDIRECT3DBASETEXTURE9)p_d3dtex);
1432     if( FAILED(hr) )
1433     {
1434         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1435         IDirect3DDevice9_EndScene(p_d3ddev);
1436         return;
1437     }
1438
1439     // Render the vertex buffer contents
1440     hr = IDirect3DDevice9_SetStreamSource(p_d3ddev, 0, p_d3dvtc, 0, sizeof(CUSTOMVERTEX));
1441     if( FAILED(hr) )
1442     {
1443         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1444         IDirect3DDevice9_EndScene(p_d3ddev);
1445         return;
1446     }
1447  
1448     // we use FVF instead of vertex shader
1449     hr = IDirect3DDevice9_SetVertexShader(p_d3ddev, NULL);
1450     if( FAILED(hr) )
1451     {
1452         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1453         IDirect3DDevice9_EndScene(p_d3ddev);
1454         return;
1455     }
1456
1457     hr = IDirect3DDevice9_SetFVF(p_d3ddev, D3DFVF_CUSTOMVERTEX);
1458     if( FAILED(hr) )
1459     {
1460         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1461         IDirect3DDevice9_EndScene(p_d3ddev);
1462         return;
1463     }
1464
1465     // draw rectangle
1466     hr = IDirect3DDevice9_DrawPrimitive(p_d3ddev, D3DPT_TRIANGLEFAN, 0, 2);
1467     if( FAILED(hr) )
1468     {
1469         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1470         IDirect3DDevice9_EndScene(p_d3ddev);
1471         return;
1472     }
1473
1474     // End the scene
1475     hr = IDirect3DDevice9_EndScene(p_d3ddev);
1476     if( FAILED(hr) )
1477     {
1478         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1479         return;
1480     }
1481 }
1482