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