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