1 /*****************************************************************************
2 * vout.c: Windows DirectX video output display method
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: directx.c,v 1.12 2003/02/01 18:54:10 sam 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() */
52 /*****************************************************************************
54 * Defining them here allows us to get rid of the dxguid library during
56 *****************************************************************************/
58 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
59 DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
61 /*****************************************************************************
63 *****************************************************************************/
64 static void ToggleOnTop ();
66 static int OpenVideo ( vlc_object_t * );
67 static void CloseVideo ( vlc_object_t * );
69 static int Init ( vout_thread_t * );
70 static void End ( vout_thread_t * );
71 static int Manage ( vout_thread_t * );
72 static void Display ( vout_thread_t *, picture_t * );
74 static int NewPictureVec ( vout_thread_t *, picture_t *, int );
75 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
76 static int UpdatePictureStruct( vout_thread_t *, picture_t *, int );
78 static int DirectXInitDDraw ( vout_thread_t *p_vout );
79 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
80 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
81 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
82 static int DirectXCreateSurface ( vout_thread_t *p_vout,
83 LPDIRECTDRAWSURFACE2 *, int, int, int );
84 static void DirectXCloseSurface ( vout_thread_t *p_vout,
85 LPDIRECTDRAWSURFACE2 );
86 static int DirectXCreateClipper ( vout_thread_t *p_vout );
87 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
88 static int DirectXGetSurfaceDesc ( vout_thread_t *p_vout, picture_t *p_pic );
90 /*****************************************************************************
92 *****************************************************************************/
93 #define ON_TOP_TEXT N_("always on top")
94 #define ON_TOP_LONGTEXT N_("place the directx window on top of other windows")
95 #define HW_YUV_TEXT N_("use hardware YUV->RGB conversions")
96 #define HW_YUV_LONGTEXT N_( \
97 "Try to use hardware acceleration for YUV->RGB conversions. " \
98 "This option doesn't have any effect when using overlays." )
99 #define SYSMEM_TEXT N_("use video buffers in system memory")
100 #define SYSMEM_LONGTEXT N_( \
101 "Create video buffers in system memory instead of video memory. This " \
102 "isn't recommended as usually using video memory allows to benefit from " \
103 "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
104 "This option doesn't have any effect when using overlays." )
107 add_category_hint( N_("Video"), NULL );
108 add_bool( "directx-on-top", 0, NULL, ON_TOP_TEXT, ON_TOP_LONGTEXT );
109 add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT );
110 add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT );
111 set_description( _("DirectX video module") );
112 set_capability( "video output", 100 );
113 add_shortcut( "directx" );
114 set_callbacks( OpenVideo, CloseVideo );
118 /* check if we registered a window class because we need to
121 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
122 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
125 /*****************************************************************************
126 * OpenVideo: allocate DirectX video thread output method
127 *****************************************************************************
128 * This function allocates and initialize the DirectX vout method.
129 *****************************************************************************/
130 static int OpenVideo( vlc_object_t *p_this )
132 vout_thread_t * p_vout = (vout_thread_t *)p_this;
135 /* Allocate structure */
136 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
137 if( p_vout->p_sys == NULL )
139 msg_Err( p_vout, "out of memory" );
143 /* Initialisations */
144 p_vout->pf_init = Init;
145 p_vout->pf_end = End;
146 p_vout->pf_manage = Manage;
147 p_vout->pf_render = NULL;
148 p_vout->pf_display = Display;
150 p_vout->p_sys->p_ddobject = NULL;
151 p_vout->p_sys->p_display = NULL;
152 p_vout->p_sys->p_current_surface = NULL;
153 p_vout->p_sys->p_clipper = NULL;
154 p_vout->p_sys->hbrush = NULL;
155 p_vout->p_sys->hwnd = NULL;
156 p_vout->p_sys->hparent = NULL;
157 p_vout->p_sys->i_changes = 0;
158 p_vout->p_sys->b_caps_overlay_clipping = 0;
159 SetRectEmpty( &p_vout->p_sys->rect_display );
160 p_vout->p_sys->b_using_overlay = config_GetInt( p_vout, "overlay" );
161 p_vout->p_sys->b_use_sysmem = config_GetInt( p_vout, "directx-use-sysmem");
162 p_vout->p_sys->b_hw_yuv = config_GetInt( p_vout, "directx-hw-yuv" );
164 p_vout->p_sys->b_cursor_hidden = 0;
165 p_vout->p_sys->i_lastmoved = mdate();
167 /* Set main window's size */
168 p_vout->p_sys->i_window_width = p_vout->i_window_width;
169 p_vout->p_sys->i_window_height = p_vout->i_window_height;
171 /* Create the DirectXEventThread, this thread is created by us to isolate
172 * the Win32 PeekMessage function calls. We want to do this because
173 * Windows can stay blocked inside this call for a long time, and when
174 * this happens it thus blocks vlc's video_output thread.
175 * DirectXEventThread will take care of the creation of the video
176 * window (because PeekMessage has to be called from the same thread which
177 * created the window). */
178 msg_Dbg( p_vout, "creating DirectXEventThread" );
179 p_vout->p_sys->p_event =
180 vlc_object_create( p_vout, sizeof(event_thread_t) );
181 p_vout->p_sys->p_event->p_vout = p_vout;
182 if( vlc_thread_create( p_vout->p_sys->p_event,
183 "DirectX Events Thread", DirectXEventThread,
186 msg_Err( p_vout, "cannot create DirectXEventThread" );
187 vlc_object_destroy( p_vout->p_sys->p_event );
188 p_vout->p_sys->p_event = NULL;
192 if( p_vout->p_sys->p_event->b_error )
194 msg_Err( p_vout, "DirectXEventThread failed" );
198 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
200 msg_Dbg( p_vout, "DirectXEventThread running" );
202 /* Initialise DirectDraw */
203 if( DirectXInitDDraw( p_vout ) )
205 msg_Err( p_vout, "cannot initialize DirectDraw" );
209 /* Create the directx display */
210 if( DirectXCreateDisplay( p_vout ) )
212 msg_Err( p_vout, "cannot initialize DirectDraw" );
216 /* Add a variable to indicate if the window should be on top of others */
217 var_Create( p_vout, "directx-on-top", VLC_VAR_BOOL );
218 val.b_bool = config_GetInt( p_vout, "directx-on-top" );
219 var_Set( p_vout, "directx-on-top", val );
224 CloseVideo( VLC_OBJECT(p_vout) );
228 /*****************************************************************************
229 * Init: initialize DirectX video thread output method
230 *****************************************************************************
231 * This function create the directx surfaces needed by the output thread.
232 * It is called at the beginning of the thread.
233 *****************************************************************************/
234 static int Init( vout_thread_t *p_vout )
238 /* Initialize the output structure.
239 * Since DirectDraw can do rescaling for us, stick to the default
240 * coordinates and aspect. */
241 p_vout->output.i_width = p_vout->render.i_width;
242 p_vout->output.i_height = p_vout->render.i_height;
243 p_vout->output.i_aspect = p_vout->render.i_aspect;
245 #define MAX_DIRECTBUFFERS 1
246 /* Right now we use only 1 directbuffer because we don't want the
247 * video decoder to decode directly into direct buffers as they are
248 * created into video memory and video memory is _really_ slow */
250 /* Choose the chroma we will try first. */
251 switch( p_vout->render.i_chroma )
253 case VLC_FOURCC('Y','U','Y','2'):
254 case VLC_FOURCC('Y','U','N','V'):
255 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
257 case VLC_FOURCC('U','Y','V','Y'):
258 case VLC_FOURCC('U','Y','N','V'):
259 case VLC_FOURCC('Y','4','2','2'):
260 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
262 case VLC_FOURCC('Y','V','Y','U'):
263 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
266 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
270 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
272 i_chroma_backup = p_vout->output.i_chroma;
274 if( !I_OUTPUTPICTURES )
276 /* hmmm, it didn't work! Let's try commonly supported chromas */
277 if( p_vout->output.i_chroma != VLC_FOURCC('Y','V','1','2') )
279 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
280 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
282 if( !I_OUTPUTPICTURES )
284 /* hmmm, it still didn't work! Let's try another one */
285 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
286 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
290 if( !I_OUTPUTPICTURES )
292 /* If it still didn't work then don't try to use an overlay */
293 p_vout->output.i_chroma = i_chroma_backup;
294 p_vout->p_sys->b_using_overlay = 0;
295 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
298 /* Change the window title bar text */
299 if( p_vout->p_sys->hparent )
301 else if( p_vout->p_sys->b_using_overlay )
302 SetWindowText( p_vout->p_sys->hwnd,
303 VOUT_TITLE " (hardware YUV overlay DirectX output)" );
304 else if( p_vout->p_sys->b_hw_yuv )
305 SetWindowText( p_vout->p_sys->hwnd,
306 VOUT_TITLE " (hardware YUV DirectX output)" );
307 else SetWindowText( p_vout->p_sys->hwnd,
308 VOUT_TITLE " (software RGB DirectX output)" );
313 /*****************************************************************************
314 * End: terminate Sys video thread output method
315 *****************************************************************************
316 * Terminate an output method created by Create.
317 * It is called at the end of the thread.
318 *****************************************************************************/
319 static void End( vout_thread_t *p_vout )
321 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
325 /*****************************************************************************
326 * CloseVideo: destroy Sys video thread output method
327 *****************************************************************************
328 * Terminate an output method created by Create
329 *****************************************************************************/
330 static void CloseVideo( vlc_object_t *p_this )
332 vout_thread_t * p_vout = (vout_thread_t *)p_this;
334 msg_Dbg( p_vout, "CloseVideo" );
336 var_Destroy( p_vout, "directs-on-top" );
338 DirectXCloseDisplay( p_vout );
339 DirectXCloseDDraw( p_vout );
341 if( p_vout->p_sys->p_event )
343 vlc_object_detach( p_vout->p_sys->p_event );
345 /* Kill DirectXEventThread */
346 p_vout->p_sys->p_event->b_die = VLC_TRUE;
348 /* we need to be sure DirectXEventThread won't stay stuck in
349 * GetMessage, so we send a fake message */
350 if( p_vout->p_sys->hwnd )
352 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
355 vlc_thread_join( p_vout->p_sys->p_event );
356 vlc_object_destroy( p_vout->p_sys->p_event );
361 free( p_vout->p_sys );
362 p_vout->p_sys = NULL;
366 /*****************************************************************************
367 * Manage: handle Sys events
368 *****************************************************************************
369 * This function should be called regularly by the video output thread.
370 * It returns a non null value if an error occured.
371 *****************************************************************************/
372 static int Manage( vout_thread_t *p_vout )
374 WINDOWPLACEMENT window_placement;
379 /* If we do not control our window, we check for geometry changes
380 * ourselves because the parent might not send us its events. */
381 if( p_vout->p_sys->hparent )
383 DirectXUpdateRects( p_vout, VLC_FALSE );
386 /* We used to call the Win32 PeekMessage function here to read the window
387 * messages. But since window can stay blocked into this function for a
388 * long time (for example when you move your window on the screen), I
389 * decided to isolate PeekMessage in another thread. */
394 if( p_vout->i_changes & VOUT_SCALE_CHANGE
395 || p_vout->p_sys->i_changes & VOUT_SCALE_CHANGE )
397 msg_Dbg( p_vout, "scale change" );
398 if( !p_vout->p_sys->b_using_overlay )
399 InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
401 DirectXUpdateOverlay( p_vout );
402 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
403 p_vout->p_sys->i_changes &= ~VOUT_SCALE_CHANGE;
409 if( p_vout->i_changes & VOUT_SIZE_CHANGE
410 || p_vout->p_sys->i_changes & VOUT_SIZE_CHANGE )
412 msg_Dbg( p_vout, "size change" );
413 if( !p_vout->p_sys->b_using_overlay )
414 InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
416 DirectXUpdateOverlay( p_vout );
417 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
418 p_vout->p_sys->i_changes &= ~VOUT_SIZE_CHANGE;
424 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
425 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
427 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
429 /* We need to switch between Maximized and Normal sized window */
430 window_placement.length = sizeof(WINDOWPLACEMENT);
431 GetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
432 if( p_vout->b_fullscreen )
434 /* Maximized window */
435 window_placement.showCmd = SW_SHOWMAXIMIZED;
436 /* Change window style, no borders and no title bar */
437 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, 0 );
443 window_placement.showCmd = SW_SHOWNORMAL;
444 /* Change window style, borders and title bar */
445 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
446 WS_OVERLAPPEDWINDOW | WS_SIZEBOX | WS_VISIBLE );
449 SetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
451 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
452 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
458 if( (!p_vout->p_sys->b_cursor_hidden) &&
459 ( (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 ) )
461 /* Hide the mouse automatically */
462 if( p_vout->p_sys->hwnd != p_vout->p_sys->hparent )
464 p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
465 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
470 * "Always on top" status change
472 hwnd = p_vout->p_sys->hwnd;
473 hMenu = GetSystemMenu( hwnd , FALSE );
474 var_Get( p_vout, "directx-on-top", &val );
477 /* Set the window on top if necessary */
478 if( !( GetWindowLong( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST ) )
480 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
481 MF_BYCOMMAND | MFS_CHECKED );
482 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
483 SWP_NOSIZE | SWP_NOMOVE );
488 /* The window shouldn't be on top */
489 if( GetWindowLong( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST )
491 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
492 MF_BYCOMMAND | MFS_UNCHECKED );
493 SetWindowPos( hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
494 SWP_NOSIZE | SWP_NOMOVE );
498 /* Check if the event thread is still running */
499 if( p_vout->p_sys->p_event->b_die )
501 return VLC_EGENERIC; /* exit */
507 /*****************************************************************************
508 * Display: displays previously rendered output
509 *****************************************************************************
510 * This function sends the currently rendered image to the display, wait until
511 * it is displayed and switch the two rendering buffers, preparing next frame.
512 *****************************************************************************/
513 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
517 if( (p_vout->p_sys->p_display == NULL) )
519 msg_Warn( p_vout, "no display!!" );
523 if( !p_vout->p_sys->b_using_overlay )
527 /* We ask for the "NOTEARING" option */
528 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
529 ddbltfx.dwSize = sizeof(DDBLTFX);
530 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
532 /* Blit video surface to display */
533 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
534 &p_vout->p_sys->rect_dest_clipped,
535 p_pic->p_sys->p_surface,
536 &p_vout->p_sys->rect_src_clipped,
537 DDBLT_ASYNC, &ddbltfx );
538 if ( dxresult == DDERR_SURFACELOST )
540 /* Our surface can be lost so be sure
541 * to check this and restore it if needed */
542 IDirectDrawSurface2_Restore( p_vout->p_sys->p_display );
544 /* Now that the surface has been restored try to display again */
545 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
546 &p_vout->p_sys->rect_dest_clipped,
547 p_pic->p_sys->p_surface,
548 &p_vout->p_sys->rect_src_clipped,
549 DDBLT_ASYNC, &ddbltfx );
552 if( dxresult != DD_OK )
554 msg_Warn( p_vout, "could not blit surface (error %i)", dxresult );
559 else /* using overlay */
561 /* Flip the overlay buffers if we are using back buffers */
562 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
567 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
569 if ( dxresult == DDERR_SURFACELOST )
571 /* Our surface can be lost so be sure
572 * to check this and restore it if needed */
573 IDirectDrawSurface2_Restore( p_vout->p_sys->p_display );
574 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
576 /* Now that the surface has been restored try to display again */
577 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
579 DirectXUpdateOverlay( p_vout );
582 if( dxresult != DD_OK )
584 msg_Warn( p_vout, "could not flip overlay (error %i)", dxresult );
587 if( DirectXGetSurfaceDesc( p_vout, p_pic ) )
590 msg_Err( p_vout, "cannot get surface desc" );
594 if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma ) )
597 msg_Err( p_vout, "invalid pic chroma" );
601 /* set currently displayed pic */
602 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
607 /* following functions are local */
609 /*****************************************************************************
610 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
611 *****************************************************************************
612 * This function initialise and allocate resources for DirectDraw.
613 *****************************************************************************/
614 static int DirectXInitDDraw( vout_thread_t *p_vout )
617 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
618 LPDIRECTDRAW p_ddobject;
620 msg_Dbg( p_vout, "DirectXInitDDraw" );
622 /* load direct draw DLL */
623 p_vout->p_sys->hddraw_dll = LoadLibrary("DDRAW.DLL");
624 if( p_vout->p_sys->hddraw_dll == NULL )
626 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
630 OurDirectDrawCreate =
631 (void *)GetProcAddress(p_vout->p_sys->hddraw_dll, "DirectDrawCreate");
632 if ( OurDirectDrawCreate == NULL )
634 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
638 /* Initialize DirectDraw now */
639 dxresult = OurDirectDrawCreate( NULL, &p_ddobject, NULL );
640 if( dxresult != DD_OK )
642 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
646 /* Get the IDirectDraw2 interface */
647 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
648 (LPVOID *)&p_vout->p_sys->p_ddobject );
649 /* Release the unused interface */
650 IDirectDraw_Release( p_ddobject );
651 if( dxresult != DD_OK )
653 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
657 /* Set DirectDraw Cooperative level, ie what control we want over Windows
659 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
660 p_vout->p_sys->hwnd, DDSCL_NORMAL );
661 if( dxresult != DD_OK )
663 msg_Err( p_vout, "cannot set direct draw cooperative level" );
667 /* Probe the capabilities of the hardware */
668 DirectXGetDDrawCaps( p_vout );
670 msg_Dbg( p_vout, "End DirectXInitDDraw" );
674 if( p_vout->p_sys->p_ddobject )
675 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
676 if( p_vout->p_sys->hddraw_dll )
677 FreeLibrary( p_vout->p_sys->hddraw_dll );
678 p_vout->p_sys->hddraw_dll = NULL;
679 p_vout->p_sys->p_ddobject = NULL;
683 /*****************************************************************************
684 * DirectXCreateDisplay: create the DirectDraw display.
685 *****************************************************************************
686 * Create and initialize display according to preferences specified in the vout
688 *****************************************************************************/
689 static int DirectXCreateDisplay( vout_thread_t *p_vout )
693 LPDIRECTDRAWSURFACE p_display;
694 DDPIXELFORMAT pixel_format;
696 msg_Dbg( p_vout, "DirectXCreateDisplay" );
698 /* Now get the primary surface. This surface is what you actually see
700 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
701 ddsd.dwSize = sizeof(DDSURFACEDESC);
702 ddsd.dwFlags = DDSD_CAPS;
703 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
705 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
708 if( dxresult != DD_OK )
710 msg_Err( p_vout, "cannot get primary surface (error %i)", dxresult );
714 dxresult = IDirectDrawSurface_QueryInterface( p_display,
715 &IID_IDirectDrawSurface2,
716 (LPVOID *)&p_vout->p_sys->p_display );
717 /* Release the old interface */
718 IDirectDrawSurface_Release( p_display );
719 if ( dxresult != DD_OK )
721 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
722 "(error %i)", dxresult );
726 /* The clipper will be used only in non-overlay mode */
727 DirectXCreateClipper( p_vout );
731 /* compute the colorkey pixel value from the RGB value we've got */
732 memset( &pixel_format, 0, sizeof( DDPIXELFORMAT ));
733 pixel_format.dwSize = sizeof( DDPIXELFORMAT );
734 dxresult = IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
736 if( dxresult != DD_OK )
738 msg_Warn( p_vout, "DirectXUpdateOverlay GetPixelFormat failed "
739 "(error %i)", dxresult );
741 p_vout->p_sys->i_colorkey = (DWORD)((( p_vout->p_sys->i_rgb_colorkey
742 * pixel_format.dwRBitMask) / 255)
743 & pixel_format.dwRBitMask );
750 /*****************************************************************************
751 * DirectXCreateClipper: Create a clipper that will be used when blitting the
752 * RGB surface to the main display.
753 *****************************************************************************
754 * This clipper prevents us to modify by mistake anything on the screen
755 * which doesn't belong to our window. For example when a part of our video
756 * window is hidden by another window.
757 *****************************************************************************/
758 static int DirectXCreateClipper( vout_thread_t *p_vout )
762 msg_Dbg( p_vout, "DirectXCreateClipper" );
764 /* Create the clipper */
765 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
766 &p_vout->p_sys->p_clipper, NULL );
767 if( dxresult != DD_OK )
769 msg_Warn( p_vout, "cannot create clipper (error %i)", dxresult );
773 /* Associate the clipper to the window */
774 dxresult = IDirectDrawClipper_SetHWnd(p_vout->p_sys->p_clipper, 0,
775 p_vout->p_sys->hwnd);
776 if( dxresult != DD_OK )
778 msg_Warn( p_vout, "cannot attach clipper to window (error %i)",
783 /* associate the clipper with the surface */
784 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
785 p_vout->p_sys->p_clipper);
786 if( dxresult != DD_OK )
788 msg_Warn( p_vout, "cannot attach clipper to surface (error %i)",
796 if( p_vout->p_sys->p_clipper )
798 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
800 p_vout->p_sys->p_clipper = NULL;
804 /*****************************************************************************
805 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
806 *****************************************************************************
807 * The best method of display is with an YUV overlay because the YUV->RGB
808 * conversion is done in hardware.
809 * You can also create a plain RGB surface.
810 * ( Maybe we could also try an RGB overlay surface, which could have hardware
811 * scaling and which would also be faster in window mode because you don't
812 * need to do any blitting to the main display...)
813 *****************************************************************************/
814 static int DirectXCreateSurface( vout_thread_t *p_vout,
815 LPDIRECTDRAWSURFACE2 *pp_surface_final,
816 int i_chroma, int b_overlay,
820 LPDIRECTDRAWSURFACE p_surface;
823 /* Create the video surface */
826 /* Now try to create the YUV overlay surface.
827 * This overlay will be displayed on top of the primary surface.
828 * A color key is used to determine whether or not the overlay will be
829 * displayed, ie the overlay will be displayed in place of the primary
830 * surface wherever the primary surface will have this color.
831 * The video window has been created with a background of this color so
832 * the overlay will be only displayed on top of this window */
834 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
835 ddsd.dwSize = sizeof(DDSURFACEDESC);
836 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
837 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
838 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
839 ddsd.dwFlags = DDSD_CAPS |
843 ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
844 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY |
846 ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
848 ddsd.dwHeight = p_vout->render.i_height;
849 ddsd.dwWidth = p_vout->render.i_width;
850 ddsd.dwBackBufferCount = i_backbuffers;
852 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
855 if( dxresult != DD_OK )
857 *pp_surface_final = NULL;
864 vlc_bool_t b_rgb_surface =
865 ( i_chroma == VLC_FOURCC('R','G','B','2') )
866 || ( i_chroma == VLC_FOURCC('R','V','1','5') )
867 || ( i_chroma == VLC_FOURCC('R','V','1','6') )
868 || ( i_chroma == VLC_FOURCC('R','V','2','4') )
869 || ( i_chroma == VLC_FOURCC('R','V','3','2') );
871 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
872 ddsd.dwSize = sizeof(DDSURFACEDESC);
873 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
874 ddsd.dwFlags = DDSD_HEIGHT |
877 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
878 ddsd.dwHeight = p_vout->render.i_height;
879 ddsd.dwWidth = p_vout->render.i_width;
881 if( p_vout->p_sys->b_use_sysmem )
882 ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
884 ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
888 ddsd.dwFlags |= DDSD_PIXELFORMAT;
889 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
890 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
893 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
896 if( dxresult != DD_OK )
898 *pp_surface_final = NULL;
903 /* Now that the surface is created, try to get a newer DirectX interface */
904 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
905 &IID_IDirectDrawSurface2,
906 (LPVOID *)pp_surface_final );
907 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
908 if ( dxresult != DD_OK )
910 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
911 "(error %i)", dxresult );
912 *pp_surface_final = NULL;
919 /*****************************************************************************
920 * DirectXUpdateOverlay: Move or resize overlay surface on video display.
921 *****************************************************************************
922 * This function is used to move or resize an overlay surface on the screen.
923 * Ususally the overlay is moved by the user and thus, by a move or resize
925 *****************************************************************************/
926 void DirectXUpdateOverlay( vout_thread_t *p_vout )
932 if( p_vout->p_sys->p_current_surface == NULL ||
933 !p_vout->p_sys->b_using_overlay )
936 /* The new window dimensions should already have been computed by the
937 * caller of this function */
939 /* Position and show the overlay */
940 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
941 ddofx.dwSize = sizeof(DDOVERLAYFX);
942 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
943 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
945 dwFlags = DDOVER_SHOW;
946 if( !p_vout->p_sys->b_caps_overlay_clipping )
947 dwFlags |= DDOVER_KEYDESTOVERRIDE;
949 dxresult = IDirectDrawSurface2_UpdateOverlay(
950 p_vout->p_sys->p_current_surface,
951 &p_vout->p_sys->rect_src_clipped,
952 p_vout->p_sys->p_display,
953 &p_vout->p_sys->rect_dest_clipped,
956 if(dxresult != DD_OK)
959 "DirectXUpdateOverlay cannot move or resize overlay" );
964 /*****************************************************************************
965 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
966 *****************************************************************************
967 * This function returns all resources allocated by DirectXInitDDraw.
968 *****************************************************************************/
969 static void DirectXCloseDDraw( vout_thread_t *p_vout )
971 msg_Dbg( p_vout, "DirectXCloseDDraw" );
972 if( p_vout->p_sys->p_ddobject != NULL )
974 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
975 p_vout->p_sys->p_ddobject = NULL;
978 if( p_vout->p_sys->hddraw_dll != NULL )
980 FreeLibrary( p_vout->p_sys->hddraw_dll );
981 p_vout->p_sys->hddraw_dll = NULL;
985 /*****************************************************************************
986 * DirectXCloseDisplay: close and reset the DirectX display device
987 *****************************************************************************
988 * This function returns all resources allocated by DirectXCreateDisplay.
989 *****************************************************************************/
990 static void DirectXCloseDisplay( vout_thread_t *p_vout )
992 msg_Dbg( p_vout, "DirectXCloseDisplay" );
994 if( p_vout->p_sys->p_clipper != NULL )
996 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
997 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
998 p_vout->p_sys->p_clipper = NULL;
1001 if( p_vout->p_sys->p_display != NULL )
1003 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1004 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1005 p_vout->p_sys->p_display = NULL;
1009 /*****************************************************************************
1010 * DirectXCloseSurface: close the YUV overlay or RGB surface.
1011 *****************************************************************************
1012 * This function returns all resources allocated for the surface.
1013 *****************************************************************************/
1014 static void DirectXCloseSurface( vout_thread_t *p_vout,
1015 LPDIRECTDRAWSURFACE2 p_surface )
1017 msg_Dbg( p_vout, "DirectXCloseSurface" );
1018 if( p_surface != NULL )
1020 IDirectDrawSurface2_Release( p_surface );
1024 /*****************************************************************************
1025 * NewPictureVec: allocate a vector of identical pictures
1026 *****************************************************************************
1027 * Returns 0 on success, -1 otherwise
1028 *****************************************************************************/
1029 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1033 int i_ret = VLC_SUCCESS;
1034 LPDIRECTDRAWSURFACE2 p_surface;
1036 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1037 p_vout->p_sys->b_using_overlay ? "yes" : "no",
1038 (char *)&p_vout->output.i_chroma );
1040 I_OUTPUTPICTURES = 0;
1042 /* First we try to use an YUV overlay surface.
1043 * The overlay surface that we create won't be used to decode directly
1044 * into it because accessing video memory directly is way to slow (remember
1045 * that pictures are decoded macroblock per macroblock). Instead the video
1046 * will be decoded in picture buffers in system memory which will then be
1047 * memcpy() to the overlay surface. */
1048 if( p_vout->p_sys->b_using_overlay )
1050 /* Triple buffering rocks! it doesn't have any processing overhead
1051 * (you don't have to wait for the vsync) and provides for a very nice
1052 * video quality (no tearing). */
1054 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1055 p_vout->output.i_chroma,
1056 p_vout->p_sys->b_using_overlay,
1057 2 /* number of backbuffers */ );
1059 if( i_ret != VLC_SUCCESS )
1061 /* Try to reduce the number of backbuffers */
1062 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1063 p_vout->output.i_chroma,
1064 p_vout->p_sys->b_using_overlay,
1065 0 /* number of backbuffers */ );
1068 if( i_ret == VLC_SUCCESS )
1071 picture_t front_pic;
1072 picture_sys_t front_pic_sys;
1073 front_pic.p_sys = &front_pic_sys;
1075 /* Allocate internal structure */
1076 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1077 if( p_pic[0].p_sys == NULL )
1079 DirectXCloseSurface( p_vout, p_surface );
1083 /* set front buffer */
1084 p_pic[0].p_sys->p_front_surface = p_surface;
1086 /* Get the back buffer */
1087 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1088 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1089 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1090 p_surface, &dds_caps,
1091 &p_pic[0].p_sys->p_surface ) )
1093 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1094 /* front buffer is the same as back buffer */
1095 p_pic[0].p_sys->p_surface = p_surface;
1099 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1100 p_pic[0].p_sys->p_front_surface;
1102 /* Reset the front buffer memory */
1103 if( !DirectXGetSurfaceDesc( p_vout, &front_pic ) &&
1104 !UpdatePictureStruct( p_vout, &front_pic,
1105 p_vout->output.i_chroma ) )
1108 for( i = 0; i < front_pic.i_planes; i++ )
1109 for( j = 0; j < front_pic.p[i].i_lines; j++)
1110 memset( front_pic.p[i].p_pixels + j *
1111 front_pic.p[i].i_pitch, 127,
1112 front_pic.p[i].i_visible_pitch );
1115 DirectXUpdateOverlay( p_vout );
1116 I_OUTPUTPICTURES = 1;
1117 msg_Dbg( p_vout, "YUV overlay created successfully" );
1121 /* As we can't have an overlay, we'll try to create a plain offscreen
1122 * surface. This surface will reside in video memory because there's a
1123 * better chance then that we'll be able to use some kind of hardware
1124 * acceleration like rescaling, blitting or YUV->RGB conversions.
1125 * We then only need to blit this surface onto the main display when we
1126 * want to display it */
1127 if( !p_vout->p_sys->b_using_overlay )
1129 if( p_vout->p_sys->b_hw_yuv )
1131 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1132 p_vout->output.i_chroma,
1134 0 /* no back buffers */ );
1137 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1139 /* Our last choice is to use a plain RGB surface */
1140 DDPIXELFORMAT ddpfPixelFormat;
1142 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1143 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1146 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1148 switch( ddpfPixelFormat.dwRGBBitCount )
1150 case 8: /* FIXME: set the palette */
1151 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
1154 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
1157 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
1160 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
1163 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
1166 msg_Err( p_vout, "unknown screen depth" );
1167 return VLC_EGENERIC;
1169 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1170 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1171 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1174 p_vout->p_sys->b_hw_yuv = 0;
1176 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1177 p_vout->output.i_chroma,
1179 0 /* no back buffers */ );
1181 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1183 /* Give it a last try with b_use_sysmem enabled */
1184 p_vout->p_sys->b_use_sysmem = 1;
1186 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1187 p_vout->output.i_chroma,
1189 0 /* no back buffers */ );
1193 if( i_ret == VLC_SUCCESS )
1195 /* Allocate internal structure */
1196 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1197 if( p_pic[0].p_sys == NULL )
1199 DirectXCloseSurface( p_vout, p_surface );
1202 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1205 I_OUTPUTPICTURES = 1;
1207 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1208 (char *)&p_vout->output.i_chroma );
1213 /* Now that we've got all our direct-buffers, we can finish filling in the
1214 * picture_t structures */
1215 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1217 p_pic[i].i_status = DESTROYED_PICTURE;
1218 p_pic[i].i_type = DIRECT_PICTURE;
1219 PP_OUTPUTPICTURE[i] = &p_pic[i];
1221 if( DirectXGetSurfaceDesc( p_vout, &p_pic[i] ) )
1224 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1225 I_OUTPUTPICTURES = 0;
1226 return VLC_EGENERIC;
1229 if( UpdatePictureStruct(p_vout, &p_pic[i], p_vout->output.i_chroma) )
1231 /* Unknown chroma, tell the guy to get lost */
1232 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1233 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1234 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1235 I_OUTPUTPICTURES = 0;
1236 return VLC_EGENERIC;
1240 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1241 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1246 /*****************************************************************************
1247 * FreePicture: destroy a picture vector allocated with NewPictureVec
1248 *****************************************************************************
1250 *****************************************************************************/
1251 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1256 for( i = 0; i < i_num_pics; i++ )
1258 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1260 for( i = 0; i < i_num_pics; i++ )
1262 free( p_pic[i].p_sys );
1267 /*****************************************************************************
1268 * UpdatePictureStruct: updates the internal data in the picture_t structure
1269 *****************************************************************************
1270 * This will setup stuff for use by the video_output thread
1271 *****************************************************************************/
1272 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1275 switch( p_vout->output.i_chroma )
1277 case VLC_FOURCC('R','G','B','2'):
1278 case VLC_FOURCC('R','V','1','5'):
1279 case VLC_FOURCC('R','V','1','6'):
1280 case VLC_FOURCC('R','V','2','4'):
1281 case VLC_FOURCC('R','V','3','2'):
1282 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1283 p_pic->p->i_lines = p_vout->output.i_height;
1284 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1285 switch( p_vout->output.i_chroma )
1287 case VLC_FOURCC('R','G','B','2'):
1288 p_pic->p->i_pixel_pitch = 1;
1290 case VLC_FOURCC('R','V','1','5'):
1291 case VLC_FOURCC('R','V','1','6'):
1292 p_pic->p->i_pixel_pitch = 2;
1294 case VLC_FOURCC('R','V','2','4'):
1295 case VLC_FOURCC('R','V','3','2'):
1296 p_pic->p->i_pixel_pitch = 4;
1299 return VLC_EGENERIC;
1301 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1302 p_pic->p->i_pixel_pitch;
1303 p_pic->i_planes = 1;
1306 case VLC_FOURCC('Y','V','1','2'):
1308 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1309 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1310 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1311 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1312 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1313 p_pic->p[Y_PLANE].i_pixel_pitch;
1315 p_pic->V_PIXELS = p_pic->Y_PIXELS
1316 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1317 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1318 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1319 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1320 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1321 p_pic->p[V_PLANE].i_pixel_pitch;
1323 p_pic->U_PIXELS = p_pic->V_PIXELS
1324 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1325 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1326 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1327 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1328 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1329 p_pic->p[U_PLANE].i_pixel_pitch;
1331 p_pic->i_planes = 3;
1334 case VLC_FOURCC('I','Y','U','V'):
1336 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1337 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1338 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1339 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1340 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1341 p_pic->p[Y_PLANE].i_pixel_pitch;
1343 p_pic->U_PIXELS = p_pic->Y_PIXELS
1344 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1345 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1346 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1347 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1348 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1349 p_pic->p[U_PLANE].i_pixel_pitch;
1351 p_pic->V_PIXELS = p_pic->U_PIXELS
1352 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1353 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1354 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1355 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1356 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1357 p_pic->p[V_PLANE].i_pixel_pitch;
1359 p_pic->i_planes = 3;
1362 case VLC_FOURCC('Y','U','Y','2'):
1364 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1365 p_pic->p->i_lines = p_vout->output.i_height;
1366 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1367 p_pic->p->i_pixel_pitch = 2;
1368 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1369 p_pic->p->i_pixel_pitch;
1371 p_pic->i_planes = 1;
1376 return VLC_EGENERIC;
1382 /*****************************************************************************
1383 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1384 *****************************************************************************
1385 * It is nice to know which features are supported by the hardware so we can
1386 * find ways to optimize our rendering.
1387 *****************************************************************************/
1388 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1393 /* This is just an indication of whether or not we'll support overlay,
1394 * but with this test we don't know if we support YUV overlay */
1395 memset( &ddcaps, 0, sizeof( DDCAPS ));
1396 ddcaps.dwSize = sizeof(DDCAPS);
1397 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1399 if(dxresult != DD_OK )
1401 msg_Warn( p_vout, "cannot get caps" );
1405 BOOL bHasOverlay, bHasOverlayFourCC, bCanClipOverlay,
1406 bHasColorKey, bCanStretch;
1408 /* Determine if the hardware supports overlay surfaces */
1409 bHasOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAY) ==
1410 DDCAPS_OVERLAY) ? TRUE : FALSE;
1411 /* Determine if the hardware supports overlay surfaces */
1412 bHasOverlayFourCC = ((ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ==
1413 DDCAPS_OVERLAYFOURCC) ? TRUE : FALSE;
1414 /* Determine if the hardware supports overlay surfaces */
1415 bCanClipOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAYCANTCLIP) ==
1417 /* Determine if the hardware supports colorkeying */
1418 bHasColorKey = ((ddcaps.dwCaps & DDCAPS_COLORKEY) ==
1419 DDCAPS_COLORKEY) ? TRUE : FALSE;
1420 /* Determine if the hardware supports scaling of the overlay surface */
1421 bCanStretch = ((ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ==
1422 DDCAPS_OVERLAYSTRETCH) ? TRUE : FALSE;
1423 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1424 "can_clip_overlay=%i colorkey=%i stretch=%i",
1425 bHasOverlay, bHasOverlayFourCC, bCanClipOverlay,
1426 bHasColorKey, bCanStretch );
1428 /* Overlay clipping support is interesting for us as it means we can
1429 * get rid of the colorkey alltogether */
1430 p_vout->p_sys->b_caps_overlay_clipping = bCanClipOverlay;
1435 /*****************************************************************************
1436 * DirectXGetSurfaceDesc: Get some more information about the surface
1437 *****************************************************************************
1438 * This function get and stores the surface descriptor which among things
1439 * has the pointer to the picture data.
1440 *****************************************************************************/
1441 static int DirectXGetSurfaceDesc( vout_thread_t *p_vout, picture_t *p_pic )
1445 /* Lock the surface to get a valid pointer to the picture buffer */
1446 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1447 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1448 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1449 NULL, &p_pic->p_sys->ddsd,
1450 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1452 if( dxresult != DD_OK )
1454 if( dxresult == DDERR_INVALIDPARAMS )
1456 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1457 * in an invalid params error */
1458 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1459 &p_pic->p_sys->ddsd,
1462 if( dxresult == DDERR_SURFACELOST )
1464 /* Your surface can be lost so be sure
1465 * to check this and restore it if needed */
1466 dxresult = IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1467 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1468 &p_pic->p_sys->ddsd,
1471 if( dxresult != DD_OK )
1473 msg_Err( p_vout, "DirectXGetSurfaceDesc cannot lock surface" );
1474 return VLC_EGENERIC;
1478 /* Unlock the Surface */
1479 dxresult = IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL );