1 /*****************************************************************************
2 * vout.c: Windows DirectX video output display method
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: directx.c,v 1.28 2003/12/13 14:01:51 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
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.
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.
36 *****************************************************************************/
37 #include <errno.h> /* ENOMEM */
38 #include <stdlib.h> /* free() */
39 #include <string.h> /* strerror() */
49 #undef GetSystemMetrics
51 #ifndef MONITOR_DEFAULTTONEAREST
52 # define MONITOR_DEFAULTTONEAREST 2
57 /*****************************************************************************
59 * Defining them here allows us to get rid of the dxguid library during
61 *****************************************************************************/
63 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
64 DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
66 /*****************************************************************************
68 *****************************************************************************/
69 static int OpenVideo ( vlc_object_t * );
70 static void CloseVideo ( vlc_object_t * );
72 static int Init ( vout_thread_t * );
73 static void End ( vout_thread_t * );
74 static int Manage ( vout_thread_t * );
75 static void Display ( vout_thread_t *, picture_t * );
77 static int NewPictureVec ( vout_thread_t *, picture_t *, int );
78 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
79 static int UpdatePictureStruct( vout_thread_t *, picture_t *, int );
81 static int DirectXInitDDraw ( vout_thread_t *p_vout );
82 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
83 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
84 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
85 static int DirectXCreateSurface ( vout_thread_t *p_vout,
86 LPDIRECTDRAWSURFACE2 *, int, int, int );
87 static void DirectXCloseSurface ( vout_thread_t *p_vout,
88 LPDIRECTDRAWSURFACE2 );
89 static int DirectXCreateClipper ( vout_thread_t *p_vout );
90 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
91 static int DirectXLockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
92 static int DirectXUnlockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
94 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t i_color );
96 /* Object variables callbacks */
97 static int OnTopCallback( vlc_object_t *, char const *,
98 vlc_value_t, vlc_value_t, void * );
100 /*****************************************************************************
102 *****************************************************************************/
103 #define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
104 #define HW_YUV_LONGTEXT N_( \
105 "Try to use hardware acceleration for YUV->RGB conversions. " \
106 "This option doesn't have any effect when using overlays." )
107 #define SYSMEM_TEXT N_("Use video buffers in system memory")
108 #define SYSMEM_LONGTEXT N_( \
109 "Create video buffers in system memory instead of video memory. This " \
110 "isn't recommended as usually using video memory allows to benefit from " \
111 "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
112 "This option doesn't have any effect when using overlays." )
113 #define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
114 #define TRIPLEBUF_LONGTEXT N_( \
115 "Try to use triple bufferring when using YUV overlays. That results in " \
116 "much better video quality (no flickering)." )
119 add_category_hint( N_("Video"), NULL, VLC_FALSE );
120 add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT, VLC_TRUE );
121 add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT, VLC_TRUE );
122 add_bool( "directx-3buffering", 1, NULL, TRIPLEBUF_TEXT, TRIPLEBUF_LONGTEXT, VLC_TRUE );
123 set_description( _("DirectX video output") );
124 set_capability( "video output", 100 );
125 add_shortcut( "directx" );
126 set_callbacks( OpenVideo, CloseVideo );
130 /* check if we registered a window class because we need to
133 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
134 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
137 /*****************************************************************************
138 * OpenVideo: allocate DirectX video thread output method
139 *****************************************************************************
140 * This function allocates and initialize the DirectX vout method.
141 *****************************************************************************/
142 static int OpenVideo( vlc_object_t *p_this )
144 vout_thread_t * p_vout = (vout_thread_t *)p_this;
145 vlc_value_t val, text;
148 /* Allocate structure */
149 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
150 if( p_vout->p_sys == NULL )
152 msg_Err( p_vout, "out of memory" );
156 /* Initialisations */
157 p_vout->pf_init = Init;
158 p_vout->pf_end = End;
159 p_vout->pf_manage = Manage;
160 p_vout->pf_render = NULL;
161 p_vout->pf_display = Display;
163 p_vout->p_sys->p_ddobject = NULL;
164 p_vout->p_sys->p_display = NULL;
165 p_vout->p_sys->p_current_surface = NULL;
166 p_vout->p_sys->p_clipper = NULL;
167 p_vout->p_sys->hwnd = NULL;
168 p_vout->p_sys->hparent = NULL;
169 p_vout->p_sys->i_changes = 0;
171 /* Multimonitor stuff */
172 p_vout->p_sys->hmonitor = NULL;
173 p_vout->p_sys->p_display_driver = NULL;
174 p_vout->p_sys->MonitorFromWindow = NULL;
175 p_vout->p_sys->GetMonitorInfo = NULL;
176 if( (huser32 = GetModuleHandle( "USER32" ) ) )
178 p_vout->p_sys->MonitorFromWindow =
179 GetProcAddress( huser32, "MonitorFromWindow" );
180 p_vout->p_sys->GetMonitorInfo =
181 GetProcAddress( huser32, "GetMonitorInfoA" );
184 var_Create( p_vout, "overlay", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
185 var_Create( p_vout, "directx-use-sysmem", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
186 var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
187 var_Create( p_vout, "directx-3buffering", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
189 p_vout->p_sys->b_cursor_hidden = 0;
190 p_vout->p_sys->i_lastmoved = mdate();
192 /* Set main window's size */
193 p_vout->p_sys->i_window_width = p_vout->i_window_width;
194 p_vout->p_sys->i_window_height = p_vout->i_window_height;
196 /* Create the DirectXEventThread, this thread is created by us to isolate
197 * the Win32 PeekMessage function calls. We want to do this because
198 * Windows can stay blocked inside this call for a long time, and when
199 * this happens it thus blocks vlc's video_output thread.
200 * DirectXEventThread will take care of the creation of the video
201 * window (because PeekMessage has to be called from the same thread which
202 * created the window). */
203 msg_Dbg( p_vout, "creating DirectXEventThread" );
204 p_vout->p_sys->p_event =
205 vlc_object_create( p_vout, sizeof(event_thread_t) );
206 p_vout->p_sys->p_event->p_vout = p_vout;
207 if( vlc_thread_create( p_vout->p_sys->p_event, "DirectX Events Thread",
208 DirectXEventThread, 0, 1 ) )
210 msg_Err( p_vout, "cannot create DirectXEventThread" );
211 vlc_object_destroy( p_vout->p_sys->p_event );
212 p_vout->p_sys->p_event = NULL;
216 if( p_vout->p_sys->p_event->b_error )
218 msg_Err( p_vout, "DirectXEventThread failed" );
222 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
224 msg_Dbg( p_vout, "DirectXEventThread running" );
226 /* Initialise DirectDraw */
227 if( DirectXInitDDraw( p_vout ) )
229 msg_Err( p_vout, "cannot initialize DirectDraw" );
233 /* Create the directx display */
234 if( DirectXCreateDisplay( p_vout ) )
236 msg_Err( p_vout, "cannot initialize DirectDraw" );
240 /* Add a variable to indicate if the window should be on top of others */
241 var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
242 text.psz_string = _("Always on top");
243 var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
244 var_Get( p_vout, "video-on-top", &val );
245 p_vout->p_sys->b_on_top_change = val.b_bool;
246 var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
251 CloseVideo( VLC_OBJECT(p_vout) );
255 /*****************************************************************************
256 * Init: initialize DirectX video thread output method
257 *****************************************************************************
258 * This function create the directx surfaces needed by the output thread.
259 * It is called at the beginning of the thread.
260 *****************************************************************************/
261 static int Init( vout_thread_t *p_vout )
266 /* Get a few default parameters */
267 var_Get( p_vout, "overlay", &val );
268 p_vout->p_sys->b_using_overlay = val.b_bool;
269 var_Get( p_vout, "directx-use-sysmem", &val );
270 p_vout->p_sys->b_use_sysmem = val.b_bool;
271 var_Get( p_vout, "directx-hw-yuv", &val );
272 p_vout->p_sys->b_hw_yuv = val.b_bool;
273 var_Get( p_vout, "directx-3buffering", &val );
274 p_vout->p_sys->b_3buf_overlay = val.b_bool;
276 /* Initialise DirectDraw if not already done.
277 * We do this here because on multi-monitor systems we may have to
278 * re-create the directdraw surfaces. */
279 if( !p_vout->p_sys->p_ddobject &&
280 DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
282 msg_Err( p_vout, "cannot initialize DirectDraw" );
286 /* Create the directx display */
287 if( !p_vout->p_sys->p_display &&
288 DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
290 msg_Err( p_vout, "cannot initialize DirectDraw" );
294 /* Initialize the output structure.
295 * Since DirectDraw can do rescaling for us, stick to the default
296 * coordinates and aspect. */
297 p_vout->output.i_width = p_vout->render.i_width;
298 p_vout->output.i_height = p_vout->render.i_height;
299 p_vout->output.i_aspect = p_vout->render.i_aspect;
301 #define MAX_DIRECTBUFFERS 1
302 /* Right now we use only 1 directbuffer because we don't want the
303 * video decoder to decode directly into direct buffers as they are
304 * created into video memory and video memory is _really_ slow */
306 /* Choose the chroma we will try first. */
307 switch( p_vout->render.i_chroma )
309 case VLC_FOURCC('Y','U','Y','2'):
310 case VLC_FOURCC('Y','U','N','V'):
311 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
313 case VLC_FOURCC('U','Y','V','Y'):
314 case VLC_FOURCC('U','Y','N','V'):
315 case VLC_FOURCC('Y','4','2','2'):
316 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
318 case VLC_FOURCC('Y','V','Y','U'):
319 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
322 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
326 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
328 i_chroma_backup = p_vout->output.i_chroma;
330 if( !I_OUTPUTPICTURES )
332 /* hmmm, it didn't work! Let's try commonly supported chromas */
333 if( p_vout->output.i_chroma != VLC_FOURCC('Y','V','1','2') )
335 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
336 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
338 if( !I_OUTPUTPICTURES )
340 /* hmmm, it still didn't work! Let's try another one */
341 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
342 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
346 if( !I_OUTPUTPICTURES )
348 /* If it still didn't work then don't try to use an overlay */
349 p_vout->output.i_chroma = i_chroma_backup;
350 p_vout->p_sys->b_using_overlay = 0;
351 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
354 /* Change the window title bar text */
355 if( p_vout->p_sys->hparent )
357 else if( p_vout->p_sys->b_using_overlay )
358 SetWindowText( p_vout->p_sys->hwnd,
359 VOUT_TITLE " (hardware YUV overlay DirectX output)" );
360 else if( p_vout->p_sys->b_hw_yuv )
361 SetWindowText( p_vout->p_sys->hwnd,
362 VOUT_TITLE " (hardware YUV DirectX output)" );
363 else SetWindowText( p_vout->p_sys->hwnd,
364 VOUT_TITLE " (software RGB DirectX output)" );
369 /*****************************************************************************
370 * End: terminate Sys video thread output method
371 *****************************************************************************
372 * Terminate an output method created by Create.
373 * It is called at the end of the thread.
374 *****************************************************************************/
375 static void End( vout_thread_t *p_vout )
377 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
379 DirectXCloseDisplay( p_vout );
380 DirectXCloseDDraw( p_vout );
385 /*****************************************************************************
386 * CloseVideo: destroy Sys video thread output method
387 *****************************************************************************
388 * Terminate an output method created by Create
389 *****************************************************************************/
390 static void CloseVideo( vlc_object_t *p_this )
392 vout_thread_t * p_vout = (vout_thread_t *)p_this;
394 msg_Dbg( p_vout, "CloseVideo" );
396 var_Destroy( p_vout, "video-on-top" );
398 if( p_vout->p_sys->p_event )
400 vlc_object_detach( p_vout->p_sys->p_event );
402 /* Kill DirectXEventThread */
403 p_vout->p_sys->p_event->b_die = VLC_TRUE;
405 /* we need to be sure DirectXEventThread won't stay stuck in
406 * GetMessage, so we send a fake message */
407 if( p_vout->p_sys->hwnd )
409 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
412 vlc_thread_join( p_vout->p_sys->p_event );
413 vlc_object_destroy( p_vout->p_sys->p_event );
418 free( p_vout->p_sys );
419 p_vout->p_sys = NULL;
423 /*****************************************************************************
424 * Manage: handle Sys events
425 *****************************************************************************
426 * This function should be called regularly by the video output thread.
427 * It returns a non null value if an error occured.
428 *****************************************************************************/
429 static int Manage( vout_thread_t *p_vout )
431 WINDOWPLACEMENT window_placement;
433 /* If we do not control our window, we check for geometry changes
434 * ourselves because the parent might not send us its events. */
435 if( p_vout->p_sys->hparent )
437 DirectXUpdateRects( p_vout, VLC_FALSE );
443 if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
445 if( p_vout->p_sys->b_using_overlay )
446 DirectXUpdateOverlay( p_vout );
448 /* Check if we are still on the same monitor */
449 if( p_vout->p_sys->MonitorFromWindow &&
450 p_vout->p_sys->hmonitor !=
451 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
452 MONITOR_DEFAULTTONEAREST ) )
454 /* This will force the vout core to recreate the picture buffers */
455 p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
458 p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
461 /* We used to call the Win32 PeekMessage function here to read the window
462 * messages. But since window can stay blocked into this function for a
463 * long time (for example when you move your window on the screen), I
464 * decided to isolate PeekMessage in another thread. */
469 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
470 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
474 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
476 /* We need to switch between Maximized and Normal sized window */
477 window_placement.length = sizeof(WINDOWPLACEMENT);
478 GetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
479 if( p_vout->b_fullscreen )
481 /* Maximized window */
482 window_placement.showCmd = SW_SHOWMAXIMIZED;
483 /* Change window style, no borders and no title bar */
484 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, WS_CLIPCHILDREN );
490 window_placement.showCmd = SW_SHOWNORMAL;
491 /* Change window style, borders and title bar */
492 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, WS_CLIPCHILDREN |
493 WS_OVERLAPPEDWINDOW | WS_SIZEBOX | WS_VISIBLE );
496 SetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
498 /* Update the object variable and trigger callback */
499 val.b_bool = p_vout->b_fullscreen;
500 var_Set( p_vout, "fullscreen", val );
502 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
503 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
509 if( (!p_vout->p_sys->b_cursor_hidden) &&
510 ( (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 ) )
512 /* Hide the mouse automatically */
513 if( p_vout->p_sys->hwnd != p_vout->p_sys->hparent )
515 p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
516 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
521 * "Always on top" status change
523 if( p_vout->p_sys->b_on_top_change )
526 HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
528 var_Get( p_vout, "video-on-top", &val );
530 /* Set the window on top if necessary */
531 if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
534 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
535 MF_BYCOMMAND | MFS_CHECKED );
536 SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
537 SWP_NOSIZE | SWP_NOMOVE );
540 /* The window shouldn't be on top */
541 if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
544 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
545 MF_BYCOMMAND | MFS_UNCHECKED );
546 SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
547 SWP_NOSIZE | SWP_NOMOVE );
550 p_vout->p_sys->b_on_top_change = VLC_FALSE;
553 /* Check if the event thread is still running */
554 if( p_vout->p_sys->p_event->b_die )
556 return VLC_EGENERIC; /* exit */
562 /*****************************************************************************
563 * Display: displays previously rendered output
564 *****************************************************************************
565 * This function sends the currently rendered image to the display, wait until
566 * it is displayed and switch the two rendering buffers, preparing next frame.
567 *****************************************************************************/
568 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
572 if( (p_vout->p_sys->p_display == NULL) )
574 msg_Warn( p_vout, "no display!!" );
578 /* Our surface can be lost so be sure to check this
579 * and restore it if need be */
580 if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
581 == DDERR_SURFACELOST )
583 if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
584 p_vout->p_sys->b_using_overlay )
585 DirectXUpdateOverlay( p_vout );
588 if( !p_vout->p_sys->b_using_overlay )
592 /* We ask for the "NOTEARING" option */
593 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
594 ddbltfx.dwSize = sizeof(DDBLTFX);
595 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
597 /* Blit video surface to display */
598 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
599 &p_vout->p_sys->rect_dest_clipped,
600 p_pic->p_sys->p_surface,
601 &p_vout->p_sys->rect_src_clipped,
602 DDBLT_ASYNC, &ddbltfx );
603 if( dxresult != DD_OK )
605 msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
610 else /* using overlay */
612 /* Flip the overlay buffers if we are using back buffers */
613 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
618 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
620 if( dxresult != DD_OK )
622 msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
625 /* set currently displayed pic */
626 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
628 /* Lock surface to get all the required info */
629 if( DirectXLockSurface( p_vout, p_pic ) )
632 msg_Warn( p_vout, "cannot lock surface" );
635 DirectXUnlockSurface( p_vout, p_pic );
639 /* following functions are local */
641 /*****************************************************************************
642 * DirectXEnumCallback: Device enumeration
643 *****************************************************************************
644 * This callback function is called by DirectDraw once for each
645 * available DirectDraw device.
646 *****************************************************************************/
647 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
648 LPTSTR psz_drivername, VOID* p_context,
651 vout_thread_t *p_vout = (vout_thread_t *)p_context;
652 msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
654 if( hmon && hmon == p_vout->p_sys->hmonitor )
656 msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
657 p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
658 if( p_vout->p_sys->p_display_driver )
659 memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
662 return TRUE; /* Keep enumerating */
665 /*****************************************************************************
666 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
667 *****************************************************************************
668 * This function initialise and allocate resources for DirectDraw.
669 *****************************************************************************/
670 static int DirectXInitDDraw( vout_thread_t *p_vout )
673 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
674 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
676 LPDIRECTDRAW p_ddobject;
678 msg_Dbg( p_vout, "DirectXInitDDraw" );
680 /* Load direct draw DLL */
681 p_vout->p_sys->hddraw_dll = LoadLibrary("DDRAW.DLL");
682 if( p_vout->p_sys->hddraw_dll == NULL )
684 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
688 OurDirectDrawCreate =
689 (void *)GetProcAddress(p_vout->p_sys->hddraw_dll, "DirectDrawCreate");
690 if( OurDirectDrawCreate == NULL )
692 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
696 OurDirectDrawEnumerateEx =
697 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
698 "DirectDrawEnumerateExA" );
700 if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
702 p_vout->p_sys->hmonitor =
703 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
704 MONITOR_DEFAULTTONEAREST );
706 /* Enumerate displays */
707 OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
708 DDENUM_ATTACHEDSECONDARYDEVICES );
711 /* Initialize DirectDraw now */
712 dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
714 if( dxresult != DD_OK )
716 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
720 /* Get the IDirectDraw2 interface */
721 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
722 (LPVOID *)&p_vout->p_sys->p_ddobject );
723 /* Release the unused interface */
724 IDirectDraw_Release( p_ddobject );
725 if( dxresult != DD_OK )
727 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
731 /* Set DirectDraw Cooperative level, ie what control we want over Windows
733 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
734 NULL, DDSCL_NORMAL );
735 if( dxresult != DD_OK )
737 msg_Err( p_vout, "cannot set direct draw cooperative level" );
741 /* Get the size of the current display device */
742 if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
744 MONITORINFO monitor_info;
745 monitor_info.cbSize = sizeof( MONITORINFO );
746 p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
748 p_vout->p_sys->rect_display = monitor_info.rcMonitor;
752 p_vout->p_sys->rect_display.left = 0;
753 p_vout->p_sys->rect_display.top = 0;
754 p_vout->p_sys->rect_display.right = GetSystemMetrics(SM_CXSCREEN);
755 p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
758 msg_Dbg( p_vout, "screen dimensions %ix%i",
759 p_vout->p_sys->rect_display.right,
760 p_vout->p_sys->rect_display.bottom );
762 /* Probe the capabilities of the hardware */
763 DirectXGetDDrawCaps( p_vout );
765 msg_Dbg( p_vout, "End DirectXInitDDraw" );
769 if( p_vout->p_sys->p_ddobject )
770 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
771 if( p_vout->p_sys->hddraw_dll )
772 FreeLibrary( p_vout->p_sys->hddraw_dll );
773 p_vout->p_sys->hddraw_dll = NULL;
774 p_vout->p_sys->p_ddobject = NULL;
778 /*****************************************************************************
779 * DirectXCreateDisplay: create the DirectDraw display.
780 *****************************************************************************
781 * Create and initialize display according to preferences specified in the vout
783 *****************************************************************************/
784 static int DirectXCreateDisplay( vout_thread_t *p_vout )
788 LPDIRECTDRAWSURFACE p_display;
790 msg_Dbg( p_vout, "DirectXCreateDisplay" );
792 /* Now get the primary surface. This surface is what you actually see
794 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
795 ddsd.dwSize = sizeof(DDSURFACEDESC);
796 ddsd.dwFlags = DDSD_CAPS;
797 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
799 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
800 &ddsd, &p_display, NULL );
801 if( dxresult != DD_OK )
803 msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
807 dxresult = IDirectDrawSurface_QueryInterface( p_display,
808 &IID_IDirectDrawSurface2,
809 (LPVOID *)&p_vout->p_sys->p_display );
810 /* Release the old interface */
811 IDirectDrawSurface_Release( p_display );
812 if ( dxresult != DD_OK )
814 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
815 "(error %li)", dxresult );
819 /* The clipper will be used only in non-overlay mode */
820 DirectXCreateClipper( p_vout );
822 /* Make sure the colorkey will be painted */
823 p_vout->p_sys->i_colorkey = 1;
824 p_vout->p_sys->i_rgb_colorkey =
825 DirectXFindColorkey( p_vout, p_vout->p_sys->i_colorkey );
827 /* Create the actual brush */
828 SetClassLong( p_vout->p_sys->hvideownd, GCL_HBRBACKGROUND,
829 (LONG)CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey ) );
830 InvalidateRect( p_vout->p_sys->hvideownd, NULL, TRUE );
831 DirectXUpdateRects( p_vout, VLC_TRUE );
836 /*****************************************************************************
837 * DirectXCreateClipper: Create a clipper that will be used when blitting the
838 * RGB surface to the main display.
839 *****************************************************************************
840 * This clipper prevents us to modify by mistake anything on the screen
841 * which doesn't belong to our window. For example when a part of our video
842 * window is hidden by another window.
843 *****************************************************************************/
844 static int DirectXCreateClipper( vout_thread_t *p_vout )
848 msg_Dbg( p_vout, "DirectXCreateClipper" );
850 /* Create the clipper */
851 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
852 &p_vout->p_sys->p_clipper, NULL );
853 if( dxresult != DD_OK )
855 msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
859 /* Associate the clipper to the window */
860 dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
861 p_vout->p_sys->hvideownd );
862 if( dxresult != DD_OK )
864 msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
869 /* associate the clipper with the surface */
870 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
871 p_vout->p_sys->p_clipper);
872 if( dxresult != DD_OK )
874 msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
882 if( p_vout->p_sys->p_clipper )
884 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
886 p_vout->p_sys->p_clipper = NULL;
890 /*****************************************************************************
891 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
892 *****************************************************************************
893 * The best method of display is with an YUV overlay because the YUV->RGB
894 * conversion is done in hardware.
895 * You can also create a plain RGB surface.
896 * ( Maybe we could also try an RGB overlay surface, which could have hardware
897 * scaling and which would also be faster in window mode because you don't
898 * need to do any blitting to the main display...)
899 *****************************************************************************/
900 static int DirectXCreateSurface( vout_thread_t *p_vout,
901 LPDIRECTDRAWSURFACE2 *pp_surface_final,
902 int i_chroma, int b_overlay,
906 LPDIRECTDRAWSURFACE p_surface;
909 /* Create the video surface */
912 /* Now try to create the YUV overlay surface.
913 * This overlay will be displayed on top of the primary surface.
914 * A color key is used to determine whether or not the overlay will be
915 * displayed, ie the overlay will be displayed in place of the primary
916 * surface wherever the primary surface will have this color.
917 * The video window has been created with a background of this color so
918 * the overlay will be only displayed on top of this window */
920 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
921 ddsd.dwSize = sizeof(DDSURFACEDESC);
922 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
923 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
924 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
925 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
926 ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
927 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
928 ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
930 ddsd.dwHeight = p_vout->render.i_height;
931 ddsd.dwWidth = p_vout->render.i_width;
932 ddsd.dwBackBufferCount = i_backbuffers;
934 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
935 &ddsd, &p_surface, NULL );
936 if( dxresult != DD_OK )
938 *pp_surface_final = NULL;
945 vlc_bool_t b_rgb_surface =
946 ( i_chroma == VLC_FOURCC('R','G','B','2') )
947 || ( i_chroma == VLC_FOURCC('R','V','1','5') )
948 || ( i_chroma == VLC_FOURCC('R','V','1','6') )
949 || ( i_chroma == VLC_FOURCC('R','V','2','4') )
950 || ( i_chroma == VLC_FOURCC('R','V','3','2') );
952 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
953 ddsd.dwSize = sizeof(DDSURFACEDESC);
954 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
955 ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
956 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
957 ddsd.dwHeight = p_vout->render.i_height;
958 ddsd.dwWidth = p_vout->render.i_width;
960 if( p_vout->p_sys->b_use_sysmem )
961 ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
963 ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
967 ddsd.dwFlags |= DDSD_PIXELFORMAT;
968 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
969 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
972 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
973 &ddsd, &p_surface, NULL );
974 if( dxresult != DD_OK )
976 *pp_surface_final = NULL;
981 /* Now that the surface is created, try to get a newer DirectX interface */
982 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
983 &IID_IDirectDrawSurface2,
984 (LPVOID *)pp_surface_final );
985 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
986 if ( dxresult != DD_OK )
988 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
989 "(error %li)", dxresult );
990 *pp_surface_final = NULL;
996 /* Check the overlay is useable as some graphics cards allow creating
997 * several overlays but only one can be used at one time. */
998 p_vout->p_sys->p_current_surface = *pp_surface_final;
999 if( DirectXUpdateOverlay( p_vout ) != VLC_SUCCESS )
1001 IDirectDrawSurface2_Release( *pp_surface_final );
1002 *pp_surface_final = NULL;
1003 msg_Err( p_vout, "overlay unuseable (might already be in use)" );
1004 return VLC_EGENERIC;
1011 /*****************************************************************************
1012 * DirectXUpdateOverlay: Move or resize overlay surface on video display.
1013 *****************************************************************************
1014 * This function is used to move or resize an overlay surface on the screen.
1015 * Ususally the overlay is moved by the user and thus, by a move or resize
1016 * event (in Manage).
1017 *****************************************************************************/
1018 int DirectXUpdateOverlay( vout_thread_t *p_vout )
1024 if( p_vout->p_sys->p_current_surface == NULL ||
1025 !p_vout->p_sys->b_using_overlay )
1026 return VLC_EGENERIC;
1028 /* The new window dimensions should already have been computed by the
1029 * caller of this function */
1031 /* Position and show the overlay */
1032 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
1033 ddofx.dwSize = sizeof(DDOVERLAYFX);
1034 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
1035 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
1037 dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1039 dxresult = IDirectDrawSurface2_UpdateOverlay(
1040 p_vout->p_sys->p_current_surface,
1041 &p_vout->p_sys->rect_src_clipped,
1042 p_vout->p_sys->p_display,
1043 &p_vout->p_sys->rect_dest_clipped,
1045 if(dxresult != DD_OK)
1048 "DirectXUpdateOverlay cannot move or resize overlay" );
1049 return VLC_EGENERIC;
1055 /*****************************************************************************
1056 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
1057 *****************************************************************************
1058 * This function returns all resources allocated by DirectXInitDDraw.
1059 *****************************************************************************/
1060 static void DirectXCloseDDraw( vout_thread_t *p_vout )
1062 msg_Dbg( p_vout, "DirectXCloseDDraw" );
1063 if( p_vout->p_sys->p_ddobject != NULL )
1065 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1066 p_vout->p_sys->p_ddobject = NULL;
1069 if( p_vout->p_sys->hddraw_dll != NULL )
1071 FreeLibrary( p_vout->p_sys->hddraw_dll );
1072 p_vout->p_sys->hddraw_dll = NULL;
1075 if( p_vout->p_sys->p_display_driver != NULL )
1077 free( p_vout->p_sys->p_display_driver );
1078 p_vout->p_sys->p_display_driver = NULL;
1081 p_vout->p_sys->hmonitor = NULL;
1084 /*****************************************************************************
1085 * DirectXCloseDisplay: close and reset the DirectX display device
1086 *****************************************************************************
1087 * This function returns all resources allocated by DirectXCreateDisplay.
1088 *****************************************************************************/
1089 static void DirectXCloseDisplay( vout_thread_t *p_vout )
1091 msg_Dbg( p_vout, "DirectXCloseDisplay" );
1093 if( p_vout->p_sys->p_clipper != NULL )
1095 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1096 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1097 p_vout->p_sys->p_clipper = NULL;
1100 if( p_vout->p_sys->p_display != NULL )
1102 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1103 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1104 p_vout->p_sys->p_display = NULL;
1108 /*****************************************************************************
1109 * DirectXCloseSurface: close the YUV overlay or RGB surface.
1110 *****************************************************************************
1111 * This function returns all resources allocated for the surface.
1112 *****************************************************************************/
1113 static void DirectXCloseSurface( vout_thread_t *p_vout,
1114 LPDIRECTDRAWSURFACE2 p_surface )
1116 msg_Dbg( p_vout, "DirectXCloseSurface" );
1117 if( p_surface != NULL )
1119 IDirectDrawSurface2_Release( p_surface );
1123 /*****************************************************************************
1124 * NewPictureVec: allocate a vector of identical pictures
1125 *****************************************************************************
1126 * Returns 0 on success, -1 otherwise
1127 *****************************************************************************/
1128 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1132 int i_ret = VLC_SUCCESS;
1133 LPDIRECTDRAWSURFACE2 p_surface;
1135 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1136 p_vout->p_sys->b_using_overlay ? "yes" : "no",
1137 (char *)&p_vout->output.i_chroma );
1139 I_OUTPUTPICTURES = 0;
1141 /* First we try to use an YUV overlay surface.
1142 * The overlay surface that we create won't be used to decode directly
1143 * into it because accessing video memory directly is way to slow (remember
1144 * that pictures are decoded macroblock per macroblock). Instead the video
1145 * will be decoded in picture buffers in system memory which will then be
1146 * memcpy() to the overlay surface. */
1147 if( p_vout->p_sys->b_using_overlay )
1149 /* Triple buffering rocks! it doesn't have any processing overhead
1150 * (you don't have to wait for the vsync) and provides for a very nice
1151 * video quality (no tearing). */
1152 if( p_vout->p_sys->b_3buf_overlay )
1153 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1154 p_vout->output.i_chroma,
1155 p_vout->p_sys->b_using_overlay,
1156 2 /* number of backbuffers */ );
1158 if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1160 /* Try to reduce the number of backbuffers */
1161 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1162 p_vout->output.i_chroma,
1163 p_vout->p_sys->b_using_overlay,
1164 0 /* number of backbuffers */ );
1167 if( i_ret == VLC_SUCCESS )
1170 picture_t front_pic;
1171 picture_sys_t front_pic_sys;
1172 front_pic.p_sys = &front_pic_sys;
1174 /* Allocate internal structure */
1175 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1176 if( p_pic[0].p_sys == NULL )
1178 DirectXCloseSurface( p_vout, p_surface );
1182 /* set front buffer */
1183 p_pic[0].p_sys->p_front_surface = p_surface;
1185 /* Get the back buffer */
1186 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1187 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1188 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1189 p_surface, &dds_caps,
1190 &p_pic[0].p_sys->p_surface ) )
1192 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1193 /* front buffer is the same as back buffer */
1194 p_pic[0].p_sys->p_surface = p_surface;
1198 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1199 p_pic[0].p_sys->p_front_surface;
1201 /* Reset the front buffer memory */
1202 if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1205 for( i = 0; i < front_pic.i_planes; i++ )
1206 for( j = 0; j < front_pic.p[i].i_lines; j++)
1207 memset( front_pic.p[i].p_pixels + j *
1208 front_pic.p[i].i_pitch, 127,
1209 front_pic.p[i].i_visible_pitch );
1211 DirectXUnlockSurface( p_vout, &front_pic );
1214 DirectXUpdateOverlay( p_vout );
1215 I_OUTPUTPICTURES = 1;
1216 msg_Dbg( p_vout, "YUV overlay created successfully" );
1220 /* As we can't have an overlay, we'll try to create a plain offscreen
1221 * surface. This surface will reside in video memory because there's a
1222 * better chance then that we'll be able to use some kind of hardware
1223 * acceleration like rescaling, blitting or YUV->RGB conversions.
1224 * We then only need to blit this surface onto the main display when we
1225 * want to display it */
1226 if( !p_vout->p_sys->b_using_overlay )
1228 if( p_vout->p_sys->b_hw_yuv )
1232 vlc_bool_t b_result = VLC_FALSE;
1234 /* Check if the chroma is supported first. This is required
1235 * because a few buggy drivers don't mind creating the surface
1236 * even if they don't know about the chroma. */
1237 if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1238 &i_codes, NULL ) == DD_OK )
1240 pi_codes = malloc( i_codes * sizeof(DWORD) );
1241 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1242 p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
1244 for( i = 0; i < (int)i_codes; i++ )
1246 if( p_vout->output.i_chroma == pi_codes[i] )
1248 b_result = VLC_TRUE;
1256 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1257 p_vout->output.i_chroma,
1259 0 /* no back buffers */ );
1261 p_vout->p_sys->b_hw_yuv = VLC_FALSE;
1264 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1266 /* Our last choice is to use a plain RGB surface */
1267 DDPIXELFORMAT ddpfPixelFormat;
1269 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1270 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1273 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1275 switch( ddpfPixelFormat.dwRGBBitCount )
1277 case 8: /* FIXME: set the palette */
1278 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
1281 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
1284 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
1287 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
1290 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
1293 msg_Err( p_vout, "unknown screen depth" );
1294 return VLC_EGENERIC;
1296 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1297 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1298 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1301 p_vout->p_sys->b_hw_yuv = 0;
1303 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1304 p_vout->output.i_chroma,
1306 0 /* no back buffers */ );
1308 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1310 /* Give it a last try with b_use_sysmem enabled */
1311 p_vout->p_sys->b_use_sysmem = 1;
1313 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1314 p_vout->output.i_chroma,
1316 0 /* no back buffers */ );
1320 if( i_ret == VLC_SUCCESS )
1322 /* Allocate internal structure */
1323 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1324 if( p_pic[0].p_sys == NULL )
1326 DirectXCloseSurface( p_vout, p_surface );
1330 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1333 I_OUTPUTPICTURES = 1;
1335 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1336 (char *)&p_vout->output.i_chroma );
1341 /* Now that we've got all our direct-buffers, we can finish filling in the
1342 * picture_t structures */
1343 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1345 p_pic[i].i_status = DESTROYED_PICTURE;
1346 p_pic[i].i_type = DIRECT_PICTURE;
1347 p_pic[i].pf_lock = DirectXLockSurface;
1348 p_pic[i].pf_unlock = DirectXUnlockSurface;
1349 PP_OUTPUTPICTURE[i] = &p_pic[i];
1351 if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1354 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1355 I_OUTPUTPICTURES = 0;
1356 msg_Err( p_vout, "cannot lock surface" );
1357 return VLC_EGENERIC;
1359 DirectXUnlockSurface( p_vout, &p_pic[i] );
1362 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1363 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1368 /*****************************************************************************
1369 * FreePicture: destroy a picture vector allocated with NewPictureVec
1370 *****************************************************************************
1372 *****************************************************************************/
1373 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1378 for( i = 0; i < i_num_pics; i++ )
1380 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1382 for( i = 0; i < i_num_pics; i++ )
1384 free( p_pic[i].p_sys );
1388 p_vout->p_sys->p_current_surface = 0;
1391 /*****************************************************************************
1392 * UpdatePictureStruct: updates the internal data in the picture_t structure
1393 *****************************************************************************
1394 * This will setup stuff for use by the video_output thread
1395 *****************************************************************************/
1396 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1399 switch( p_vout->output.i_chroma )
1401 case VLC_FOURCC('R','G','B','2'):
1402 case VLC_FOURCC('R','V','1','5'):
1403 case VLC_FOURCC('R','V','1','6'):
1404 case VLC_FOURCC('R','V','2','4'):
1405 case VLC_FOURCC('R','V','3','2'):
1406 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1407 p_pic->p->i_lines = p_vout->output.i_height;
1408 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1409 switch( p_vout->output.i_chroma )
1411 case VLC_FOURCC('R','G','B','2'):
1412 p_pic->p->i_pixel_pitch = 1;
1414 case VLC_FOURCC('R','V','1','5'):
1415 case VLC_FOURCC('R','V','1','6'):
1416 p_pic->p->i_pixel_pitch = 2;
1418 case VLC_FOURCC('R','V','2','4'):
1419 p_pic->p->i_pixel_pitch = 3;
1421 case VLC_FOURCC('R','V','3','2'):
1422 p_pic->p->i_pixel_pitch = 4;
1425 return VLC_EGENERIC;
1427 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1428 p_pic->p->i_pixel_pitch;
1429 p_pic->i_planes = 1;
1432 case VLC_FOURCC('Y','V','1','2'):
1434 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1435 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1436 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1437 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1438 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1439 p_pic->p[Y_PLANE].i_pixel_pitch;
1441 p_pic->V_PIXELS = p_pic->Y_PIXELS
1442 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1443 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1444 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1445 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1446 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1447 p_pic->p[V_PLANE].i_pixel_pitch;
1449 p_pic->U_PIXELS = p_pic->V_PIXELS
1450 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1451 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1452 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1453 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1454 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1455 p_pic->p[U_PLANE].i_pixel_pitch;
1457 p_pic->i_planes = 3;
1460 case VLC_FOURCC('I','Y','U','V'):
1462 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1463 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1464 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1465 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1466 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1467 p_pic->p[Y_PLANE].i_pixel_pitch;
1469 p_pic->U_PIXELS = p_pic->Y_PIXELS
1470 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1471 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1472 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1473 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1474 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1475 p_pic->p[U_PLANE].i_pixel_pitch;
1477 p_pic->V_PIXELS = p_pic->U_PIXELS
1478 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1479 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1480 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1481 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1482 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1483 p_pic->p[V_PLANE].i_pixel_pitch;
1485 p_pic->i_planes = 3;
1488 case VLC_FOURCC('Y','U','Y','2'):
1490 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1491 p_pic->p->i_lines = p_vout->output.i_height;
1492 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1493 p_pic->p->i_pixel_pitch = 2;
1494 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1495 p_pic->p->i_pixel_pitch;
1497 p_pic->i_planes = 1;
1501 /* Unknown chroma, tell the guy to get lost */
1502 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1503 p_vout->output.i_chroma,
1504 (char*)&p_vout->output.i_chroma );
1505 return VLC_EGENERIC;
1511 /*****************************************************************************
1512 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1513 *****************************************************************************
1514 * It is nice to know which features are supported by the hardware so we can
1515 * find ways to optimize our rendering.
1516 *****************************************************************************/
1517 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1522 /* This is just an indication of whether or not we'll support overlay,
1523 * but with this test we don't know if we support YUV overlay */
1524 memset( &ddcaps, 0, sizeof( DDCAPS ));
1525 ddcaps.dwSize = sizeof(DDCAPS);
1526 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1528 if(dxresult != DD_OK )
1530 msg_Warn( p_vout, "cannot get caps" );
1534 BOOL bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1535 bHasColorKey, bCanStretch, bCanBltFourcc;
1537 /* Determine if the hardware supports overlay surfaces */
1538 bHasOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAY) ==
1539 DDCAPS_OVERLAY) ? TRUE : FALSE;
1540 /* Determine if the hardware supports overlay surfaces */
1541 bHasOverlayFourCC = ((ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ==
1542 DDCAPS_OVERLAYFOURCC) ? TRUE : FALSE;
1543 /* Determine if the hardware supports overlay deinterlacing */
1544 bCanDeinterlace = ((ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ==
1546 /* Determine if the hardware supports colorkeying */
1547 bHasColorKey = ((ddcaps.dwCaps & DDCAPS_COLORKEY) ==
1548 DDCAPS_COLORKEY) ? TRUE : FALSE;
1549 /* Determine if the hardware supports scaling of the overlay surface */
1550 bCanStretch = ((ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ==
1551 DDCAPS_OVERLAYSTRETCH) ? TRUE : FALSE;
1552 /* Determine if the hardware supports color conversion during a blit */
1553 bCanBltFourcc = ((ddcaps.dwCaps & DDCAPS_BLTFOURCC ) ==
1554 DDCAPS_BLTFOURCC) ? TRUE : FALSE;
1556 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1557 "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1559 bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1560 bHasColorKey, bCanStretch, bCanBltFourcc );
1562 /* Don't ask for troubles */
1563 if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1567 /*****************************************************************************
1568 * DirectXLockSurface: Lock surface and get picture data pointer
1569 *****************************************************************************
1570 * This function locks a surface and get the surface descriptor which amongst
1571 * other things has the pointer to the picture data.
1572 *****************************************************************************/
1573 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1577 /* Lock the surface to get a valid pointer to the picture buffer */
1578 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1579 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1580 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1581 NULL, &p_pic->p_sys->ddsd,
1582 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1584 if( dxresult != DD_OK )
1586 if( dxresult == DDERR_INVALIDPARAMS )
1588 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1589 * in an invalid params error */
1590 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1591 &p_pic->p_sys->ddsd,
1594 if( dxresult == DDERR_SURFACELOST )
1596 /* Your surface can be lost so be sure
1597 * to check this and restore it if needed */
1599 /* When using overlays with back-buffers, we need to restore
1600 * the front buffer so the back-buffers get restored as well. */
1601 if( p_vout->p_sys->b_using_overlay )
1602 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1604 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1606 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1607 &p_pic->p_sys->ddsd,
1609 if( dxresult == DDERR_SURFACELOST )
1610 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1612 if( dxresult != DD_OK )
1614 return VLC_EGENERIC;
1618 /* Now we have a pointer to the surface memory, we can update our picture
1620 if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma )
1623 DirectXUnlockSurface( p_vout, p_pic );
1624 return VLC_EGENERIC;
1630 /*****************************************************************************
1631 * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1632 *****************************************************************************/
1633 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1635 /* Unlock the Surface */
1636 if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1639 return VLC_EGENERIC;
1642 /*****************************************************************************
1643 * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
1644 *****************************************************************************/
1645 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t i_color )
1650 uint32_t i_pixel_backup;
1653 ddsd.dwSize = sizeof(ddsd);
1654 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1655 &ddsd, DDLOCK_WAIT, NULL );
1656 if( dxresult != DD_OK ) return 0;
1658 i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1660 switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1663 *(uint8_t *)ddsd.lpSurface = 0x11;
1666 *(uint8_t *)ddsd.lpSurface = 0x01;
1669 *(uint16_t *)ddsd.lpSurface = 0x01;
1672 *(uint32_t *)ddsd.lpSurface = 0x0100;
1675 *(uint32_t *)ddsd.lpSurface = 0x01;
1679 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1681 if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1683 i_rgb = GetPixel( hdc, 0, 0 );
1684 IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1687 ddsd.dwSize = sizeof(ddsd);
1688 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1689 &ddsd, DDLOCK_WAIT, NULL );
1690 if( dxresult != DD_OK ) return i_rgb;
1692 *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1694 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1699 /*****************************************************************************
1700 * object variables callbacks: a bunch of object variables are used by the
1701 * interfaces to interact with the vout.
1702 *****************************************************************************/
1703 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1704 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1706 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1707 p_vout->p_sys->b_on_top_change = VLC_TRUE;