1 /*****************************************************************************
2 * sdl.c: SDL video output display method
3 *****************************************************************************
4 * Copyright (C) 1998-2001 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Pierre Baillet <oct@zoy.org>
9 * Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_interface.h>
38 #include <vlc_playlist.h>
41 //#include <vlc_aout.h>
43 #include <sys/types.h>
45 # include <netinet/in.h> /* BSD: struct in_addr */
48 #include SDL_INCLUDE_FILE
50 /* SDL is not able to crop overlays - so use only 1 direct buffer */
51 #define SDL_MAX_DIRECTBUFFERS 1
52 #define SDL_DEFAULT_BPP 16
54 /*****************************************************************************
55 * vout_sys_t: video output SDL method descriptor
56 *****************************************************************************
57 * This structure is part of the video output thread descriptor.
58 * It describes the SDL specific properties of an output thread.
59 *****************************************************************************/
62 SDL_Surface * p_display; /* display device */
67 #if SDL_VERSION_ATLEAST(1,2,10)
68 unsigned int i_desktop_width;
69 unsigned int i_desktop_height;
73 SDL_Overlay * p_overlay; /* An overlay we keep to grab the XVideo port */
79 bool b_cursor_autohidden;
81 mtime_t i_mouse_hide_timeout;
82 mtime_t i_lastpressed; /* to track dbl-clicks */
85 /*****************************************************************************
86 * picture_sys_t: direct buffer method descriptor
87 *****************************************************************************
88 * This structure is part of the picture descriptor, it describes the
89 * SDL specific properties of a direct buffer.
90 *****************************************************************************/
93 SDL_Overlay *p_overlay;
96 /*****************************************************************************
98 *****************************************************************************/
99 static int Open ( vlc_object_t * );
100 static void Close ( vlc_object_t * );
101 static int Init ( vout_thread_t * );
102 static void End ( vout_thread_t * );
103 static int Manage ( vout_thread_t * );
104 static void Display ( vout_thread_t *, picture_t * );
106 static int OpenDisplay ( vout_thread_t * );
107 static void CloseDisplay ( vout_thread_t * );
108 static int NewPicture ( vout_thread_t *, picture_t * );
109 static void SetPalette ( vout_thread_t *,
110 uint16_t *, uint16_t *, uint16_t * );
112 static int ConvertKey( SDLKey );
115 #define CHROMA_TEXT N_("SDL chroma format")
116 #define CHROMA_LONGTEXT N_( \
117 "Force the SDL renderer to use a specific chroma format instead of " \
118 "trying to improve performances by using the most efficient one.")
120 /*****************************************************************************
122 *****************************************************************************/
124 set_shortname( "SDL" );
125 set_category( CAT_VIDEO );
126 set_subcategory( SUBCAT_VIDEO_VOUT );
127 set_description( N_("Simple DirectMedia Layer video output") );
128 set_capability( "video output", 60 );
129 add_shortcut( "sdl" );
130 add_string( "sdl-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true );
131 set_callbacks( Open, Close );
132 #if defined( __i386__ ) || defined( __x86_64__ )
133 /* On i386, SDL is linked against svgalib */
134 linked_with_a_crap_library_which_uses_atexit();
138 /*****************************************************************************
139 * OpenVideo: allocate SDL video thread output method
140 *****************************************************************************
141 * This function allocate and initialize a SDL vout method. It uses some of the
142 * vout properties to choose the correct mode, and change them according to the
143 * mode actually used.
144 *****************************************************************************/
145 static int Open ( vlc_object_t *p_this )
147 vout_thread_t * p_vout = (vout_thread_t *)p_this;
148 /* XXX: check for conflicts with the SDL audio output */
149 vlc_mutex_t *lock = var_AcquireMutex( "sdl" );
158 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
159 if( p_vout->p_sys == NULL )
161 vlc_mutex_unlock( lock );
165 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
167 /* Check if SDL video module has been initialized */
168 if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
170 vlc_mutex_unlock( lock );
171 free( p_vout->p_sys );
175 /* Allocate structure */
176 p_vout->pf_init = Init;
177 p_vout->pf_end = End;
178 p_vout->pf_manage = Manage;
179 p_vout->pf_render = NULL;
180 p_vout->pf_display = Display;
181 p_vout->pf_control = NULL;
184 char* psz = psz_method = config_GetPsz( p_vout, "vout" );
187 while( *psz_method && *psz_method != ':' )
194 setenv( "SDL_VIDEODRIVER", psz_method + 1, 1 );
200 /* Initialize library */
201 if( SDL_Init( SDL_INIT_VIDEO
203 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
204 | SDL_INIT_EVENTTHREAD
207 /* In debug mode you may want vlc to dump a core instead of staying
209 | SDL_INIT_NOPARACHUTE
213 msg_Err( p_vout, "cannot initialize SDL (%s)", SDL_GetError() );
214 free( p_vout->p_sys );
215 vlc_mutex_unlock( lock );
219 vlc_mutex_unlock( lock );
221 /* Translate keys into unicode */
222 SDL_EnableUNICODE(1);
224 /* Get the desktop resolution */
225 #if SDL_VERSION_ATLEAST(1,2,10)
226 /* FIXME: SDL has a problem with virtual desktop */
227 p_vout->p_sys->i_desktop_width = SDL_GetVideoInfo()->current_w;
228 p_vout->p_sys->i_desktop_height = SDL_GetVideoInfo()->current_h;
231 /* Create the cursor */
232 p_vout->p_sys->b_cursor = 1;
233 p_vout->p_sys->b_cursor_autohidden = 0;
234 p_vout->p_sys->i_lastmoved = p_vout->p_sys->i_lastpressed = mdate();
235 p_vout->p_sys->i_mouse_hide_timeout =
236 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
238 if( OpenDisplay( p_vout ) )
240 msg_Err( p_vout, "cannot set up SDL (%s)", SDL_GetError() );
241 SDL_QuitSubSystem( SDL_INIT_VIDEO );
242 free( p_vout->p_sys );
249 /*****************************************************************************
250 * Init: initialize SDL video thread output method
251 *****************************************************************************
252 * This function initialize the SDL display device.
253 *****************************************************************************/
254 static int Init( vout_thread_t *p_vout )
259 p_vout->p_sys->i_surfaces = 0;
261 I_OUTPUTPICTURES = 0;
263 /* Initialize the output structure */
264 if( p_vout->p_sys->p_overlay == NULL )
266 /* All we have is an RGB image with square pixels */
267 p_vout->output.i_width = p_vout->p_sys->i_width;
268 p_vout->output.i_height = p_vout->p_sys->i_height;
269 p_vout->output.i_aspect = p_vout->output.i_width
271 / p_vout->output.i_height;
275 /* We may need to convert the chroma, but at least we keep the
277 p_vout->output.i_width = p_vout->render.i_width;
278 p_vout->output.i_height = p_vout->render.i_height;
279 p_vout->output.i_aspect = p_vout->render.i_aspect;
282 /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
283 while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
287 /* Find an empty picture slot */
288 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
290 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
292 p_pic = p_vout->p_picture + i_index;
297 /* Allocate the picture if we found one */
298 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
303 p_pic->i_status = DESTROYED_PICTURE;
304 p_pic->i_type = DIRECT_PICTURE;
306 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
314 /*****************************************************************************
315 * End: terminate Sys video thread output method
316 *****************************************************************************
317 * Terminate an output method created by OpenVideo
318 *****************************************************************************/
319 static void End( vout_thread_t *p_vout )
323 /* Free the output buffers we allocated */
324 for( i_index = I_OUTPUTPICTURES ; i_index ; )
327 if( p_vout->p_sys->p_overlay == NULL )
333 SDL_UnlockYUVOverlay(
334 PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
336 PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
338 free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
342 /*****************************************************************************
343 * CloseVideo: destroy Sys video thread output method
344 *****************************************************************************
345 * Terminate an output method created by vout_SDLCreate
346 *****************************************************************************/
347 static void Close ( vlc_object_t *p_this )
349 vout_thread_t * p_vout = (vout_thread_t *)p_this;
351 CloseDisplay( p_vout );
352 SDL_QuitSubSystem( SDL_INIT_VIDEO );
354 free( p_vout->p_sys );
357 /*****************************************************************************
358 * Manage: handle Sys events
359 *****************************************************************************
360 * This function should be called regularly by video output thread. It returns
361 * a non null value if an error occurred.
362 *****************************************************************************/
363 static int Manage( vout_thread_t *p_vout )
365 SDL_Event event; /* SDL event */
367 unsigned int i_width, i_height, i_x, i_y;
370 while( SDL_PollEvent( &event ) )
374 /* Resizing of window */
375 case SDL_VIDEORESIZE:
376 p_vout->i_changes |= VOUT_SIZE_CHANGE;
377 p_vout->i_window_width = p_vout->p_sys->i_width = event.resize.w;
378 p_vout->i_window_height = p_vout->p_sys->i_height = event.resize.h;
382 case SDL_MOUSEMOTION:
383 vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
384 p_vout->p_sys->i_height,
385 &i_x, &i_y, &i_width, &i_height );
387 /* Compute the x coordinate and check if the value is
388 in [0,p_vout->fmt_in.i_visible_width] */
389 val.i_int = ( event.motion.x - i_x ) *
390 p_vout->fmt_in.i_visible_width / i_width +
391 p_vout->fmt_in.i_x_offset;
393 if( (int)(event.motion.x - i_x) < 0 )
395 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
396 val.i_int = p_vout->fmt_in.i_visible_width;
398 var_Set( p_vout, "mouse-x", val );
400 /* compute the y coordinate and check if the value is
401 in [0,p_vout->fmt_in.i_visible_height] */
402 val.i_int = ( event.motion.y - i_y ) *
403 p_vout->fmt_in.i_visible_height / i_height +
404 p_vout->fmt_in.i_y_offset;
406 if( (int)(event.motion.y - i_y) < 0 )
408 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
409 val.i_int = p_vout->fmt_in.i_visible_height;
411 var_Set( p_vout, "mouse-y", val );
414 var_Set( p_vout, "mouse-moved", val );
416 if( p_vout->p_sys->b_cursor )
418 if( p_vout->p_sys->b_cursor_autohidden )
420 p_vout->p_sys->b_cursor_autohidden = 0;
425 p_vout->p_sys->i_lastmoved = mdate();
430 /* Mouse button released */
431 case SDL_MOUSEBUTTONUP:
432 switch( event.button.button )
434 case SDL_BUTTON_LEFT:
436 var_Get( p_vout, "mouse-button-down", &val );
438 var_Set( p_vout, "mouse-button-down", val );
441 var_Set( p_vout, "mouse-clicked", val );
444 var_Set( p_vout->p_libvlc, "intf-popupmenu", val );
448 case SDL_BUTTON_MIDDLE:
450 var_Get( p_vout, "mouse-button-down", &val );
452 var_Set( p_vout, "mouse-button-down", val );
455 var_Get( p_vout->p_libvlc, "intf-show", &val );
456 val.b_bool = !val.b_bool;
457 var_Set( p_vout->p_libvlc, "intf-show", val );
461 case SDL_BUTTON_RIGHT:
463 intf_thread_t *p_intf;
465 var_Get( p_vout, "mouse-button-down", &val );
467 var_Set( p_vout, "mous-button-down", val );
468 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
472 p_intf->b_menu_change = 1;
473 vlc_object_release( p_intf );
477 var_Set( p_vout->p_libvlc, "intf-popupmenu", val );
483 /* Mouse button pressed */
484 case SDL_MOUSEBUTTONDOWN:
485 switch( event.button.button )
487 case SDL_BUTTON_LEFT:
488 var_Get( p_vout, "mouse-button-down", &val );
490 var_Set( p_vout, "mouse-button-down", val );
492 /* detect double-clicks */
493 if( ( mdate() - p_vout->p_sys->i_lastpressed ) < 300000 )
494 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
496 p_vout->p_sys->i_lastpressed = mdate();
499 case SDL_BUTTON_MIDDLE:
500 var_Get( p_vout, "mouse-button-down", &val );
502 var_Set( p_vout, "mouse-button-down", val );
505 case SDL_BUTTON_RIGHT:
506 var_Get( p_vout, "mouse-button-down", &val );
508 var_Set( p_vout, "mouse-button-down", val );
513 /* Quit event (close the window) */
517 playlist_t *p_playlist = pl_Yield( p_vout );
518 if( p_playlist != NULL )
520 playlist_Stop( p_playlist );
521 pl_Release( p_vout );
524 #warning FIXME FIXME ?
531 /* convert the key if possible */
532 val.i_int = ConvertKey( event.key.keysym.sym );
536 /* Find the right caracter */
537 if( ( event.key.keysym.unicode & 0xff80 ) == 0 )
539 val.i_int = event.key.keysym.unicode & 0x7f;
540 /* FIXME: find a better solution than this
541 hack to find the right caracter */
542 if( val.i_int >= 1 && val.i_int <= 26 )
544 else if( val.i_int >= 65 && val.i_int <= 90 )
551 if( ( event.key.keysym.mod & KMOD_SHIFT ) )
553 val.i_int |= KEY_MODIFIER_SHIFT;
555 if( ( event.key.keysym.mod & KMOD_CTRL ) )
557 val.i_int |= KEY_MODIFIER_CTRL;
559 if( ( event.key.keysym.mod & KMOD_ALT ) )
561 val.i_int |= KEY_MODIFIER_ALT;
563 var_Set( p_vout->p_libvlc, "key-pressed", val );
571 /* Fullscreen change */
572 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
576 /* Update the object variable and trigger callback */
577 val_fs.b_bool = !p_vout->b_fullscreen;
578 p_vout->b_fullscreen = !p_vout->b_fullscreen;
579 var_Set( p_vout, "fullscreen", val_fs );
581 /*TODO: add the "always on top" code here !*/
583 p_vout->p_sys->b_cursor_autohidden = 0;
584 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
585 ! p_vout->p_sys->b_cursor_autohidden );
587 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
588 p_vout->i_changes |= VOUT_SIZE_CHANGE;
591 /* Crop or Aspect Ratio Changes */
592 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
593 p_vout->i_changes & VOUT_ASPECT_CHANGE )
595 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
596 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
598 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
599 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
600 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
601 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
602 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
603 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
604 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
605 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
607 p_vout->i_changes |= VOUT_SIZE_CHANGE;
611 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
613 msg_Dbg( p_vout, "video display resized (%dx%d)",
614 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
616 CloseDisplay( p_vout );
617 OpenDisplay( p_vout );
619 /* We don't need to signal the vout thread about the size change if
620 * we can handle rescaling ourselves */
621 if( p_vout->p_sys->p_overlay != NULL )
622 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
626 if( ! p_vout->p_sys->b_cursor_autohidden &&
627 ( mdate() - p_vout->p_sys->i_lastmoved >
628 p_vout->p_sys->i_mouse_hide_timeout ) )
630 /* Hide the mouse automatically */
631 p_vout->p_sys->b_cursor_autohidden = 1;
638 /*****************************************************************************
639 * Key events handling
640 *****************************************************************************/
645 } sdlkeys_to_vlckeys[] =
656 { SDLK_F10, KEY_F10 },
657 { SDLK_F11, KEY_F11 },
658 { SDLK_F12, KEY_F12 },
660 { SDLK_RETURN, KEY_ENTER },
661 { SDLK_KP_ENTER, KEY_ENTER },
662 { SDLK_SPACE, KEY_SPACE },
663 { SDLK_ESCAPE, KEY_ESC },
665 { SDLK_MENU, KEY_MENU },
666 { SDLK_LEFT, KEY_LEFT },
667 { SDLK_RIGHT, KEY_RIGHT },
669 { SDLK_DOWN, KEY_DOWN },
671 { SDLK_HOME, KEY_HOME },
672 { SDLK_END, KEY_END },
673 { SDLK_PAGEUP, KEY_PAGEUP },
674 { SDLK_PAGEDOWN, KEY_PAGEDOWN },
676 { SDLK_INSERT, KEY_INSERT },
677 { SDLK_DELETE, KEY_DELETE },
678 /*TODO: find a equivalent for SDL
679 { , KEY_MEDIA_NEXT_TRACK }
680 { , KEY_MEDIA_PREV_TRACK }
681 { , KEY_VOLUME_MUTE }
682 { , KEY_VOLUME_DOWN }
684 { , KEY_MEDIA_PLAY_PAUSE }
685 { , KEY_MEDIA_PLAY_PAUSE }*/
690 static int ConvertKey( SDLKey sdl_key )
693 for( i=0; sdlkeys_to_vlckeys[i].sdl_key != 0; i++ )
695 if( sdlkeys_to_vlckeys[i].sdl_key == sdl_key )
697 return sdlkeys_to_vlckeys[i].i_vlckey;
704 /*****************************************************************************
705 * Display: displays previously rendered output
706 *****************************************************************************
707 * This function sends the currently rendered image to the display.
708 *****************************************************************************/
709 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
711 unsigned int x, y, w, h;
714 vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
721 if( p_vout->p_sys->p_overlay == NULL )
724 SDL_Flip( p_vout->p_sys->p_display );
728 /* Overlay picture */
729 SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
730 SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
731 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
735 /* following functions are local */
737 /*****************************************************************************
738 * OpenDisplay: open and initialize SDL device
739 *****************************************************************************
740 * Open and initialize display according to preferences specified in the vout
742 *****************************************************************************/
743 static int OpenDisplay( vout_thread_t *p_vout )
748 /* SDL fucked up fourcc definitions on bigendian machines */
749 uint32_t i_sdl_chroma;
750 char *psz_chroma = NULL;
751 uint32_t i_chroma = 0;
753 bool b_overlay = config_GetInt( p_vout, "overlay" );
755 /* Set main window's size */
756 #if SDL_VERSION_ATLEAST(1,2,10)
757 p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_width :
758 p_vout->i_window_width;
759 p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_height :
760 p_vout->i_window_height;
762 p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->output.i_width :
763 p_vout->i_window_width;
764 p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->output.i_height :
765 p_vout->i_window_height;
768 /* Initialize flags and cursor */
769 i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
770 i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
772 i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
773 SDL_DEFAULT_BPP, i_flags );
776 msg_Err( p_vout, "no video mode available" );
780 p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
781 p_vout->p_sys->i_height,
784 if( p_vout->p_sys->p_display == NULL )
786 msg_Err( p_vout, "cannot set video mode" );
790 SDL_LockSurface( p_vout->p_sys->p_display );
792 if( ( psz_chroma = config_GetPsz( p_vout, "sdl-chroma" ) ) )
794 if( strlen( psz_chroma ) >= 4 )
796 memcpy(&i_chroma, psz_chroma, 4);
797 msg_Dbg( p_vout, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
808 /* Choose the chroma we will try first. */
811 if( !psz_chroma ) i_chroma = 0;
812 switch( i_chroma ? i_chroma : p_vout->render.i_chroma )
814 case VLC_FOURCC('Y','U','Y','2'):
815 case VLC_FOURCC('Y','U','N','V'):
816 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
817 i_sdl_chroma = SDL_YUY2_OVERLAY;
819 case VLC_FOURCC('U','Y','V','Y'):
820 case VLC_FOURCC('U','Y','N','V'):
821 case VLC_FOURCC('Y','4','2','2'):
822 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
823 i_sdl_chroma = SDL_UYVY_OVERLAY;
825 case VLC_FOURCC('Y','V','Y','U'):
826 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
827 i_sdl_chroma = SDL_YVYU_OVERLAY;
829 case VLC_FOURCC('Y','V','1','2'):
830 case VLC_FOURCC('I','4','2','0'):
831 case VLC_FOURCC('I','Y','U','V'):
833 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
834 i_sdl_chroma = SDL_YV12_OVERLAY;
837 free( psz_chroma ); psz_chroma = NULL;
839 p_vout->p_sys->p_overlay =
840 SDL_CreateYUVOverlay( 32, 32, i_sdl_chroma,
841 p_vout->p_sys->p_display );
842 /* FIXME: if the first overlay we find is software, don't stop,
843 * because we may find a hardware one later ... */
845 while( i_chroma && !p_vout->p_sys->p_overlay );
848 /* If this best choice failed, fall back to other chromas */
849 if( p_vout->p_sys->p_overlay == NULL )
851 p_vout->output.i_chroma = VLC_FOURCC('I','Y','U','V');
852 p_vout->p_sys->p_overlay =
853 SDL_CreateYUVOverlay( 32, 32, SDL_IYUV_OVERLAY,
854 p_vout->p_sys->p_display );
857 if( p_vout->p_sys->p_overlay == NULL )
859 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
860 p_vout->p_sys->p_overlay =
861 SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
862 p_vout->p_sys->p_display );
865 if( p_vout->p_sys->p_overlay == NULL )
867 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
868 p_vout->p_sys->p_overlay =
869 SDL_CreateYUVOverlay( 32, 32, SDL_YUY2_OVERLAY,
870 p_vout->p_sys->p_display );
874 if( p_vout->p_sys->p_overlay == NULL )
877 msg_Warn( p_vout, "no SDL overlay for 0x%.8x (%4.4s)",
878 p_vout->render.i_chroma,
879 (char*)&p_vout->render.i_chroma );
881 msg_Warn( p_vout, "SDL overlay disabled by the user" );
883 switch( p_vout->p_sys->p_display->format->BitsPerPixel )
886 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
887 p_vout->output.pf_setpalette = SetPalette;
890 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
893 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
896 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
899 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
902 msg_Err( p_vout, "unknown screen depth %i",
903 p_vout->p_sys->p_display->format->BitsPerPixel );
904 SDL_UnlockSurface( p_vout->p_sys->p_display );
905 SDL_FreeSurface( p_vout->p_sys->p_display );
909 p_vout->output.i_rmask = p_vout->p_sys->p_display->format->Rmask;
910 p_vout->output.i_gmask = p_vout->p_sys->p_display->format->Gmask;
911 p_vout->output.i_bmask = p_vout->p_sys->p_display->format->Bmask;
913 SDL_WM_SetCaption( VOUT_TITLE " (software RGB SDL output)",
914 VOUT_TITLE " (software RGB SDL output)" );
918 if( p_vout->p_sys->p_overlay->hw_overlay )
920 SDL_WM_SetCaption( VOUT_TITLE " (hardware YUV SDL output)",
921 VOUT_TITLE " (hardware YUV SDL output)" );
925 SDL_WM_SetCaption( VOUT_TITLE " (software YUV SDL output)",
926 VOUT_TITLE " (software YUV SDL output)" );
930 SDL_EventState( SDL_KEYUP, SDL_IGNORE ); /* ignore keys up */
935 /*****************************************************************************
936 * CloseDisplay: close and reset SDL device
937 *****************************************************************************
938 * This function returns all resources allocated by OpenDisplay and restore
939 * the original state of the device.
940 *****************************************************************************/
941 static void CloseDisplay( vout_thread_t *p_vout )
943 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
944 SDL_UnlockSurface ( p_vout->p_sys->p_display );
945 SDL_FreeSurface( p_vout->p_sys->p_display );
948 /*****************************************************************************
949 * NewPicture: allocate a picture
950 *****************************************************************************
951 * Returns 0 on success, -1 otherwise
952 *****************************************************************************/
953 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
955 int i_width = p_vout->output.i_width;
956 int i_height = p_vout->output.i_height;
958 if( p_vout->p_sys->p_overlay == NULL )
961 if( p_vout->p_sys->i_surfaces )
963 /* We already allocated this surface, return */
967 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
969 if( p_pic->p_sys == NULL )
974 switch( p_vout->p_sys->p_display->format->BitsPerPixel )
977 p_pic->p->i_pixel_pitch = 1;
981 p_pic->p->i_pixel_pitch = 2;
985 p_pic->p->i_pixel_pitch = 4;
991 p_pic->p->p_pixels = p_vout->p_sys->p_display->pixels;
992 p_pic->p->i_lines = p_vout->p_sys->p_display->h;
993 p_pic->p->i_visible_lines = p_vout->p_sys->p_display->h;
994 p_pic->p->i_pitch = p_vout->p_sys->p_display->pitch;
995 p_pic->p->i_visible_pitch =
996 p_pic->p->i_pixel_pitch * p_vout->p_sys->p_display->w;
998 p_vout->p_sys->i_surfaces++;
1000 p_pic->i_planes = 1;
1004 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1006 if( p_pic->p_sys == NULL )
1011 p_pic->p_sys->p_overlay =
1012 SDL_CreateYUVOverlay( i_width, i_height,
1013 p_vout->output.i_chroma,
1014 p_vout->p_sys->p_display );
1016 if( p_pic->p_sys->p_overlay == NULL )
1018 free( p_pic->p_sys );
1019 return VLC_EGENERIC;
1022 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
1024 p_pic->Y_PIXELS = p_pic->p_sys->p_overlay->pixels[0];
1025 p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_overlay->h;
1026 p_pic->p[Y_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h;
1027 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[0];
1029 switch( p_vout->output.i_chroma )
1031 case SDL_YV12_OVERLAY:
1032 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1033 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1035 p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1036 p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1037 p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1038 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1039 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1040 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1042 p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1043 p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1044 p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1045 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1046 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1047 p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1049 p_pic->i_planes = 3;
1052 case SDL_IYUV_OVERLAY:
1053 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1054 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1056 p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1057 p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1058 p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1059 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1060 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1061 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1063 p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1064 p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1065 p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1066 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1067 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1068 p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1070 p_pic->i_planes = 3;
1074 p_pic->p[Y_PLANE].i_pixel_pitch = 2;
1075 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w * 2;
1077 p_pic->i_planes = 1;
1085 /*****************************************************************************
1086 * SetPalette: sets an 8 bpp palette
1087 *****************************************************************************/
1088 static void SetPalette( vout_thread_t *p_vout,
1089 uint16_t *red, uint16_t *green, uint16_t *blue )
1091 SDL_Color colors[256];
1094 /* Fill colors with color information */
1095 for( i = 0; i < 256; i++ )
1097 colors[ i ].r = red[ i ] >> 8;
1098 colors[ i ].g = green[ i ] >> 8;
1099 colors[ i ].b = blue[ i ] >> 8;
1103 if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
1105 msg_Err( p_vout, "failed to set palette" );