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 */
36 #include <vlc_interface.h>
37 #include <vlc_playlist.h>
40 //#include <vlc_aout.h>
42 #include <sys/types.h>
44 # include <netinet/in.h> /* BSD: struct in_addr */
47 #include SDL_INCLUDE_FILE
49 /* SDL is not able to crop overlays - so use only 1 direct buffer */
50 #define SDL_MAX_DIRECTBUFFERS 1
51 #define SDL_DEFAULT_BPP 16
53 /*****************************************************************************
54 * vout_sys_t: video output SDL method descriptor
55 *****************************************************************************
56 * This structure is part of the video output thread descriptor.
57 * It describes the SDL specific properties of an output thread.
58 *****************************************************************************/
61 SDL_Surface * p_display; /* display device */
66 #if SDL_VERSION_ATLEAST(1,2,10)
67 unsigned int i_desktop_width;
68 unsigned int i_desktop_height;
72 SDL_Overlay * p_overlay; /* An overlay we keep to grab the XVideo port */
78 vlc_bool_t b_cursor_autohidden;
80 mtime_t i_lastpressed; /* to track dbl-clicks */
83 /*****************************************************************************
84 * picture_sys_t: direct buffer method descriptor
85 *****************************************************************************
86 * This structure is part of the picture descriptor, it describes the
87 * SDL specific properties of a direct buffer.
88 *****************************************************************************/
91 SDL_Overlay *p_overlay;
94 /*****************************************************************************
96 *****************************************************************************/
97 static int Open ( vlc_object_t * );
98 static void Close ( vlc_object_t * );
99 static int Init ( vout_thread_t * );
100 static void End ( vout_thread_t * );
101 static int Manage ( vout_thread_t * );
102 static void Display ( vout_thread_t *, picture_t * );
104 static int OpenDisplay ( vout_thread_t * );
105 static void CloseDisplay ( vout_thread_t * );
106 static int NewPicture ( vout_thread_t *, picture_t * );
107 static void SetPalette ( vout_thread_t *,
108 uint16_t *, uint16_t *, uint16_t * );
110 static int ConvertKey( SDLKey );
113 #define CHROMA_TEXT N_("SDL chroma format")
114 #define CHROMA_LONGTEXT N_( \
115 "Force the SDL renderer to use a specific chroma format instead of " \
116 "trying to improve performances by using the most efficient one.")
118 /*****************************************************************************
120 *****************************************************************************/
122 set_shortname( "SDL" );
123 set_category( CAT_VIDEO );
124 set_subcategory( SUBCAT_VIDEO_VOUT );
125 set_description( _("Simple DirectMedia Layer video output") );
126 set_capability( "video output", 60 );
127 add_shortcut( "sdl" );
128 add_string( "sdl-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT, VLC_TRUE );
129 set_callbacks( Open, Close );
130 #if defined( __i386__ ) || defined( __x86_64__ )
131 /* On i386, SDL is linked against svgalib */
132 linked_with_a_crap_library_which_uses_atexit();
136 /*****************************************************************************
137 * OpenVideo: allocate SDL video thread output method
138 *****************************************************************************
139 * This function allocate and initialize a SDL vout method. It uses some of the
140 * vout properties to choose the correct mode, and change them according to the
141 * mode actually used.
142 *****************************************************************************/
143 static int Open ( vlc_object_t *p_this )
145 vout_thread_t * p_vout = (vout_thread_t *)p_this;
146 /* XXX: check for conflicts with the SDL audio output */
147 vlc_mutex_t *lock = var_AcquireMutex( "sdl" );
156 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
157 if( p_vout->p_sys == NULL )
159 vlc_mutex_unlock( lock );
163 /* Check if SDL video module has been initialized */
164 if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
166 vlc_mutex_unlock( lock );
167 free( p_vout->p_sys );
171 /* Allocate structure */
172 p_vout->pf_init = Init;
173 p_vout->pf_end = End;
174 p_vout->pf_manage = Manage;
175 p_vout->pf_render = NULL;
176 p_vout->pf_display = Display;
177 p_vout->pf_control = NULL;
180 psz_method = config_GetPsz( p_vout, "vout" );
183 while( *psz_method && *psz_method != ':' )
190 setenv( "SDL_VIDEODRIVER", psz_method + 1, 1 );
195 /* Initialize library */
196 if( SDL_Init( SDL_INIT_VIDEO
198 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
199 | SDL_INIT_EVENTTHREAD
202 /* In debug mode you may want vlc to dump a core instead of staying
204 | SDL_INIT_NOPARACHUTE
208 msg_Err( p_vout, "cannot initialize SDL (%s)", SDL_GetError() );
209 free( p_vout->p_sys );
210 vlc_mutex_unlock( lock );
214 vlc_mutex_unlock( lock );
216 /* Translate keys into unicode */
217 SDL_EnableUNICODE(1);
219 /* Get the desktop resolution */
220 #if SDL_VERSION_ATLEAST(1,2,10)
221 /* FIXME: SDL has a problem with virtual desktop */
222 p_vout->p_sys->i_desktop_width = SDL_GetVideoInfo()->current_w;
223 p_vout->p_sys->i_desktop_height = SDL_GetVideoInfo()->current_h;
226 /* Create the cursor */
227 p_vout->p_sys->b_cursor = 1;
228 p_vout->p_sys->b_cursor_autohidden = 0;
229 p_vout->p_sys->i_lastmoved = p_vout->p_sys->i_lastpressed = mdate();
231 if( OpenDisplay( p_vout ) )
233 msg_Err( p_vout, "cannot set up SDL (%s)", SDL_GetError() );
234 SDL_QuitSubSystem( SDL_INIT_VIDEO );
235 free( p_vout->p_sys );
242 /*****************************************************************************
243 * Init: initialize SDL video thread output method
244 *****************************************************************************
245 * This function initialize the SDL display device.
246 *****************************************************************************/
247 static int Init( vout_thread_t *p_vout )
252 p_vout->p_sys->i_surfaces = 0;
254 I_OUTPUTPICTURES = 0;
256 /* Initialize the output structure */
257 if( p_vout->p_sys->p_overlay == NULL )
259 /* All we have is an RGB image with square pixels */
260 p_vout->output.i_width = p_vout->p_sys->i_width;
261 p_vout->output.i_height = p_vout->p_sys->i_height;
262 p_vout->output.i_aspect = p_vout->output.i_width
264 / p_vout->output.i_height;
268 /* We may need to convert the chroma, but at least we keep the
270 p_vout->output.i_width = p_vout->render.i_width;
271 p_vout->output.i_height = p_vout->render.i_height;
272 p_vout->output.i_aspect = p_vout->render.i_aspect;
275 /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
276 while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
280 /* Find an empty picture slot */
281 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
283 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
285 p_pic = p_vout->p_picture + i_index;
290 /* Allocate the picture if we found one */
291 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
296 p_pic->i_status = DESTROYED_PICTURE;
297 p_pic->i_type = DIRECT_PICTURE;
299 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
307 /*****************************************************************************
308 * End: terminate Sys video thread output method
309 *****************************************************************************
310 * Terminate an output method created by OpenVideo
311 *****************************************************************************/
312 static void End( vout_thread_t *p_vout )
316 /* Free the output buffers we allocated */
317 for( i_index = I_OUTPUTPICTURES ; i_index ; )
320 if( p_vout->p_sys->p_overlay == NULL )
326 SDL_UnlockYUVOverlay(
327 PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
329 PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
331 free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
335 /*****************************************************************************
336 * CloseVideo: destroy Sys video thread output method
337 *****************************************************************************
338 * Terminate an output method created by vout_SDLCreate
339 *****************************************************************************/
340 static void Close ( vlc_object_t *p_this )
342 vout_thread_t * p_vout = (vout_thread_t *)p_this;
344 CloseDisplay( p_vout );
345 SDL_QuitSubSystem( SDL_INIT_VIDEO );
347 free( p_vout->p_sys );
350 /*****************************************************************************
351 * Manage: handle Sys events
352 *****************************************************************************
353 * This function should be called regularly by video output thread. It returns
354 * a non null value if an error occurred.
355 *****************************************************************************/
356 static int Manage( vout_thread_t *p_vout )
358 SDL_Event event; /* SDL event */
360 unsigned int i_width, i_height, i_x, i_y;
363 while( SDL_PollEvent( &event ) )
367 /* Resizing of window */
368 case SDL_VIDEORESIZE:
369 p_vout->i_changes |= VOUT_SIZE_CHANGE;
370 p_vout->i_window_width = p_vout->p_sys->i_width = event.resize.w;
371 p_vout->i_window_height = p_vout->p_sys->i_height = event.resize.h;
375 case SDL_MOUSEMOTION:
376 vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
377 p_vout->p_sys->i_height,
378 &i_x, &i_y, &i_width, &i_height );
380 /* Compute the x coordinate and check if the value is
381 in [0,p_vout->fmt_in.i_visible_width] */
382 val.i_int = ( event.motion.x - i_x ) *
383 p_vout->fmt_in.i_visible_width / i_width +
384 p_vout->fmt_in.i_x_offset;
386 if( (int)(event.motion.x - i_x) < 0 )
388 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
389 val.i_int = p_vout->fmt_in.i_visible_width;
391 var_Set( p_vout, "mouse-x", val );
393 /* compute the y coordinate and check if the value is
394 in [0,p_vout->fmt_in.i_visible_height] */
395 val.i_int = ( event.motion.y - i_y ) *
396 p_vout->fmt_in.i_visible_height / i_height +
397 p_vout->fmt_in.i_y_offset;
399 if( (int)(event.motion.y - i_y) < 0 )
401 else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
402 val.i_int = p_vout->fmt_in.i_visible_height;
404 var_Set( p_vout, "mouse-y", val );
406 val.b_bool = VLC_TRUE;
407 var_Set( p_vout, "mouse-moved", val );
409 if( p_vout->p_sys->b_cursor )
411 if( p_vout->p_sys->b_cursor_autohidden )
413 p_vout->p_sys->b_cursor_autohidden = 0;
418 p_vout->p_sys->i_lastmoved = mdate();
423 /* Mouse button released */
424 case SDL_MOUSEBUTTONUP:
425 switch( event.button.button )
427 case SDL_BUTTON_LEFT:
428 var_Get( p_vout, "mouse-button-down", &val );
430 var_Set( p_vout, "mouse-button-down", val );
432 val.b_bool = VLC_TRUE;
433 var_Set( p_vout, "mouse-clicked", val );
436 case SDL_BUTTON_MIDDLE:
438 playlist_t *p_playlist;
440 var_Get( p_vout, "mouse-button-down", &val );
442 var_Set( p_vout, "mouse-button-down", val );
444 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
446 if( p_playlist != NULL )
449 var_Get( p_playlist, "intf-show", &val );
450 val.b_bool = !val.b_bool;
451 var_Set( p_playlist, "intf-show", val );
452 vlc_object_release( p_playlist );
457 case SDL_BUTTON_RIGHT:
459 intf_thread_t *p_intf;
460 playlist_t *p_playlist;
462 var_Get( p_vout, "mouse-button-down", &val );
464 var_Set( p_vout, "mous-button-down", val );
465 p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
469 p_intf->b_menu_change = 1;
470 vlc_object_release( p_intf );
473 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
476 if( p_playlist != NULL )
479 val.b_bool = VLC_TRUE;
480 var_Set( p_playlist, "intf-popupmenu", val );
481 vlc_object_release( p_playlist );
488 /* Mouse button pressed */
489 case SDL_MOUSEBUTTONDOWN:
490 switch( event.button.button )
492 case SDL_BUTTON_LEFT:
493 var_Get( p_vout, "mouse-button-down", &val );
495 var_Set( p_vout, "mouse-button-down", val );
497 /* detect double-clicks */
498 if( ( mdate() - p_vout->p_sys->i_lastpressed ) < 300000 )
499 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
501 p_vout->p_sys->i_lastpressed = mdate();
504 case SDL_BUTTON_MIDDLE:
505 var_Get( p_vout, "mouse-button-down", &val );
507 var_Set( p_vout, "mouse-button-down", val );
510 case SDL_BUTTON_RIGHT:
511 var_Get( p_vout, "mouse-button-down", &val );
513 var_Set( p_vout, "mouse-button-down", val );
518 /* Quit event (close the window) */
521 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
522 if( p_playlist != NULL )
524 playlist_Stop( p_playlist );
525 vlc_object_release( p_playlist );
532 /* convert the key if possible */
533 val.i_int = ConvertKey( event.key.keysym.sym );
537 /* Find the right caracter */
538 if( ( event.key.keysym.unicode & 0xff80 ) == 0 )
540 val.i_int = event.key.keysym.unicode & 0x7f;
541 /* FIXME: find a better solution than this
542 hack to find the right caracter */
543 if( val.i_int >= 1 && val.i_int <= 26 )
545 else if( val.i_int >= 65 && val.i_int <= 90 )
552 if( ( event.key.keysym.mod & KMOD_SHIFT ) )
554 val.i_int |= KEY_MODIFIER_SHIFT;
556 if( ( event.key.keysym.mod & KMOD_CTRL ) )
558 val.i_int |= KEY_MODIFIER_CTRL;
560 if( ( event.key.keysym.mod & KMOD_ALT ) )
562 val.i_int |= KEY_MODIFIER_ALT;
564 var_Set( p_vout->p_libvlc, "key-pressed", val );
572 /* Fullscreen change */
573 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
577 /* Update the object variable and trigger callback */
578 val_fs.b_bool = !p_vout->b_fullscreen;
579 p_vout->b_fullscreen = !p_vout->b_fullscreen;
580 var_Set( p_vout, "fullscreen", val_fs );
582 /*TODO: add the "always on top" code here !*/
584 p_vout->p_sys->b_cursor_autohidden = 0;
585 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
586 ! p_vout->p_sys->b_cursor_autohidden );
588 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
589 p_vout->i_changes |= VOUT_SIZE_CHANGE;
592 /* Crop or Aspect Ratio Changes */
593 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
594 p_vout->i_changes & VOUT_ASPECT_CHANGE )
596 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
597 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
599 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
600 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
601 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
602 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
603 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
604 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
605 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
606 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
608 p_vout->i_changes |= VOUT_SIZE_CHANGE;
612 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
614 msg_Dbg( p_vout, "video display resized (%dx%d)",
615 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
617 CloseDisplay( p_vout );
618 OpenDisplay( p_vout );
620 /* We don't need to signal the vout thread about the size change if
621 * we can handle rescaling ourselves */
622 if( p_vout->p_sys->p_overlay != NULL )
623 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
627 if( ! p_vout->p_sys->b_cursor_autohidden &&
628 ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
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 /* Set main window's size */
754 #if SDL_VERSION_ATLEAST(1,2,10)
755 p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_width :
756 p_vout->i_window_width;
757 p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_height :
758 p_vout->i_window_height;
760 p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->output.i_width :
761 p_vout->i_window_width;
762 p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->output.i_height :
763 p_vout->i_window_height;
766 /* Initialize flags and cursor */
767 i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
768 i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
770 i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
771 SDL_DEFAULT_BPP, i_flags );
774 msg_Err( p_vout, "no video mode available" );
778 p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
779 p_vout->p_sys->i_height,
782 if( p_vout->p_sys->p_display == NULL )
784 msg_Err( p_vout, "cannot set video mode" );
788 SDL_LockSurface( p_vout->p_sys->p_display );
790 if( ( psz_chroma = config_GetPsz( p_vout, "sdl-chroma" ) ) )
792 if( strlen( psz_chroma ) >= 4 )
794 memcpy(&i_chroma, psz_chroma, 4);
795 msg_Dbg( p_vout, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
804 /* Choose the chroma we will try first. */
807 if( !psz_chroma ) i_chroma = 0;
808 switch( i_chroma ? i_chroma : p_vout->render.i_chroma )
810 case VLC_FOURCC('Y','U','Y','2'):
811 case VLC_FOURCC('Y','U','N','V'):
812 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
813 i_sdl_chroma = SDL_YUY2_OVERLAY;
815 case VLC_FOURCC('U','Y','V','Y'):
816 case VLC_FOURCC('U','Y','N','V'):
817 case VLC_FOURCC('Y','4','2','2'):
818 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
819 i_sdl_chroma = SDL_UYVY_OVERLAY;
821 case VLC_FOURCC('Y','V','Y','U'):
822 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
823 i_sdl_chroma = SDL_YVYU_OVERLAY;
825 case VLC_FOURCC('Y','V','1','2'):
826 case VLC_FOURCC('I','4','2','0'):
827 case VLC_FOURCC('I','Y','U','V'):
829 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
830 i_sdl_chroma = SDL_YV12_OVERLAY;
833 free( psz_chroma ); psz_chroma = NULL;
835 p_vout->p_sys->p_overlay =
836 SDL_CreateYUVOverlay( 32, 32, i_sdl_chroma,
837 p_vout->p_sys->p_display );
838 /* FIXME: if the first overlay we find is software, don't stop,
839 * because we may find a hardware one later ... */
841 while( i_chroma && !p_vout->p_sys->p_overlay );
844 /* If this best choice failed, fall back to other chromas */
845 if( p_vout->p_sys->p_overlay == NULL )
847 p_vout->output.i_chroma = VLC_FOURCC('I','Y','U','V');
848 p_vout->p_sys->p_overlay =
849 SDL_CreateYUVOverlay( 32, 32, SDL_IYUV_OVERLAY,
850 p_vout->p_sys->p_display );
853 if( p_vout->p_sys->p_overlay == NULL )
855 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
856 p_vout->p_sys->p_overlay =
857 SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
858 p_vout->p_sys->p_display );
861 if( p_vout->p_sys->p_overlay == NULL )
863 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
864 p_vout->p_sys->p_overlay =
865 SDL_CreateYUVOverlay( 32, 32, SDL_YUY2_OVERLAY,
866 p_vout->p_sys->p_display );
869 if( p_vout->p_sys->p_overlay == NULL )
871 msg_Warn( p_vout, "no SDL overlay for 0x%.8x (%4.4s)",
872 p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma );
874 switch( p_vout->p_sys->p_display->format->BitsPerPixel )
877 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
878 p_vout->output.pf_setpalette = SetPalette;
881 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
884 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
887 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
890 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
893 msg_Err( p_vout, "unknown screen depth %i",
894 p_vout->p_sys->p_display->format->BitsPerPixel );
895 SDL_UnlockSurface( p_vout->p_sys->p_display );
896 SDL_FreeSurface( p_vout->p_sys->p_display );
900 p_vout->output.i_rmask = p_vout->p_sys->p_display->format->Rmask;
901 p_vout->output.i_gmask = p_vout->p_sys->p_display->format->Gmask;
902 p_vout->output.i_bmask = p_vout->p_sys->p_display->format->Bmask;
904 SDL_WM_SetCaption( VOUT_TITLE " (software RGB SDL output)",
905 VOUT_TITLE " (software RGB SDL output)" );
909 if( p_vout->p_sys->p_overlay->hw_overlay )
911 SDL_WM_SetCaption( VOUT_TITLE " (hardware YUV SDL output)",
912 VOUT_TITLE " (hardware YUV SDL output)" );
916 SDL_WM_SetCaption( VOUT_TITLE " (software YUV SDL output)",
917 VOUT_TITLE " (software YUV SDL output)" );
921 SDL_EventState( SDL_KEYUP, SDL_IGNORE ); /* ignore keys up */
926 /*****************************************************************************
927 * CloseDisplay: close and reset SDL device
928 *****************************************************************************
929 * This function returns all resources allocated by OpenDisplay and restore
930 * the original state of the device.
931 *****************************************************************************/
932 static void CloseDisplay( vout_thread_t *p_vout )
934 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
935 SDL_UnlockSurface ( p_vout->p_sys->p_display );
936 SDL_FreeSurface( p_vout->p_sys->p_display );
939 /*****************************************************************************
940 * NewPicture: allocate a picture
941 *****************************************************************************
942 * Returns 0 on success, -1 otherwise
943 *****************************************************************************/
944 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
946 int i_width = p_vout->output.i_width;
947 int i_height = p_vout->output.i_height;
949 if( p_vout->p_sys->p_overlay == NULL )
952 if( p_vout->p_sys->i_surfaces )
954 /* We already allocated this surface, return */
958 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
960 if( p_pic->p_sys == NULL )
965 switch( p_vout->p_sys->p_display->format->BitsPerPixel )
968 p_pic->p->i_pixel_pitch = 1;
972 p_pic->p->i_pixel_pitch = 2;
976 p_pic->p->i_pixel_pitch = 4;
982 p_pic->p->p_pixels = p_vout->p_sys->p_display->pixels;
983 p_pic->p->i_lines = p_vout->p_sys->p_display->h;
984 p_pic->p->i_visible_lines = p_vout->p_sys->p_display->h;
985 p_pic->p->i_pitch = p_vout->p_sys->p_display->pitch;
986 p_pic->p->i_visible_pitch =
987 p_pic->p->i_pixel_pitch * p_vout->p_sys->p_display->w;
989 p_vout->p_sys->i_surfaces++;
995 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
997 if( p_pic->p_sys == NULL )
1002 p_pic->p_sys->p_overlay =
1003 SDL_CreateYUVOverlay( i_width, i_height,
1004 p_vout->output.i_chroma,
1005 p_vout->p_sys->p_display );
1007 if( p_pic->p_sys->p_overlay == NULL )
1009 free( p_pic->p_sys );
1010 return VLC_EGENERIC;
1013 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
1015 p_pic->Y_PIXELS = p_pic->p_sys->p_overlay->pixels[0];
1016 p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_overlay->h;
1017 p_pic->p[Y_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h;
1018 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[0];
1020 switch( p_vout->output.i_chroma )
1022 case SDL_YV12_OVERLAY:
1023 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1024 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1026 p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1027 p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1028 p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1029 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1030 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1031 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1033 p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1034 p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1035 p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1036 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1037 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1038 p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1040 p_pic->i_planes = 3;
1043 case SDL_IYUV_OVERLAY:
1044 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1045 p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1047 p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1048 p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1049 p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1050 p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1051 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1052 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1054 p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1055 p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1056 p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1057 p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1058 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1059 p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1061 p_pic->i_planes = 3;
1065 p_pic->p[Y_PLANE].i_pixel_pitch = 2;
1066 p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w * 2;
1068 p_pic->i_planes = 1;
1076 /*****************************************************************************
1077 * SetPalette: sets an 8 bpp palette
1078 *****************************************************************************/
1079 static void SetPalette( vout_thread_t *p_vout,
1080 uint16_t *red, uint16_t *green, uint16_t *blue )
1082 SDL_Color colors[256];
1085 /* Fill colors with color information */
1086 for( i = 0; i < 256; i++ )
1088 colors[ i ].r = red[ i ] >> 8;
1089 colors[ i ].g = green[ i ] >> 8;
1090 colors[ i ].b = blue[ i ] >> 8;
1094 if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
1096 msg_Err( p_vout, "failed to set palette" );