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