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 static vlc_mutex_t sdl_lock = VLC_STATIC_MUTEX;
140 /*****************************************************************************
141 * OpenVideo: allocate SDL video thread output method
142 *****************************************************************************
143 * This function allocate and initialize a SDL vout method. It uses some of the
144 * vout properties to choose the correct mode, and change them according to the
145 * mode actually used.
146 *****************************************************************************/
147 static int Open ( vlc_object_t *p_this )
149 vout_thread_t * p_vout = (vout_thread_t *)p_this;
150 /* XXX: check for conflicts with the SDL audio output */
151 vlc_mutex_lock( &sdl_lock );
157 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
158 if( p_vout->p_sys == NULL )
160 vlc_mutex_unlock( &sdl_lock );
164 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
166 /* Check if SDL video module has been initialized */
167 if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
169 vlc_mutex_unlock( &sdl_lock );
170 free( p_vout->p_sys );
174 /* Allocate structure */
175 p_vout->pf_init = Init;
176 p_vout->pf_end = End;
177 p_vout->pf_manage = Manage;
178 p_vout->pf_render = NULL;
179 p_vout->pf_display = Display;
180 p_vout->pf_control = NULL;
183 char* psz = psz_method = config_GetPsz( p_vout, "vout" );
186 while( *psz_method && *psz_method != ':' )
193 setenv( "SDL_VIDEODRIVER", psz_method + 1, 1 );
199 /* Initialize library */
200 if( SDL_Init( SDL_INIT_VIDEO
202 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
203 | SDL_INIT_EVENTTHREAD
206 /* In debug mode you may want vlc to dump a core instead of staying
208 | SDL_INIT_NOPARACHUTE
212 msg_Err( p_vout, "cannot initialize SDL (%s)", SDL_GetError() );
213 free( p_vout->p_sys );
214 vlc_mutex_unlock( &sdl_lock );
218 vlc_mutex_unlock( &sdl_lock );
220 /* Translate keys into unicode */
221 SDL_EnableUNICODE(1);
223 /* Get the desktop resolution */
224 #if SDL_VERSION_ATLEAST(1,2,10)
225 /* FIXME: SDL has a problem with virtual desktop */
226 p_vout->p_sys->i_desktop_width = SDL_GetVideoInfo()->current_w;
227 p_vout->p_sys->i_desktop_height = SDL_GetVideoInfo()->current_h;
230 /* Create the cursor */
231 p_vout->p_sys->b_cursor = 1;
232 p_vout->p_sys->b_cursor_autohidden = 0;
233 p_vout->p_sys->i_lastmoved = p_vout->p_sys->i_lastpressed = mdate();
234 p_vout->p_sys->i_mouse_hide_timeout =
235 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
237 if( OpenDisplay( p_vout ) )
239 msg_Err( p_vout, "cannot set up SDL (%s)", SDL_GetError() );
240 SDL_QuitSubSystem( SDL_INIT_VIDEO );
241 free( p_vout->p_sys );
248 /*****************************************************************************
249 * Init: initialize SDL video thread output method
250 *****************************************************************************
251 * This function initialize the SDL display device.
252 *****************************************************************************/
253 static int Init( vout_thread_t *p_vout )
258 p_vout->p_sys->i_surfaces = 0;
260 I_OUTPUTPICTURES = 0;
262 /* Initialize the output structure */
263 if( p_vout->p_sys->p_overlay == NULL )
265 /* All we have is an RGB image with square pixels */
266 p_vout->output.i_width = p_vout->p_sys->i_width;
267 p_vout->output.i_height = p_vout->p_sys->i_height;
268 p_vout->output.i_aspect = p_vout->output.i_width
270 / p_vout->output.i_height;
274 /* We may need to convert the chroma, but at least we keep the
276 p_vout->output.i_width = p_vout->render.i_width;
277 p_vout->output.i_height = p_vout->render.i_height;
278 p_vout->output.i_aspect = p_vout->render.i_aspect;
281 /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
282 while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
286 /* Find an empty picture slot */
287 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
289 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
291 p_pic = p_vout->p_picture + i_index;
296 /* Allocate the picture if we found one */
297 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
302 p_pic->i_status = DESTROYED_PICTURE;
303 p_pic->i_type = DIRECT_PICTURE;
305 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
313 /*****************************************************************************
314 * End: terminate Sys video thread output method
315 *****************************************************************************
316 * Terminate an output method created by OpenVideo
317 *****************************************************************************/
318 static void End( vout_thread_t *p_vout )
322 /* Free the output buffers we allocated */
323 for( i_index = I_OUTPUTPICTURES ; i_index ; )
326 if( p_vout->p_sys->p_overlay == NULL )
332 SDL_UnlockYUVOverlay(
333 PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
335 PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
337 free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
341 /*****************************************************************************
342 * CloseVideo: destroy Sys video thread output method
343 *****************************************************************************
344 * Terminate an output method created by vout_SDLCreate
345 *****************************************************************************/
346 static void Close ( vlc_object_t *p_this )
348 vout_thread_t * p_vout = (vout_thread_t *)p_this;
350 CloseDisplay( p_vout );
351 SDL_QuitSubSystem( SDL_INIT_VIDEO );
353 free( p_vout->p_sys );
356 /*****************************************************************************
357 * Manage: handle Sys events
358 *****************************************************************************
359 * This function should be called regularly by video output thread. It returns
360 * a non null value if an error occurred.
361 *****************************************************************************/
362 static int Manage( vout_thread_t *p_vout )
364 SDL_Event event; /* SDL event */
366 unsigned int i_width, i_height, i_x, i_y;
369 while( SDL_PollEvent( &event ) )
373 /* Resizing of window */
374 case SDL_VIDEORESIZE:
375 p_vout->i_changes |= VOUT_SIZE_CHANGE;
376 p_vout->i_window_width = p_vout->p_sys->i_width = event.resize.w;
377 p_vout->i_window_height = p_vout->p_sys->i_height = event.resize.h;
381 case SDL_MOUSEMOTION:
382 vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
383 p_vout->p_sys->i_height,
384 &i_x, &i_y, &i_width, &i_height );
386 /* Compute the x coordinate and check if the value is
387 in [0,p_vout->fmt_in.i_visible_width] */
388 val.i_int = ( event.motion.x - i_x ) *
389 p_vout->fmt_in.i_visible_width / i_width +
390 p_vout->fmt_in.i_x_offset;
392 if( (int)(event.motion.x - i_x) < 0 )
394 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
395 val.i_int = p_vout->fmt_in.i_visible_width;
397 var_Set( p_vout, "mouse-x", val );
399 /* compute the y coordinate and check if the value is
400 in [0,p_vout->fmt_in.i_visible_height] */
401 val.i_int = ( event.motion.y - i_y ) *
402 p_vout->fmt_in.i_visible_height / i_height +
403 p_vout->fmt_in.i_y_offset;
405 if( (int)(event.motion.y - i_y) < 0 )
407 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
408 val.i_int = p_vout->fmt_in.i_visible_height;
410 var_Set( p_vout, "mouse-y", val );
411 var_SetBool( p_vout, "mouse-moved", true );
413 if( p_vout->p_sys->b_cursor )
415 if( p_vout->p_sys->b_cursor_autohidden )
417 p_vout->p_sys->b_cursor_autohidden = 0;
422 p_vout->p_sys->i_lastmoved = mdate();
427 /* Mouse button released */
428 case SDL_MOUSEBUTTONUP:
429 switch( event.button.button )
431 case SDL_BUTTON_LEFT:
433 var_Get( p_vout, "mouse-button-down", &val );
435 var_Set( p_vout, "mouse-button-down", val );
437 var_SetBool( p_vout, "mouse-clicked", true );
438 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", false );
442 case SDL_BUTTON_MIDDLE:
444 var_Get( p_vout, "mouse-button-down", &val );
446 var_Set( p_vout, "mouse-button-down", val );
448 var_Get( p_vout->p_libvlc, "intf-show", &val );
449 val.b_bool = !val.b_bool;
450 var_Set( p_vout->p_libvlc, "intf-show", val );
454 case SDL_BUTTON_RIGHT:
456 var_Get( p_vout, "mouse-button-down", &val );
458 var_Set( p_vout, "mouse-button-down", val );
460 var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
466 /* Mouse button pressed */
467 case SDL_MOUSEBUTTONDOWN:
468 switch( event.button.button )
470 case SDL_BUTTON_LEFT:
471 var_Get( p_vout, "mouse-button-down", &val );
473 var_Set( p_vout, "mouse-button-down", val );
475 /* detect double-clicks */
476 if( ( mdate() - p_vout->p_sys->i_lastpressed ) < 300000 )
477 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
479 p_vout->p_sys->i_lastpressed = mdate();
482 case SDL_BUTTON_MIDDLE:
483 var_Get( p_vout, "mouse-button-down", &val );
485 var_Set( p_vout, "mouse-button-down", val );
488 case SDL_BUTTON_RIGHT:
489 var_Get( p_vout, "mouse-button-down", &val );
491 var_Set( p_vout, "mouse-button-down", val );
496 /* Quit event (close the window) */
500 playlist_t *p_playlist = pl_Hold( p_vout );
501 if( p_playlist != NULL )
503 playlist_Stop( p_playlist );
504 pl_Release( p_vout );
507 #warning FIXME FIXME ?
514 /* convert the key if possible */
515 val.i_int = ConvertKey( event.key.keysym.sym );
519 /* Find the right caracter */
520 if( ( event.key.keysym.unicode & 0xff80 ) == 0 )
522 val.i_int = event.key.keysym.unicode & 0x7f;
523 /* FIXME: find a better solution than this
524 hack to find the right caracter */
525 if( val.i_int >= 1 && val.i_int <= 26 )
527 else if( val.i_int >= 65 && val.i_int <= 90 )
534 if( ( event.key.keysym.mod & KMOD_SHIFT ) )
536 val.i_int |= KEY_MODIFIER_SHIFT;
538 if( ( event.key.keysym.mod & KMOD_CTRL ) )
540 val.i_int |= KEY_MODIFIER_CTRL;
542 if( ( event.key.keysym.mod & KMOD_ALT ) )
544 val.i_int |= KEY_MODIFIER_ALT;
546 var_Set( p_vout->p_libvlc, "key-pressed", val );
554 /* Fullscreen change */
555 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
559 /* Update the object variable and trigger callback */
560 val_fs.b_bool = !p_vout->b_fullscreen;
561 p_vout->b_fullscreen = !p_vout->b_fullscreen;
562 var_Set( p_vout, "fullscreen", val_fs );
564 /*TODO: add the "always on top" code here !*/
566 p_vout->p_sys->b_cursor_autohidden = 0;
567 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
568 ! p_vout->p_sys->b_cursor_autohidden );
570 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
571 p_vout->i_changes |= VOUT_SIZE_CHANGE;
574 /* autoscale toggle */
575 if( p_vout->i_changes & VOUT_SCALE_CHANGE )
577 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
579 p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
580 p_vout->i_zoom = (int) ZOOM_FP_FACTOR;
582 p_vout->i_changes |= VOUT_SIZE_CHANGE;
585 /* scaling factor (if no-autoscale) */
586 if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
588 p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
590 p_vout->b_autoscale = false;
591 p_vout->i_zoom = (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
593 p_vout->i_changes |= VOUT_SIZE_CHANGE;
596 /* Crop or Aspect Ratio Changes */
597 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
598 p_vout->i_changes & VOUT_ASPECT_CHANGE )
600 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
601 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
603 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
604 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
605 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
606 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
607 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
608 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
609 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
610 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
612 p_vout->i_changes |= VOUT_SIZE_CHANGE;
616 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
618 msg_Dbg( p_vout, "video display resized (%dx%d)",
619 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
621 CloseDisplay( p_vout );
622 OpenDisplay( p_vout );
624 /* We don't need to signal the vout thread about the size change if
625 * we can handle rescaling ourselves */
626 if( p_vout->p_sys->p_overlay != NULL )
627 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
631 if( ! p_vout->p_sys->b_cursor_autohidden &&
632 ( mdate() - p_vout->p_sys->i_lastmoved >
633 p_vout->p_sys->i_mouse_hide_timeout ) )
635 /* Hide the mouse automatically */
636 p_vout->p_sys->b_cursor_autohidden = 1;
643 /*****************************************************************************
644 * Key events handling
645 *****************************************************************************/
650 } sdlkeys_to_vlckeys[] =
661 { SDLK_F10, KEY_F10 },
662 { SDLK_F11, KEY_F11 },
663 { SDLK_F12, KEY_F12 },
665 { SDLK_RETURN, KEY_ENTER },
666 { SDLK_KP_ENTER, KEY_ENTER },
667 { SDLK_SPACE, KEY_SPACE },
668 { SDLK_ESCAPE, KEY_ESC },
670 { SDLK_MENU, KEY_MENU },
671 { SDLK_LEFT, KEY_LEFT },
672 { SDLK_RIGHT, KEY_RIGHT },
674 { SDLK_DOWN, KEY_DOWN },
676 { SDLK_HOME, KEY_HOME },
677 { SDLK_END, KEY_END },
678 { SDLK_PAGEUP, KEY_PAGEUP },
679 { SDLK_PAGEDOWN, KEY_PAGEDOWN },
681 { SDLK_INSERT, KEY_INSERT },
682 { SDLK_DELETE, KEY_DELETE },
683 /*TODO: find a equivalent for SDL
684 { , KEY_MEDIA_NEXT_TRACK }
685 { , KEY_MEDIA_PREV_TRACK }
686 { , KEY_VOLUME_MUTE }
687 { , KEY_VOLUME_DOWN }
689 { , KEY_MEDIA_PLAY_PAUSE }
690 { , KEY_MEDIA_PLAY_PAUSE }*/
695 static int ConvertKey( SDLKey sdl_key )
698 for( i=0; sdlkeys_to_vlckeys[i].sdl_key != 0; i++ )
700 if( sdlkeys_to_vlckeys[i].sdl_key == sdl_key )
702 return sdlkeys_to_vlckeys[i].i_vlckey;
709 /*****************************************************************************
710 * Display: displays previously rendered output
711 *****************************************************************************
712 * This function sends the currently rendered image to the display.
713 *****************************************************************************/
714 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
716 unsigned int x, y, w, h;
719 vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
726 if( p_vout->p_sys->p_overlay == NULL )
729 SDL_Flip( p_vout->p_sys->p_display );
733 /* Overlay picture */
734 SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
735 SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
736 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
740 /* following functions are local */
742 /*****************************************************************************
743 * OpenDisplay: open and initialize SDL device
744 *****************************************************************************
745 * Open and initialize display according to preferences specified in the vout
747 *****************************************************************************/
748 static int OpenDisplay( vout_thread_t *p_vout )
753 /* SDL fucked up fourcc definitions on bigendian machines */
754 uint32_t i_sdl_chroma;
755 char *psz_chroma = NULL;
756 vlc_fourcc_t i_chroma = 0;
758 bool b_overlay = config_GetInt( p_vout, "overlay" );
760 /* Set main window's size */
761 #if SDL_VERSION_ATLEAST(1,2,10)
762 p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_width :
763 p_vout->i_window_width;
764 p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_height :
765 p_vout->i_window_height;
767 p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->output.i_width :
768 p_vout->i_window_width;
769 p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->output.i_height :
770 p_vout->i_window_height;
773 /* Initialize flags and cursor */
774 i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
775 i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
777 i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
778 SDL_DEFAULT_BPP, i_flags );
781 msg_Err( p_vout, "no video mode available" );
785 p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
786 p_vout->p_sys->i_height,
789 if( p_vout->p_sys->p_display == NULL )
791 msg_Err( p_vout, "cannot set video mode" );
795 SDL_LockSurface( p_vout->p_sys->p_display );
797 if( ( psz_chroma = config_GetPsz( p_vout, "sdl-chroma" ) ) )
799 i_chroma = vlc_fourcc_GetCodecFromString( VIDEO_ES, psz_chroma );
802 msg_Dbg( p_vout, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
813 /* Choose the chroma we will try first. */
816 if( !psz_chroma ) i_chroma = 0;
817 switch( i_chroma ? i_chroma : p_vout->render.i_chroma )
820 p_vout->output.i_chroma = VLC_CODEC_YUYV;
821 i_sdl_chroma = SDL_YUY2_OVERLAY;
824 p_vout->output.i_chroma = VLC_CODEC_UYVY;
825 i_sdl_chroma = SDL_UYVY_OVERLAY;
828 p_vout->output.i_chroma = VLC_CODEC_YVYU;
829 i_sdl_chroma = SDL_YVYU_OVERLAY;
834 p_vout->output.i_chroma = VLC_CODEC_YV12;
835 i_sdl_chroma = SDL_YV12_OVERLAY;
838 free( psz_chroma ); psz_chroma = NULL;
840 p_vout->p_sys->p_overlay =
841 SDL_CreateYUVOverlay( 32, 32, i_sdl_chroma,
842 p_vout->p_sys->p_display );
843 /* FIXME: if the first overlay we find is software, don't stop,
844 * because we may find a hardware one later ... */
846 while( i_chroma && !p_vout->p_sys->p_overlay );
849 /* If this best choice failed, fall back to other chromas */
850 if( p_vout->p_sys->p_overlay == NULL )
852 p_vout->output.i_chroma = VLC_CODEC_I420;
853 p_vout->p_sys->p_overlay =
854 SDL_CreateYUVOverlay( 32, 32, SDL_IYUV_OVERLAY,
855 p_vout->p_sys->p_display );
858 if( p_vout->p_sys->p_overlay == NULL )
860 p_vout->output.i_chroma = VLC_CODEC_YV12;
861 p_vout->p_sys->p_overlay =
862 SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
863 p_vout->p_sys->p_display );
866 if( p_vout->p_sys->p_overlay == NULL )
868 p_vout->output.i_chroma = VLC_CODEC_YUYV;
869 p_vout->p_sys->p_overlay =
870 SDL_CreateYUVOverlay( 32, 32, SDL_YUY2_OVERLAY,
871 p_vout->p_sys->p_display );
875 if( p_vout->p_sys->p_overlay == NULL )
878 msg_Warn( p_vout, "no SDL overlay for 0x%.8x (%4.4s)",
879 p_vout->render.i_chroma,
880 (char*)&p_vout->render.i_chroma );
882 msg_Warn( p_vout, "SDL overlay disabled by the user" );
884 switch( p_vout->p_sys->p_display->format->BitsPerPixel )
887 p_vout->output.i_chroma = VLC_CODEC_RGB8;
888 p_vout->output.pf_setpalette = SetPalette;
891 p_vout->output.i_chroma = VLC_CODEC_RGB15;
894 p_vout->output.i_chroma = VLC_CODEC_RGB16;
897 p_vout->output.i_chroma = VLC_CODEC_RGB24;
900 p_vout->output.i_chroma = VLC_CODEC_RGB32;
903 msg_Err( p_vout, "unknown screen depth %i",
904 p_vout->p_sys->p_display->format->BitsPerPixel );
905 SDL_UnlockSurface( p_vout->p_sys->p_display );
906 SDL_FreeSurface( p_vout->p_sys->p_display );
910 p_vout->output.i_rmask = p_vout->p_sys->p_display->format->Rmask;
911 p_vout->output.i_gmask = p_vout->p_sys->p_display->format->Gmask;
912 p_vout->output.i_bmask = p_vout->p_sys->p_display->format->Bmask;
914 SDL_WM_SetCaption( VOUT_TITLE " (software RGB SDL output)",
915 VOUT_TITLE " (software RGB SDL output)" );
919 if( p_vout->p_sys->p_overlay->hw_overlay )
921 SDL_WM_SetCaption( VOUT_TITLE " (hardware YUV SDL output)",
922 VOUT_TITLE " (hardware YUV SDL output)" );
926 SDL_WM_SetCaption( VOUT_TITLE " (software YUV SDL output)",
927 VOUT_TITLE " (software YUV SDL output)" );
931 SDL_EventState( SDL_KEYUP, SDL_IGNORE ); /* ignore keys up */
936 /*****************************************************************************
937 * CloseDisplay: close and reset SDL device
938 *****************************************************************************
939 * This function returns all resources allocated by OpenDisplay and restore
940 * the original state of the device.
941 *****************************************************************************/
942 static void CloseDisplay( vout_thread_t *p_vout )
944 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
945 SDL_UnlockSurface ( p_vout->p_sys->p_display );
946 SDL_FreeSurface( p_vout->p_sys->p_display );
949 /*****************************************************************************
950 * NewPicture: allocate a picture
951 *****************************************************************************
952 * Returns 0 on success, -1 otherwise
953 *****************************************************************************/
954 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
956 int i_width = p_vout->output.i_width;
957 int i_height = p_vout->output.i_height;
959 if( p_vout->p_sys->p_overlay == NULL )
962 if( p_vout->p_sys->i_surfaces )
964 /* We already allocated this surface, return */
968 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
970 if( p_pic->p_sys == NULL )
975 switch( p_vout->p_sys->p_display->format->BitsPerPixel )
978 p_pic->p->i_pixel_pitch = 1;
982 p_pic->p->i_pixel_pitch = 2;
986 p_pic->p->i_pixel_pitch = 4;
992 p_pic->p->p_pixels = p_vout->p_sys->p_display->pixels;
993 p_pic->p->i_lines = p_vout->p_sys->p_display->h;
994 p_pic->p->i_visible_lines = p_vout->p_sys->p_display->h;
995 p_pic->p->i_pitch = p_vout->p_sys->p_display->pitch;
996 p_pic->p->i_visible_pitch =
997 p_pic->p->i_pixel_pitch * p_vout->p_sys->p_display->w;
999 p_vout->p_sys->i_surfaces++;
1001 p_pic->i_planes = 1;
1005 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1007 if( p_pic->p_sys == NULL )
1012 p_pic->p_sys->p_overlay =
1013 SDL_CreateYUVOverlay( i_width, i_height,
1014 p_vout->output.i_chroma,
1015 p_vout->p_sys->p_display );
1017 if( p_pic->p_sys->p_overlay == NULL )
1019 free( p_pic->p_sys );
1020 return VLC_EGENERIC;
1023 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
1025 p_pic->Y_PIXELS = p_pic->p_sys->p_overlay->pixels[0];
1026 p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_overlay->h;
1027 p_pic->p[Y_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h;
1028 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[0];
1030 switch( p_vout->output.i_chroma )
1032 case SDL_YV12_OVERLAY:
1033 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1034 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1036 p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1037 p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1038 p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1039 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1040 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1041 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1043 p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1044 p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1045 p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1046 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1047 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1048 p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1050 p_pic->i_planes = 3;
1053 case SDL_IYUV_OVERLAY:
1054 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1055 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1057 p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1058 p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1059 p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1060 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1061 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1062 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1064 p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1065 p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1066 p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1067 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1068 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1069 p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1071 p_pic->i_planes = 3;
1075 p_pic->p[Y_PLANE].i_pixel_pitch = 2;
1076 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w * 2;
1078 p_pic->i_planes = 1;
1086 /*****************************************************************************
1087 * SetPalette: sets an 8 bpp palette
1088 *****************************************************************************/
1089 static void SetPalette( vout_thread_t *p_vout,
1090 uint16_t *red, uint16_t *green, uint16_t *blue )
1092 SDL_Color colors[256];
1095 /* Fill colors with color information */
1096 for( i = 0; i < 256; i++ )
1098 colors[ i ].r = red[ i ] >> 8;
1099 colors[ i ].g = green[ i ] >> 8;
1100 colors[ i ].b = blue[ i ] >> 8;
1104 if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
1106 msg_Err( p_vout, "failed to set palette" );