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