1 /*****************************************************************************
2 * vout.c: Windows DirectX video output display method
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: directx.c,v 1.19 2003/05/15 22:27:37 massiot 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() */
50 /*****************************************************************************
52 * Defining them here allows us to get rid of the dxguid library during
54 *****************************************************************************/
56 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
57 DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
59 /*****************************************************************************
61 *****************************************************************************/
62 static int OpenVideo ( vlc_object_t * );
63 static void CloseVideo ( vlc_object_t * );
65 static int Init ( vout_thread_t * );
66 static void End ( vout_thread_t * );
67 static int Manage ( vout_thread_t * );
68 static void Display ( vout_thread_t *, picture_t * );
70 static int NewPictureVec ( vout_thread_t *, picture_t *, int );
71 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
72 static int UpdatePictureStruct( vout_thread_t *, picture_t *, int );
74 static int DirectXInitDDraw ( vout_thread_t *p_vout );
75 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
76 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
77 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
78 static int DirectXCreateSurface ( vout_thread_t *p_vout,
79 LPDIRECTDRAWSURFACE2 *, int, int, int );
80 static void DirectXCloseSurface ( vout_thread_t *p_vout,
81 LPDIRECTDRAWSURFACE2 );
82 static int DirectXCreateClipper ( vout_thread_t *p_vout );
83 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
84 static int DirectXLockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
85 static int DirectXUnlockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
87 /*****************************************************************************
89 *****************************************************************************/
90 #define ON_TOP_TEXT N_("Always on top")
91 #define ON_TOP_LONGTEXT N_("Place the directx window on top of other windows")
92 #define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
93 #define HW_YUV_LONGTEXT N_( \
94 "Try to use hardware acceleration for YUV->RGB conversions. " \
95 "This option doesn't have any effect when using overlays." )
96 #define SYSMEM_TEXT N_("Use video buffers in system memory")
97 #define SYSMEM_LONGTEXT N_( \
98 "Create video buffers in system memory instead of video memory. This " \
99 "isn't recommended as usually using video memory allows to benefit from " \
100 "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
101 "This option doesn't have any effect when using overlays." )
102 #define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
103 #define TRIPLEBUF_LONGTEXT N_( \
104 "Try to use triple bufferring when using YUV overlays. That results in " \
105 "much better video quality (no flickering)." )
108 add_category_hint( N_("Video"), NULL, VLC_FALSE );
109 add_bool( "directx-on-top", 0, NULL, ON_TOP_TEXT, ON_TOP_LONGTEXT, VLC_FALSE );
110 add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT, VLC_TRUE );
111 add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT, VLC_TRUE );
112 add_bool( "directx-3buffering", 1, NULL, TRIPLEBUF_TEXT, TRIPLEBUF_LONGTEXT, VLC_TRUE );
113 set_description( _("DirectX video output") );
114 set_capability( "video output", 100 );
115 add_shortcut( "directx" );
116 set_callbacks( OpenVideo, CloseVideo );
120 /* check if we registered a window class because we need to
123 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
124 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
127 /*****************************************************************************
128 * OpenVideo: allocate DirectX video thread output method
129 *****************************************************************************
130 * This function allocates and initialize the DirectX vout method.
131 *****************************************************************************/
132 static int OpenVideo( vlc_object_t *p_this )
134 vout_thread_t * p_vout = (vout_thread_t *)p_this;
137 /* Allocate structure */
138 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
139 if( p_vout->p_sys == NULL )
141 msg_Err( p_vout, "out of memory" );
145 /* Initialisations */
146 p_vout->pf_init = Init;
147 p_vout->pf_end = End;
148 p_vout->pf_manage = Manage;
149 p_vout->pf_render = NULL;
150 p_vout->pf_display = Display;
152 p_vout->p_sys->p_ddobject = NULL;
153 p_vout->p_sys->p_display = NULL;
154 p_vout->p_sys->p_current_surface = NULL;
155 p_vout->p_sys->p_clipper = NULL;
156 p_vout->p_sys->hbrush = NULL;
157 p_vout->p_sys->hwnd = NULL;
158 p_vout->p_sys->hparent = NULL;
159 p_vout->p_sys->i_changes = 0;
160 p_vout->p_sys->b_caps_overlay_clipping = 0;
161 SetRectEmpty( &p_vout->p_sys->rect_display );
162 p_vout->p_sys->b_using_overlay = config_GetInt( p_vout, "overlay" );
163 p_vout->p_sys->b_use_sysmem = config_GetInt( p_vout, "directx-use-sysmem");
164 p_vout->p_sys->b_hw_yuv = config_GetInt( p_vout, "directx-hw-yuv" );
165 p_vout->p_sys->b_3buf_overlay = config_GetInt( p_vout, "directx-3buffering" );
167 p_vout->p_sys->b_cursor_hidden = 0;
168 p_vout->p_sys->i_lastmoved = mdate();
170 /* Set main window's size */
171 p_vout->p_sys->i_window_width = p_vout->i_window_width;
172 p_vout->p_sys->i_window_height = p_vout->i_window_height;
174 /* Create the DirectXEventThread, this thread is created by us to isolate
175 * the Win32 PeekMessage function calls. We want to do this because
176 * Windows can stay blocked inside this call for a long time, and when
177 * this happens it thus blocks vlc's video_output thread.
178 * DirectXEventThread will take care of the creation of the video
179 * window (because PeekMessage has to be called from the same thread which
180 * created the window). */
181 msg_Dbg( p_vout, "creating DirectXEventThread" );
182 p_vout->p_sys->p_event =
183 vlc_object_create( p_vout, sizeof(event_thread_t) );
184 p_vout->p_sys->p_event->p_vout = p_vout;
185 if( vlc_thread_create( p_vout->p_sys->p_event,
186 "DirectX Events Thread", DirectXEventThread,
189 msg_Err( p_vout, "cannot create DirectXEventThread" );
190 vlc_object_destroy( p_vout->p_sys->p_event );
191 p_vout->p_sys->p_event = NULL;
195 if( p_vout->p_sys->p_event->b_error )
197 msg_Err( p_vout, "DirectXEventThread failed" );
201 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
203 msg_Dbg( p_vout, "DirectXEventThread running" );
205 /* Initialise DirectDraw */
206 if( DirectXInitDDraw( p_vout ) )
208 msg_Err( p_vout, "cannot initialize DirectDraw" );
212 /* Create the directx display */
213 if( DirectXCreateDisplay( p_vout ) )
215 msg_Err( p_vout, "cannot initialize DirectDraw" );
219 /* Add a variable to indicate if the window should be on top of others */
220 var_Create( p_vout, "directx-on-top", VLC_VAR_BOOL );
221 val.b_bool = config_GetInt( p_vout, "directx-on-top" );
222 var_Set( p_vout, "directx-on-top", val );
227 CloseVideo( VLC_OBJECT(p_vout) );
231 /*****************************************************************************
232 * Init: initialize DirectX video thread output method
233 *****************************************************************************
234 * This function create the directx surfaces needed by the output thread.
235 * It is called at the beginning of the thread.
236 *****************************************************************************/
237 static int Init( vout_thread_t *p_vout )
241 /* Initialize the output structure.
242 * Since DirectDraw can do rescaling for us, stick to the default
243 * coordinates and aspect. */
244 p_vout->output.i_width = p_vout->render.i_width;
245 p_vout->output.i_height = p_vout->render.i_height;
246 p_vout->output.i_aspect = p_vout->render.i_aspect;
248 #define MAX_DIRECTBUFFERS 1
249 /* Right now we use only 1 directbuffer because we don't want the
250 * video decoder to decode directly into direct buffers as they are
251 * created into video memory and video memory is _really_ slow */
253 /* Choose the chroma we will try first. */
254 switch( p_vout->render.i_chroma )
256 case VLC_FOURCC('Y','U','Y','2'):
257 case VLC_FOURCC('Y','U','N','V'):
258 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
260 case VLC_FOURCC('U','Y','V','Y'):
261 case VLC_FOURCC('U','Y','N','V'):
262 case VLC_FOURCC('Y','4','2','2'):
263 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
265 case VLC_FOURCC('Y','V','Y','U'):
266 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
269 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
273 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
275 i_chroma_backup = p_vout->output.i_chroma;
277 if( !I_OUTPUTPICTURES )
279 /* hmmm, it didn't work! Let's try commonly supported chromas */
280 if( p_vout->output.i_chroma != VLC_FOURCC('Y','V','1','2') )
282 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
283 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
285 if( !I_OUTPUTPICTURES )
287 /* hmmm, it still didn't work! Let's try another one */
288 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
289 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
293 if( !I_OUTPUTPICTURES )
295 /* If it still didn't work then don't try to use an overlay */
296 p_vout->output.i_chroma = i_chroma_backup;
297 p_vout->p_sys->b_using_overlay = 0;
298 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
301 /* Change the window title bar text */
302 if( p_vout->p_sys->hparent )
304 else if( p_vout->p_sys->b_using_overlay )
305 SetWindowText( p_vout->p_sys->hwnd,
306 VOUT_TITLE " (hardware YUV overlay DirectX output)" );
307 else if( p_vout->p_sys->b_hw_yuv )
308 SetWindowText( p_vout->p_sys->hwnd,
309 VOUT_TITLE " (hardware YUV DirectX output)" );
310 else SetWindowText( p_vout->p_sys->hwnd,
311 VOUT_TITLE " (software RGB DirectX output)" );
316 /*****************************************************************************
317 * End: terminate Sys video thread output method
318 *****************************************************************************
319 * Terminate an output method created by Create.
320 * It is called at the end of the thread.
321 *****************************************************************************/
322 static void End( vout_thread_t *p_vout )
324 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
328 /*****************************************************************************
329 * CloseVideo: destroy Sys video thread output method
330 *****************************************************************************
331 * Terminate an output method created by Create
332 *****************************************************************************/
333 static void CloseVideo( vlc_object_t *p_this )
335 vout_thread_t * p_vout = (vout_thread_t *)p_this;
337 msg_Dbg( p_vout, "CloseVideo" );
339 var_Destroy( p_vout, "directs-on-top" );
341 DirectXCloseDisplay( p_vout );
342 DirectXCloseDDraw( p_vout );
344 if( p_vout->p_sys->p_event )
346 vlc_object_detach( p_vout->p_sys->p_event );
348 /* Kill DirectXEventThread */
349 p_vout->p_sys->p_event->b_die = VLC_TRUE;
351 /* we need to be sure DirectXEventThread won't stay stuck in
352 * GetMessage, so we send a fake message */
353 if( p_vout->p_sys->hwnd )
355 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
358 vlc_thread_join( p_vout->p_sys->p_event );
359 vlc_object_destroy( p_vout->p_sys->p_event );
364 free( p_vout->p_sys );
365 p_vout->p_sys = NULL;
369 /*****************************************************************************
370 * Manage: handle Sys events
371 *****************************************************************************
372 * This function should be called regularly by the video output thread.
373 * It returns a non null value if an error occured.
374 *****************************************************************************/
375 static int Manage( vout_thread_t *p_vout )
377 WINDOWPLACEMENT window_placement;
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 if( p_vout->p_sys->hparent )
386 DirectXUpdateRects( p_vout, VLC_FALSE );
389 /* We used to call the Win32 PeekMessage function here to read the window
390 * messages. But since window can stay blocked into this function for a
391 * long time (for example when you move your window on the screen), I
392 * decided to isolate PeekMessage in another thread. */
397 if( p_vout->i_changes & VOUT_SCALE_CHANGE
398 || p_vout->p_sys->i_changes & VOUT_SCALE_CHANGE )
400 msg_Dbg( p_vout, "scale change" );
401 if( !p_vout->p_sys->b_using_overlay )
402 InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
404 DirectXUpdateOverlay( p_vout );
405 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
406 p_vout->p_sys->i_changes &= ~VOUT_SCALE_CHANGE;
412 if( p_vout->i_changes & VOUT_SIZE_CHANGE
413 || p_vout->p_sys->i_changes & VOUT_SIZE_CHANGE )
415 msg_Dbg( p_vout, "size change" );
416 if( !p_vout->p_sys->b_using_overlay )
417 InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
419 DirectXUpdateOverlay( p_vout );
420 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
421 p_vout->p_sys->i_changes &= ~VOUT_SIZE_CHANGE;
427 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
428 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
430 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
432 /* We need to switch between Maximized and Normal sized window */
433 window_placement.length = sizeof(WINDOWPLACEMENT);
434 GetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
435 if( p_vout->b_fullscreen )
437 /* Maximized window */
438 window_placement.showCmd = SW_SHOWMAXIMIZED;
439 /* Change window style, no borders and no title bar */
440 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, 0 );
446 window_placement.showCmd = SW_SHOWNORMAL;
447 /* Change window style, borders and title bar */
448 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
449 WS_OVERLAPPEDWINDOW | WS_SIZEBOX | WS_VISIBLE );
452 SetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
454 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
455 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
461 if( (!p_vout->p_sys->b_cursor_hidden) &&
462 ( (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 ) )
464 /* Hide the mouse automatically */
465 if( p_vout->p_sys->hwnd != p_vout->p_sys->hparent )
467 p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
468 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
473 * "Always on top" status change
475 hwnd = p_vout->p_sys->hwnd;
476 hMenu = GetSystemMenu( hwnd , FALSE );
477 var_Get( p_vout, "directx-on-top", &val );
480 /* Set the window on top if necessary */
481 if( !( GetWindowLong( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST ) )
483 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
484 MF_BYCOMMAND | MFS_CHECKED );
485 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
486 SWP_NOSIZE | SWP_NOMOVE );
491 /* The window shouldn't be on top */
492 if( GetWindowLong( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST )
494 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
495 MF_BYCOMMAND | MFS_UNCHECKED );
496 SetWindowPos( hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
497 SWP_NOSIZE | SWP_NOMOVE );
501 /* Check if the event thread is still running */
502 if( p_vout->p_sys->p_event->b_die )
504 return VLC_EGENERIC; /* exit */
510 /*****************************************************************************
511 * Display: displays previously rendered output
512 *****************************************************************************
513 * This function sends the currently rendered image to the display, wait until
514 * it is displayed and switch the two rendering buffers, preparing next frame.
515 *****************************************************************************/
516 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
520 if( (p_vout->p_sys->p_display == NULL) )
522 msg_Warn( p_vout, "no display!!" );
526 /* Our surface can be lost so be sure to check this
527 * and restore it if need be */
528 if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
529 == DDERR_SURFACELOST )
531 if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
532 p_vout->p_sys->b_using_overlay )
533 DirectXUpdateOverlay( p_vout );
536 if( !p_vout->p_sys->b_using_overlay )
540 /* We ask for the "NOTEARING" option */
541 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
542 ddbltfx.dwSize = sizeof(DDBLTFX);
543 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
545 /* Blit video surface to display */
546 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
547 &p_vout->p_sys->rect_dest_clipped,
548 p_pic->p_sys->p_surface,
549 &p_vout->p_sys->rect_src_clipped,
550 DDBLT_ASYNC, &ddbltfx );
551 if( dxresult != DD_OK )
553 msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
558 else /* using overlay */
560 /* Flip the overlay buffers if we are using back buffers */
561 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
566 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
568 if( dxresult != DD_OK )
570 msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
573 /* set currently displayed pic */
574 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
576 /* Lock surface to get all the required info */
577 if( DirectXLockSurface( p_vout, p_pic ) )
580 msg_Warn( p_vout, "cannot lock surface" );
583 DirectXUnlockSurface( p_vout, p_pic );
588 /* following functions are local */
590 /*****************************************************************************
591 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
592 *****************************************************************************
593 * This function initialise and allocate resources for DirectDraw.
594 *****************************************************************************/
595 static int DirectXInitDDraw( vout_thread_t *p_vout )
598 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
599 LPDIRECTDRAW p_ddobject;
601 msg_Dbg( p_vout, "DirectXInitDDraw" );
603 /* load direct draw DLL */
604 p_vout->p_sys->hddraw_dll = LoadLibrary("DDRAW.DLL");
605 if( p_vout->p_sys->hddraw_dll == NULL )
607 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
611 OurDirectDrawCreate =
612 (void *)GetProcAddress(p_vout->p_sys->hddraw_dll, "DirectDrawCreate");
613 if ( OurDirectDrawCreate == NULL )
615 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
619 /* Initialize DirectDraw now */
620 dxresult = OurDirectDrawCreate( NULL, &p_ddobject, NULL );
621 if( dxresult != DD_OK )
623 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
627 /* Get the IDirectDraw2 interface */
628 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
629 (LPVOID *)&p_vout->p_sys->p_ddobject );
630 /* Release the unused interface */
631 IDirectDraw_Release( p_ddobject );
632 if( dxresult != DD_OK )
634 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
638 /* Set DirectDraw Cooperative level, ie what control we want over Windows
640 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
641 p_vout->p_sys->hwnd, DDSCL_NORMAL );
642 if( dxresult != DD_OK )
644 msg_Err( p_vout, "cannot set direct draw cooperative level" );
648 /* Probe the capabilities of the hardware */
649 DirectXGetDDrawCaps( p_vout );
651 msg_Dbg( p_vout, "End DirectXInitDDraw" );
655 if( p_vout->p_sys->p_ddobject )
656 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
657 if( p_vout->p_sys->hddraw_dll )
658 FreeLibrary( p_vout->p_sys->hddraw_dll );
659 p_vout->p_sys->hddraw_dll = NULL;
660 p_vout->p_sys->p_ddobject = NULL;
664 /*****************************************************************************
665 * DirectXCreateDisplay: create the DirectDraw display.
666 *****************************************************************************
667 * Create and initialize display according to preferences specified in the vout
669 *****************************************************************************/
670 static int DirectXCreateDisplay( vout_thread_t *p_vout )
674 LPDIRECTDRAWSURFACE p_display;
675 DDPIXELFORMAT pixel_format;
677 msg_Dbg( p_vout, "DirectXCreateDisplay" );
679 /* Now get the primary surface. This surface is what you actually see
681 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
682 ddsd.dwSize = sizeof(DDSURFACEDESC);
683 ddsd.dwFlags = DDSD_CAPS;
684 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
686 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
689 if( dxresult != DD_OK )
691 msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
695 dxresult = IDirectDrawSurface_QueryInterface( p_display,
696 &IID_IDirectDrawSurface2,
697 (LPVOID *)&p_vout->p_sys->p_display );
698 /* Release the old interface */
699 IDirectDrawSurface_Release( p_display );
700 if ( dxresult != DD_OK )
702 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
703 "(error %li)", dxresult );
707 /* The clipper will be used only in non-overlay mode */
708 DirectXCreateClipper( p_vout );
712 /* compute the colorkey pixel value from the RGB value we've got */
713 memset( &pixel_format, 0, sizeof( DDPIXELFORMAT ));
714 pixel_format.dwSize = sizeof( DDPIXELFORMAT );
715 dxresult = IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
717 if( dxresult != DD_OK )
719 msg_Warn( p_vout, "DirectXUpdateOverlay GetPixelFormat failed "
720 "(error %li)", dxresult );
722 p_vout->p_sys->i_colorkey = (DWORD)((( p_vout->p_sys->i_rgb_colorkey
723 * pixel_format.dwRBitMask) / 255)
724 & pixel_format.dwRBitMask );
731 /*****************************************************************************
732 * DirectXCreateClipper: Create a clipper that will be used when blitting the
733 * RGB surface to the main display.
734 *****************************************************************************
735 * This clipper prevents us to modify by mistake anything on the screen
736 * which doesn't belong to our window. For example when a part of our video
737 * window is hidden by another window.
738 *****************************************************************************/
739 static int DirectXCreateClipper( vout_thread_t *p_vout )
743 msg_Dbg( p_vout, "DirectXCreateClipper" );
745 /* Create the clipper */
746 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
747 &p_vout->p_sys->p_clipper, NULL );
748 if( dxresult != DD_OK )
750 msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
754 /* Associate the clipper to the window */
755 dxresult = IDirectDrawClipper_SetHWnd(p_vout->p_sys->p_clipper, 0,
756 p_vout->p_sys->hwnd);
757 if( dxresult != DD_OK )
759 msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
764 /* associate the clipper with the surface */
765 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
766 p_vout->p_sys->p_clipper);
767 if( dxresult != DD_OK )
769 msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
777 if( p_vout->p_sys->p_clipper )
779 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
781 p_vout->p_sys->p_clipper = NULL;
785 /*****************************************************************************
786 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
787 *****************************************************************************
788 * The best method of display is with an YUV overlay because the YUV->RGB
789 * conversion is done in hardware.
790 * You can also create a plain RGB surface.
791 * ( Maybe we could also try an RGB overlay surface, which could have hardware
792 * scaling and which would also be faster in window mode because you don't
793 * need to do any blitting to the main display...)
794 *****************************************************************************/
795 static int DirectXCreateSurface( vout_thread_t *p_vout,
796 LPDIRECTDRAWSURFACE2 *pp_surface_final,
797 int i_chroma, int b_overlay,
801 LPDIRECTDRAWSURFACE p_surface;
804 /* Create the video surface */
807 /* Now try to create the YUV overlay surface.
808 * This overlay will be displayed on top of the primary surface.
809 * A color key is used to determine whether or not the overlay will be
810 * displayed, ie the overlay will be displayed in place of the primary
811 * surface wherever the primary surface will have this color.
812 * The video window has been created with a background of this color so
813 * the overlay will be only displayed on top of this window */
815 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
816 ddsd.dwSize = sizeof(DDSURFACEDESC);
817 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
818 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
819 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
820 ddsd.dwFlags = DDSD_CAPS |
824 ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
825 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY |
827 ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
829 ddsd.dwHeight = p_vout->render.i_height;
830 ddsd.dwWidth = p_vout->render.i_width;
831 ddsd.dwBackBufferCount = i_backbuffers;
833 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
836 if( dxresult != DD_OK )
838 *pp_surface_final = NULL;
845 vlc_bool_t b_rgb_surface =
846 ( i_chroma == VLC_FOURCC('R','G','B','2') )
847 || ( i_chroma == VLC_FOURCC('R','V','1','5') )
848 || ( i_chroma == VLC_FOURCC('R','V','1','6') )
849 || ( i_chroma == VLC_FOURCC('R','V','2','4') )
850 || ( i_chroma == VLC_FOURCC('R','V','3','2') );
852 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
853 ddsd.dwSize = sizeof(DDSURFACEDESC);
854 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
855 ddsd.dwFlags = DDSD_HEIGHT |
858 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
859 ddsd.dwHeight = p_vout->render.i_height;
860 ddsd.dwWidth = p_vout->render.i_width;
862 if( p_vout->p_sys->b_use_sysmem )
863 ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
865 ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
869 ddsd.dwFlags |= DDSD_PIXELFORMAT;
870 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
871 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
874 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
877 if( dxresult != DD_OK )
879 *pp_surface_final = NULL;
884 /* Now that the surface is created, try to get a newer DirectX interface */
885 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
886 &IID_IDirectDrawSurface2,
887 (LPVOID *)pp_surface_final );
888 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
889 if ( dxresult != DD_OK )
891 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
892 "(error %li)", dxresult );
893 *pp_surface_final = NULL;
900 /*****************************************************************************
901 * DirectXUpdateOverlay: Move or resize overlay surface on video display.
902 *****************************************************************************
903 * This function is used to move or resize an overlay surface on the screen.
904 * Ususally the overlay is moved by the user and thus, by a move or resize
906 *****************************************************************************/
907 void DirectXUpdateOverlay( vout_thread_t *p_vout )
913 if( p_vout->p_sys->p_current_surface == NULL ||
914 !p_vout->p_sys->b_using_overlay )
917 /* The new window dimensions should already have been computed by the
918 * caller of this function */
920 /* Position and show the overlay */
921 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
922 ddofx.dwSize = sizeof(DDOVERLAYFX);
923 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
924 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
926 dwFlags = DDOVER_SHOW;
927 if( !p_vout->p_sys->b_caps_overlay_clipping )
928 dwFlags |= DDOVER_KEYDESTOVERRIDE;
930 dxresult = IDirectDrawSurface2_UpdateOverlay(
931 p_vout->p_sys->p_current_surface,
932 &p_vout->p_sys->rect_src_clipped,
933 p_vout->p_sys->p_display,
934 &p_vout->p_sys->rect_dest_clipped,
936 if(dxresult != DD_OK)
939 "DirectXUpdateOverlay cannot move or resize overlay" );
943 /*****************************************************************************
944 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
945 *****************************************************************************
946 * This function returns all resources allocated by DirectXInitDDraw.
947 *****************************************************************************/
948 static void DirectXCloseDDraw( vout_thread_t *p_vout )
950 msg_Dbg( p_vout, "DirectXCloseDDraw" );
951 if( p_vout->p_sys->p_ddobject != NULL )
953 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
954 p_vout->p_sys->p_ddobject = NULL;
957 if( p_vout->p_sys->hddraw_dll != NULL )
959 FreeLibrary( p_vout->p_sys->hddraw_dll );
960 p_vout->p_sys->hddraw_dll = NULL;
964 /*****************************************************************************
965 * DirectXCloseDisplay: close and reset the DirectX display device
966 *****************************************************************************
967 * This function returns all resources allocated by DirectXCreateDisplay.
968 *****************************************************************************/
969 static void DirectXCloseDisplay( vout_thread_t *p_vout )
971 msg_Dbg( p_vout, "DirectXCloseDisplay" );
973 if( p_vout->p_sys->p_clipper != NULL )
975 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
976 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
977 p_vout->p_sys->p_clipper = NULL;
980 if( p_vout->p_sys->p_display != NULL )
982 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
983 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
984 p_vout->p_sys->p_display = NULL;
988 /*****************************************************************************
989 * DirectXCloseSurface: close the YUV overlay or RGB surface.
990 *****************************************************************************
991 * This function returns all resources allocated for the surface.
992 *****************************************************************************/
993 static void DirectXCloseSurface( vout_thread_t *p_vout,
994 LPDIRECTDRAWSURFACE2 p_surface )
996 msg_Dbg( p_vout, "DirectXCloseSurface" );
997 if( p_surface != NULL )
999 IDirectDrawSurface2_Release( p_surface );
1003 /*****************************************************************************
1004 * NewPictureVec: allocate a vector of identical pictures
1005 *****************************************************************************
1006 * Returns 0 on success, -1 otherwise
1007 *****************************************************************************/
1008 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1012 int i_ret = VLC_SUCCESS;
1013 LPDIRECTDRAWSURFACE2 p_surface;
1015 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1016 p_vout->p_sys->b_using_overlay ? "yes" : "no",
1017 (char *)&p_vout->output.i_chroma );
1019 I_OUTPUTPICTURES = 0;
1021 /* First we try to use an YUV overlay surface.
1022 * The overlay surface that we create won't be used to decode directly
1023 * into it because accessing video memory directly is way to slow (remember
1024 * that pictures are decoded macroblock per macroblock). Instead the video
1025 * will be decoded in picture buffers in system memory which will then be
1026 * memcpy() to the overlay surface. */
1027 if( p_vout->p_sys->b_using_overlay )
1029 /* Triple buffering rocks! it doesn't have any processing overhead
1030 * (you don't have to wait for the vsync) and provides for a very nice
1031 * video quality (no tearing). */
1032 if( p_vout->p_sys->b_3buf_overlay )
1033 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1034 p_vout->output.i_chroma,
1035 p_vout->p_sys->b_using_overlay,
1036 2 /* number of backbuffers */ );
1038 if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1040 /* Try to reduce the number of backbuffers */
1041 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1042 p_vout->output.i_chroma,
1043 p_vout->p_sys->b_using_overlay,
1044 0 /* number of backbuffers */ );
1047 if( i_ret == VLC_SUCCESS )
1050 picture_t front_pic;
1051 picture_sys_t front_pic_sys;
1052 front_pic.p_sys = &front_pic_sys;
1054 /* Allocate internal structure */
1055 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1056 if( p_pic[0].p_sys == NULL )
1058 DirectXCloseSurface( p_vout, p_surface );
1062 /* set front buffer */
1063 p_pic[0].p_sys->p_front_surface = p_surface;
1065 /* Get the back buffer */
1066 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1067 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1068 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1069 p_surface, &dds_caps,
1070 &p_pic[0].p_sys->p_surface ) )
1072 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1073 /* front buffer is the same as back buffer */
1074 p_pic[0].p_sys->p_surface = p_surface;
1078 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1079 p_pic[0].p_sys->p_front_surface;
1081 /* Reset the front buffer memory */
1082 if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1085 for( i = 0; i < front_pic.i_planes; i++ )
1086 for( j = 0; j < front_pic.p[i].i_lines; j++)
1087 memset( front_pic.p[i].p_pixels + j *
1088 front_pic.p[i].i_pitch, 127,
1089 front_pic.p[i].i_visible_pitch );
1091 DirectXUnlockSurface( p_vout, &front_pic );
1094 DirectXUpdateOverlay( p_vout );
1095 I_OUTPUTPICTURES = 1;
1096 msg_Dbg( p_vout, "YUV overlay created successfully" );
1100 /* As we can't have an overlay, we'll try to create a plain offscreen
1101 * surface. This surface will reside in video memory because there's a
1102 * better chance then that we'll be able to use some kind of hardware
1103 * acceleration like rescaling, blitting or YUV->RGB conversions.
1104 * We then only need to blit this surface onto the main display when we
1105 * want to display it */
1106 if( !p_vout->p_sys->b_using_overlay )
1108 if( p_vout->p_sys->b_hw_yuv )
1112 vlc_bool_t b_result = VLC_FALSE;
1114 /* Check if the chroma is supported first. This is required
1115 * because a few buggy drivers don't mind creating the surface
1116 * even if they don't know about the chroma. */
1117 if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1120 pi_codes = malloc( i_codes * sizeof(DWORD) );
1121 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1122 p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) )
1124 for( i = 0; i < (int)i_codes; i++ )
1126 if( p_vout->output.i_chroma == pi_codes[i] )
1128 b_result = VLC_TRUE;
1136 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1137 p_vout->output.i_chroma,
1139 0 /* no back buffers */ );
1141 p_vout->p_sys->b_hw_yuv = VLC_FALSE;
1144 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1146 /* Our last choice is to use a plain RGB surface */
1147 DDPIXELFORMAT ddpfPixelFormat;
1149 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1150 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1153 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1155 switch( ddpfPixelFormat.dwRGBBitCount )
1157 case 8: /* FIXME: set the palette */
1158 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
1161 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
1164 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
1167 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
1170 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
1173 msg_Err( p_vout, "unknown screen depth" );
1174 return VLC_EGENERIC;
1176 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1177 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1178 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1181 p_vout->p_sys->b_hw_yuv = 0;
1183 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1184 p_vout->output.i_chroma,
1186 0 /* no back buffers */ );
1188 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1190 /* Give it a last try with b_use_sysmem enabled */
1191 p_vout->p_sys->b_use_sysmem = 1;
1193 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1194 p_vout->output.i_chroma,
1196 0 /* no back buffers */ );
1200 if( i_ret == VLC_SUCCESS )
1202 /* Allocate internal structure */
1203 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1204 if( p_pic[0].p_sys == NULL )
1206 DirectXCloseSurface( p_vout, p_surface );
1210 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1213 I_OUTPUTPICTURES = 1;
1215 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1216 (char *)&p_vout->output.i_chroma );
1221 /* Now that we've got all our direct-buffers, we can finish filling in the
1222 * picture_t structures */
1223 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1225 p_pic[i].i_status = DESTROYED_PICTURE;
1226 p_pic[i].i_type = DIRECT_PICTURE;
1227 p_pic[i].pf_lock = DirectXLockSurface;
1228 p_pic[i].pf_unlock = DirectXUnlockSurface;
1229 PP_OUTPUTPICTURE[i] = &p_pic[i];
1231 if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1234 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1235 I_OUTPUTPICTURES = 0;
1236 msg_Err( p_vout, "cannot lock surface" );
1237 return VLC_EGENERIC;
1239 DirectXUnlockSurface( p_vout, &p_pic[i] );
1242 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1243 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1248 /*****************************************************************************
1249 * FreePicture: destroy a picture vector allocated with NewPictureVec
1250 *****************************************************************************
1252 *****************************************************************************/
1253 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1258 for( i = 0; i < i_num_pics; i++ )
1260 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1262 for( i = 0; i < i_num_pics; i++ )
1264 free( p_pic[i].p_sys );
1269 /*****************************************************************************
1270 * UpdatePictureStruct: updates the internal data in the picture_t structure
1271 *****************************************************************************
1272 * This will setup stuff for use by the video_output thread
1273 *****************************************************************************/
1274 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1277 switch( p_vout->output.i_chroma )
1279 case VLC_FOURCC('R','G','B','2'):
1280 case VLC_FOURCC('R','V','1','5'):
1281 case VLC_FOURCC('R','V','1','6'):
1282 case VLC_FOURCC('R','V','2','4'):
1283 case VLC_FOURCC('R','V','3','2'):
1284 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1285 p_pic->p->i_lines = p_vout->output.i_height;
1286 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1287 switch( p_vout->output.i_chroma )
1289 case VLC_FOURCC('R','G','B','2'):
1290 p_pic->p->i_pixel_pitch = 1;
1292 case VLC_FOURCC('R','V','1','5'):
1293 case VLC_FOURCC('R','V','1','6'):
1294 p_pic->p->i_pixel_pitch = 2;
1296 case VLC_FOURCC('R','V','2','4'):
1297 case VLC_FOURCC('R','V','3','2'):
1298 p_pic->p->i_pixel_pitch = 4;
1301 return VLC_EGENERIC;
1303 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1304 p_pic->p->i_pixel_pitch;
1305 p_pic->i_planes = 1;
1308 case VLC_FOURCC('Y','V','1','2'):
1310 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1311 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1312 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1313 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1314 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1315 p_pic->p[Y_PLANE].i_pixel_pitch;
1317 p_pic->V_PIXELS = p_pic->Y_PIXELS
1318 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1319 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1320 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1321 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1322 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1323 p_pic->p[V_PLANE].i_pixel_pitch;
1325 p_pic->U_PIXELS = p_pic->V_PIXELS
1326 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1327 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1328 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1329 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1330 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1331 p_pic->p[U_PLANE].i_pixel_pitch;
1333 p_pic->i_planes = 3;
1336 case VLC_FOURCC('I','Y','U','V'):
1338 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1339 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1340 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1341 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1342 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1343 p_pic->p[Y_PLANE].i_pixel_pitch;
1345 p_pic->U_PIXELS = p_pic->Y_PIXELS
1346 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1347 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1348 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1349 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1350 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1351 p_pic->p[U_PLANE].i_pixel_pitch;
1353 p_pic->V_PIXELS = p_pic->U_PIXELS
1354 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1355 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1356 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1357 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1358 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1359 p_pic->p[V_PLANE].i_pixel_pitch;
1361 p_pic->i_planes = 3;
1364 case VLC_FOURCC('Y','U','Y','2'):
1366 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1367 p_pic->p->i_lines = p_vout->output.i_height;
1368 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1369 p_pic->p->i_pixel_pitch = 2;
1370 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1371 p_pic->p->i_pixel_pitch;
1373 p_pic->i_planes = 1;
1377 /* Unknown chroma, tell the guy to get lost */
1378 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1379 p_vout->output.i_chroma,
1380 (char*)&p_vout->output.i_chroma );
1381 return VLC_EGENERIC;
1387 /*****************************************************************************
1388 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1389 *****************************************************************************
1390 * It is nice to know which features are supported by the hardware so we can
1391 * find ways to optimize our rendering.
1392 *****************************************************************************/
1393 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1398 /* This is just an indication of whether or not we'll support overlay,
1399 * but with this test we don't know if we support YUV overlay */
1400 memset( &ddcaps, 0, sizeof( DDCAPS ));
1401 ddcaps.dwSize = sizeof(DDCAPS);
1402 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1404 if(dxresult != DD_OK )
1406 msg_Warn( p_vout, "cannot get caps" );
1410 BOOL bHasOverlay, bHasOverlayFourCC, bCanClipOverlay,
1411 bHasColorKey, bCanStretch, bCanBltFourcc;
1413 /* Determine if the hardware supports overlay surfaces */
1414 bHasOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAY) ==
1415 DDCAPS_OVERLAY) ? TRUE : FALSE;
1416 /* Determine if the hardware supports overlay surfaces */
1417 bHasOverlayFourCC = ((ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ==
1418 DDCAPS_OVERLAYFOURCC) ? TRUE : FALSE;
1419 /* Determine if the hardware supports overlay surfaces */
1420 bCanClipOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAYCANTCLIP) ==
1422 /* Determine if the hardware supports colorkeying */
1423 bHasColorKey = ((ddcaps.dwCaps & DDCAPS_COLORKEY) ==
1424 DDCAPS_COLORKEY) ? TRUE : FALSE;
1425 /* Determine if the hardware supports scaling of the overlay surface */
1426 bCanStretch = ((ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ==
1427 DDCAPS_OVERLAYSTRETCH) ? TRUE : FALSE;
1428 /* Determine if the hardware supports color conversion during a blit */
1429 bCanBltFourcc = ((ddcaps.dwCaps & DDCAPS_BLTFOURCC ) ==
1430 DDCAPS_BLTFOURCC) ? TRUE : FALSE;
1432 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1433 "can_clip_overlay=%i colorkey=%i stretch=%i "
1435 bHasOverlay, bHasOverlayFourCC, bCanClipOverlay,
1436 bHasColorKey, bCanStretch, bCanBltFourcc );
1438 /* Overlay clipping support is interesting for us as it means we can
1439 * get rid of the colorkey alltogether */
1440 p_vout->p_sys->b_caps_overlay_clipping = bCanClipOverlay;
1442 /* Don't ask for troubles */
1443 if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1447 /*****************************************************************************
1448 * DirectXLockSurface: Lock surface and get picture data pointer
1449 *****************************************************************************
1450 * This function locks a surface and get the surface descriptor which amongst
1451 * other things has the pointer to the picture data.
1452 *****************************************************************************/
1453 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1457 /* Lock the surface to get a valid pointer to the picture buffer */
1458 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1459 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1460 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1461 NULL, &p_pic->p_sys->ddsd,
1462 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1464 if( dxresult != DD_OK )
1466 if( dxresult == DDERR_INVALIDPARAMS )
1468 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1469 * in an invalid params error */
1470 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1471 &p_pic->p_sys->ddsd,
1474 if( dxresult == DDERR_SURFACELOST )
1476 /* Your surface can be lost so be sure
1477 * to check this and restore it if needed */
1479 /* When using overlays with back-buffers, we need to restore
1480 * the front buffer so the back-buffers get restored as well. */
1481 if( p_vout->p_sys->b_using_overlay )
1482 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1484 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1486 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1487 &p_pic->p_sys->ddsd,
1489 if( dxresult == DDERR_SURFACELOST )
1490 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1492 if( dxresult != DD_OK )
1494 return VLC_EGENERIC;
1498 /* Now we have a pointer to the surface memory, we can update our picture
1500 if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma )
1503 DirectXUnlockSurface( p_vout, p_pic );
1504 return VLC_EGENERIC;
1510 /*****************************************************************************
1511 * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1512 *****************************************************************************/
1513 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1515 /* Unlock the Surface */
1516 if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1519 return VLC_EGENERIC;