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