]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
* Got rid of TRACE and intf_DbgMsg which were seldom used anyway.
[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.82 2002/02/19 00:50:19 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                 break;
477             }
478             break;
479
480         default:
481             break;
482         }
483     }
484
485     /* Fullscreen change */
486     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
487     {
488         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
489
490         SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
491
492         p_vout->p_sys->b_cursor_autohidden = 0;
493         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
494                         ! p_vout->p_sys->b_cursor_autohidden );
495
496         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
497     }
498
499     /* Pointer change */
500     if( ! p_vout->p_sys->b_cursor_autohidden &&
501         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
502     {
503         /* Hide the mouse automatically */
504         p_vout->p_sys->b_cursor_autohidden = 1;
505         SDL_ShowCursor( 0 );
506     }
507
508     return( 0 );
509 }
510
511 /*****************************************************************************
512  * vout_Render: render previously calculated output
513  *****************************************************************************/
514 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
515 {
516     ;
517 }
518
519 /*****************************************************************************
520  * vout_Display: displays previously rendered output
521  *****************************************************************************
522  * This function sends the currently rendered image to the display.
523  *****************************************************************************/
524 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
525 {
526     int x, y, w, h;
527     SDL_Rect disp;
528
529     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
530                        &x, &y, &w, &h );
531     disp.x = x;
532     disp.y = y;
533     disp.w = w;
534     disp.h = h;
535
536     if( p_vout->p_sys->p_overlay == NULL )
537     {
538         /* RGB picture */
539         SDL_Flip( p_vout->p_sys->p_display );
540     }
541     else
542     {
543         /* Overlay picture */
544         SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
545         SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
546         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
547     }
548 }
549
550 /* following functions are local */
551
552 /*****************************************************************************
553  * SDLOpenDisplay: open and initialize SDL device
554  *****************************************************************************
555  * Open and initialize display according to preferences specified in the vout
556  * thread fields.
557  *****************************************************************************/
558 static int SDLOpenDisplay( vout_thread_t *p_vout )
559 {
560     Uint32 i_flags;
561     int    i_bpp;
562
563     /* Initialize flags and cursor */
564     i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
565     i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
566
567     i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
568                              SDL_DEFAULT_BPP, i_flags );
569     if( i_bpp == 0 )
570     {
571         intf_ErrMsg( "vout error: no video mode available" );
572         return( 1 );
573     }
574
575     p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
576                                                  p_vout->p_sys->i_height,
577                                                  i_bpp, i_flags );
578
579     if( p_vout->p_sys->p_display == NULL )
580     {
581         intf_ErrMsg( "vout error: cannot set video mode" );
582         return( 1 );
583     }
584
585     SDL_LockSurface( p_vout->p_sys->p_display );
586
587     /* Choose the chroma we will try first. */
588     switch( p_vout->render.i_chroma )
589     {
590         case FOURCC_YUY2:
591         case FOURCC_YUNV:
592             p_vout->output.i_chroma = SDL_YUY2_OVERLAY;
593             break;
594         case FOURCC_UYVY:
595         case FOURCC_UYNV:
596         case FOURCC_Y422:
597             p_vout->output.i_chroma = SDL_UYVY_OVERLAY;
598             break;
599         case FOURCC_YVYU:
600             p_vout->output.i_chroma = SDL_YVYU_OVERLAY;
601             break;
602         case FOURCC_YV12:
603         case FOURCC_I420:
604         case FOURCC_IYUV:
605         default:
606             p_vout->output.i_chroma = SDL_YV12_OVERLAY;
607             break;
608     }
609
610     p_vout->p_sys->p_overlay =
611         SDL_CreateYUVOverlay( 32, 32, p_vout->output.i_chroma,
612                               p_vout->p_sys->p_display );
613
614     /* FIXME: if the first overlay we find is software, don't stop,
615      * because we may find a hardware one later ... */
616
617     /* If this best choice failed, fall back to other chromas */
618     if( p_vout->p_sys->p_overlay == NULL )
619     {
620         p_vout->output.i_chroma = SDL_IYUV_OVERLAY;
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
626     if( p_vout->p_sys->p_overlay == NULL )
627     {
628         p_vout->output.i_chroma = SDL_YV12_OVERLAY;
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
634     if( p_vout->p_sys->p_overlay == NULL )
635     {
636         p_vout->output.i_chroma = SDL_YUY2_OVERLAY;
637         p_vout->p_sys->p_overlay =
638             SDL_CreateYUVOverlay( 32, 32, p_vout->output.i_chroma,
639                                   p_vout->p_sys->p_display );
640     }
641
642     if( p_vout->p_sys->p_overlay == NULL )
643     {
644         intf_WarnMsg( 3, "vout warning: no SDL overlay for 0x%.8x (%4.4s)",
645                          p_vout->render.i_chroma,
646                          (char*)&p_vout->render.i_chroma );
647
648         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
649         {
650             case 8:
651                 p_vout->output.i_chroma = FOURCC_BI_RGB;
652                 break;
653             case 15:
654                 p_vout->output.i_chroma = FOURCC_RV15;
655                 break;
656             case 16:
657                 p_vout->output.i_chroma = FOURCC_RV16;
658                 break;
659             case 24:
660                 p_vout->output.i_chroma = FOURCC_BI_BITFIELDS;
661                 break;
662             case 32:
663                 p_vout->output.i_chroma = FOURCC_BI_BITFIELDS;
664                 break;
665             default:
666                 intf_ErrMsg( "vout error: unknown screen depth" );
667                 SDL_UnlockSurface( p_vout->p_sys->p_display );
668                 SDL_FreeSurface( p_vout->p_sys->p_display );
669                 return( -1 );
670         }
671
672         p_vout->p_sys->i_red_mask = p_vout->p_sys->p_display->format->Rmask;
673         p_vout->p_sys->i_green_mask = p_vout->p_sys->p_display->format->Gmask;
674         p_vout->p_sys->i_blue_mask = p_vout->p_sys->p_display->format->Bmask;
675
676         SDL_WM_SetCaption( VOUT_TITLE " (software RGB SDL output)",
677                            VOUT_TITLE " (software RGB SDL output)" );
678     }
679     else
680     {
681         if( p_vout->p_sys->p_overlay->hw_overlay )
682         {
683             SDL_WM_SetCaption( VOUT_TITLE " (hardware YUV SDL output)",
684                                VOUT_TITLE " (hardware YUV SDL output)" );
685         }
686         else
687         {
688             SDL_WM_SetCaption( VOUT_TITLE " (software YUV SDL output)",
689                                VOUT_TITLE " (software YUV SDL output)" );
690         }
691     }
692
693     SDL_EventState( SDL_KEYUP, SDL_IGNORE );               /* ignore keys up */
694
695     return( 0 );
696 }
697
698 /*****************************************************************************
699  * SDLCloseDisplay: close and reset SDL device
700  *****************************************************************************
701  * This function returns all resources allocated by SDLOpenDisplay and restore
702  * the original state of the device.
703  *****************************************************************************/
704 static void SDLCloseDisplay( vout_thread_t *p_vout )
705 {
706     SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
707     SDL_UnlockSurface ( p_vout->p_sys->p_display );
708     SDL_FreeSurface( p_vout->p_sys->p_display );
709 }
710
711 /*****************************************************************************
712  * SDLNewPicture: allocate a picture
713  *****************************************************************************
714  * Returns 0 on success, -1 otherwise
715  *****************************************************************************/
716 static int SDLNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
717 {
718     int i_width  = p_vout->output.i_width;
719     int i_height = p_vout->output.i_height;
720
721     if( p_vout->p_sys->p_overlay == NULL )
722     {
723         /* RGB picture */
724         if( p_vout->p_sys->i_surfaces )
725         {
726             /* We already allocated this surface, return */
727             return -1;
728         }
729
730         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
731
732         if( p_pic->p_sys == NULL )
733         {
734             return -1;
735         }
736
737         p_pic->p->p_pixels = p_vout->p_sys->p_display->pixels;
738         p_pic->p->i_lines = p_vout->p_sys->p_display->h;
739         p_pic->p->i_pitch = p_vout->p_sys->p_display->pitch;
740         p_pic->p->i_pixel_bytes = 2;
741
742         if( p_pic->p->i_pitch == 2 * p_vout->p_sys->p_display->w )
743         {
744             p_pic->p->b_margin = 0;
745         }
746         else
747         {
748             p_pic->p->b_margin = 1;
749             p_pic->p->b_hidden = 1;
750             p_pic->p->i_visible_bytes = 2 * p_vout->p_sys->p_display->w;
751         }
752
753         p_pic->p->i_red_mask = p_vout->p_sys->p_display->format->Rmask;
754         p_pic->p->i_green_mask = p_vout->p_sys->p_display->format->Gmask;
755         p_pic->p->i_blue_mask = p_vout->p_sys->p_display->format->Bmask;
756
757         p_vout->p_sys->i_surfaces++;
758
759         p_pic->i_planes = 1;
760     }
761     else
762     {
763         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
764
765         if( p_pic->p_sys == NULL )
766         {
767             return -1;
768         }
769
770         p_pic->p_sys->p_overlay =
771             SDL_CreateYUVOverlay( i_width, i_height,
772                                   p_vout->output.i_chroma,
773                                   p_vout->p_sys->p_display );
774
775         if( p_pic->p_sys->p_overlay == NULL )
776         {
777             free( p_pic->p_sys );
778             return -1;
779         }
780
781         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
782
783         p_pic->Y_PIXELS = p_pic->p_sys->p_overlay->pixels[0];
784         p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_overlay->h;
785         p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[0];
786
787         switch( p_vout->output.i_chroma )
788         {
789         case SDL_YV12_OVERLAY:
790             p_pic->p[Y_PLANE].i_pixel_bytes = 1;
791             p_pic->p[Y_PLANE].b_margin = 0;
792
793             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
794             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
795             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
796             p_pic->p[U_PLANE].i_pixel_bytes = 1;
797             p_pic->p[U_PLANE].b_margin = 0;
798
799             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
800             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
801             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
802             p_pic->p[V_PLANE].i_pixel_bytes = 1;
803             p_pic->p[V_PLANE].b_margin = 0;
804
805             p_pic->i_planes = 3;
806             break;
807
808         case SDL_IYUV_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[1];
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[1];
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[2];
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[2];
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         default:
828             p_pic->p[Y_PLANE].i_pixel_bytes = 2;
829             p_pic->p[Y_PLANE].b_margin = 0;
830
831             p_pic->i_planes = 1;
832             break;
833         }
834     }
835
836     return 0;
837 }
838