1 /*****************************************************************************
2 * vout_directx.c: Windows DirectX video output display method
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: vout_directx.c,v 1.29 2002/04/01 16:08:23 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 enables us to display
34 * video in window mode.
35 * Another fallback method (which isn't implemented) would be take the
36 * exclusive control of the screen so we could spare the blitting process and
37 * decode directly to video memory. This should theoretically allow for better
38 * performance (although on my system it is actually slower) but this is
39 * restricted to fullscreen video.
41 *****************************************************************************/
42 #include <errno.h> /* ENOMEM */
43 #include <stdlib.h> /* free() */
44 #include <string.h> /* strerror() */
46 #include <videolan/vlc.h>
53 #include "video_output.h"
55 #include "interface.h"
57 #include "vout_directx.h"
59 /*****************************************************************************
61 * Defining them here allows us to get rid of the dxguid library during
63 *****************************************************************************/
65 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
66 DEFINE_GUID( IID_IDirectDrawSurface3, 0xDA044E00,0x69B2,0x11D0,0xA1,0xD5,0x00,0xAA,0x00,0xB8,0xDF,0xBB );
68 /*****************************************************************************
70 *****************************************************************************/
71 static int vout_Create ( vout_thread_t * );
72 static void vout_Destroy ( vout_thread_t * );
73 static int vout_Init ( vout_thread_t * );
74 static void vout_End ( vout_thread_t * );
75 static int vout_Manage ( vout_thread_t * );
76 static void vout_Render ( vout_thread_t *, picture_t * );
77 static void vout_Display ( vout_thread_t *, picture_t * );
79 static int NewPictureVec ( vout_thread_t *, picture_t *, int );
80 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
81 static int UpdatePictureStruct( vout_thread_t *, picture_t *, int );
83 static int DirectXInitDDraw ( vout_thread_t *p_vout );
84 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
85 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
86 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
87 static int DirectXCreateSurface ( vout_thread_t *p_vout,
88 LPDIRECTDRAWSURFACE3 *, int, int );
89 static void DirectXCloseSurface ( vout_thread_t *p_vout,
90 LPDIRECTDRAWSURFACE3 );
91 static int DirectXCreateClipper ( vout_thread_t *p_vout );
92 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
93 static int DirectXGetSurfaceDesc ( picture_t *p_pic );
95 /*****************************************************************************
96 * Functions exported as capabilities. They are declared as static so that
97 * we don't pollute the namespace too much.
98 *****************************************************************************/
99 void _M( vout_getfunctions )( function_list_t * p_function_list )
101 p_function_list->functions.vout.pf_create = vout_Create;
102 p_function_list->functions.vout.pf_init = vout_Init;
103 p_function_list->functions.vout.pf_end = vout_End;
104 p_function_list->functions.vout.pf_destroy = vout_Destroy;
105 p_function_list->functions.vout.pf_manage = vout_Manage;
106 p_function_list->functions.vout.pf_render = vout_Render;
107 p_function_list->functions.vout.pf_display = vout_Display;
110 /*****************************************************************************
111 * vout_Create: allocate DirectX video thread output method
112 *****************************************************************************
113 * This function allocates and initialize the DirectX vout method.
114 *****************************************************************************/
115 static int vout_Create( vout_thread_t *p_vout )
117 /* Allocate structure */
118 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
119 if( p_vout->p_sys == NULL )
121 intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
125 /* Initialisations */
126 p_vout->p_sys->p_ddobject = NULL;
127 p_vout->p_sys->p_display = NULL;
128 p_vout->p_sys->p_current_surface = NULL;
129 p_vout->p_sys->p_clipper = NULL;
130 p_vout->p_sys->hbrush = NULL;
131 p_vout->p_sys->hwnd = NULL;
132 p_vout->p_sys->i_changes = 0;
133 p_vout->p_sys->b_event_thread_die = 0;
134 p_vout->p_sys->b_caps_overlay_clipping = 0;
135 SetRectEmpty( &p_vout->p_sys->rect_display );
136 p_vout->p_sys->b_using_overlay = !config_GetIntVariable( "nooverlay" );
138 p_vout->p_sys->b_cursor = 1;
140 p_vout->p_sys->b_cursor_autohidden = 0;
141 p_vout->p_sys->i_lastmoved = mdate();
143 /* Set main window's size */
144 p_vout->p_sys->i_window_width = p_vout->i_window_width;
145 p_vout->p_sys->i_window_height = p_vout->i_window_height;
147 /* Set locks and condition variables */
148 vlc_mutex_init( &p_vout->p_sys->event_thread_lock );
149 vlc_cond_init( &p_vout->p_sys->event_thread_wait );
150 p_vout->p_sys->i_event_thread_status = THREAD_CREATE;
152 /* Create the DirectXEventThread, this thread is created by us to isolate
153 * the Win32 PeekMessage function calls. We want to do this because
154 * Windows can stay blocked inside this call for a long time, and when
155 * this happens it thus blocks vlc's video_output thread.
156 * DirectXEventThread will take care of the creation of the video
157 * window (because PeekMessage has to be called from the same thread which
158 * created the window). */
159 intf_WarnMsg( 3, "vout: vout_Create creating DirectXEventThread" );
160 if( vlc_thread_create( &p_vout->p_sys->event_thread_id,
161 "DirectX Events Thread",
162 (void *) DirectXEventThread, (void *) p_vout) )
164 intf_ErrMsg( "vout error: can't create DirectXEventThread" );
165 intf_ErrMsg("vout error: %s", strerror(ENOMEM));
166 free( p_vout->p_sys );
170 /* We need to wait for the actual creation of the thread and window */
171 vlc_mutex_lock( &p_vout->p_sys->event_thread_lock );
172 if( p_vout->p_sys->i_event_thread_status == THREAD_CREATE )
174 vlc_cond_wait ( &p_vout->p_sys->event_thread_wait,
175 &p_vout->p_sys->event_thread_lock );
177 vlc_mutex_unlock( &p_vout->p_sys->event_thread_lock );
178 if( p_vout->p_sys->i_event_thread_status != THREAD_READY )
180 intf_ErrMsg( "vout error: DirectXEventThread failed" );
181 free( p_vout->p_sys );
185 intf_WarnMsg( 3, "vout: vout_Create DirectXEventThread running" );
188 /* Initialise DirectDraw */
189 if( DirectXInitDDraw( p_vout ) )
191 intf_ErrMsg( "vout error: can't initialise DirectDraw" );
193 /* Kill DirectXEventThread */
194 p_vout->p_sys->b_event_thread_die = 1;
195 /* we need to be sure DirectXEventThread won't stay stuck in
196 * GetMessage, so we send a fake message */
197 PostMessage( p_vout->p_sys->hwnd, WM_CHAR, (WPARAM)'^', 0);
198 vlc_thread_join( p_vout->p_sys->event_thread_id );
203 /* Create the directx display */
204 if( DirectXCreateDisplay( p_vout ) )
206 intf_ErrMsg( "vout error: can't initialise DirectDraw" );
207 DirectXCloseDDraw( p_vout );
209 /* Kill DirectXEventThread */
210 p_vout->p_sys->b_event_thread_die = 1;
211 /* we need to be sure DirectXEventThread won't stay stuck in
212 * GetMessage, so we send a fake message */
213 PostMessage( p_vout->p_sys->hwnd, WM_CHAR, (WPARAM)'^', 0);
214 vlc_thread_join( p_vout->p_sys->event_thread_id );
219 /* Attach the current thread input queue to the events thread qeue.
220 * This allows us to hide or show the cursor in vout_Manage() */
221 ShowCursor( TRUE ); ShowCursor( FALSE ); /* create input queue */
222 AttachThreadInput( GetCurrentThreadId(),
223 GetWindowThreadProcessId( p_vout->p_sys->hwnd, NULL ),
229 /*****************************************************************************
230 * vout_Init: initialize DirectX video thread output method
231 *****************************************************************************
232 * This function create the directx surfaces needed by the output thread.
233 * It is called at the beginning of the thread.
234 *****************************************************************************/
235 static int vout_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
247 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
249 /* Change the window title bar text */
250 if( p_vout->p_sys->b_using_overlay )
251 SetWindowText( p_vout->p_sys->hwnd,
252 "VLC DirectX (using hardware overlay)" );
257 /*****************************************************************************
258 * vout_End: terminate Sys video thread output method
259 *****************************************************************************
260 * Terminate an output method created by vout_Create.
261 * It is called at the end of the thread.
262 *****************************************************************************/
263 static void vout_End( vout_thread_t *p_vout )
265 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
269 /*****************************************************************************
270 * vout_Destroy: destroy Sys video thread output method
271 *****************************************************************************
272 * Terminate an output method created by vout_Create
273 *****************************************************************************/
274 static void vout_Destroy( vout_thread_t *p_vout )
276 intf_WarnMsg( 3, "vout: vout_Destroy" );
277 DirectXCloseDisplay( p_vout );
278 DirectXCloseDDraw( p_vout );
280 /* Kill DirectXEventThread */
281 p_vout->p_sys->b_event_thread_die = 1;
282 /* we need to be sure DirectXEventThread won't stay stuck in GetMessage,
283 * so we send a fake message */
284 if( p_vout->p_sys->i_event_thread_status == THREAD_READY &&
285 p_vout->p_sys->hwnd )
287 PostMessage( p_vout->p_sys->hwnd, WM_CHAR, (WPARAM)'q', 0);
288 vlc_thread_join( p_vout->p_sys->event_thread_id );
291 if( p_vout->p_sys != NULL )
293 free( p_vout->p_sys );
294 p_vout->p_sys = NULL;
298 /*****************************************************************************
299 * vout_Manage: handle Sys events
300 *****************************************************************************
301 * This function should be called regularly by the video output thread.
302 * It returns a non null value if an error occured.
303 *****************************************************************************/
304 static int vout_Manage( vout_thread_t *p_vout )
306 WINDOWPLACEMENT window_placement;
308 /* We used to call the Win32 PeekMessage function here to read the window
309 * messages. But since window can stay blocked into this function for a
310 * long time (for example when you move your window on the screen), I
311 * decided to isolate PeekMessage in another thread. */
316 if( p_vout->i_changes & VOUT_SCALE_CHANGE
317 || p_vout->p_sys->i_changes & VOUT_SCALE_CHANGE)
319 intf_WarnMsg( 3, "vout: vout_Manage Scale Change" );
320 if( !p_vout->p_sys->b_using_overlay )
321 InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
323 DirectXUpdateOverlay( p_vout );
324 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
325 p_vout->p_sys->i_changes &= ~VOUT_SCALE_CHANGE;
331 if( p_vout->i_changes & VOUT_SIZE_CHANGE
332 || p_vout->p_sys->i_changes & VOUT_SIZE_CHANGE )
334 intf_WarnMsg( 3, "vout: vout_Manage Size Change" );
335 if( !p_vout->p_sys->b_using_overlay )
336 InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
338 DirectXUpdateOverlay( p_vout );
339 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
340 p_vout->p_sys->i_changes &= ~VOUT_SIZE_CHANGE;
346 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
347 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
349 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
351 /* We need to switch between Maximized and Normal sized window */
352 window_placement.length = sizeof(WINDOWPLACEMENT);
353 GetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
354 if( p_vout->b_fullscreen )
356 /* Maximized window */
357 window_placement.showCmd = SW_SHOWMAXIMIZED;
358 /* Change window style, no borders and no title bar */
359 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, 0 );
365 window_placement.showCmd = SW_SHOWNORMAL;
366 /* Change window style, borders and title bar */
367 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
368 WS_OVERLAPPEDWINDOW | WS_SIZEBOX | WS_VISIBLE );
371 SetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
373 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
374 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
380 if( ! p_vout->p_sys->b_cursor_autohidden &&
381 ( mdate() - p_vout->p_sys->i_lastmoved > 5000000 ) )
383 /* Hide the mouse automatically */
384 p_vout->p_sys->b_cursor_autohidden = 1;
389 if( p_vout->i_changes & VOUT_CURSOR_CHANGE
390 || p_vout->p_sys->i_changes & VOUT_CURSOR_CHANGE )
392 p_vout->p_sys->b_cursor = ! p_vout->p_sys->b_cursor;
394 ShowCursor( p_vout->p_sys->b_cursor &&
395 ! p_vout->p_sys->b_cursor_autohidden );
397 p_vout->i_changes &= ~VOUT_CURSOR_CHANGE;
398 p_vout->p_sys->i_changes &= ~VOUT_CURSOR_CHANGE;
402 /* Check if the event thread is still running */
403 if( p_vout->p_sys->b_event_thread_die )
409 /*****************************************************************************
410 * vout_Render: render previously calculated output
411 *****************************************************************************/
412 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
417 /*****************************************************************************
418 * vout_Display: displays previously rendered output
419 *****************************************************************************
420 * This function sends the currently rendered image to the display, wait until
421 * it is displayed and switch the two rendering buffers, preparing next frame.
422 *****************************************************************************/
423 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
427 if( (p_vout->p_sys->p_display == NULL) )
429 intf_WarnMsg( 3, "vout error: vout_Display no display!!" );
433 if( !p_vout->p_sys->b_using_overlay )
437 /* We ask for the "NOTEARING" option */
438 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
439 ddbltfx.dwSize = sizeof(DDBLTFX);
440 ddbltfx.dwDDFX = DDBLTFX_NOTEARING | DDBLT_ASYNC;
442 /* Blit video surface to display */
443 dxresult = IDirectDrawSurface3_Blt(p_vout->p_sys->p_display,
444 &p_vout->p_sys->rect_dest_clipped,
445 p_pic->p_sys->p_surface,
446 &p_vout->p_sys->rect_src_clipped,
448 if ( dxresult == DDERR_SURFACELOST )
450 /* Our surface can be lost so be sure
451 * to check this and restore it if needed */
452 IDirectDrawSurface3_Restore( p_vout->p_sys->p_display );
454 /* Now that the surface has been restored try to display again */
455 dxresult = IDirectDrawSurface3_Blt(p_vout->p_sys->p_display,
456 &p_vout->p_sys->rect_dest_clipped,
457 p_pic->p_sys->p_surface,
458 &p_vout->p_sys->rect_src_clipped,
462 if( dxresult != DD_OK )
464 intf_WarnMsg( 3, "vout: could not Blit the surface" );
469 else /* using overlay */
473 /* Flip the overlay buffers */
474 dxresult = IDirectDrawSurface3_Flip( p_pic->p_sys->p_front_surface,
476 if ( dxresult == DDERR_SURFACELOST )
478 /* Our surface can be lost so be sure
479 * to check this and restore it if needed */
480 IDirectDrawSurface3_Restore( p_vout->p_sys->p_display );
481 IDirectDrawSurface3_Restore( p_pic->p_sys->p_front_surface );
483 /* Now that the surface has been restored try to display again */
484 dxresult = IDirectDrawSurface3_Flip( p_pic->p_sys->p_front_surface,
486 DirectXUpdateOverlay( p_vout );
489 if( dxresult != DD_OK )
490 intf_WarnMsg( 8, "vout: couldn't flip overlay surface" );
493 if( !DirectXGetSurfaceDesc( p_pic ) )
496 intf_ErrMsg( "vout error: vout_Display cannot get surface desc" );
500 if( !UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma ) )
503 intf_ErrMsg( "vout error: vout_Display unvalid pic chroma" );
507 /* set currently displayed pic */
508 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
514 /* following functions are local */
516 /*****************************************************************************
517 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
518 *****************************************************************************
519 * This function initialise and allocate resources for DirectDraw.
520 *****************************************************************************/
521 static int DirectXInitDDraw( vout_thread_t *p_vout )
524 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
525 LPDIRECTDRAW p_ddobject;
527 intf_WarnMsg( 3, "vout: DirectXInitDDraw" );
529 /* load direct draw DLL */
530 p_vout->p_sys->hddraw_dll = LoadLibrary("DDRAW.DLL");
531 if( p_vout->p_sys->hddraw_dll == NULL )
533 intf_WarnMsg( 3, "vout: DirectXInitDDraw failed loading ddraw.dll" );
537 OurDirectDrawCreate =
538 (void *)GetProcAddress(p_vout->p_sys->hddraw_dll, "DirectDrawCreate");
539 if ( OurDirectDrawCreate == NULL )
541 intf_ErrMsg( "vout error: DirectXInitDDraw failed GetProcAddress" );
542 FreeLibrary( p_vout->p_sys->hddraw_dll );
543 p_vout->p_sys->hddraw_dll = NULL;
547 /* Initialize DirectDraw now */
548 dxresult = OurDirectDrawCreate( NULL, &p_ddobject, NULL );
549 if( dxresult != DD_OK )
551 intf_ErrMsg( "vout error: DirectXInitDDraw can't initialize DDraw" );
552 p_vout->p_sys->p_ddobject = NULL;
553 FreeLibrary( p_vout->p_sys->hddraw_dll );
554 p_vout->p_sys->hddraw_dll = NULL;
558 /* Set DirectDraw Cooperative level, ie what control we want over Windows
560 dxresult = IDirectDraw_SetCooperativeLevel( p_ddobject,
561 p_vout->p_sys->hwnd, DDSCL_NORMAL );
562 if( dxresult != DD_OK )
564 intf_ErrMsg( "vout error: can't set direct draw cooperative level." );
565 IDirectDraw_Release( p_ddobject );
566 p_vout->p_sys->p_ddobject = NULL;
567 FreeLibrary( p_vout->p_sys->hddraw_dll );
568 p_vout->p_sys->hddraw_dll = NULL;
572 /* Get the IDirectDraw2 interface */
573 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
574 (LPVOID *)&p_vout->p_sys->p_ddobject );
575 if( dxresult != DD_OK )
577 intf_ErrMsg( "vout error: can't get IDirectDraw2 interface." );
578 IDirectDraw_Release( p_ddobject );
579 p_vout->p_sys->p_ddobject = NULL;
580 FreeLibrary( p_vout->p_sys->hddraw_dll );
581 p_vout->p_sys->hddraw_dll = NULL;
586 /* Release the unused interface */
587 IDirectDraw_Release( p_ddobject );
590 /* Probe the capabilities of the hardware */
591 DirectXGetDDrawCaps( p_vout );
593 intf_WarnMsg( 3, "vout: End DirectXInitDDraw" );
597 /*****************************************************************************
598 * DirectXCreateDisplay: create the DirectDraw display.
599 *****************************************************************************
600 * Create and initialize display according to preferences specified in the vout
602 *****************************************************************************/
603 static int DirectXCreateDisplay( vout_thread_t *p_vout )
607 LPDIRECTDRAWSURFACE p_display;
608 DDPIXELFORMAT pixel_format;
610 intf_WarnMsg( 3, "vout: DirectXCreateDisplay" );
612 /* Now get the primary surface. This surface is what you actually see
614 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
615 ddsd.dwSize = sizeof(DDSURFACEDESC);
616 ddsd.dwFlags = DDSD_CAPS;
617 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
619 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
622 if( dxresult != DD_OK )
624 intf_ErrMsg( "vout error: can't get direct draw primary surface." );
625 p_vout->p_sys->p_display = NULL;
629 dxresult = IDirectDrawSurface_QueryInterface( p_display,
630 &IID_IDirectDrawSurface3,
631 (LPVOID *)&p_vout->p_sys->p_display );
632 if ( dxresult != DD_OK )
634 intf_ErrMsg( "vout error: can't get IDirectDrawSurface3 interface." );
635 IDirectDrawSurface_Release( p_display );
636 p_vout->p_sys->p_display = NULL;
641 /* Release the old interface */
642 IDirectDrawSurface_Release( p_display );
646 /* The clipper will be used only in non-overlay mode */
647 DirectXCreateClipper( p_vout );
651 /* compute the colorkey pixel value from the RGB value we've got */
652 memset( &pixel_format, 0, sizeof( DDPIXELFORMAT ));
653 pixel_format.dwSize = sizeof( DDPIXELFORMAT );
654 dxresult = IDirectDrawSurface3_GetPixelFormat( p_vout->p_sys->p_display,
656 if( dxresult != DD_OK )
657 intf_WarnMsg( 3, "vout: DirectXUpdateOverlay GetPixelFormat failed" );
658 p_vout->p_sys->i_colorkey = (DWORD)((( p_vout->p_sys->i_rgb_colorkey
659 * pixel_format.dwRBitMask) / 255)
660 & pixel_format.dwRBitMask);
667 /*****************************************************************************
668 * DirectXCreateClipper: Create a clipper that will be used when blitting the
669 * RGB surface to the main display.
670 *****************************************************************************
671 * This clipper prevents us to modify by mistake anything on the screen
672 * which doesn't belong to our window. For example when a part of our video
673 * window is hidden by another window.
674 *****************************************************************************/
675 static int DirectXCreateClipper( vout_thread_t *p_vout )
679 intf_WarnMsg( 3, "vout: DirectXCreateClipper" );
681 /* Create the clipper */
682 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
683 &p_vout->p_sys->p_clipper, NULL );
684 if( dxresult != DD_OK )
686 intf_WarnMsg( 3, "vout: DirectXCreateClipper can't create clipper." );
687 p_vout->p_sys->p_clipper = NULL;
691 /* associate the clipper to the window */
692 dxresult = IDirectDrawClipper_SetHWnd(p_vout->p_sys->p_clipper, 0,
693 p_vout->p_sys->hwnd);
694 if( dxresult != DD_OK )
697 "vout: DirectXCreateClipper can't attach clipper to window." );
698 IDirectDrawSurface_Release( p_vout->p_sys->p_clipper );
699 p_vout->p_sys->p_clipper = NULL;
703 /* associate the clipper with the surface */
704 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
705 p_vout->p_sys->p_clipper);
706 if( dxresult != DD_OK )
709 "vout: DirectXCreateClipper can't attach clipper to surface." );
710 IDirectDrawSurface_Release( p_vout->p_sys->p_clipper );
711 p_vout->p_sys->p_clipper = NULL;
718 /*****************************************************************************
719 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
720 *****************************************************************************
721 * The best method of display is with an YUV overlay because the YUV->RGB
722 * conversion is done in hardware.
723 * You can also create a plain RGB surface.
724 * ( Maybe we could also try an RGB overlay surface, which could have hardware
725 * scaling and which would also be faster in window mode because you don't
726 * need to do any blitting to the main display...)
727 *****************************************************************************/
728 static int DirectXCreateSurface( vout_thread_t *p_vout,
729 LPDIRECTDRAWSURFACE3 *pp_surface_final,
730 int i_chroma, int b_overlay )
733 LPDIRECTDRAWSURFACE p_surface;
736 intf_WarnMsg( 3, "vout: DirectXCreateSurface" );
738 /* Create the video surface */
741 /* Now try to create the YUV overlay surface.
742 * This overlay will be displayed on top of the primary surface.
743 * A color key is used to determine whether or not the overlay will be
744 * displayed, ie the overlay will be displayed in place of the primary
745 * surface wherever the primary surface will have this color.
746 * The video window has been created with a background of this color so
747 * the overlay will be only displayed on top of this window */
749 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
750 ddsd.dwSize = sizeof(DDSURFACEDESC);
751 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
752 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
753 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
754 ddsd.dwFlags = DDSD_CAPS |
757 //DDSD_BACKBUFFERCOUNT |
759 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY |
763 ddsd.dwHeight = p_vout->render.i_height;
764 ddsd.dwWidth = p_vout->render.i_width;
765 ddsd.dwBackBufferCount = 1; /* One back buffer */
767 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
770 if( dxresult == DD_OK )
772 intf_WarnMsg( 3,"vout: DirectX YUV overlay created successfully" );
776 intf_ErrMsg( "vout error: can't create YUV overlay surface." );
777 *pp_surface_final = NULL;
784 /* Now try to create a plain RGB surface. */
785 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
786 ddsd.dwSize = sizeof(DDSURFACEDESC);
787 ddsd.dwFlags = DDSD_HEIGHT |
790 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN |
791 DDSCAPS_SYSTEMMEMORY;
792 ddsd.dwHeight = p_vout->render.i_height;
793 ddsd.dwWidth = p_vout->render.i_width;
795 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
798 if( dxresult == DD_OK )
800 intf_WarnMsg( 3,"vout: DirectX RGB surface created successfully" );
804 intf_ErrMsg( "vout error: can't create RGB surface." );
805 *pp_surface_final = NULL;
810 /* Now that the surface is created, try to get a newer DirectX interface */
811 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
812 &IID_IDirectDrawSurface3,
813 (LPVOID *)pp_surface_final );
814 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
815 if ( dxresult != DD_OK )
817 intf_ErrMsg( "vout error: can't get IDirectDrawSurface3 interface." );
818 *pp_surface_final = NULL;
826 /*****************************************************************************
827 * DirectXUpdateOverlay: Move or resize overlay surface on video display.
828 *****************************************************************************
829 * This function is used to move or resize an overlay surface on the screen.
830 * Ususally the overlay is moved by the user and thus, by a move or resize
831 * event (in vout_Manage).
832 *****************************************************************************/
833 void DirectXUpdateOverlay( vout_thread_t *p_vout )
839 if( p_vout->p_sys->p_current_surface == NULL ||
840 !p_vout->p_sys->b_using_overlay )
842 intf_WarnMsg( 5, "vout: DirectXUpdateOverlay no overlay !!" );
846 /* The new window dimensions should already have been computed by the
847 * caller of this function */
849 /* Position and show the overlay */
850 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
851 ddofx.dwSize = sizeof(DDOVERLAYFX);
852 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
853 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
855 dwFlags = DDOVER_SHOW;
856 if( !p_vout->p_sys->b_caps_overlay_clipping )
857 dwFlags |= DDOVER_KEYDESTOVERRIDE;
859 dxresult = IDirectDrawSurface3_UpdateOverlay(
860 p_vout->p_sys->p_current_surface,
861 &p_vout->p_sys->rect_src_clipped,
862 p_vout->p_sys->p_display,
863 &p_vout->p_sys->rect_dest_clipped,
866 if(dxresult != DD_OK)
869 "vout: DirectXUpdateOverlay can't move or resize overlay" );
874 /*****************************************************************************
875 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
876 *****************************************************************************
877 * This function returns all resources allocated by DirectXInitDDraw.
878 *****************************************************************************/
879 static void DirectXCloseDDraw( vout_thread_t *p_vout )
881 intf_WarnMsg(3, "vout: DirectXCloseDDraw" );
882 if( p_vout->p_sys->p_ddobject != NULL )
884 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
885 p_vout->p_sys->p_ddobject = NULL;
888 if( p_vout->p_sys->hddraw_dll != NULL )
890 FreeLibrary( p_vout->p_sys->hddraw_dll );
891 p_vout->p_sys->hddraw_dll = NULL;
895 /*****************************************************************************
896 * DirectXCloseDisplay: close and reset the DirectX display device
897 *****************************************************************************
898 * This function returns all resources allocated by DirectXCreateDisplay.
899 *****************************************************************************/
900 static void DirectXCloseDisplay( vout_thread_t *p_vout )
902 intf_WarnMsg( 3, "vout: DirectXCloseDisplay" );
904 if( p_vout->p_sys->p_clipper != NULL )
906 intf_WarnMsg( 3, "vout: DirectXCloseDisplay clipper" );
907 IDirectDraw2_Release( p_vout->p_sys->p_clipper );
908 p_vout->p_sys->p_clipper = NULL;
911 if( p_vout->p_sys->p_display != NULL )
913 intf_WarnMsg( 3, "vout: DirectXCloseDisplay display" );
914 IDirectDraw2_Release( p_vout->p_sys->p_display );
915 p_vout->p_sys->p_display = NULL;
919 /*****************************************************************************
920 * DirectXCloseSurface: close the YUV overlay or RGB surface.
921 *****************************************************************************
922 * This function returns all resources allocated for the surface.
923 *****************************************************************************/
924 static void DirectXCloseSurface( vout_thread_t *p_vout,
925 LPDIRECTDRAWSURFACE3 p_surface )
927 intf_WarnMsg( 3, "vout: DirectXCloseSurface" );
928 if( p_surface != NULL )
930 IDirectDraw2_Release( p_surface );
934 /*****************************************************************************
935 * NewPictureVec: allocate a vector of identical pictures
936 *****************************************************************************
937 * Returns 0 on success, -1 otherwise
938 *****************************************************************************/
939 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
943 LPDIRECTDRAWSURFACE3 p_surface;
946 /* We couldn't use an YUV overlay so we need to indicate to video_output
947 * which format we are falling back to */
950 case 8: /* FIXME: set the palette */
951 p_vout->output.i_chroma = FOURCC_RGB2; break;
953 p_vout->output.i_chroma = FOURCC_RV15; break;
955 p_vout->output.i_chroma = FOURCC_RV16; break;
957 p_vout->output.i_chroma = FOURCC_RV24; break;
959 p_vout->output.i_chroma = FOURCC_RV32; break;
961 intf_ErrMsg( "vout error: unknown screen depth" );
966 intf_WarnMsg( 3, "vout: NewPictureVec" );
968 I_OUTPUTPICTURES = 0;
970 /* chroma asked for */
971 p_vout->output.i_chroma = p_vout->render.i_chroma;
975 if( p_vout->render.i_chroma == FOURCC_I420 )
976 p_vout->output.i_chroma = FOURCC_YV12;
979 /* First we try to create an overlay surface.
980 * It looks like with most hardware it's not possible to create several
981 * overlay surfaces, and even if it was I bet it would be slower anyway to
982 * use them as direct buffers because they usually reside in video memory
983 * which is quite slow.
984 * So the overlay surface (with a back-buffer) that we create won't be used
985 * to decode directly into it but instead picture buffers in system memory
986 * will be blitted to it. */
987 if( p_vout->p_sys->b_using_overlay )
989 if( DirectXCreateSurface( p_vout, &p_surface, p_vout->output.i_chroma,
990 p_vout->p_sys->b_using_overlay ) )
994 /* Allocate internal structure */
995 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
996 if( p_pic[0].p_sys == NULL )
998 DirectXCloseSurface( p_vout, p_surface );
1002 /* set front buffer */
1003 p_pic[0].p_sys->p_front_surface = p_surface;
1005 /* Get the back buffer */
1006 memset( &dds_caps, 0, sizeof( DDSCAPS ));
1007 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1008 if( DD_OK != IDirectDrawSurface3_GetAttachedSurface(
1009 p_surface, &dds_caps,
1010 &p_pic[0].p_sys->p_surface ) )
1012 intf_WarnMsg( 3, "vout: NewPictureVec couldn't get "
1014 /* front buffer is the same as back buffer */
1015 p_pic[0].p_sys->p_surface = p_surface;
1019 p_vout->p_sys->p_current_surface= p_pic[0].p_sys->p_front_surface;
1020 DirectXUpdateOverlay( p_vout );
1021 I_OUTPUTPICTURES = 1;
1023 else p_vout->p_sys->b_using_overlay = 0;
1026 /* As we can't have overlays, we'll try to create plain RBG surfaces in
1027 * system memory. These surfaces will then be blitted onto the primary
1028 * surface (display) so they can be displayed */
1029 if( !p_vout->p_sys->b_using_overlay )
1032 p_vout->output.i_chroma = FOURCC_RV16;
1033 p_vout->output.i_rmask = 0x001f;
1034 p_vout->output.i_gmask = 0x03e0;
1035 p_vout->output.i_bmask = 0x7c00;
1037 for( i = 0; i < i_num_pics; i++ )
1039 if( DirectXCreateSurface( p_vout, &p_surface,
1040 p_vout->output.i_chroma,
1041 p_vout->p_sys->b_using_overlay ) )
1043 /* Allocate internal structure */
1044 p_pic[i].p_sys = malloc( sizeof( picture_sys_t ) );
1045 if( p_pic[i].p_sys == NULL )
1047 DirectXCloseSurface( p_vout, p_surface );
1048 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1049 I_OUTPUTPICTURES = 0;
1052 p_pic[i].p_sys->p_surface = p_surface;
1053 p_pic[i].p_sys->p_front_surface = NULL;
1062 /* Now that we've got all our direct-buffers, we can finish filling in the
1063 * picture_t structures */
1064 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1066 p_pic[i].i_status = DESTROYED_PICTURE;
1067 p_pic[i].i_type = DIRECT_PICTURE;
1068 PP_OUTPUTPICTURE[i] = &p_pic[i];
1070 if( !DirectXGetSurfaceDesc( &p_pic[i] ) )
1073 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1074 I_OUTPUTPICTURES = 0;
1078 if( !UpdatePictureStruct(p_vout, &p_pic[i], p_vout->output.i_chroma) )
1081 /* Unknown chroma, tell the guy to get lost */
1082 intf_ErrMsg( "vout error: never heard of chroma 0x%.8x (%4.4s)",
1083 p_vout->output.i_chroma,
1084 (char*)&p_vout->output.i_chroma );
1085 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1086 I_OUTPUTPICTURES = 0;
1091 intf_WarnMsg( 3, "vout: End NewPictureVec");
1095 /*****************************************************************************
1096 * FreePicture: destroy a picture vector allocated with NewPictureVec
1097 *****************************************************************************
1099 *****************************************************************************/
1100 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1105 for( i = 0; i < i_num_pics; i++ )
1108 if( p_pic->p_sys->p_front_surface &&
1109 ( p_pic->p_sys->p_surface != p_pic->p_sys->p_front_surface ) )
1110 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1113 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_surface );
1115 for( i = 0; i < i_num_pics; i++ )
1117 free( p_pic[i].p_sys );
1122 /*****************************************************************************
1123 * UpdatePictureStruct: updates the internal data in the picture_t structure
1124 *****************************************************************************
1125 * This will setup stuff for use by the video_output thread
1126 *****************************************************************************/
1127 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1131 switch( p_vout->output.i_chroma )
1136 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1137 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1138 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1139 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1140 p_pic->p[Y_PLANE].b_margin = 0;
1142 p_pic->V_PIXELS = p_pic->Y_PIXELS
1143 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1144 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1145 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1146 p_pic->p[V_PLANE].i_pixel_bytes = 1;
1147 p_pic->p[V_PLANE].b_margin = 0;
1149 p_pic->U_PIXELS = p_pic->V_PIXELS
1150 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1151 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1152 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1153 p_pic->p[U_PLANE].i_pixel_bytes = 1;
1154 p_pic->p[U_PLANE].b_margin = 0;
1156 p_pic->i_planes = 3;
1161 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1162 p_pic->p->i_lines = p_vout->output.i_height;
1163 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1164 p_pic->p->i_pixel_bytes = 2;
1165 p_pic->p->b_margin = 0;
1167 p_pic->i_planes = 1;
1172 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1173 p_pic->p->i_lines = p_vout->output.i_height;
1174 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1175 p_pic->p->i_pixel_bytes = 2;
1176 p_pic->p->b_margin = 0;
1178 p_pic->i_planes = 1;
1183 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1184 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1185 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1186 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
1187 p_pic->p[Y_PLANE].b_margin = 0;
1189 p_pic->U_PIXELS = p_pic->Y_PIXELS
1190 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1191 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1192 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1193 p_pic->p[U_PLANE].i_pixel_bytes = 1;
1194 p_pic->p[U_PLANE].b_margin = 0;
1196 p_pic->V_PIXELS = p_pic->U_PIXELS
1197 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1198 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1199 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1200 p_pic->p[V_PLANE].i_pixel_bytes = 1;
1201 p_pic->p[V_PLANE].b_margin = 0;
1203 p_pic->i_planes = 3;
1209 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1210 + p_pic->p_sys->p_image->offsets[0];
1211 p_pic->p->i_lines = p_vout->output.i_height;
1212 /* XXX: this just looks so plain wrong... check it out ! */
1213 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0] / 4;
1214 p_pic->p->i_pixel_bytes = 4;
1215 p_pic->p->b_margin = 0;
1217 p_pic->i_planes = 1;
1223 p_pic->p->p_pixels = p_pic->p_sys->p_image->data
1224 + p_pic->p_sys->p_image->offsets[0];
1225 p_pic->p->i_lines = p_vout->output.i_height;
1226 p_pic->p->i_pitch = p_pic->p_sys->p_image->pitches[0];
1227 p_pic->p->i_pixel_bytes = 4;
1228 p_pic->p->b_margin = 0;
1230 p_pic->i_planes = 1;
1243 /*****************************************************************************
1244 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1245 *****************************************************************************
1246 * It is nice to know which features are supported by the hardware so we can
1247 * find ways to optimize our rendering.
1248 *****************************************************************************/
1249 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1254 /* This is just an indication of whether or not we'll support overlay,
1255 * but with this test we don't know if we support YUV overlay */
1256 memset( &ddcaps, 0, sizeof( DDCAPS ));
1257 ddcaps.dwSize = sizeof(DDCAPS);
1258 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1260 if(dxresult != DD_OK )
1262 intf_WarnMsg( 3,"vout error: can't get caps." );
1266 BOOL bHasOverlay, bHasOverlayFourCC, bCanClipOverlay,
1267 bHasColorKey, bCanStretch;
1269 /* Determine if the hardware supports overlay surfaces */
1270 bHasOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAY) ==
1271 DDCAPS_OVERLAY) ? TRUE : FALSE;
1272 /* Determine if the hardware supports overlay surfaces */
1273 bHasOverlayFourCC = ((ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ==
1274 DDCAPS_OVERLAYFOURCC) ? TRUE : FALSE;
1275 /* Determine if the hardware supports overlay surfaces */
1276 bCanClipOverlay = ((ddcaps.dwCaps & DDCAPS_OVERLAYCANTCLIP) ==
1278 /* Determine if the hardware supports colorkeying */
1279 bHasColorKey = ((ddcaps.dwCaps & DDCAPS_COLORKEY) ==
1280 DDCAPS_COLORKEY) ? TRUE : FALSE;
1281 /* Determine if the hardware supports scaling of the overlay surface */
1282 bCanStretch = ((ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ==
1283 DDCAPS_OVERLAYSTRETCH) ? TRUE : FALSE;
1284 intf_WarnMsg( 3, "vout: DirectDraw Capabilities:" );
1285 intf_WarnMsg( 3, " overlay=%i yuvoverlay=%i can_clip_overlay=%i "
1286 "colorkey=%i stretch=%i",
1287 bHasOverlay, bHasOverlayFourCC, bCanClipOverlay,
1288 bHasColorKey, bCanStretch );
1290 /* Overlay clipping support is interesting for us as it means we can
1291 * get rid of the colorkey alltogether */
1292 p_vout->p_sys->b_caps_overlay_clipping = bCanClipOverlay;
1297 /*****************************************************************************
1298 * DirectXGetSurfaceDesc: Get some more information about the surface
1299 *****************************************************************************
1300 * This function get and stores the surface descriptor which among things
1301 * has the pointer to the picture data.
1302 *****************************************************************************/
1303 static int DirectXGetSurfaceDesc( picture_t *p_pic )
1307 /* Lock the surface to get a valid pointer to the picture buffer */
1308 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1309 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1310 dxresult = IDirectDrawSurface3_Lock( p_pic->p_sys->p_surface,
1311 NULL, &p_pic->p_sys->ddsd,
1314 if ( dxresult == DDERR_SURFACELOST )
1316 /* Your surface can be lost so be sure
1317 * to check this and restore it if needed */
1318 dxresult = IDirectDrawSurface3_Restore( p_pic->p_sys->p_surface );
1319 dxresult = IDirectDrawSurface3_Lock( p_pic->p_sys->p_surface, NULL,
1320 &p_pic->p_sys->ddsd,
1321 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1324 if( dxresult != DD_OK )
1326 intf_ErrMsg( "vout: DirectXGetSurfaceDesc can't lock surface" );
1330 /* Unlock the Surface */
1331 dxresult = IDirectDrawSurface3_Unlock( p_pic->p_sys->p_surface, NULL );