]> git.sesse.net Git - vlc/blob - modules/video_output/sdl.c
* ./modules/video_output/sdl.c: avoid crashes at exit due to SDL being
[vlc] / modules / video_output / sdl.c
1 /*****************************************************************************
2  * sdl.c: SDL video output display method
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: sdl.c,v 1.12 2003/05/04 12:40:58 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Pierre Baillet <oct@zoy.org>
9  *          Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
10  *
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.
15  *
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.
20  *
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdlib.h>                                                /* free() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/intf.h>
35 #include <vlc/vout.h>
36 #include <vlc/aout.h>
37
38 #include <sys/types.h>
39 #ifndef WIN32
40 #   include <netinet/in.h>                            /* BSD: struct in_addr */
41 #endif
42
43 #include SDL_INCLUDE_FILE
44
45 #include "netutils.h"
46
47 #define SDL_MAX_DIRECTBUFFERS 10
48 #define SDL_DEFAULT_BPP 16
49
50 /*****************************************************************************
51  * vout_sys_t: video output SDL method descriptor
52  *****************************************************************************
53  * This structure is part of the video output thread descriptor.
54  * It describes the SDL specific properties of an output thread.
55  *****************************************************************************/
56 struct vout_sys_t
57 {
58     SDL_Surface *   p_display;                             /* display device */
59
60     int i_width;
61     int i_height;
62
63     /* For YUV output */
64     SDL_Overlay * p_overlay;   /* An overlay we keep to grab the XVideo port */
65
66     /* For RGB output */
67     int i_surfaces;
68
69     vlc_bool_t  b_cursor;
70     vlc_bool_t  b_cursor_autohidden;
71     mtime_t     i_lastmoved;
72     mtime_t     i_lastpressed;                        /* to track dbl-clicks */
73 };
74
75 /*****************************************************************************
76  * picture_sys_t: direct buffer method descriptor
77  *****************************************************************************
78  * This structure is part of the picture descriptor, it describes the
79  * SDL specific properties of a direct buffer.
80  *****************************************************************************/
81 struct picture_sys_t
82 {
83     SDL_Overlay *p_overlay;
84 };
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89 static int  Open      ( vlc_object_t * );
90 static void Close     ( vlc_object_t * );
91 static int  Init      ( vout_thread_t * );
92 static void End       ( vout_thread_t * );
93 static int  Manage    ( vout_thread_t * );
94 static void Display   ( vout_thread_t *, picture_t * );
95
96 static int  OpenDisplay     ( vout_thread_t * );
97 static void CloseDisplay    ( vout_thread_t * );
98 static int  NewPicture      ( vout_thread_t *, picture_t * );
99 static void SetPalette      ( vout_thread_t *,
100                               uint16_t *, uint16_t *, uint16_t * );
101
102 /*****************************************************************************
103  * Module descriptor
104  *****************************************************************************/
105 vlc_module_begin();
106     set_description( _("Simple DirectMedia Layer video output") );
107     set_capability( "video output", 60 );
108     add_shortcut( "sdl" );
109     set_callbacks( Open, Close );
110     /* XXX: check for conflicts with the SDL audio output */
111     var_Create( p_module->p_libvlc, "sdl", VLC_VAR_MUTEX );
112 #if defined( __i386__ )
113     /* On i386, SDL is linked against svgalib */
114     linked_with_a_crap_library_which_uses_atexit();
115 #endif
116 vlc_module_end();
117
118 /*****************************************************************************
119  * OpenVideo: allocate SDL video thread output method
120  *****************************************************************************
121  * This function allocate and initialize a SDL vout method. It uses some of the
122  * vout properties to choose the correct mode, and change them according to the
123  * mode actually used.
124  *****************************************************************************/
125 static int Open ( vlc_object_t *p_this )
126 {
127     vout_thread_t * p_vout = (vout_thread_t *)p_this;
128     vlc_value_t lockval;
129
130 #ifdef HAVE_SETENV
131     char *psz_method;
132 #endif
133
134     var_Get( p_this->p_libvlc, "sdl", &lockval );
135     vlc_mutex_lock( lockval.p_address );
136
137     if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
138     {
139         vlc_mutex_unlock( lockval.p_address );
140         return VLC_EGENERIC;
141     }
142
143     /* Allocate structure */
144     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
145     if( p_vout->p_sys == NULL )
146     {
147         msg_Err( p_vout, "out of memory" );
148         vlc_mutex_unlock( lockval.p_address );
149         return VLC_ENOMEM;
150     }
151
152     p_vout->pf_init = Init;
153     p_vout->pf_end = End;
154     p_vout->pf_manage = Manage;
155     p_vout->pf_render = NULL;
156     p_vout->pf_display = Display;
157
158 #ifdef HAVE_SETENV
159     psz_method = config_GetPsz( p_vout, "vout" );
160     if( psz_method )
161     {
162         while( *psz_method && *psz_method != ':' )
163         {
164             psz_method++;
165         }
166
167         if( *psz_method )
168         {
169             setenv( "SDL_VIDEODRIVER", psz_method + 1, 1 );
170         }
171     }
172 #endif
173
174     /* Initialize library */
175     if( SDL_Init( SDL_INIT_VIDEO
176 #ifndef WIN32
177     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
178                 | SDL_INIT_EVENTTHREAD
179 #endif
180 #ifdef DEBUG
181     /* In debug mode you may want vlc to dump a core instead of staying
182      * stuck */
183                 | SDL_INIT_NOPARACHUTE
184 #endif
185                 ) < 0 )
186     {
187         msg_Err( p_vout, "cannot initialize SDL (%s)", SDL_GetError() );
188         free( p_vout->p_sys );
189         vlc_mutex_unlock( lockval.p_address );
190         return VLC_EGENERIC;
191     }
192
193     vlc_mutex_unlock( lockval.p_address );
194
195     p_vout->p_sys->b_cursor = 1;
196     p_vout->p_sys->b_cursor_autohidden = 0;
197     p_vout->p_sys->i_lastmoved = mdate();
198
199     if( OpenDisplay( p_vout ) )
200     {
201         msg_Err( p_vout, "cannot set up SDL (%s)", SDL_GetError() );
202         SDL_QuitSubSystem( SDL_INIT_VIDEO );
203         free( p_vout->p_sys );
204         return VLC_EGENERIC;
205     }
206
207     return VLC_SUCCESS;
208 }
209
210 /*****************************************************************************
211  * Init: initialize SDL video thread output method
212  *****************************************************************************
213  * This function initialize the SDL display device.
214  *****************************************************************************/
215 static int Init( vout_thread_t *p_vout )
216 {
217     int i_index;
218     picture_t *p_pic;
219
220     p_vout->p_sys->i_surfaces = 0;
221
222     I_OUTPUTPICTURES = 0;
223
224     /* Initialize the output structure */
225     if( p_vout->p_sys->p_overlay == NULL )
226     {
227         /* All we have is an RGB image with square pixels */
228         p_vout->output.i_width  = p_vout->p_sys->i_width;
229         p_vout->output.i_height = p_vout->p_sys->i_height;
230         p_vout->output.i_aspect = p_vout->output.i_width
231                                    * VOUT_ASPECT_FACTOR
232                                    / p_vout->output.i_height;
233     }
234     else
235     {
236         /* We may need to convert the chroma, but at least we keep the
237          * aspect ratio */
238         p_vout->output.i_width  = p_vout->render.i_width;
239         p_vout->output.i_height = p_vout->render.i_height;
240         p_vout->output.i_aspect = p_vout->render.i_aspect;
241     }
242
243     /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
244     while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
245     {
246         p_pic = NULL;
247
248         /* Find an empty picture slot */
249         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
250         {
251             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
252             {
253                 p_pic = p_vout->p_picture + i_index;
254                 break;
255             }
256         }
257
258         /* Allocate the picture if we found one */
259         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
260         {
261             break;
262         }
263
264         p_pic->i_status = DESTROYED_PICTURE;
265         p_pic->i_type   = DIRECT_PICTURE;
266
267         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
268
269         I_OUTPUTPICTURES++;
270     }
271
272     return VLC_SUCCESS;
273 }
274
275 /*****************************************************************************
276  * End: terminate Sys video thread output method
277  *****************************************************************************
278  * Terminate an output method created by OpenVideo
279  *****************************************************************************/
280 static void End( vout_thread_t *p_vout )
281 {
282     int i_index;
283
284     /* Free the output buffers we allocated */
285     for( i_index = I_OUTPUTPICTURES ; i_index ; )
286     {
287         i_index--;
288         if( p_vout->p_sys->p_overlay == NULL )
289         {
290             /* RGB picture */
291         }
292         else
293         {
294             SDL_UnlockYUVOverlay(
295                     PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
296             SDL_FreeYUVOverlay(
297                     PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
298         }
299         free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
300     }
301 }
302
303 /*****************************************************************************
304  * CloseVideo: destroy Sys video thread output method
305  *****************************************************************************
306  * Terminate an output method created by vout_SDLCreate
307  *****************************************************************************/
308 static void Close ( vlc_object_t *p_this )
309 {
310     vout_thread_t * p_vout = (vout_thread_t *)p_this;
311
312     CloseDisplay( p_vout );
313     SDL_QuitSubSystem( SDL_INIT_VIDEO );
314
315     free( p_vout->p_sys );
316 }
317
318 /*****************************************************************************
319  * Manage: handle Sys events
320  *****************************************************************************
321  * This function should be called regularly by video output thread. It returns
322  * a non null value if an error occured.
323  *****************************************************************************/
324 static int Manage( vout_thread_t *p_vout )
325 {
326     SDL_Event event;                                            /* SDL event */
327     vlc_value_t val;
328     int i_width, i_height, i_x, i_y;
329
330     /* Process events */
331     while( SDL_PollEvent(&event) )
332     {
333         switch( event.type )
334         {
335         case SDL_VIDEORESIZE:                          /* Resizing of window */
336             /* Update dimensions */
337             p_vout->i_changes |= VOUT_SIZE_CHANGE;
338             p_vout->i_window_width = p_vout->p_sys->i_width = event.resize.w;
339             p_vout->i_window_height = p_vout->p_sys->i_height = event.resize.h;
340             break;
341
342         case SDL_MOUSEMOTION:
343             vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
344                                p_vout->p_sys->i_height,
345                                &i_x, &i_y, &i_width, &i_height );
346
347             val.i_int = ( event.motion.x - i_x )
348                          * p_vout->render.i_width / i_width;
349             var_Set( p_vout, "mouse-x", val );
350             val.i_int = ( event.motion.y - i_y )
351                          * p_vout->render.i_height / i_height;
352             var_Set( p_vout, "mouse-y", val );
353
354             val.b_bool = VLC_TRUE;
355             var_Set( p_vout, "mouse-moved", val );
356
357             if( p_vout->p_sys->b_cursor &&
358                 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
359             {
360                 if( p_vout->p_sys->b_cursor_autohidden )
361                 {
362                     p_vout->p_sys->b_cursor_autohidden = 0;
363                     SDL_ShowCursor( 1 );
364                 }
365                 else
366                 {
367                     p_vout->p_sys->i_lastmoved = mdate();
368                 }
369             }
370             break;
371
372         case SDL_MOUSEBUTTONUP:
373             switch( event.button.button )
374             {
375             case SDL_BUTTON_LEFT:
376                 val.b_bool = VLC_TRUE;
377                 var_Set( p_vout, "mouse-clicked", val );
378                 break;
379
380             case SDL_BUTTON_RIGHT:
381                 {
382                     intf_thread_t *p_intf;
383                     p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
384                                                       FIND_ANYWHERE );
385                     if( p_intf )
386                     {
387                         p_intf->b_menu_change = 1;
388                         vlc_object_release( p_intf );
389                     }
390                 }
391                 break;
392             }
393             break;
394
395         case SDL_MOUSEBUTTONDOWN:
396             switch( event.button.button )
397             {
398             case SDL_BUTTON_LEFT:
399                 /* In this part we will eventually manage
400                  * clicks for DVD navigation for instance. */
401
402                 /* detect double-clicks */
403                 if( ( mdate() - p_vout->p_sys->i_lastpressed ) < 300000 )
404                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
405
406                 p_vout->p_sys->i_lastpressed = mdate();
407                 break;
408
409             case 4:
410                 input_Seek( p_vout, 15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
411                 break;
412
413             case 5:
414                 input_Seek( p_vout, -15, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
415                 break;
416             }
417             break;
418
419         case SDL_QUIT:
420             p_vout->p_vlc->b_die = 1;
421             break;
422
423         case SDL_KEYDOWN:                             /* if a key is pressed */
424
425             switch( event.key.keysym.sym )
426             {
427             case SDLK_ESCAPE:
428                 if( p_vout->b_fullscreen )
429                 {
430                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
431                 }
432                 else
433                 {
434                     p_vout->p_vlc->b_die = 1;
435                 }
436                 break;
437
438             case SDLK_q:                                             /* quit */
439                 p_vout->p_vlc->b_die = 1;
440                 break;
441
442             case SDLK_f:                             /* switch to fullscreen */
443                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
444                 break;
445
446             case SDLK_c:                                 /* toggle grayscale */
447                 p_vout->b_grayscale = ! p_vout->b_grayscale;
448                 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
449                 break;
450
451             case SDLK_i:                                      /* toggle info */
452                 p_vout->b_info = ! p_vout->b_info;
453                 p_vout->i_changes |= VOUT_INFO_CHANGE;
454                 break;
455
456             case SDLK_s:                                   /* toggle scaling */
457                 p_vout->b_scale = ! p_vout->b_scale;
458                 p_vout->i_changes |= VOUT_SCALE_CHANGE;
459                 break;
460
461             case SDLK_SPACE:                             /* toggle interface */
462                 p_vout->b_interface = ! p_vout->b_interface;
463                 p_vout->i_changes |= VOUT_INTF_CHANGE;
464                 break;
465
466             case SDLK_MENU:
467                 {
468                     intf_thread_t *p_intf;
469                     p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
470                                                       FIND_ANYWHERE );
471                     if( p_intf != NULL )
472                     {
473                         p_intf->b_menu_change = 1;
474                         vlc_object_release( p_intf );
475                     }
476                 }
477                 break;
478
479             case SDLK_LEFT:
480                 input_Seek( p_vout, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
481                 break;
482
483             case SDLK_RIGHT:
484                 input_Seek( p_vout, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
485                 break;
486
487             case SDLK_UP:
488                 input_Seek( p_vout, 60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
489                 break;
490
491             case SDLK_DOWN:
492                 input_Seek( p_vout, -60, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
493                 break;
494
495             case SDLK_F1: network_ChannelJoin( p_vout, 1 ); break;
496             case SDLK_F2: network_ChannelJoin( p_vout, 2 ); break;
497             case SDLK_F3: network_ChannelJoin( p_vout, 3 ); break;
498             case SDLK_F4: network_ChannelJoin( p_vout, 4 ); break;
499             case SDLK_F5: network_ChannelJoin( p_vout, 5 ); break;
500             case SDLK_F6: network_ChannelJoin( p_vout, 6 ); break;
501             case SDLK_F7: network_ChannelJoin( p_vout, 7 ); break;
502             case SDLK_F8: network_ChannelJoin( p_vout, 8 ); break;
503             case SDLK_F9: network_ChannelJoin( p_vout, 9 ); break;
504             case SDLK_F10: network_ChannelJoin( p_vout, 10 ); break;
505             case SDLK_F11: network_ChannelJoin( p_vout, 11 ); break;
506             case SDLK_F12: network_ChannelJoin( p_vout, 12 ); break;
507
508             case SDLK_b:
509                 {
510                     audio_volume_t i_volume;
511                     if ( !aout_VolumeDown( p_vout, 1, &i_volume ) )
512                     {
513                         msg_Dbg( p_vout, "audio volume is now %d", i_volume );
514                     }
515                     else
516                     {
517                         msg_Dbg( p_vout, "audio volume: operation not supported" );
518                     }
519                 }
520                 break;
521
522             case SDLK_n:
523                 {
524                     audio_volume_t i_volume;
525                     if ( !aout_VolumeUp( p_vout, 1, &i_volume ) )
526                     {
527                         msg_Dbg( p_vout, "audio volume is now %d", i_volume );
528                     }
529                     else
530                     {
531                         msg_Dbg( p_vout, "audio volume: operation not supported" );
532                     }
533                 }
534                 break;
535
536              default:
537                 break;
538             }
539             break;
540
541         default:
542             break;
543         }
544     }
545
546     /* Fullscreen change */
547     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
548     {
549         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
550
551         p_vout->p_sys->b_cursor_autohidden = 0;
552         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
553                         ! p_vout->p_sys->b_cursor_autohidden );
554
555         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
556         p_vout->i_changes |= VOUT_SIZE_CHANGE;
557     }
558
559     /*
560      * Size change
561      */
562     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
563     {
564         msg_Dbg( p_vout, "video display resized (%dx%d)",
565                  p_vout->p_sys->i_width, p_vout->p_sys->i_height );
566
567         CloseDisplay( p_vout );
568         OpenDisplay( p_vout );
569
570         /* We don't need to signal the vout thread about the size change if
571          * we can handle rescaling ourselves */
572         if( p_vout->p_sys->p_overlay != NULL )
573             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
574
575     }
576
577     /* Pointer change */
578     if( ! p_vout->p_sys->b_cursor_autohidden &&
579         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
580     {
581         /* Hide the mouse automatically */
582         p_vout->p_sys->b_cursor_autohidden = 1;
583         SDL_ShowCursor( 0 );
584     }
585
586     return VLC_SUCCESS;
587 }
588
589 /*****************************************************************************
590  * Display: displays previously rendered output
591  *****************************************************************************
592  * This function sends the currently rendered image to the display.
593  *****************************************************************************/
594 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
595 {
596     int x, y, w, h;
597     SDL_Rect disp;
598
599     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
600                        &x, &y, &w, &h );
601     disp.x = x;
602     disp.y = y;
603     disp.w = w;
604     disp.h = h;
605
606     if( p_vout->p_sys->p_overlay == NULL )
607     {
608         /* RGB picture */
609         SDL_Flip( p_vout->p_sys->p_display );
610     }
611     else
612     {
613         /* Overlay picture */
614         SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
615         SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
616         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
617     }
618 }
619
620 /* following functions are local */
621
622 /*****************************************************************************
623  * OpenDisplay: open and initialize SDL device
624  *****************************************************************************
625  * Open and initialize display according to preferences specified in the vout
626  * thread fields.
627  *****************************************************************************/
628 static int OpenDisplay( vout_thread_t *p_vout )
629 {
630     Uint32   i_flags;
631     int      i_bpp;
632
633     /* SDL fucked up fourcc definitions on bigendian machines */
634     uint32_t i_sdl_chroma;
635
636     /* Set main window's size */
637     p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->output.i_width :
638                                                     p_vout->i_window_width;
639     p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->output.i_height :
640                                                      p_vout->i_window_height;
641
642     /* Initialize flags and cursor */
643     i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
644     i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
645
646     i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
647                              SDL_DEFAULT_BPP, i_flags );
648     if( i_bpp == 0 )
649     {
650         msg_Err( p_vout, "no video mode available" );
651         return VLC_EGENERIC;
652     }
653
654     p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
655                                                  p_vout->p_sys->i_height,
656                                                  i_bpp, i_flags );
657
658     if( p_vout->p_sys->p_display == NULL )
659     {
660         msg_Err( p_vout, "cannot set video mode" );
661         return VLC_EGENERIC;
662     }
663
664     SDL_LockSurface( p_vout->p_sys->p_display );
665
666     /* Choose the chroma we will try first. */
667     switch( p_vout->render.i_chroma )
668     {
669         case VLC_FOURCC('Y','U','Y','2'):
670         case VLC_FOURCC('Y','U','N','V'):
671             p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
672             i_sdl_chroma = SDL_YUY2_OVERLAY;
673             break;
674         case VLC_FOURCC('U','Y','V','Y'):
675         case VLC_FOURCC('U','Y','N','V'):
676         case VLC_FOURCC('Y','4','2','2'):
677             p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
678             i_sdl_chroma = SDL_UYVY_OVERLAY;
679             break;
680         case VLC_FOURCC('Y','V','Y','U'):
681             p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
682             i_sdl_chroma = SDL_YVYU_OVERLAY;
683             break;
684         case VLC_FOURCC('Y','V','1','2'):
685         case VLC_FOURCC('I','4','2','0'):
686         case VLC_FOURCC('I','Y','U','V'):
687         default:
688             p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
689             i_sdl_chroma = SDL_YV12_OVERLAY;
690             break;
691     }
692
693     p_vout->p_sys->p_overlay =
694         SDL_CreateYUVOverlay( 32, 32, i_sdl_chroma, p_vout->p_sys->p_display );
695     /* FIXME: if the first overlay we find is software, don't stop,
696      * because we may find a hardware one later ... */
697
698     /* If this best choice failed, fall back to other chromas */
699     if( p_vout->p_sys->p_overlay == NULL )
700     {
701         p_vout->output.i_chroma = VLC_FOURCC('I','Y','U','V');
702         p_vout->p_sys->p_overlay =
703             SDL_CreateYUVOverlay( 32, 32, SDL_IYUV_OVERLAY,
704                                   p_vout->p_sys->p_display );
705     }
706
707     if( p_vout->p_sys->p_overlay == NULL )
708     {
709         p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
710         p_vout->p_sys->p_overlay =
711             SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
712                                   p_vout->p_sys->p_display );
713     }
714
715     if( p_vout->p_sys->p_overlay == NULL )
716     {
717         p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
718         p_vout->p_sys->p_overlay =
719             SDL_CreateYUVOverlay( 32, 32, SDL_YUY2_OVERLAY,
720                                   p_vout->p_sys->p_display );
721     }
722
723     if( p_vout->p_sys->p_overlay == NULL )
724     {
725         msg_Warn( p_vout, "no SDL overlay for 0x%.8x (%4.4s)",
726                   p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma );
727
728         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
729         {
730             case 8:
731                 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
732                 p_vout->output.pf_setpalette = SetPalette;
733                 break;
734             case 15:
735                 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
736                 break;
737             case 16:
738                 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
739                 break;
740             case 24:
741                 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
742                 break;
743             case 32:
744                 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
745                 break;
746             default:
747                 msg_Err( p_vout, "unknown screen depth %i",
748                          p_vout->p_sys->p_display->format->BitsPerPixel );
749                 SDL_UnlockSurface( p_vout->p_sys->p_display );
750                 SDL_FreeSurface( p_vout->p_sys->p_display );
751                 return VLC_EGENERIC;
752         }
753
754         p_vout->output.i_rmask = p_vout->p_sys->p_display->format->Rmask;
755         p_vout->output.i_gmask = p_vout->p_sys->p_display->format->Gmask;
756         p_vout->output.i_bmask = p_vout->p_sys->p_display->format->Bmask;
757
758         SDL_WM_SetCaption( VOUT_TITLE " (software RGB SDL output)",
759                            VOUT_TITLE " (software RGB SDL output)" );
760     }
761     else
762     {
763         if( p_vout->p_sys->p_overlay->hw_overlay )
764         {
765             SDL_WM_SetCaption( VOUT_TITLE " (hardware YUV SDL output)",
766                                VOUT_TITLE " (hardware YUV SDL output)" );
767         }
768         else
769         {
770             SDL_WM_SetCaption( VOUT_TITLE " (software YUV SDL output)",
771                                VOUT_TITLE " (software YUV SDL output)" );
772         }
773     }
774
775     SDL_EventState( SDL_KEYUP, SDL_IGNORE );               /* ignore keys up */
776
777     return VLC_SUCCESS;
778 }
779
780 /*****************************************************************************
781  * CloseDisplay: close and reset SDL device
782  *****************************************************************************
783  * This function returns all resources allocated by OpenDisplay and restore
784  * the original state of the device.
785  *****************************************************************************/
786 static void CloseDisplay( vout_thread_t *p_vout )
787 {
788     SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
789     SDL_UnlockSurface ( p_vout->p_sys->p_display );
790     SDL_FreeSurface( p_vout->p_sys->p_display );
791 }
792
793 /*****************************************************************************
794  * NewPicture: allocate a picture
795  *****************************************************************************
796  * Returns 0 on success, -1 otherwise
797  *****************************************************************************/
798 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
799 {
800     int i_width  = p_vout->output.i_width;
801     int i_height = p_vout->output.i_height;
802
803     if( p_vout->p_sys->p_overlay == NULL )
804     {
805         /* RGB picture */
806         if( p_vout->p_sys->i_surfaces )
807         {
808             /* We already allocated this surface, return */
809             return VLC_EGENERIC;
810         }
811
812         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
813
814         if( p_pic->p_sys == NULL )
815         {
816             return VLC_ENOMEM;
817         }
818
819         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
820         {
821             case 8:
822                 p_pic->p->i_pixel_pitch = 1;
823                 break;
824             case 15:
825             case 16:
826                 p_pic->p->i_pixel_pitch = 2;
827                 break;
828             case 24:
829             case 32:
830                 p_pic->p->i_pixel_pitch = 4;
831                 break;
832             default:
833                 return VLC_EGENERIC;
834         }
835
836         p_pic->p->p_pixels = p_vout->p_sys->p_display->pixels;
837         p_pic->p->i_lines = p_vout->p_sys->p_display->h;
838         p_pic->p->i_pitch = p_vout->p_sys->p_display->pitch;
839         p_pic->p->i_visible_pitch =
840             p_pic->p->i_pixel_pitch * p_vout->p_sys->p_display->w;
841
842         p_vout->p_sys->i_surfaces++;
843
844         p_pic->i_planes = 1;
845     }
846     else
847     {
848         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
849
850         if( p_pic->p_sys == NULL )
851         {
852             return VLC_ENOMEM;
853         }
854
855         p_pic->p_sys->p_overlay =
856             SDL_CreateYUVOverlay( i_width, i_height,
857                                   p_vout->output.i_chroma,
858                                   p_vout->p_sys->p_display );
859
860         if( p_pic->p_sys->p_overlay == NULL )
861         {
862             free( p_pic->p_sys );
863             return VLC_EGENERIC;
864         }
865
866         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
867
868         p_pic->Y_PIXELS = p_pic->p_sys->p_overlay->pixels[0];
869         p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_overlay->h;
870         p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[0];
871
872         switch( p_vout->output.i_chroma )
873         {
874         case SDL_YV12_OVERLAY:
875             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
876             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
877
878             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
879             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
880             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
881             p_pic->p[U_PLANE].i_pixel_pitch = 1;
882             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
883
884             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
885             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
886             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
887             p_pic->p[V_PLANE].i_pixel_pitch = 1;
888             p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
889
890             p_pic->i_planes = 3;
891             break;
892
893         case SDL_IYUV_OVERLAY:
894             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
895             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
896
897             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
898             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
899             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
900             p_pic->p[U_PLANE].i_pixel_pitch = 1;
901             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
902
903             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
904             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
905             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
906             p_pic->p[V_PLANE].i_pixel_pitch = 1;
907             p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
908
909             p_pic->i_planes = 3;
910             break;
911
912         default:
913             p_pic->p[Y_PLANE].i_pixel_pitch = 2;
914             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w * 2;
915
916             p_pic->i_planes = 1;
917             break;
918         }
919     }
920
921     return VLC_SUCCESS;
922 }
923
924 /*****************************************************************************
925  * SetPalette: sets an 8 bpp palette
926  *****************************************************************************/
927 static void SetPalette( vout_thread_t *p_vout,
928                         uint16_t *red, uint16_t *green, uint16_t *blue )
929 {
930     SDL_Color colors[256];
931     int i;
932
933     /* Fill colors with color information */
934     for( i = 0; i < 256; i++ )
935     {
936         colors[ i ].r = red[ i ] >> 8;
937         colors[ i ].g = green[ i ] >> 8;
938         colors[ i ].b = blue[ i ] >> 8;
939     }
940
941     /* Set palette */
942     if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
943     {
944         msg_Err( p_vout, "failed setting palette" );
945     }
946 }
947