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