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