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