]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
* Fixed the sdl video output plugin that I broke 2 days ago.
[vlc] / plugins / sdl / vout_sdl.c
1 /*****************************************************************************
2  * vout_sdl.c: SDL video output display method
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  * $Id: vout_sdl.c,v 1.62 2001/08/06 20:45:55 gbazin 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 "threads.h"
48 #include "mtime.h"
49 #include "tests.h"
50
51 #include "video.h"
52 #include "video_output.h"
53
54 #include "intf_msg.h"
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 = main_GetIntVariable( VOUT_WIDTH_VAR, 
197                                 VOUT_WIDTH_DEFAULT );
198     p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
199                                 VOUT_HEIGHT_DEFAULT );
200
201     p_vout->p_sys->p_display = NULL;
202     p_vout->p_sys->p_overlay = NULL;
203
204     if( SDLOpenDisplay(p_vout) )
205     {
206         intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
207         free( p_vout->p_sys );
208         return( 1 );
209     }
210
211     /* FIXME: get rid of this ASAP, it's FUCKING UGLY */
212     { intf_thread_t * p_intf = p_main->p_intf;
213     intf_AssignKey(p_intf, SDLK_q,      INTF_KEY_QUIT, 0);
214     intf_AssignKey(p_intf, SDLK_ESCAPE, INTF_KEY_QUIT, 0);
215     /* intf_AssignKey(p_intf,3,'Q'); */
216     intf_AssignKey(p_intf, SDLK_0,      INTF_KEY_SET_CHANNEL,0);
217     intf_AssignKey(p_intf, SDLK_1,      INTF_KEY_SET_CHANNEL,1);
218     intf_AssignKey(p_intf, SDLK_2,      INTF_KEY_SET_CHANNEL,2);
219     intf_AssignKey(p_intf, SDLK_3,      INTF_KEY_SET_CHANNEL,3);
220     intf_AssignKey(p_intf, SDLK_4,      INTF_KEY_SET_CHANNEL,4);
221     intf_AssignKey(p_intf, SDLK_5,      INTF_KEY_SET_CHANNEL,5);
222     intf_AssignKey(p_intf, SDLK_6,      INTF_KEY_SET_CHANNEL,6);
223     intf_AssignKey(p_intf, SDLK_7,      INTF_KEY_SET_CHANNEL,7);
224     intf_AssignKey(p_intf, SDLK_8,      INTF_KEY_SET_CHANNEL,8);
225     intf_AssignKey(p_intf, SDLK_9,      INTF_KEY_SET_CHANNEL,9);
226     intf_AssignKey(p_intf, SDLK_PLUS,   INTF_KEY_INC_VOLUME, 0);
227     intf_AssignKey(p_intf, SDLK_MINUS,  INTF_KEY_DEC_VOLUME, 0);
228     intf_AssignKey(p_intf, SDLK_m,      INTF_KEY_TOGGLE_VOLUME, 0);
229     /* intf_AssignKey(p_intf,'M','M'); */
230     intf_AssignKey(p_intf, SDLK_g,      INTF_KEY_DEC_GAMMA, 0);
231     /* intf_AssignKey(p_intf,'G','G'); */
232     intf_AssignKey(p_intf, SDLK_c,      INTF_KEY_TOGGLE_GRAYSCALE, 0);
233     intf_AssignKey(p_intf, SDLK_SPACE,  INTF_KEY_TOGGLE_INTERFACE, 0);
234     intf_AssignKey(p_intf, SDLK_i,      INTF_KEY_TOGGLE_INFO, 0);
235     intf_AssignKey(p_intf, SDLK_s,      INTF_KEY_TOGGLE_SCALING, 0);
236     intf_AssignKey(p_intf, SDLK_d,      INTF_KEY_DUMP_STREAM, 0); }
237
238     return( 0 );
239 }
240
241 /*****************************************************************************
242  * vout_Init: initialize SDL video thread output method
243  *****************************************************************************
244  * This function initialize the SDL display device.
245  *****************************************************************************/
246 static int vout_Init( vout_thread_t *p_vout )
247 {
248     /* This hack is hugly, but hey, you are, too. */
249
250     SDL_Overlay *   p_overlay;
251     
252     p_overlay = SDL_CreateYUVOverlay( 
253            main_GetIntVariable( VOUT_WIDTH_VAR,VOUT_WIDTH_DEFAULT ),
254            main_GetIntVariable( VOUT_HEIGHT_VAR,VOUT_HEIGHT_DEFAULT ),
255                                       SDL_YV12_OVERLAY, 
256                                       p_vout->p_sys->p_display );
257
258     if( p_overlay == NULL )
259     {
260         intf_ErrMsg( "vout error: could not create SDL overlay" );
261         p_vout->b_need_render = 1;
262         return( 0 );
263     }
264
265     intf_WarnMsg( 2, "vout: YUV acceleration %s",
266               p_overlay->hw_overlay ? "activated" : "unavailable !" ); 
267     p_vout->b_need_render = !p_overlay->hw_overlay;
268
269     SDL_FreeYUVOverlay( p_overlay );
270
271     return( 0 );
272 }
273
274 /*****************************************************************************
275  * vout_End: terminate Sys video thread output method
276  *****************************************************************************
277  * Terminate an output method created by vout_SDLCreate
278  *****************************************************************************/
279 static void vout_End( vout_thread_t *p_vout )
280 {
281     SDLCloseDisplay( p_vout );
282     SDL_QuitSubSystem( SDL_INIT_VIDEO );
283 }
284
285 /*****************************************************************************
286  * vout_Destroy: destroy Sys video thread output method
287  *****************************************************************************
288  * Terminate an output method created by vout_SDLCreate
289  *****************************************************************************/
290 static void vout_Destroy( vout_thread_t *p_vout )
291 {
292     free( p_vout->p_sys );
293 }
294
295 /*****************************************************************************
296  * vout_Manage: handle Sys events
297  *****************************************************************************
298  * This function should be called regularly by video output thread. It returns
299  * a non null value if an error occured.
300  *****************************************************************************/
301 static int vout_Manage( vout_thread_t *p_vout )
302 {
303     SDL_Event event;                                            /* SDL event */
304     char *    p_key;
305
306     /* Process events */
307     while( SDL_PollEvent(&event) )
308     {
309         switch( event.type )
310         {
311         case SDL_VIDEORESIZE:                          /* Resizing of window */
312             p_vout->i_width = event.resize.w;
313             p_vout->i_height = event.resize.h;
314             p_vout->i_changes |= VOUT_SIZE_CHANGE;
315             break;
316
317         case SDL_MOUSEMOTION:
318             if( p_vout->p_sys->b_cursor &&
319                 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
320             {
321                 if( p_vout->p_sys->b_cursor_autohidden )
322                 {
323                     p_vout->p_sys->b_cursor_autohidden = 0;
324                     SDL_ShowCursor( 1 );
325                 }
326                 else
327                 {
328                     p_vout->p_sys->i_lastmoved = mdate();
329                 }
330             }
331             break;
332
333         case SDL_MOUSEBUTTONUP:
334             switch( event.button.button )
335             {
336             case SDL_BUTTON_RIGHT:
337                 p_main->p_intf->b_menu_change = 1;
338                 break;
339             }
340             break;
341
342         case SDL_MOUSEBUTTONDOWN:
343             switch( event.button.button )
344             {
345             case SDL_BUTTON_MIDDLE:
346                 p_vout->i_changes |= VOUT_CURSOR_CHANGE;
347                 break;
348             }
349             break;
350
351         case SDL_QUIT:
352             intf_ProcessKey( p_main->p_intf, SDLK_q );
353             break;
354
355         case SDL_KEYDOWN:                             /* if a key is pressed */
356
357             switch( event.key.keysym.sym )
358             {
359             case SDLK_f:                             /* switch to fullscreen */
360                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
361                 break;
362
363             case SDLK_y:                               /* switch to hard YUV */
364                 p_vout->i_changes |= VOUT_YUV_CHANGE;
365                 break;
366
367             case SDLK_c:                                 /* toggle grayscale */
368                 p_vout->b_grayscale = ! p_vout->b_grayscale;
369                 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
370                 break;
371
372             case SDLK_i:                                      /* toggle info */
373                 p_vout->b_info = ! p_vout->b_info;
374                 p_vout->i_changes |= VOUT_INFO_CHANGE;
375                 break;
376
377             case SDLK_s:                                   /* toggle scaling */
378                 p_vout->b_scale = ! p_vout->b_scale;
379                 p_vout->i_changes |= VOUT_SCALE_CHANGE;
380                 break;
381
382             case SDLK_SPACE:                             /* toggle interface */
383                 p_vout->b_interface = ! p_vout->b_interface;
384                 p_vout->i_changes |= VOUT_INTF_CHANGE;
385                 break;
386             
387             case SDLK_F10:
388                 network_ChannelJoin( 0 );
389                 break;
390             case SDLK_F1:
391                 network_ChannelJoin( 1 );
392                 break;
393             case SDLK_F2:
394                 network_ChannelJoin( 2 );
395                 break;
396             case SDLK_F3:
397                 network_ChannelJoin( 3 );
398                 break;
399             case SDLK_F4:
400                 network_ChannelJoin( 4 );
401                 break;
402             case SDLK_F5:
403                 network_ChannelJoin( 5 );
404                 break;
405             case SDLK_F6:
406                 network_ChannelJoin( 6 );
407                 break;
408             case SDLK_F7:
409                 network_ChannelJoin( 7 );
410                 break;
411             case SDLK_F8:
412                 network_ChannelJoin( 8 );
413                 break;
414             case SDLK_F9:
415                 network_ChannelJoin( 9 );
416                 break;
417
418             case SDLK_MENU:
419                 p_main->p_intf->b_menu_change = 1;
420                 break;
421                 
422             default:
423                 p_key = SDL_GetKeyName( event.key.keysym.sym ) ;
424                 if( intf_ProcessKey( p_main->p_intf, 
425                                      (char )event.key.keysym.sym ) )
426                 {
427                    intf_DbgMsg( "unhandled key '%c' (%i)", 
428                                 (char)event.key.keysym.sym, 
429                                 event.key.keysym.sym );                
430                 }
431                 break;
432             }
433             break;
434
435         default:
436             break;
437         }
438     }
439
440     /*
441      * Size Change 
442      */
443     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
444     {
445         p_vout->p_sys->i_width = p_vout->i_width;
446         p_vout->p_sys->i_height = p_vout->i_height;
447
448         /* Need to reopen display */
449         SDLCloseDisplay( p_vout );
450         if( SDLOpenDisplay( p_vout ) )
451         {
452           intf_ErrMsg( "vout error: can't reopen display after resize" );
453           return( 1 );
454         }
455         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
456     }
457     
458     /*
459      * YUV Change 
460      */
461     if( p_vout->i_changes & VOUT_YUV_CHANGE )
462     {
463         p_vout->b_need_render = ! p_vout->b_need_render;
464         
465         /* Need to reopen display */
466         SDLCloseDisplay( p_vout );
467         if( SDLOpenDisplay( p_vout ) )
468         {
469           intf_ErrMsg( "error: can't reopen display after YUV change" );
470           return( 1 );
471         }
472         p_vout->i_changes &= ~VOUT_YUV_CHANGE;
473     }
474
475     /*
476      * Fullscreen change
477      */
478     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
479     {
480         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
481
482         SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
483
484         p_vout->p_sys->b_cursor_autohidden = 0;
485         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
486                         ! p_vout->p_sys->b_cursor_autohidden );
487
488         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
489     }
490
491     /*
492      * Pointer change
493      */
494     if( ! p_vout->p_sys->b_cursor_autohidden &&
495         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
496     {
497         /* Hide the mouse automatically */
498         p_vout->p_sys->b_cursor_autohidden = 1;
499         SDL_ShowCursor( 0 );
500     }
501
502     if( p_vout->i_changes & VOUT_CURSOR_CHANGE )
503     {
504         p_vout->p_sys->b_cursor = ! p_vout->p_sys->b_cursor;
505
506         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
507                         ! p_vout->p_sys->b_cursor_autohidden );
508
509         p_vout->i_changes &= ~VOUT_CURSOR_CHANGE;
510     }
511     
512     return( 0 );
513 }
514
515 /*****************************************************************************
516  * vout_SetPalette: sets an 8 bpp palette
517  *****************************************************************************
518  * This function sets the palette given as an argument. It does not return
519  * anything, but could later send information on which colors it was unable
520  * to set.
521  *****************************************************************************/
522 static void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
523                          u16 *blue, u16 *transp)
524 {
525      /* Create a display surface with a grayscale palette */
526     SDL_Color colors[256];
527     int i;
528   
529     /* Fill colors with color information */
530     for( i = 0; i < 256; i++ )
531     {
532         colors[ i ].r = red[ i ] >> 8;
533         colors[ i ].g = green[ i ] >> 8;
534         colors[ i ].b = blue[ i ] >> 8;
535     }
536
537     /* Set palette */
538     if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
539     {
540         intf_ErrMsg( "vout error: failed setting palette" );
541     }
542
543 }
544
545 /*****************************************************************************
546  * vout_Display: displays previously rendered output
547  *****************************************************************************
548  * This function send the currently rendered image to the display, wait until
549  * it is displayed and switch the two rendering buffer, preparing next frame.
550  *****************************************************************************/
551 static void vout_Display( vout_thread_t *p_vout )
552 {
553     SDL_Rect    disp;
554
555     if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
556     {
557         if( !p_vout->b_need_render )
558         {
559             /*
560              * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
561              * render 
562              */
563             /* TODO: support for streams other than 4:2:0 */
564             /* create the overlay if necessary */
565             if( p_vout->p_sys->p_overlay == NULL )
566             {
567                 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay( 
568                                              p_vout->p_rendered_pic->i_width, 
569                                              p_vout->p_rendered_pic->i_height,
570                                              SDL_YV12_OVERLAY, 
571                                              p_vout->p_sys->p_display );
572
573                 if( p_vout->p_sys->p_overlay != NULL )
574                 {
575                     intf_WarnMsg( 2, "vout: YUV acceleration %s",
576                                   p_vout->p_sys->p_overlay->hw_overlay
577                                    ? "activated" : "unavailable !" ); 
578                 }
579             }
580
581             if( p_vout->p_sys->p_overlay == NULL )
582             {
583                 /* Overlay allocation failed, switch back to software mode */
584                 intf_ErrMsg( "vout error: could not create SDL overlay" );
585                 p_vout->b_need_render = 1;
586             }
587             else
588             {
589                 int i_x, i_y, i_w, i_h;
590
591                 SDL_LockYUVOverlay( p_vout->p_sys->p_overlay );
592                 /* copy the data into video buffers */
593                 /* Y first */
594                 memcpy( p_vout->p_sys->p_overlay->pixels[0],
595                         p_vout->p_rendered_pic->p_y,
596                         p_vout->p_sys->p_overlay->h *
597                         p_vout->p_sys->p_overlay->pitches[0] );
598                 /* then V */
599                 memcpy( p_vout->p_sys->p_overlay->pixels[1],
600                         p_vout->p_rendered_pic->p_v,
601                         p_vout->p_sys->p_overlay->h *
602                         p_vout->p_sys->p_overlay->pitches[1] / 2 );
603                 /* and U */
604                 memcpy( p_vout->p_sys->p_overlay->pixels[2],
605                         p_vout->p_rendered_pic->p_u,
606                         p_vout->p_sys->p_overlay->h *
607                         p_vout->p_sys->p_overlay->pitches[2] / 2 );
608
609                 OutputCoords( p_vout->p_rendered_pic, 1,
610                               p_vout->p_sys->i_width,
611                               p_vout->p_sys->i_height,
612                               &i_x, &i_y,
613                               &i_w, &i_h);
614                 disp.x = i_x;
615                 disp.y = i_y;
616                 disp.w = i_w;
617                 disp.h = i_h;
618
619                 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
620                 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
621
622                 return;
623             }
624         }
625     
626         /* Software YUV: change display frame */
627         SDL_Flip( p_vout->p_sys->p_display );
628     }
629 }
630
631 /* following functions are local */
632
633 /*****************************************************************************
634  * SDLOpenDisplay: open and initialize SDL device
635  *****************************************************************************
636  * Open and initialize display according to preferences specified in the vout
637  * thread fields.
638  *****************************************************************************/
639 static int SDLOpenDisplay( vout_thread_t *p_vout )
640 {
641     SDL_Rect    clipping_rect;
642     Uint32      flags;
643     int bpp;
644     /* Open display 
645      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
646      */
647
648     /* init flags and cursor */
649     flags = SDL_ANYFORMAT | SDL_HWPALETTE;
650
651     if( p_vout->b_fullscreen )
652     {
653         flags |= SDL_FULLSCREEN;
654     }
655     else
656     {
657         flags |= SDL_RESIZABLE;
658     }
659
660     if( p_vout->b_need_render )
661     {
662         flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
663     }
664     else
665     {
666         flags |= SDL_SWSURFACE; /* save video memory */
667     }
668
669     bpp = SDL_VideoModeOK( p_vout->p_sys->i_width,
670                            p_vout->p_sys->i_height,
671                            p_vout->i_screen_depth, flags );
672
673     if( bpp == 0 )
674     {
675         intf_ErrMsg( "vout error: no video mode available" );
676         return( 1 );
677     }
678
679     p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
680                                                 p_vout->p_sys->i_height,
681                                                 bpp, flags);
682
683     if( p_vout->p_sys->p_display == NULL )
684     {
685         intf_ErrMsg( "vout error: cannot set video mode" );
686         return( 1 );
687     }
688
689     SDL_LockSurface( p_vout->p_sys->p_display );
690
691     SDL_WM_SetCaption( VOUT_TITLE " (SDL output)",
692                        VOUT_TITLE " (SDL output)" );
693     SDL_EventState(SDL_KEYUP , SDL_IGNORE);                /* ignore keys up */
694
695     if( p_vout->b_need_render )
696     {
697         p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
698         SDL_Flip(p_vout->p_sys->p_display);
699         p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
700         SDL_Flip(p_vout->p_sys->p_display);
701
702         /* Set clipping for text */
703         clipping_rect.x = 0;
704         clipping_rect.y = 0;
705         clipping_rect.w = p_vout->p_sys->p_display->w;
706         clipping_rect.h = p_vout->p_sys->p_display->h;
707         SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
708
709         /* Set thread information */
710         p_vout->i_width =           p_vout->p_sys->p_display->w;
711         p_vout->i_height =          p_vout->p_sys->p_display->h;
712         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
713
714         p_vout->i_screen_depth =
715             p_vout->p_sys->p_display->format->BitsPerPixel;
716         p_vout->i_bytes_per_pixel =
717             p_vout->p_sys->p_display->format->BytesPerPixel;
718
719         p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
720         p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
721         p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
722
723         /* FIXME: palette in 8bpp ?? */
724         /* Set and initialize buffers */
725         p_vout->pf_setbuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
726                                        p_vout->p_sys->p_sdl_buf[ 1 ] );
727     }
728     else
729     {
730         p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
731         p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
732
733         /* Set thread information */
734         p_vout->i_width =           p_vout->p_sys->p_display->w;
735         p_vout->i_height =          p_vout->p_sys->p_display->h;
736         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
737
738         p_vout->pf_setbuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
739                                        p_vout->p_sys->p_sdl_buf[ 1 ] );
740     }
741
742     p_vout->p_sys->b_reopen_display = 0;
743
744     return( 0 );
745 }
746
747 /*****************************************************************************
748  * SDLCloseDisplay: close and reset SDL device
749  *****************************************************************************
750  * This function returns all resources allocated by SDLOpenDisplay and restore
751  * the original state of the device.
752  *****************************************************************************/
753 static void SDLCloseDisplay( vout_thread_t *p_vout )
754 {
755     if( p_vout->p_sys->p_display != NULL )
756     {
757         if( p_vout->p_sys->p_overlay != NULL )
758         {            
759             SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
760             p_vout->p_sys->p_overlay = NULL;
761         }
762
763         SDL_UnlockSurface ( p_vout->p_sys->p_display );
764         SDL_FreeSurface( p_vout->p_sys->p_display );
765         p_vout->p_sys->p_display = NULL;
766     }
767 }
768
769 /*****************************************************************************
770  * OutputCoords: compute the dimensions of the destination image
771  *****************************************************************************
772  * This based on some code in SetBufferPicture... , it is also in use in the
773  * the xvideo plugin. Maybe we should think about putting standard video
774  * processing functions in a common library ?
775  *****************************************************************************/
776 static void OutputCoords( const picture_t *p_pic, const boolean_t scale,
777                           const int win_w, const int win_h,
778                           int *dx, int *dy, int *w, int *h )
779 {
780     if( !scale )
781     {
782         *w = p_pic->i_width; *h = p_pic->i_height;
783     }
784     else
785     {
786         *w = win_w;
787         switch( p_pic->i_aspect_ratio )
788         {
789             case AR_3_4_PICTURE:
790                 *h = win_w * 3 / 4;
791                 break;
792
793             case AR_16_9_PICTURE:
794                 *h = win_w * 9 / 16;
795                 break;
796
797             case AR_221_1_PICTURE:
798                 *h = win_w * 100 / 221;
799                 break;
800
801             case AR_SQUARE_PICTURE:
802             default:
803                 *h = win_w * p_pic->i_height / p_pic->i_width;
804                 break;
805         }
806
807         if( *h > win_h )
808         {
809             *h = win_h;
810             switch( p_pic->i_aspect_ratio )
811             {
812                 case AR_3_4_PICTURE:
813                     *w = win_h * 4 / 3;
814                     break;
815
816                 case AR_16_9_PICTURE:
817                     *w = win_h * 16 / 9;
818                     break;
819
820                 case AR_221_1_PICTURE:
821                     *w = win_h * 221 / 100;
822                     break;
823
824                 case AR_SQUARE_PICTURE:
825                 default:
826                     *w = win_h * p_pic->i_width / p_pic->i_height;
827                     break;
828             }
829         }
830     }
831
832     /* Set picture position */
833     *dx = (win_w - *w) / 2;
834     *dy = (win_h - *h) / 2;
835 }