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