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