]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
* Borrowed MPlayer's fast memcpy() routines. Best is autodetected, choose
[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.66 2001/12/03 16:18:37 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 #define MODULE_NAME sdl
27 #include "modules_inner.h"
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include "defs.h"
33
34 #include <errno.h>                                                 /* ENOMEM */
35 #include <stdlib.h>                                                /* free() */
36 #include <string.h>                                            /* strerror() */
37
38 #include <sys/types.h>
39 #ifndef WIN32
40 #   include <netinet/in.h>                            /* BSD: struct in_addr */
41 #endif
42
43 #include SDL_INCLUDE_FILE
44
45 #include "config.h"
46 #include "common.h"
47 #include "intf_msg.h"
48 #include "threads.h"
49 #include "mtime.h"
50 #include "tests.h"
51
52 #include "video.h"
53 #include "video_output.h"
54
55 #include "interface.h"
56 /* FIXME: get rid of this */
57 #include "keystrokes.h"
58 #include "main.h"
59 #include "netutils.h"
60
61 #include "modules.h"
62 #include "modules_export.h"
63
64 /*****************************************************************************
65  * FIXME: this file is ...                                                   *
66  *                                                                           *
67  *              XXX   XXX     FIXME     XXX     XXX   XXX   XXX              *
68  *              XXX   XXX   XXX   XXX   XXX     XXX   XXX   XXX              *
69  *              XXX   XXX   XXX         XXX       FIXME     XXX              *
70  *              XXX   XXX   XXX  TODO   XXX        XXX      XXX              *
71  *              XXX   XXX   XXX   XXX   XXX        XXX                       *
72  *                FIXME       FIXME       FIXME    XXX      XXX              *
73  *                                                                           *
74  *****************************************************************************/
75
76 /*****************************************************************************
77  * vout_sys_t: video output SDL method descriptor
78  *****************************************************************************
79  * This structure is part of the video output thread descriptor.
80  * It describes the SDL specific properties of an output thread.
81  *****************************************************************************/
82 typedef struct vout_sys_s
83 {
84     int i_width;
85     int i_height;
86
87     SDL_Surface *   p_display;                             /* display device */
88     SDL_Overlay *   p_overlay;                             /* overlay device */
89
90     boolean_t   b_overlay;
91     boolean_t   b_cursor;
92     boolean_t   b_reopen_display;
93
94     boolean_t   b_cursor_autohidden;
95     mtime_t     i_lastmoved;
96
97     Uint8   *   p_sdl_buf[2];                          /* Buffer information */
98
99 } vout_sys_t;
100
101 /*****************************************************************************
102  * Local prototypes.
103  *****************************************************************************/
104 static int  vout_Probe     ( probedata_t *p_data );
105 static int  vout_Create    ( struct vout_thread_s * );
106 static int  vout_Init      ( struct vout_thread_s * );
107 static void vout_End       ( struct vout_thread_s * );
108 static void vout_Destroy   ( struct vout_thread_s * );
109 static int  vout_Manage    ( struct vout_thread_s * );
110 static void vout_Display   ( struct vout_thread_s * );
111 static void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
112                              u16 *blue, u16 *transp );
113
114 static int  SDLOpenDisplay     ( vout_thread_t *p_vout );
115 static void SDLCloseDisplay    ( vout_thread_t *p_vout );
116 static void OutputCoords       ( const picture_t *p_pic, const boolean_t scale,
117                                  const int win_w, const int win_h,
118                                  int *dx, int *dy, int *w, int *h );
119
120 /*****************************************************************************
121  * Functions exported as capabilities. They are declared as static so that
122  * we don't pollute the namespace too much.
123  *****************************************************************************/
124 void _M( vout_getfunctions )( function_list_t * p_function_list )
125 {
126     p_function_list->pf_probe = vout_Probe;
127     p_function_list->functions.vout.pf_create     = vout_Create;
128     p_function_list->functions.vout.pf_init       = vout_Init;
129     p_function_list->functions.vout.pf_end        = vout_End;
130     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
131     p_function_list->functions.vout.pf_manage     = vout_Manage;
132     p_function_list->functions.vout.pf_display    = vout_Display;
133     p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
134 }
135
136 /*****************************************************************************
137  * vout_Probe: probe the video driver and return a score
138  *****************************************************************************
139  * This function tries to initialize SDL and returns a score to the
140  * plugin manager so that it can select the best plugin.
141  *****************************************************************************/
142 static int vout_Probe( probedata_t *p_data )
143 {
144     if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
145     {
146         return( 999 );
147     }
148
149     return( 100 );
150 }
151
152 /*****************************************************************************
153  * vout_Create: allocate SDL video thread output method
154  *****************************************************************************
155  * This function allocate and initialize a SDL vout method. It uses some of the
156  * vout properties to choose the correct mode, and change them according to the
157  * mode actually used.
158  *****************************************************************************/
159 static int vout_Create( vout_thread_t *p_vout )
160 {
161     /* Allocate structure */
162     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
163     if( p_vout->p_sys == NULL )
164     {
165         intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
166         return( 1 );
167     }
168
169     /* Initialize library */
170     if( SDL_Init( SDL_INIT_VIDEO
171 #ifndef WIN32
172     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
173                 | SDL_INIT_EVENTTHREAD
174 #endif
175 #ifdef DEBUG
176     /* In debug mode you may want vlc to dump a core instead of staying
177      * stuck */
178                 | SDL_INIT_NOPARACHUTE
179 #endif
180                 ) < 0 )
181     {
182         intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
183         free( p_vout->p_sys );
184         return( 1 );
185     }
186
187     p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
188
189     p_vout->p_sys->b_cursor_autohidden = 0;
190     p_vout->p_sys->i_lastmoved = mdate();
191
192     p_vout->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
193                                 VOUT_FULLSCREEN_DEFAULT );
194     p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
195                                 VOUT_OVERLAY_DEFAULT );
196     p_vout->p_sys->i_width = p_vout->i_width;
197     p_vout->p_sys->i_height = p_vout->i_height;
198
199     p_vout->p_sys->p_display = NULL;
200     p_vout->p_sys->p_overlay = NULL;
201
202     if( SDLOpenDisplay(p_vout) )
203     {
204         intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
205         free( p_vout->p_sys );
206         return( 1 );
207     }
208
209     /* FIXME: get rid of this ASAP, it's FUCKING UGLY */
210     { intf_thread_t * p_intf = p_main->p_intf;
211     intf_AssignKey(p_intf, SDLK_q,      INTF_KEY_QUIT, 0);
212     intf_AssignKey(p_intf, SDLK_ESCAPE, INTF_KEY_QUIT, 0);
213     /* intf_AssignKey(p_intf,3,'Q'); */
214     intf_AssignKey(p_intf, SDLK_0,      INTF_KEY_SET_CHANNEL,0);
215     intf_AssignKey(p_intf, SDLK_1,      INTF_KEY_SET_CHANNEL,1);
216     intf_AssignKey(p_intf, SDLK_2,      INTF_KEY_SET_CHANNEL,2);
217     intf_AssignKey(p_intf, SDLK_3,      INTF_KEY_SET_CHANNEL,3);
218     intf_AssignKey(p_intf, SDLK_4,      INTF_KEY_SET_CHANNEL,4);
219     intf_AssignKey(p_intf, SDLK_5,      INTF_KEY_SET_CHANNEL,5);
220     intf_AssignKey(p_intf, SDLK_6,      INTF_KEY_SET_CHANNEL,6);
221     intf_AssignKey(p_intf, SDLK_7,      INTF_KEY_SET_CHANNEL,7);
222     intf_AssignKey(p_intf, SDLK_8,      INTF_KEY_SET_CHANNEL,8);
223     intf_AssignKey(p_intf, SDLK_9,      INTF_KEY_SET_CHANNEL,9);
224     intf_AssignKey(p_intf, SDLK_PLUS,   INTF_KEY_INC_VOLUME, 0);
225     intf_AssignKey(p_intf, SDLK_MINUS,  INTF_KEY_DEC_VOLUME, 0);
226     intf_AssignKey(p_intf, SDLK_m,      INTF_KEY_TOGGLE_VOLUME, 0);
227     /* intf_AssignKey(p_intf,'M','M'); */
228     intf_AssignKey(p_intf, SDLK_g,      INTF_KEY_DEC_GAMMA, 0);
229     /* intf_AssignKey(p_intf,'G','G'); */
230     intf_AssignKey(p_intf, SDLK_c,      INTF_KEY_TOGGLE_GRAYSCALE, 0);
231     intf_AssignKey(p_intf, SDLK_SPACE,  INTF_KEY_TOGGLE_INTERFACE, 0);
232     intf_AssignKey(p_intf, SDLK_i,      INTF_KEY_TOGGLE_INFO, 0);
233     intf_AssignKey(p_intf, SDLK_s,      INTF_KEY_TOGGLE_SCALING, 0);
234     intf_AssignKey(p_intf, SDLK_d,      INTF_KEY_DUMP_STREAM, 0); }
235
236     return( 0 );
237 }
238
239 /*****************************************************************************
240  * vout_Init: initialize SDL video thread output method
241  *****************************************************************************
242  * This function initialize the SDL display device.
243  *****************************************************************************/
244 static int vout_Init( vout_thread_t *p_vout )
245 {
246     /* This hack is hugly, but hey, you are, too. */
247
248     SDL_Overlay *   p_overlay;
249     
250     p_overlay = SDL_CreateYUVOverlay( 
251            main_GetIntVariable( VOUT_WIDTH_VAR,VOUT_WIDTH_DEFAULT ),
252            main_GetIntVariable( VOUT_HEIGHT_VAR,VOUT_HEIGHT_DEFAULT ),
253                                       SDL_YV12_OVERLAY, 
254                                       p_vout->p_sys->p_display );
255
256     if( p_overlay == NULL )
257     {
258         intf_ErrMsg( "vout error: could not create SDL overlay" );
259         p_vout->b_need_render = 1;
260         return( 0 );
261     }
262
263     intf_WarnMsg( 2, "vout: YUV acceleration %s",
264               p_overlay->hw_overlay ? "activated" : "unavailable !" ); 
265     p_vout->b_need_render = !p_overlay->hw_overlay;
266
267     SDL_FreeYUVOverlay( p_overlay );
268
269     return( 0 );
270 }
271
272 /*****************************************************************************
273  * vout_End: terminate Sys video thread output method
274  *****************************************************************************
275  * Terminate an output method created by vout_SDLCreate
276  *****************************************************************************/
277 static void vout_End( vout_thread_t *p_vout )
278 {
279     SDLCloseDisplay( p_vout );
280     SDL_QuitSubSystem( SDL_INIT_VIDEO );
281 }
282
283 /*****************************************************************************
284  * vout_Destroy: destroy Sys video thread output method
285  *****************************************************************************
286  * Terminate an output method created by vout_SDLCreate
287  *****************************************************************************/
288 static void vout_Destroy( vout_thread_t *p_vout )
289 {
290     free( p_vout->p_sys );
291 }
292
293 /*****************************************************************************
294  * vout_Manage: handle Sys events
295  *****************************************************************************
296  * This function should be called regularly by video output thread. It returns
297  * a non null value if an error occured.
298  *****************************************************************************/
299 static int vout_Manage( vout_thread_t *p_vout )
300 {
301     SDL_Event event;                                            /* SDL event */
302     char *    p_key;
303
304     /* Process events */
305     while( SDL_PollEvent(&event) )
306     {
307         switch( event.type )
308         {
309         case SDL_VIDEORESIZE:                          /* Resizing of window */
310             p_vout->i_width = event.resize.w;
311             p_vout->i_height = event.resize.h;
312             p_vout->i_changes |= VOUT_SIZE_CHANGE;
313             break;
314
315         case SDL_MOUSEMOTION:
316             if( p_vout->p_sys->b_cursor &&
317                 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
318             {
319                 if( p_vout->p_sys->b_cursor_autohidden )
320                 {
321                     p_vout->p_sys->b_cursor_autohidden = 0;
322                     SDL_ShowCursor( 1 );
323                 }
324                 else
325                 {
326                     p_vout->p_sys->i_lastmoved = mdate();
327                 }
328             }
329             break;
330
331         case SDL_MOUSEBUTTONUP:
332             switch( event.button.button )
333             {
334             case SDL_BUTTON_RIGHT:
335                 p_main->p_intf->b_menu_change = 1;
336                 break;
337             }
338             break;
339
340         case SDL_MOUSEBUTTONDOWN:
341             switch( event.button.button )
342             {
343             case SDL_BUTTON_MIDDLE:
344                 p_vout->i_changes |= VOUT_CURSOR_CHANGE;
345                 break;
346             }
347             break;
348
349         case SDL_QUIT:
350             intf_ProcessKey( p_main->p_intf, SDLK_q );
351             break;
352
353         case SDL_KEYDOWN:                             /* if a key is pressed */
354
355             switch( event.key.keysym.sym )
356             {
357             case SDLK_f:                             /* switch to fullscreen */
358                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
359                 break;
360
361             case SDLK_y:                               /* switch to hard YUV */
362                 p_vout->i_changes |= VOUT_YUV_CHANGE;
363                 break;
364
365             case SDLK_c:                                 /* toggle grayscale */
366                 p_vout->b_grayscale = ! p_vout->b_grayscale;
367                 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
368                 break;
369
370             case SDLK_i:                                      /* toggle info */
371                 p_vout->b_info = ! p_vout->b_info;
372                 p_vout->i_changes |= VOUT_INFO_CHANGE;
373                 break;
374
375             case SDLK_s:                                   /* toggle scaling */
376                 p_vout->b_scale = ! p_vout->b_scale;
377                 p_vout->i_changes |= VOUT_SCALE_CHANGE;
378                 break;
379
380             case SDLK_SPACE:                             /* toggle interface */
381                 p_vout->b_interface = ! p_vout->b_interface;
382                 p_vout->i_changes |= VOUT_INTF_CHANGE;
383                 break;
384             
385             case SDLK_F10:
386                 network_ChannelJoin( 0 );
387                 break;
388             case SDLK_F1:
389                 network_ChannelJoin( 1 );
390                 break;
391             case SDLK_F2:
392                 network_ChannelJoin( 2 );
393                 break;
394             case SDLK_F3:
395                 network_ChannelJoin( 3 );
396                 break;
397             case SDLK_F4:
398                 network_ChannelJoin( 4 );
399                 break;
400             case SDLK_F5:
401                 network_ChannelJoin( 5 );
402                 break;
403             case SDLK_F6:
404                 network_ChannelJoin( 6 );
405                 break;
406             case SDLK_F7:
407                 network_ChannelJoin( 7 );
408                 break;
409             case SDLK_F8:
410                 network_ChannelJoin( 8 );
411                 break;
412             case SDLK_F9:
413                 network_ChannelJoin( 9 );
414                 break;
415
416             case SDLK_MENU:
417                 p_main->p_intf->b_menu_change = 1;
418                 break;
419                 
420             default:
421                 p_key = SDL_GetKeyName( event.key.keysym.sym ) ;
422                 if( intf_ProcessKey( p_main->p_intf, 
423                                      (char )event.key.keysym.sym ) )
424                 {
425                    intf_DbgMsg( "unhandled key '%c' (%i)", 
426                                 (char)event.key.keysym.sym, 
427                                 event.key.keysym.sym );                
428                 }
429                 break;
430             }
431             break;
432
433         default:
434             break;
435         }
436     }
437
438     /*
439      * Size Change 
440      */
441     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
442     {
443         p_vout->p_sys->i_width = p_vout->i_width;
444         p_vout->p_sys->i_height = p_vout->i_height;
445
446         /* Need to reopen display */
447         SDLCloseDisplay( p_vout );
448         if( SDLOpenDisplay( p_vout ) )
449         {
450           intf_ErrMsg( "vout error: can't reopen display after resize" );
451           return( 1 );
452         }
453         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
454     }
455     
456     /*
457      * YUV Change 
458      */
459     if( p_vout->i_changes & VOUT_YUV_CHANGE )
460     {
461         p_vout->b_need_render = ! p_vout->b_need_render;
462         
463         /* Need to reopen display */
464         SDLCloseDisplay( p_vout );
465         if( SDLOpenDisplay( p_vout ) )
466         {
467           intf_ErrMsg( "error: can't reopen display after YUV change" );
468           return( 1 );
469         }
470         p_vout->i_changes &= ~VOUT_YUV_CHANGE;
471     }
472
473     /*
474      * Fullscreen change
475      */
476     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
477     {
478         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
479
480         SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
481
482         p_vout->p_sys->b_cursor_autohidden = 0;
483         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
484                         ! p_vout->p_sys->b_cursor_autohidden );
485
486         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
487     }
488
489     /*
490      * Pointer change
491      */
492     if( ! p_vout->p_sys->b_cursor_autohidden &&
493         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
494     {
495         /* Hide the mouse automatically */
496         p_vout->p_sys->b_cursor_autohidden = 1;
497         SDL_ShowCursor( 0 );
498     }
499
500     if( p_vout->i_changes & VOUT_CURSOR_CHANGE )
501     {
502         p_vout->p_sys->b_cursor = ! p_vout->p_sys->b_cursor;
503
504         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
505                         ! p_vout->p_sys->b_cursor_autohidden );
506
507         p_vout->i_changes &= ~VOUT_CURSOR_CHANGE;
508     }
509     
510     return( 0 );
511 }
512
513 /*****************************************************************************
514  * vout_SetPalette: sets an 8 bpp palette
515  *****************************************************************************
516  * This function sets the palette given as an argument. It does not return
517  * anything, but could later send information on which colors it was unable
518  * to set.
519  *****************************************************************************/
520 static void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
521                          u16 *blue, u16 *transp)
522 {
523      /* Create a display surface with a grayscale palette */
524     SDL_Color colors[256];
525     int i;
526   
527     /* Fill colors with color information */
528     for( i = 0; i < 256; i++ )
529     {
530         colors[ i ].r = red[ i ] >> 8;
531         colors[ i ].g = green[ i ] >> 8;
532         colors[ i ].b = blue[ i ] >> 8;
533     }
534
535     /* Set palette */
536     if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
537     {
538         intf_ErrMsg( "vout error: failed setting palette" );
539     }
540
541 }
542
543 /*****************************************************************************
544  * vout_Display: displays previously rendered output
545  *****************************************************************************
546  * This function send the currently rendered image to the display, wait until
547  * it is displayed and switch the two rendering buffer, preparing next frame.
548  *****************************************************************************/
549 static void vout_Display( vout_thread_t *p_vout )
550 {
551     SDL_Rect    disp;
552
553     if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
554     {
555         if( !p_vout->b_need_render )
556         {
557             /*
558              * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
559              * render 
560              */
561             /* TODO: support for streams other than 4:2:0 */
562             if( p_vout->p_rendered_pic->i_type != YUV_420_PICTURE )
563             {
564                 intf_ErrMsg("sdl vout error: no support for that kind of pictures");
565                 return;
566             }
567             /* create the overlay if necessary */
568             if( p_vout->p_sys->p_overlay == NULL )
569             {
570                 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay( 
571                                              p_vout->p_rendered_pic->i_width, 
572                                              p_vout->p_rendered_pic->i_height,
573                                              SDL_YV12_OVERLAY, 
574                                              p_vout->p_sys->p_display );
575
576                 if( p_vout->p_sys->p_overlay != NULL )
577                 {
578                     intf_WarnMsg( 2, "vout: YUV acceleration %s",
579                                   p_vout->p_sys->p_overlay->hw_overlay
580                                    ? "activated" : "unavailable !" ); 
581                 }
582             }
583
584             if( p_vout->p_sys->p_overlay == NULL )
585             {
586                 /* Overlay allocation failed, switch back to software mode */
587                 intf_ErrMsg( "vout error: could not create SDL overlay" );
588                 p_vout->b_need_render = 1;
589             }
590             else
591             {
592                 int i_x, i_y, i_w, i_h;
593
594                 SDL_LockYUVOverlay( p_vout->p_sys->p_overlay );
595                 /* copy the data into video buffers */
596                 /* Y first */
597                 pf_fast_memcpy( p_vout->p_sys->p_overlay->pixels[0],
598                                 p_vout->p_rendered_pic->p_y,
599                                 p_vout->p_sys->p_overlay->h *
600                                 p_vout->p_sys->p_overlay->pitches[0] );
601                 /* then V */
602                 pf_fast_memcpy( p_vout->p_sys->p_overlay->pixels[1],
603                                 p_vout->p_rendered_pic->p_v,
604                                 p_vout->p_sys->p_overlay->h *
605                                 p_vout->p_sys->p_overlay->pitches[1] / 2 );
606                 /* and U */
607                 pf_fast_memcpy( p_vout->p_sys->p_overlay->pixels[2],
608                                 p_vout->p_rendered_pic->p_u,
609                                 p_vout->p_sys->p_overlay->h *
610                                 p_vout->p_sys->p_overlay->pitches[2] / 2 );
611
612                 OutputCoords( p_vout->p_rendered_pic, 1,
613                               p_vout->p_sys->i_width,
614                               p_vout->p_sys->i_height,
615                               &i_x, &i_y,
616                               &i_w, &i_h);
617                 disp.x = i_x;
618                 disp.y = i_y;
619                 disp.w = i_w;
620                 disp.h = i_h;
621
622                 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
623                 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
624
625                 return;
626             }
627         }
628     
629         /* Software YUV: change display frame */
630         SDL_Flip( p_vout->p_sys->p_display );
631     }
632 }
633
634 /* following functions are local */
635
636 /*****************************************************************************
637  * SDLOpenDisplay: open and initialize SDL device
638  *****************************************************************************
639  * Open and initialize display according to preferences specified in the vout
640  * thread fields.
641  *****************************************************************************/
642 static int SDLOpenDisplay( vout_thread_t *p_vout )
643 {
644     SDL_Rect    clipping_rect;
645     Uint32      flags;
646     int bpp;
647     /* Open display 
648      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
649      */
650
651     /* init flags and cursor */
652     flags = SDL_ANYFORMAT | SDL_HWPALETTE;
653
654     if( p_vout->b_fullscreen )
655     {
656         flags |= SDL_FULLSCREEN;
657     }
658     else
659     {
660         flags |= SDL_RESIZABLE;
661     }
662
663     if( p_vout->b_need_render )
664     {
665         flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
666     }
667     else
668     {
669         flags |= SDL_SWSURFACE; /* save video memory */
670     }
671
672     bpp = SDL_VideoModeOK( p_vout->p_sys->i_width,
673                            p_vout->p_sys->i_height,
674                            p_vout->i_screen_depth, flags );
675
676     if( bpp == 0 )
677     {
678         intf_ErrMsg( "vout error: no video mode available" );
679         return( 1 );
680     }
681
682     p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
683                                                 p_vout->p_sys->i_height,
684                                                 bpp, flags);
685
686     if( p_vout->p_sys->p_display == NULL )
687     {
688         intf_ErrMsg( "vout error: cannot set video mode" );
689         return( 1 );
690     }
691
692     SDL_LockSurface( p_vout->p_sys->p_display );
693
694     SDL_WM_SetCaption( VOUT_TITLE " (SDL output)",
695                        VOUT_TITLE " (SDL output)" );
696     SDL_EventState(SDL_KEYUP , SDL_IGNORE);                /* ignore keys up */
697
698     if( p_vout->b_need_render )
699     {
700         p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
701         SDL_Flip(p_vout->p_sys->p_display);
702         p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
703         SDL_Flip(p_vout->p_sys->p_display);
704
705         /* Set clipping for text */
706         clipping_rect.x = 0;
707         clipping_rect.y = 0;
708         clipping_rect.w = p_vout->p_sys->p_display->w;
709         clipping_rect.h = p_vout->p_sys->p_display->h;
710         SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
711
712         /* Set thread information */
713         p_vout->i_width =           p_vout->p_sys->p_display->w;
714         p_vout->i_height =          p_vout->p_sys->p_display->h;
715         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
716
717         p_vout->i_screen_depth =
718             p_vout->p_sys->p_display->format->BitsPerPixel;
719         p_vout->i_bytes_per_pixel =
720             p_vout->p_sys->p_display->format->BytesPerPixel;
721
722         p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
723         p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
724         p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
725
726         /* FIXME: palette in 8bpp ?? */
727         /* Set and initialize buffers */
728         p_vout->pf_setbuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
729                                        p_vout->p_sys->p_sdl_buf[ 1 ] );
730     }
731     else
732     {
733         p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
734         p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
735
736         /* Set thread information */
737         p_vout->i_width =           p_vout->p_sys->p_display->w;
738         p_vout->i_height =          p_vout->p_sys->p_display->h;
739         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
740
741         p_vout->pf_setbuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
742                                        p_vout->p_sys->p_sdl_buf[ 1 ] );
743     }
744
745     p_vout->p_sys->b_reopen_display = 0;
746
747     return( 0 );
748 }
749
750 /*****************************************************************************
751  * SDLCloseDisplay: close and reset SDL device
752  *****************************************************************************
753  * This function returns all resources allocated by SDLOpenDisplay and restore
754  * the original state of the device.
755  *****************************************************************************/
756 static void SDLCloseDisplay( vout_thread_t *p_vout )
757 {
758     if( p_vout->p_sys->p_display != NULL )
759     {
760         if( p_vout->p_sys->p_overlay != NULL )
761         {            
762             SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
763             p_vout->p_sys->p_overlay = NULL;
764         }
765
766         SDL_UnlockSurface ( p_vout->p_sys->p_display );
767         SDL_FreeSurface( p_vout->p_sys->p_display );
768         p_vout->p_sys->p_display = NULL;
769     }
770 }
771
772 /*****************************************************************************
773  * OutputCoords: compute the dimensions of the destination image
774  *****************************************************************************
775  * This based on some code in SetBufferPicture... , it is also in use in the
776  * the xvideo plugin. Maybe we should think about putting standard video
777  * processing functions in a common library ?
778  *****************************************************************************/
779 static void OutputCoords( const picture_t *p_pic, const boolean_t scale,
780                           const int win_w, const int win_h,
781                           int *dx, int *dy, int *w, int *h )
782 {
783     if( !scale )
784     {
785         *w = p_pic->i_width; *h = p_pic->i_height;
786     }
787     else
788     {
789         *w = win_w;
790         switch( p_pic->i_aspect_ratio )
791         {
792             case AR_3_4_PICTURE:
793                 *h = win_w * 3 / 4;
794                 break;
795
796             case AR_16_9_PICTURE:
797                 *h = win_w * 9 / 16;
798                 break;
799
800             case AR_221_1_PICTURE:
801                 *h = win_w * 100 / 221;
802                 break;
803
804             case AR_SQUARE_PICTURE:
805             default:
806                 *h = win_w * p_pic->i_height / p_pic->i_width;
807                 break;
808         }
809
810         if( *h > win_h )
811         {
812             *h = win_h;
813             switch( p_pic->i_aspect_ratio )
814             {
815                 case AR_3_4_PICTURE:
816                     *w = win_h * 4 / 3;
817                     break;
818
819                 case AR_16_9_PICTURE:
820                     *w = win_h * 16 / 9;
821                     break;
822
823                 case AR_221_1_PICTURE:
824                     *w = win_h * 221 / 100;
825                     break;
826
827                 case AR_SQUARE_PICTURE:
828                 default:
829                     *w = win_h * p_pic->i_width / p_pic->i_height;
830                     break;
831             }
832         }
833     }
834
835     /* Set picture position */
836     *dx = (win_w - *w) / 2;
837     *dy = (win_h - *h) / 2;
838 }