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