1 /*****************************************************************************
2 * vout.c: Windows DirectX video output display method
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: directx.c,v 1.8 2002/11/26 19:31:50 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
27 * This plugin will use YUV overlay if supported, using overlay will result in
28 * the best video quality (hardware interpolation when rescaling the picture)
29 * and the fastest display as it requires less processing.
31 * If YUV overlay is not supported this plugin will use RGB offscreen video
32 * surfaces that will be blitted onto the primary surface (display) to
33 * effectively display the pictures. This fallback method also enables us to
34 * display video in window mode.
36 *****************************************************************************/
37 #include <errno.h> /* ENOMEM */
38 #include <stdlib.h> /* free() */
39 #include <string.h> /* strerror() */
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 int OpenVideo ( vlc_object_t * );
65 static void CloseVideo ( vlc_object_t * );
67 static int Init ( vout_thread_t * );
68 static void End ( vout_thread_t * );
69 static int Manage ( vout_thread_t * );
70 static void Display ( vout_thread_t *, picture_t * );
72 static int NewPictureVec ( vout_thread_t *, picture_t *, int );
73 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
74 static int UpdatePictureStruct( vout_thread_t *, picture_t *, int );
76 static int DirectXInitDDraw ( vout_thread_t *p_vout );
77 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
78 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
79 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
80 static int DirectXCreateSurface ( vout_thread_t *p_vout,
81 LPDIRECTDRAWSURFACE2 *, int, int, int );
82 static void DirectXCloseSurface ( vout_thread_t *p_vout,
83 LPDIRECTDRAWSURFACE2 );
84 static int DirectXCreateClipper ( vout_thread_t *p_vout );
85 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
86 static int DirectXGetSurfaceDesc ( vout_thread_t *p_vout, picture_t *p_pic );
88 /*****************************************************************************
90 *****************************************************************************/
91 #define HW_YUV_TEXT N_("use hardware YUV->RGB conversions")
92 #define HW_YUV_LONGTEXT N_( \
93 "Try to use hardware acceleration for YUV->RGB conversions. " \
94 "This option doesn't have any effect when using overlays." )
95 #define SYSMEM_TEXT N_("use video buffers in system memory")
96 #define SYSMEM_LONGTEXT N_( \
97 "Create video buffers in system memory instead of video memory. This " \
98 "isn't recommended as usually using video memory allows to benefit from " \
99 "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
100 "This option doesn't have any effect when using overlays." )
101 #define WINDOW_TEXT N_("specify an existing window")
102 #define WINDOW_LONGTEXT N_( \
103 "Specify a window to use instead of opening a new one. This option is " \
104 "DANGEROUS, use with care." )
107 add_category_hint( N_("Video"), NULL );
108 add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT );
109 add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT );
110 add_integer( "directx-window", 0, NULL, WINDOW_TEXT, WINDOW_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;
134 /* Allocate structure */
135 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
136 if( p_vout->p_sys == NULL )
138 msg_Err( p_vout, "out of memory" );
142 /* Initialisations */
143 p_vout->pf_init = Init;
144 p_vout->pf_end = End;
145 p_vout->pf_manage = Manage;
146 p_vout->pf_render = NULL;
147 p_vout->pf_display = Display;
149 p_vout->p_sys->p_ddobject = NULL;
150 p_vout->p_sys->p_display = NULL;
151 p_vout->p_sys->p_current_surface = NULL;
152 p_vout->p_sys->p_clipper = NULL;
153 p_vout->p_sys->hbrush = NULL;
154 p_vout->p_sys->hwnd = NULL;
155 p_vout->p_sys->hparent = NULL;
156 p_vout->p_sys->i_changes = 0;
157 p_vout->p_sys->b_caps_overlay_clipping = 0;
158 SetRectEmpty( &p_vout->p_sys->rect_display );
159 p_vout->p_sys->b_using_overlay = config_GetInt( p_vout, "overlay" );
160 p_vout->p_sys->b_use_sysmem = config_GetInt( p_vout, "directx-use-sysmem");
161 p_vout->p_sys->b_hw_yuv = config_GetInt( p_vout, "directx-hw-yuv" );
163 p_vout->p_sys->b_cursor_hidden = 0;
164 p_vout->p_sys->i_lastmoved = mdate();
166 /* Set main window's size */
167 p_vout->p_sys->i_window_width = p_vout->i_window_width;
168 p_vout->p_sys->i_window_height = p_vout->i_window_height;
170 /* Create the DirectXEventThread, this thread is created by us to isolate
171 * the Win32 PeekMessage function calls. We want to do this because
172 * Windows can stay blocked inside this call for a long time, and when
173 * this happens it thus blocks vlc's video_output thread.
174 * DirectXEventThread will take care of the creation of the video
175 * window (because PeekMessage has to be called from the same thread which
176 * created the window). */
177 msg_Dbg( p_vout, "creating DirectXEventThread" );
178 p_vout->p_sys->p_event =
179 vlc_object_create( p_vout, sizeof(event_thread_t) );
180 p_vout->p_sys->p_event->p_vout = p_vout;
181 if( vlc_thread_create( p_vout->p_sys->p_event,
182 "DirectX Events Thread", DirectXEventThread,
185 msg_Err( p_vout, "cannot create DirectXEventThread" );
186 vlc_object_destroy( p_vout->p_sys->p_event );
187 p_vout->p_sys->p_event = NULL;
191 if( p_vout->p_sys->p_event->b_error )
193 msg_Err( p_vout, "DirectXEventThread failed" );
197 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
199 msg_Dbg( p_vout, "DirectXEventThread running" );
201 /* Initialise DirectDraw */
202 if( DirectXInitDDraw( p_vout ) )
204 msg_Err( p_vout, "cannot initialize DirectDraw" );
208 /* Create the directx display */
209 if( DirectXCreateDisplay( p_vout ) )
211 msg_Err( p_vout, "cannot initialize DirectDraw" );
218 CloseVideo( VLC_OBJECT(p_vout) );
222 /*****************************************************************************
223 * Init: initialize DirectX video thread output method
224 *****************************************************************************
225 * This function create the directx surfaces needed by the output thread.
226 * It is called at the beginning of the thread.
227 *****************************************************************************/
228 static int Init( vout_thread_t *p_vout )
232 /* Initialize the output structure.
233 * Since DirectDraw can do rescaling for us, stick to the default
234 * coordinates and aspect. */
235 p_vout->output.i_width = p_vout->render.i_width;
236 p_vout->output.i_height = p_vout->render.i_height;
237 p_vout->output.i_aspect = p_vout->render.i_aspect;
239 #define MAX_DIRECTBUFFERS 1
240 /* Right now we use only 1 directbuffer because we don't want the
241 * video decoder to decode directly into direct buffers as they are
242 * created into video memory and video memory is _really_ slow */
244 /* Choose the chroma we will try first. */
245 switch( p_vout->render.i_chroma )
247 case VLC_FOURCC('Y','U','Y','2'):
248 case VLC_FOURCC('Y','U','N','V'):
249 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
251 case VLC_FOURCC('U','Y','V','Y'):
252 case VLC_FOURCC('U','Y','N','V'):
253 case VLC_FOURCC('Y','4','2','2'):
254 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
256 case VLC_FOURCC('Y','V','Y','U'):
257 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
260 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
264 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
266 i_chroma_backup = p_vout->output.i_chroma;
268 if( !I_OUTPUTPICTURES )
270 /* hmmm, it didn't work! Let's try commonly supported chromas */
271 if( p_vout->output.i_chroma != VLC_FOURCC('Y','V','1','2') )
273 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
274 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
276 if( !I_OUTPUTPICTURES )
278 /* hmmm, it still didn't work! Let's try another one */
279 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
280 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
284 if( !I_OUTPUTPICTURES )
286 /* If it still didn't work then don't try to use an overlay */
287 p_vout->output.i_chroma = i_chroma_backup;
288 p_vout->p_sys->b_using_overlay = 0;
289 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
292 /* Change the window title bar text */
293 if( p_vout->p_sys->hparent )
295 else if( p_vout->p_sys->b_using_overlay )
296 SetWindowText( p_vout->p_sys->hwnd,
297 VOUT_TITLE " (hardware YUV overlay DirectX output)" );
298 else if( p_vout->p_sys->b_hw_yuv )
299 SetWindowText( p_vout->p_sys->hwnd,
300 VOUT_TITLE " (hardware YUV DirectX output)" );
301 else SetWindowText( p_vout->p_sys->hwnd,
302 VOUT_TITLE " (software RGB DirectX output)" );
307 /*****************************************************************************
308 * End: terminate Sys video thread output method
309 *****************************************************************************
310 * Terminate an output method created by Create.
311 * It is called at the end of the thread.
312 *****************************************************************************/
313 static void End( vout_thread_t *p_vout )
315 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
319 /*****************************************************************************
320 * CloseVideo: destroy Sys video thread output method
321 *****************************************************************************
322 * Terminate an output method created by Create
323 *****************************************************************************/
324 static void CloseVideo( vlc_object_t *p_this )
326 vout_thread_t * p_vout = (vout_thread_t *)p_this;
328 msg_Dbg( p_vout, "CloseVideo" );
330 DirectXCloseDisplay( p_vout );
331 DirectXCloseDDraw( p_vout );
333 if( p_vout->p_sys->p_event )
335 vlc_object_detach( p_vout->p_sys->p_event );
337 /* Kill DirectXEventThread */
338 p_vout->p_sys->p_event->b_die = VLC_TRUE;
340 /* we need to be sure DirectXEventThread won't stay stuck in
341 * GetMessage, so we send a fake message */
342 if( p_vout->p_sys->hwnd )
344 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
347 vlc_thread_join( p_vout->p_sys->p_event );
348 vlc_object_destroy( p_vout->p_sys->p_event );
353 free( p_vout->p_sys );
354 p_vout->p_sys = NULL;
358 /*****************************************************************************
359 * Manage: handle Sys events
360 *****************************************************************************
361 * This function should be called regularly by the video output thread.
362 * It returns a non null value if an error occured.
363 *****************************************************************************/
364 static int Manage( vout_thread_t *p_vout )
366 WINDOWPLACEMENT window_placement;
368 /* If we do not control our window, we check for geometry changes
369 * ourselves because the parent might not send us its events. */
370 if( p_vout->p_sys->hparent )
372 DirectXUpdateRects( p_vout, VLC_FALSE );
375 /* We used to call the Win32 PeekMessage function here to read the window
376 * messages. But since window can stay blocked into this function for a
377 * long time (for example when you move your window on the screen), I
378 * decided to isolate PeekMessage in another thread. */
383 if( p_vout->i_changes & VOUT_SCALE_CHANGE
384 || p_vout->p_sys->i_changes & VOUT_SCALE_CHANGE )
386 msg_Dbg( p_vout, "scale change" );
387 if( !p_vout->p_sys->b_using_overlay )
388 InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
390 DirectXUpdateOverlay( p_vout );
391 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
392 p_vout->p_sys->i_changes &= ~VOUT_SCALE_CHANGE;
398 if( p_vout->i_changes & VOUT_SIZE_CHANGE
399 || p_vout->p_sys->i_changes & VOUT_SIZE_CHANGE )
401 msg_Dbg( p_vout, "size change" );
402 if( !p_vout->p_sys->b_using_overlay )
403 InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
405 DirectXUpdateOverlay( p_vout );
406 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
407 p_vout->p_sys->i_changes &= ~VOUT_SIZE_CHANGE;
413 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
414 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
416 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
418 /* We need to switch between Maximized and Normal sized window */
419 window_placement.length = sizeof(WINDOWPLACEMENT);
420 GetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
421 if( p_vout->b_fullscreen )
423 /* Maximized window */
424 window_placement.showCmd = SW_SHOWMAXIMIZED;
425 /* Change window style, no borders and no title bar */
426 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, 0 );
432 window_placement.showCmd = SW_SHOWNORMAL;
433 /* Change window style, borders and title bar */
434 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
435 WS_OVERLAPPEDWINDOW | WS_SIZEBOX | WS_VISIBLE );
438 SetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
440 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
441 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
447 if( (!p_vout->p_sys->b_cursor_hidden) &&
448 ( (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 ) )
450 /* Hide the mouse automatically */
451 if( p_vout->p_sys->hwnd != p_vout->p_sys->hparent )
453 p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
454 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
458 /* Check if the event thread is still running */
459 if( p_vout->p_sys->p_event->b_die )
461 return VLC_EGENERIC; /* exit */
467 /*****************************************************************************
468 * Display: displays previously rendered output
469 *****************************************************************************
470 * This function sends the currently rendered image to the display, wait until
471 * it is displayed and switch the two rendering buffers, preparing next frame.
472 *****************************************************************************/
473 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
477 if( (p_vout->p_sys->p_display == NULL) )
479 msg_Warn( p_vout, "no display!!" );
483 if( !p_vout->p_sys->b_using_overlay )
487 /* We ask for the "NOTEARING" option */
488 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
489 ddbltfx.dwSize = sizeof(DDBLTFX);
490 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
492 /* Blit video surface to display */
493 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
494 &p_vout->p_sys->rect_dest_clipped,
495 p_pic->p_sys->p_surface,
496 &p_vout->p_sys->rect_src_clipped,
497 DDBLT_ASYNC, &ddbltfx );
498 if ( dxresult == DDERR_SURFACELOST )
500 /* Our surface can be lost so be sure
501 * to check this and restore it if needed */
502 IDirectDrawSurface2_Restore( p_vout->p_sys->p_display );
504 /* Now that the surface has been restored try to display again */
505 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
506 &p_vout->p_sys->rect_dest_clipped,
507 p_pic->p_sys->p_surface,
508 &p_vout->p_sys->rect_src_clipped,
509 DDBLT_ASYNC, &ddbltfx );
512 if( dxresult != DD_OK )
514 msg_Warn( p_vout, "could not blit surface (error %i)", dxresult );
519 else /* using overlay */
521 /* Flip the overlay buffers if we are using back buffers */
522 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
527 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
529 if ( dxresult == DDERR_SURFACELOST )
531 /* Our surface can be lost so be sure
532 * to check this and restore it if needed */
533 IDirectDrawSurface2_Restore( p_vout->p_sys->p_display );
534 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
536 /* Now that the surface has been restored try to display again */
537 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
539 DirectXUpdateOverlay( p_vout );
542 if( dxresult != DD_OK )
544 msg_Warn( p_vout, "could not flip overlay (error %i)", dxresult );
547 if( DirectXGetSurfaceDesc( p_vout, p_pic ) )
550 msg_Err( p_vout, "cannot get surface desc" );
554 if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma ) )
557 msg_Err( p_vout, "invalid pic chroma" );
561 /* set currently displayed pic */
562 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
567 /* following functions are local */
569 /*****************************************************************************
570 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
571 *****************************************************************************
572 * This function initialise and allocate resources for DirectDraw.
573 *****************************************************************************/
574 static int DirectXInitDDraw( vout_thread_t *p_vout )
577 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
578 LPDIRECTDRAW p_ddobject;
580 msg_Dbg( p_vout, "DirectXInitDDraw" );
582 /* load direct draw DLL */
583 p_vout->p_sys->hddraw_dll = LoadLibrary("DDRAW.DLL");
584 if( p_vout->p_sys->hddraw_dll == NULL )
586 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
590 OurDirectDrawCreate =
591 (void *)GetProcAddress(p_vout->p_sys->hddraw_dll, "DirectDrawCreate");
592 if ( OurDirectDrawCreate == NULL )
594 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
598 /* Initialize DirectDraw now */
599 dxresult = OurDirectDrawCreate( NULL, &p_ddobject, NULL );
600 if( dxresult != DD_OK )
602 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
606 /* Get the IDirectDraw2 interface */
607 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
608 (LPVOID *)&p_vout->p_sys->p_ddobject );
609 /* Release the unused interface */
610 IDirectDraw_Release( p_ddobject );
611 if( dxresult != DD_OK )
613 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
617 /* Set DirectDraw Cooperative level, ie what control we want over Windows
619 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
620 p_vout->p_sys->hwnd, DDSCL_NORMAL );
621 if( dxresult != DD_OK )
623 msg_Err( p_vout, "cannot set direct draw cooperative level" );
627 /* Probe the capabilities of the hardware */
628 DirectXGetDDrawCaps( p_vout );
630 msg_Dbg( p_vout, "End DirectXInitDDraw" );
634 if( p_vout->p_sys->p_ddobject )
635 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
636 if( p_vout->p_sys->hddraw_dll )
637 FreeLibrary( p_vout->p_sys->hddraw_dll );
638 p_vout->p_sys->hddraw_dll = NULL;
639 p_vout->p_sys->p_ddobject = NULL;
643 /*****************************************************************************
644 * DirectXCreateDisplay: create the DirectDraw display.
645 *****************************************************************************
646 * Create and initialize display according to preferences specified in the vout
648 *****************************************************************************/
649 static int DirectXCreateDisplay( vout_thread_t *p_vout )
653 LPDIRECTDRAWSURFACE p_display;
654 DDPIXELFORMAT pixel_format;
656 msg_Dbg( p_vout, "DirectXCreateDisplay" );
658 /* Now get the primary surface. This surface is what you actually see
660 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
661 ddsd.dwSize = sizeof(DDSURFACEDESC);
662 ddsd.dwFlags = DDSD_CAPS;
663 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
665 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
668 if( dxresult != DD_OK )
670 msg_Err( p_vout, "cannot get primary surface (error %i)", dxresult );
674 dxresult = IDirectDrawSurface_QueryInterface( p_display,
675 &IID_IDirectDrawSurface2,
676 (LPVOID *)&p_vout->p_sys->p_display );
677 /* Release the old interface */
678 IDirectDrawSurface_Release( p_display );
679 if ( dxresult != DD_OK )
681 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
682 "(error %i)", dxresult );
686 /* The clipper will be used only in non-overlay mode */
687 DirectXCreateClipper( p_vout );
691 /* compute the colorkey pixel value from the RGB value we've got */
692 memset( &pixel_format, 0, sizeof( DDPIXELFORMAT ));
693 pixel_format.dwSize = sizeof( DDPIXELFORMAT );
694 dxresult = IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
696 if( dxresult != DD_OK )
698 msg_Warn( p_vout, "DirectXUpdateOverlay GetPixelFormat failed "
699 "(error %i)", dxresult );
701 p_vout->p_sys->i_colorkey = (DWORD)((( p_vout->p_sys->i_rgb_colorkey
702 * pixel_format.dwRBitMask) / 255)
703 & pixel_format.dwRBitMask );
710 /*****************************************************************************
711 * DirectXCreateClipper: Create a clipper that will be used when blitting the
712 * RGB surface to the main display.
713 *****************************************************************************
714 * This clipper prevents us to modify by mistake anything on the screen
715 * which doesn't belong to our window. For example when a part of our video
716 * window is hidden by another window.
717 *****************************************************************************/
718 static int DirectXCreateClipper( vout_thread_t *p_vout )
722 msg_Dbg( p_vout, "DirectXCreateClipper" );
724 /* Create the clipper */
725 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
726 &p_vout->p_sys->p_clipper, NULL );
727 if( dxresult != DD_OK )
729 msg_Warn( p_vout, "cannot create clipper (error %i)", dxresult );
733 /* Associate the clipper to the window */
734 dxresult = IDirectDrawClipper_SetHWnd(p_vout->p_sys->p_clipper, 0,
735 p_vout->p_sys->hwnd);
736 if( dxresult != DD_OK )
738 msg_Warn( p_vout, "cannot attach clipper to window (error %i)",
743 /* associate the clipper with the surface */
744 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
745 p_vout->p_sys->p_clipper);
746 if( dxresult != DD_OK )
748 msg_Warn( p_vout, "cannot attach clipper to surface (error %i)",
756 if( p_vout->p_sys->p_clipper )
758 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
760 p_vout->p_sys->p_clipper = NULL;
764 /*****************************************************************************
765 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
766 *****************************************************************************
767 * The best method of display is with an YUV overlay because the YUV->RGB
768 * conversion is done in hardware.
769 * You can also create a plain RGB surface.
770 * ( Maybe we could also try an RGB overlay surface, which could have hardware
771 * scaling and which would also be faster in window mode because you don't
772 * need to do any blitting to the main display...)
773 *****************************************************************************/
774 static int DirectXCreateSurface( vout_thread_t *p_vout,
775 LPDIRECTDRAWSURFACE2 *pp_surface_final,
776 int i_chroma, int b_overlay,
780 LPDIRECTDRAWSURFACE p_surface;
783 /* Create the video surface */
786 /* Now try to create the YUV overlay surface.
787 * This overlay will be displayed on top of the primary surface.
788 * A color key is used to determine whether or not the overlay will be
789 * displayed, ie the overlay will be displayed in place of the primary
790 * surface wherever the primary surface will have this color.
791 * The video window has been created with a background of this color so
792 * the overlay will be only displayed on top of this window */
794 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
795 ddsd.dwSize = sizeof(DDSURFACEDESC);
796 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
797 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
798 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
799 ddsd.dwFlags = DDSD_CAPS |
803 ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
804 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY |
806 ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
808 ddsd.dwHeight = p_vout->render.i_height;
809 ddsd.dwWidth = p_vout->render.i_width;
810 ddsd.dwBackBufferCount = i_backbuffers;
812 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
815 if( dxresult != DD_OK )
817 *pp_surface_final = NULL;
824 vlc_bool_t b_rgb_surface =
825 ( i_chroma == VLC_FOURCC('R','G','B','2') )
826 || ( i_chroma == VLC_FOURCC('R','V','1','5') )
827 || ( i_chroma == VLC_FOURCC('R','V','1','6') )
828 || ( i_chroma == VLC_FOURCC('R','V','2','4') )
829 || ( i_chroma == VLC_FOURCC('R','V','3','2') );
831 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
832 ddsd.dwSize = sizeof(DDSURFACEDESC);
833 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
834 ddsd.dwFlags = DDSD_HEIGHT |
837 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
838 ddsd.dwHeight = p_vout->render.i_height;
839 ddsd.dwWidth = p_vout->render.i_width;
841 if( p_vout->p_sys->b_use_sysmem )
842 ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
844 ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
848 ddsd.dwFlags |= DDSD_PIXELFORMAT;
849 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
850 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
853 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
856 if( dxresult != DD_OK )
858 *pp_surface_final = NULL;
863 /* Now that the surface is created, try to get a newer DirectX interface */
864 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
865 &IID_IDirectDrawSurface2,
866 (LPVOID *)pp_surface_final );
867 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
868 if ( dxresult != DD_OK )
870 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
871 "(error %i)", dxresult );
872 *pp_surface_final = NULL;
879 /*****************************************************************************
880 * DirectXUpdateOverlay: Move or resize overlay surface on video display.
881 *****************************************************************************
882 * This function is used to move or resize an overlay surface on the screen.
883 * Ususally the overlay is moved by the user and thus, by a move or resize
885 *****************************************************************************/
886 void DirectXUpdateOverlay( vout_thread_t *p_vout )
892 if( p_vout->p_sys->p_current_surface == NULL ||
893 !p_vout->p_sys->b_using_overlay )
896 /* The new window dimensions should already have been computed by the
897 * caller of this function */
899 /* Position and show the overlay */
900 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
901 ddofx.dwSize = sizeof(DDOVERLAYFX);
902 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
903 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
905 dwFlags = DDOVER_SHOW;
906 if( !p_vout->p_sys->b_caps_overlay_clipping )
907 dwFlags |= DDOVER_KEYDESTOVERRIDE;
909 dxresult = IDirectDrawSurface2_UpdateOverlay(
910 p_vout->p_sys->p_current_surface,
911 &p_vout->p_sys->rect_src_clipped,
912 p_vout->p_sys->p_display,
913 &p_vout->p_sys->rect_dest_clipped,
916 if(dxresult != DD_OK)
919 "DirectXUpdateOverlay cannot move or resize overlay" );
924 /*****************************************************************************
925 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
926 *****************************************************************************
927 * This function returns all resources allocated by DirectXInitDDraw.
928 *****************************************************************************/
929 static void DirectXCloseDDraw( vout_thread_t *p_vout )
931 msg_Dbg( p_vout, "DirectXCloseDDraw" );
932 if( p_vout->p_sys->p_ddobject != NULL )
934 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
935 p_vout->p_sys->p_ddobject = NULL;
938 if( p_vout->p_sys->hddraw_dll != NULL )
940 FreeLibrary( p_vout->p_sys->hddraw_dll );
941 p_vout->p_sys->hddraw_dll = NULL;
945 /*****************************************************************************
946 * DirectXCloseDisplay: close and reset the DirectX display device
947 *****************************************************************************
948 * This function returns all resources allocated by DirectXCreateDisplay.
949 *****************************************************************************/
950 static void DirectXCloseDisplay( vout_thread_t *p_vout )
952 msg_Dbg( p_vout, "DirectXCloseDisplay" );
954 if( p_vout->p_sys->p_clipper != NULL )
956 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
957 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
958 p_vout->p_sys->p_clipper = NULL;
961 if( p_vout->p_sys->p_display != NULL )
963 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
964 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
965 p_vout->p_sys->p_display = NULL;
969 /*****************************************************************************
970 * DirectXCloseSurface: close the YUV overlay or RGB surface.
971 *****************************************************************************
972 * This function returns all resources allocated for the surface.
973 *****************************************************************************/
974 static void DirectXCloseSurface( vout_thread_t *p_vout,
975 LPDIRECTDRAWSURFACE2 p_surface )
977 msg_Dbg( p_vout, "DirectXCloseSurface" );
978 if( p_surface != NULL )
980 IDirectDrawSurface2_Release( p_surface );
984 /*****************************************************************************
985 * NewPictureVec: allocate a vector of identical pictures
986 *****************************************************************************
987 * Returns 0 on success, -1 otherwise
988 *****************************************************************************/
989 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
993 int i_ret = VLC_SUCCESS;
994 LPDIRECTDRAWSURFACE2 p_surface;
996 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
997 p_vout->p_sys->b_using_overlay ? "yes" : "no",
998 (char *)&p_vout->output.i_chroma );
1000 I_OUTPUTPICTURES = 0;
1002 /* First we try to use an YUV overlay surface.
1003 * The overlay surface that we create won't be used to decode directly
1004 * into it because accessing video memory directly is way to slow (remember
1005 * that pictures are decoded macroblock per macroblock). Instead the video
1006 * will be decoded in picture buffers in system memory which will then be
1007 * memcpy() to the overlay surface. */
1008 if( p_vout->p_sys->b_using_overlay )
1010 /* Triple buffering rocks! it doesn't have any processing overhead
1011 * (you don't have to wait for the vsync) and provides for a very nice
1012 * video quality (no tearing). */
1014 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1015 p_vout->output.i_chroma,
1016 p_vout->p_sys->b_using_overlay,
1017 2 /* number of backbuffers */ );
1019 if( i_ret != VLC_SUCCESS )
1021 /* Try to reduce the number of backbuffers */
1022 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1023 p_vout->output.i_chroma,
1024 p_vout->p_sys->b_using_overlay,
1025 0 /* number of backbuffers */ );
1028 if( i_ret == VLC_SUCCESS )
1031 picture_t front_pic;
1032 picture_sys_t front_pic_sys;
1033 front_pic.p_sys = &front_pic_sys;
1035 /* Allocate internal structure */
1036 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1037 if( p_pic[0].p_sys == NULL )
1039 DirectXCloseSurface( p_vout, p_surface );
1043 /* set front buffer */
1044 p_pic[0].p_sys->p_front_surface = p_surface;
1046 /* Get the back buffer */
1047 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1048 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1049 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1050 p_surface, &dds_caps,
1051 &p_pic[0].p_sys->p_surface ) )
1053 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1054 /* front buffer is the same as back buffer */
1055 p_pic[0].p_sys->p_surface = p_surface;
1059 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1060 p_pic[0].p_sys->p_front_surface;
1062 /* Reset the front buffer memory */
1063 if( !DirectXGetSurfaceDesc( p_vout, &front_pic ) &&
1064 !UpdatePictureStruct( p_vout, &front_pic,
1065 p_vout->output.i_chroma ) )
1068 for( i = 0; i < front_pic.i_planes; i++ )
1069 for( j = 0; j < front_pic.p[i].i_lines; j++)
1070 memset( front_pic.p[i].p_pixels + j *
1071 front_pic.p[i].i_pitch, 127,
1072 front_pic.p[i].i_visible_pitch );
1075 DirectXUpdateOverlay( p_vout );
1076 I_OUTPUTPICTURES = 1;
1077 msg_Dbg( p_vout, "YUV overlay created successfully" );
1081 /* As we can't have an overlay, we'll try to create a plain offscreen
1082 * surface. This surface will reside in video memory because there's a
1083 * better chance then that we'll be able to use some kind of hardware
1084 * acceleration like rescaling, blitting or YUV->RGB conversions.
1085 * We then only need to blit this surface onto the main display when we
1086 * want to display it */
1087 if( !p_vout->p_sys->b_using_overlay )
1089 if( p_vout->p_sys->b_hw_yuv )
1091 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1092 p_vout->output.i_chroma,
1094 0 /* no back buffers */ );
1097 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1099 /* Our last choice is to use a plain RGB surface */
1100 DDPIXELFORMAT ddpfPixelFormat;
1102 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1103 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1106 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1108 switch( ddpfPixelFormat.dwRGBBitCount )
1110 case 8: /* FIXME: set the palette */
1111 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
1114 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
1117 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
1120 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
1123 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
1126 msg_Err( p_vout, "unknown screen depth" );
1127 return VLC_EGENERIC;
1129 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1130 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1131 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1134 p_vout->p_sys->b_hw_yuv = 0;
1136 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1137 p_vout->output.i_chroma,
1139 0 /* no back buffers */ );
1141 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1143 /* Give it a last try with b_use_sysmem enabled */
1144 p_vout->p_sys->b_use_sysmem = 1;
1146 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1147 p_vout->output.i_chroma,
1149 0 /* no back buffers */ );
1153 if( i_ret == VLC_SUCCESS )
1155 /* Allocate internal structure */
1156 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1157 if( p_pic[0].p_sys == NULL )
1159 DirectXCloseSurface( p_vout, p_surface );
1162 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1165 I_OUTPUTPICTURES = 1;
1167 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1168 (char *)&p_vout->output.i_chroma );
1173 /* Now that we've got all our direct-buffers, we can finish filling in the
1174 * picture_t structures */
1175 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1177 p_pic[i].i_status = DESTROYED_PICTURE;
1178 p_pic[i].i_type = DIRECT_PICTURE;
1179 PP_OUTPUTPICTURE[i] = &p_pic[i];
1181 if( DirectXGetSurfaceDesc( p_vout, &p_pic[i] ) )
1184 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1185 I_OUTPUTPICTURES = 0;
1186 return VLC_EGENERIC;
1189 if( UpdatePictureStruct(p_vout, &p_pic[i], p_vout->output.i_chroma) )
1191 /* Unknown chroma, tell the guy to get lost */
1192 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1193 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
1194 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1195 I_OUTPUTPICTURES = 0;
1196 return VLC_EGENERIC;
1200 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1201 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1206 /*****************************************************************************
1207 * FreePicture: destroy a picture vector allocated with NewPictureVec
1208 *****************************************************************************
1210 *****************************************************************************/
1211 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1216 for( i = 0; i < i_num_pics; i++ )
1218 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1220 for( i = 0; i < i_num_pics; i++ )
1222 free( p_pic[i].p_sys );
1227 /*****************************************************************************
1228 * UpdatePictureStruct: updates the internal data in the picture_t structure
1229 *****************************************************************************
1230 * This will setup stuff for use by the video_output thread
1231 *****************************************************************************/
1232 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1235 switch( p_vout->output.i_chroma )
1237 case VLC_FOURCC('R','G','B','2'):
1238 case VLC_FOURCC('R','V','1','5'):
1239 case VLC_FOURCC('R','V','1','6'):
1240 case VLC_FOURCC('R','V','2','4'):
1241 case VLC_FOURCC('R','V','3','2'):
1242 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1243 p_pic->p->i_lines = p_vout->output.i_height;
1244 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1245 switch( p_vout->output.i_chroma )
1247 case VLC_FOURCC('R','G','B','2'):
1248 p_pic->p->i_pixel_pitch = 1;
1250 case VLC_FOURCC('R','V','1','5'):
1251 case VLC_FOURCC('R','V','1','6'):
1252 p_pic->p->i_pixel_pitch = 2;
1254 case VLC_FOURCC('R','V','2','4'):
1255 case VLC_FOURCC('R','V','3','2'):
1256 p_pic->p->i_pixel_pitch = 4;
1259 return VLC_EGENERIC;
1261 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1262 p_pic->p->i_pixel_pitch;
1263 p_pic->i_planes = 1;
1266 case VLC_FOURCC('Y','V','1','2'):
1268 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1269 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1270 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1271 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1272 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1273 p_pic->p[Y_PLANE].i_pixel_pitch;
1275 p_pic->V_PIXELS = p_pic->Y_PIXELS
1276 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1277 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1278 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1279 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1280 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1281 p_pic->p[V_PLANE].i_pixel_pitch;
1283 p_pic->U_PIXELS = p_pic->V_PIXELS
1284 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1285 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1286 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1287 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1288 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1289 p_pic->p[U_PLANE].i_pixel_pitch;
1291 p_pic->i_planes = 3;
1294 case VLC_FOURCC('I','Y','U','V'):
1296 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1297 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1298 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1299 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1300 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1301 p_pic->p[Y_PLANE].i_pixel_pitch;
1303 p_pic->U_PIXELS = p_pic->Y_PIXELS
1304 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1305 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1306 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1307 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1308 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1309 p_pic->p[U_PLANE].i_pixel_pitch;
1311 p_pic->V_PIXELS = p_pic->U_PIXELS
1312 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1313 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1314 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1315 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1316 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1317 p_pic->p[V_PLANE].i_pixel_pitch;
1319 p_pic->i_planes = 3;
1322 case VLC_FOURCC('Y','U','Y','2'):
1324 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1325 p_pic->p->i_lines = p_vout->output.i_height;
1326 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1327 p_pic->p->i_pixel_pitch = 2;
1328 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1329 p_pic->p->i_pixel_pitch;
1331 p_pic->i_planes = 1;
1336 return VLC_EGENERIC;
1342 /*****************************************************************************
1343 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1344 *****************************************************************************
1345 * It is nice to know which features are supported by the hardware so we can
1346 * find ways to optimize our rendering.
1347 *****************************************************************************/
1348 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1353 /* This is just an indication of whether or not we'll support overlay,
1354 * but with this test we don't know if we support YUV overlay */
1355 memset( &ddcaps, 0, sizeof( DDCAPS ));
1356 ddcaps.dwSize = sizeof(DDCAPS);
1357 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1359 if(dxresult != DD_OK )
1361 msg_Warn( p_vout, "cannot get caps" );
1365 BOOL bHasOverlay, bHasOverlayFourCC, bCanClipOverlay,
1366 bHasColorKey, bCanStretch;
1368 /* Determine if the hardware supports overlay surfaces */
1369 bHasOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAY) ==
1370 DDCAPS_OVERLAY) ? TRUE : FALSE;
1371 /* Determine if the hardware supports overlay surfaces */
1372 bHasOverlayFourCC = ((ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ==
1373 DDCAPS_OVERLAYFOURCC) ? TRUE : FALSE;
1374 /* Determine if the hardware supports overlay surfaces */
1375 bCanClipOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAYCANTCLIP) ==
1377 /* Determine if the hardware supports colorkeying */
1378 bHasColorKey = ((ddcaps.dwCaps & DDCAPS_COLORKEY) ==
1379 DDCAPS_COLORKEY) ? TRUE : FALSE;
1380 /* Determine if the hardware supports scaling of the overlay surface */
1381 bCanStretch = ((ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ==
1382 DDCAPS_OVERLAYSTRETCH) ? TRUE : FALSE;
1383 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1384 "can_clip_overlay=%i colorkey=%i stretch=%i",
1385 bHasOverlay, bHasOverlayFourCC, bCanClipOverlay,
1386 bHasColorKey, bCanStretch );
1388 /* Overlay clipping support is interesting for us as it means we can
1389 * get rid of the colorkey alltogether */
1390 p_vout->p_sys->b_caps_overlay_clipping = bCanClipOverlay;
1395 /*****************************************************************************
1396 * DirectXGetSurfaceDesc: Get some more information about the surface
1397 *****************************************************************************
1398 * This function get and stores the surface descriptor which among things
1399 * has the pointer to the picture data.
1400 *****************************************************************************/
1401 static int DirectXGetSurfaceDesc( vout_thread_t *p_vout, picture_t *p_pic )
1405 /* Lock the surface to get a valid pointer to the picture buffer */
1406 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1407 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1408 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1409 NULL, &p_pic->p_sys->ddsd,
1410 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1412 if( dxresult != DD_OK )
1414 if( dxresult == DDERR_INVALIDPARAMS )
1416 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1417 * in an invalid params error */
1418 dxresult = IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1419 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1420 &p_pic->p_sys->ddsd,
1423 if( dxresult == DDERR_SURFACELOST )
1425 /* Your surface can be lost so be sure
1426 * to check this and restore it if needed */
1427 dxresult = IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1428 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1429 &p_pic->p_sys->ddsd,
1432 if( dxresult != DD_OK )
1434 msg_Err( p_vout, "DirectXGetSurfaceDesc cannot lock surface" );
1435 return VLC_EGENERIC;
1439 /* Unlock the Surface */
1440 dxresult = IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL );