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