]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
* Fixed aspect ratio handling.
[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.69 2001/12/13 12:47:17 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 "common.h"
46 #include "intf_msg.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 "interface.h"
55
56 #include "modules.h"
57 #include "modules_export.h"
58
59 #define SDL_MAX_DIRECTBUFFERS 5
60 #define SDL_DEFAULT_BPP 16
61
62 /*****************************************************************************
63  * vout_sys_t: video output SDL method descriptor
64  *****************************************************************************
65  * This structure is part of the video output thread descriptor.
66  * It describes the SDL specific properties of an output thread.
67  *****************************************************************************/
68 typedef struct vout_sys_s
69 {
70     SDL_Surface *   p_display;                             /* display device */
71
72     int i_width;
73     int i_height;
74
75     boolean_t   b_cursor;
76     boolean_t   b_cursor_autohidden;
77     mtime_t     i_lastmoved;
78
79 } vout_sys_t;
80
81 /*****************************************************************************
82  * picture_sys_t: direct buffer method descriptor
83  *****************************************************************************
84  * This structure is part of the picture descriptor, it describes the
85  * SDL specific properties of a direct buffer.
86  *****************************************************************************/
87 typedef struct picture_sys_s
88 {
89     SDL_Overlay *p_overlay;
90
91 } picture_sys_t;
92
93 /*****************************************************************************
94  * Local prototypes.
95  *****************************************************************************/
96 static int  vout_Probe      ( probedata_t *p_data );
97 static int  vout_Create     ( struct vout_thread_s * );
98 static int  vout_Init       ( struct vout_thread_s * );
99 static void vout_End        ( struct vout_thread_s * );
100 static void vout_Destroy    ( struct vout_thread_s * );
101 static int  vout_Manage     ( struct vout_thread_s * );
102 static void vout_Display    ( struct vout_thread_s *, struct picture_s * );
103
104 static int  SDLOpenDisplay      ( vout_thread_t *p_vout );
105 static void SDLCloseDisplay     ( vout_thread_t *p_vout );
106 static int  SDLNewPicture       ( vout_thread_t *p_vout, picture_t *p_pic );
107
108 /*****************************************************************************
109  * Functions exported as capabilities. They are declared as static so that
110  * we don't pollute the namespace too much.
111  *****************************************************************************/
112 void _M( vout_getfunctions )( function_list_t * p_function_list )
113 {
114     p_function_list->pf_probe = vout_Probe;
115     p_function_list->functions.vout.pf_create     = vout_Create;
116     p_function_list->functions.vout.pf_init       = vout_Init;
117     p_function_list->functions.vout.pf_end        = vout_End;
118     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
119     p_function_list->functions.vout.pf_manage     = vout_Manage;
120     p_function_list->functions.vout.pf_display    = vout_Display;
121     p_function_list->functions.vout.pf_setpalette = NULL;
122 }
123
124 /*****************************************************************************
125  * vout_Probe: probe the video driver and return a score
126  *****************************************************************************
127  * This function tries to initialize SDL and returns a score to the
128  * plugin manager so that it can select the best plugin.
129  *****************************************************************************/
130 static int vout_Probe( probedata_t *p_data )
131 {
132     if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
133     {
134         return( 999 );
135     }
136
137     return( 100 );
138 }
139
140 /*****************************************************************************
141  * vout_Create: allocate SDL video thread output method
142  *****************************************************************************
143  * This function allocate and initialize a SDL vout method. It uses some of the
144  * vout properties to choose the correct mode, and change them according to the
145  * mode actually used.
146  *****************************************************************************/
147 static int vout_Create( vout_thread_t *p_vout )
148 {
149     /* Allocate structure */
150     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
151     if( p_vout->p_sys == NULL )
152     {
153         intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
154         return( 1 );
155     }
156
157     /* Initialize library */
158     if( SDL_Init( SDL_INIT_VIDEO
159 #ifndef WIN32
160     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
161                 | SDL_INIT_EVENTTHREAD
162 #endif
163 #ifdef DEBUG
164     /* In debug mode you may want vlc to dump a core instead of staying
165      * stuck */
166                 | SDL_INIT_NOPARACHUTE
167 #endif
168                 ) < 0 )
169     {
170         intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
171         free( p_vout->p_sys );
172         return( 1 );
173     }
174
175     p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
176     p_vout->p_sys->b_cursor_autohidden = 0;
177     p_vout->p_sys->i_lastmoved = mdate();
178
179     p_vout->p_sys->i_width = p_vout->render.i_width;
180     p_vout->p_sys->i_height = p_vout->render.i_height;
181
182     if( SDLOpenDisplay( p_vout ) )
183     {
184         intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
185         free( p_vout->p_sys );
186         return( 1 );
187     }
188
189     return( 0 );
190 }
191
192 /*****************************************************************************
193  * vout_Init: initialize SDL video thread output method
194  *****************************************************************************
195  * This function initialize the SDL display device.
196  *****************************************************************************/
197 static int vout_Init( vout_thread_t *p_vout )
198 {
199     int i_index;
200     picture_t *p_pic;
201
202     I_OUTPUTPICTURES = 0;
203
204     /* Initialize the output structure */
205     switch( p_vout->render.i_chroma )
206     {
207         case YUV_420_PICTURE:
208             p_vout->output.i_chroma = p_vout->render.i_chroma;
209             p_vout->output.i_width  = p_vout->render.i_width;
210             p_vout->output.i_height = p_vout->render.i_height;
211             p_vout->output.i_aspect = p_vout->render.i_aspect;
212             break;
213
214         default:
215             return( 0 );
216     }
217
218     /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
219     while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
220     {
221         p_pic = NULL;
222
223         /* Find an empty picture slot */
224         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
225         {
226             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
227             {
228                 p_pic = p_vout->p_picture + i_index;
229                 break;
230             }
231         }
232
233         /* Allocate the picture */
234         if( SDLNewPicture( p_vout, p_pic ) )
235         {
236             break;
237         }
238
239         p_pic->i_status        = DESTROYED_PICTURE;
240         p_pic->i_type          = DIRECT_PICTURE;
241
242         p_pic->i_left_margin   =
243         p_pic->i_right_margin  =
244         p_pic->i_top_margin    =
245         p_pic->i_bottom_margin = 0;
246
247         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
248
249         I_OUTPUTPICTURES++;
250     }
251
252     return( 0 );
253 }
254
255 /*****************************************************************************
256  * vout_End: terminate Sys video thread output method
257  *****************************************************************************
258  * Terminate an output method created by vout_SDLCreate
259  *****************************************************************************/
260 static void vout_End( vout_thread_t *p_vout )
261 {
262     int i_index;
263
264     /* Free the output buffers we allocated */
265     for( i_index = I_OUTPUTPICTURES ; i_index ; )
266     {
267         i_index--;
268         SDL_UnlockYUVOverlay( PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
269         SDL_FreeYUVOverlay( PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
270         free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
271     }
272 }
273
274 /*****************************************************************************
275  * vout_Destroy: destroy Sys video thread output method
276  *****************************************************************************
277  * Terminate an output method created by vout_SDLCreate
278  *****************************************************************************/
279 static void vout_Destroy( vout_thread_t *p_vout )
280 {
281     SDLCloseDisplay( p_vout );
282
283     SDL_QuitSubSystem( SDL_INIT_VIDEO );
284
285     free( p_vout->p_sys );
286 }
287
288 /*****************************************************************************
289  * vout_Manage: handle Sys events
290  *****************************************************************************
291  * This function should be called regularly by video output thread. It returns
292  * a non null value if an error occured.
293  *****************************************************************************/
294 static int vout_Manage( vout_thread_t *p_vout )
295 {
296     SDL_Event event;                                            /* SDL event */
297
298     /* Process events */
299     while( SDL_PollEvent(&event) )
300     {
301         switch( event.type )
302         {
303         case SDL_VIDEORESIZE:                          /* Resizing of window */
304             p_vout->p_sys->i_width = event.resize.w;
305             p_vout->p_sys->i_height = event.resize.h;
306             SDLCloseDisplay( p_vout );
307             SDLOpenDisplay( p_vout );
308             break;
309
310         case SDL_MOUSEMOTION:
311             if( p_vout->p_sys->b_cursor &&
312                 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
313             {
314                 if( p_vout->p_sys->b_cursor_autohidden )
315                 {
316                     p_vout->p_sys->b_cursor_autohidden = 0;
317                     SDL_ShowCursor( 1 );
318                 }
319                 else
320                 {
321                     p_vout->p_sys->i_lastmoved = mdate();
322                 }
323             }
324             break;
325
326         case SDL_MOUSEBUTTONUP:
327             switch( event.button.button )
328             {
329             case SDL_BUTTON_RIGHT:
330                 p_main->p_intf->b_menu_change = 1;
331                 break;
332             }
333             break;
334
335         case SDL_MOUSEBUTTONDOWN:
336             switch( event.button.button )
337             {
338             case SDL_BUTTON_LEFT:
339                 /* Handle clicks */
340                 break;
341             }
342             break;
343
344         case SDL_QUIT:
345             p_main->p_intf->b_die = 1;
346             break;
347
348         case SDL_KEYDOWN:                             /* if a key is pressed */
349
350             switch( event.key.keysym.sym )
351             {
352             case SDLK_q:                                             /* quit */
353             case SDLK_ESCAPE:
354                 p_main->p_intf->b_die = 1;
355                 break;
356
357             case SDLK_f:                             /* switch to fullscreen */
358                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
359                 break;
360
361             case SDLK_c:                                 /* toggle grayscale */
362                 p_vout->b_grayscale = ! p_vout->b_grayscale;
363                 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
364                 break;
365
366             case SDLK_i:                                      /* toggle info */
367                 p_vout->b_info = ! p_vout->b_info;
368                 p_vout->i_changes |= VOUT_INFO_CHANGE;
369                 break;
370
371             case SDLK_s:                                   /* toggle scaling */
372                 p_vout->b_scale = ! p_vout->b_scale;
373                 p_vout->i_changes |= VOUT_SCALE_CHANGE;
374                 break;
375
376             case SDLK_SPACE:                             /* toggle interface */
377                 p_vout->b_interface = ! p_vout->b_interface;
378                 p_vout->i_changes |= VOUT_INTF_CHANGE;
379                 break;
380             
381             case SDLK_MENU:
382                 p_main->p_intf->b_menu_change = 1;
383                 break;
384                 
385             case SDLK_F10: network_ChannelJoin( 0 ); break;
386             case SDLK_F1:  network_ChannelJoin( 1 ); break;
387             case SDLK_F2:  network_ChannelJoin( 2 ); break;
388             case SDLK_F3:  network_ChannelJoin( 3 ); break;
389             case SDLK_F4:  network_ChannelJoin( 4 ); break;
390             case SDLK_F5:  network_ChannelJoin( 5 ); break;
391             case SDLK_F6:  network_ChannelJoin( 6 ); break;
392             case SDLK_F7:  network_ChannelJoin( 7 ); break;
393             case SDLK_F8:  network_ChannelJoin( 8 ); break;
394             case SDLK_F9:  network_ChannelJoin( 9 ); break;
395
396             default:
397                 intf_DbgMsg( "unhandled key %i", event.key.keysym.sym );
398                 break;
399             }
400             break;
401
402         default:
403             break;
404         }
405     }
406
407     /* Fullscreen change */
408     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
409     {
410         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
411
412         SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
413
414         p_vout->p_sys->b_cursor_autohidden = 0;
415         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
416                         ! p_vout->p_sys->b_cursor_autohidden );
417
418         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
419     }
420
421     /* Pointer change */
422     if( ! p_vout->p_sys->b_cursor_autohidden &&
423         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
424     {
425         /* Hide the mouse automatically */
426         p_vout->p_sys->b_cursor_autohidden = 1;
427         SDL_ShowCursor( 0 );
428     }
429
430     return( 0 );
431 }
432
433 /*****************************************************************************
434  * vout_Display: displays previously rendered output
435  *****************************************************************************
436  * This function sends the currently rendered image to the display.
437  *****************************************************************************/
438 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
439 {
440     int x, y, w, h;
441     SDL_Rect disp;
442
443     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
444                        &x, &y, &w, &h );
445     disp.x = x;
446     disp.y = y;
447     disp.w = w;
448     disp.h = h;
449
450     SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
451     SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
452     SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
453 }
454
455 /* following functions are local */
456
457 /*****************************************************************************
458  * SDLOpenDisplay: open and initialize SDL device
459  *****************************************************************************
460  * Open and initialize display according to preferences specified in the vout
461  * thread fields.
462  *****************************************************************************/
463 static int SDLOpenDisplay( vout_thread_t *p_vout )
464 {
465     Uint32 i_flags;
466     int    i_bpp;
467
468     /* Initialize flags and cursor */
469     i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE;
470     i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
471
472     i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
473                              SDL_DEFAULT_BPP, i_flags );
474     if( i_bpp == 0 )
475     {
476         intf_ErrMsg( "vout error: no video mode available" );
477         return( 1 );
478     }
479
480     p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
481                                                  p_vout->p_sys->i_height,
482                                                  i_bpp, i_flags );
483
484     if( p_vout->p_sys->p_display == NULL )
485     {
486         intf_ErrMsg( "vout error: cannot set video mode" );
487         return( 1 );
488     }
489
490     SDL_LockSurface( p_vout->p_sys->p_display );
491
492     SDL_WM_SetCaption( VOUT_TITLE " (SDL output)",
493                        VOUT_TITLE " (SDL output)" );
494     SDL_EventState( SDL_KEYUP, SDL_IGNORE );               /* ignore keys up */
495
496     return( 0 );
497 }
498
499 /*****************************************************************************
500  * SDLCloseDisplay: close and reset SDL device
501  *****************************************************************************
502  * This function returns all resources allocated by SDLOpenDisplay and restore
503  * the original state of the device.
504  *****************************************************************************/
505 static void SDLCloseDisplay( vout_thread_t *p_vout )
506 {
507     SDL_UnlockSurface ( p_vout->p_sys->p_display );
508     SDL_FreeSurface( p_vout->p_sys->p_display );
509 }
510
511 /*****************************************************************************
512  * SDLNewPicture: allocate a picture
513  *****************************************************************************
514  * Returns 0 on success, -1 otherwise
515  *****************************************************************************/
516 static int SDLNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
517 {
518     int i_width  = p_vout->output.i_width;
519     int i_height = p_vout->output.i_height;
520
521     switch( p_vout->output.i_chroma )
522     {
523         case YUV_420_PICTURE:
524             /* We know this chroma, allocate a buffer which will be used
525              * directly by the decoder */
526             p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
527
528             if( p_pic->p_sys == NULL )
529             {
530                 return -1;
531             }
532
533             p_pic->p_sys->p_overlay =
534                 SDL_CreateYUVOverlay( i_width, i_height,
535                                       SDL_YV12_OVERLAY,
536                                       p_vout->p_sys->p_display );
537
538             if( p_pic->p_sys->p_overlay == NULL )
539             {
540                 free( p_pic->p_sys );
541                 return -1;
542             }
543
544             SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
545
546             /* Precalculate some values */
547             p_pic->i_size         = i_width * i_height;
548             p_pic->i_chroma_width = i_width / 2;
549             p_pic->i_chroma_size  = i_height * ( i_width / 2 );
550
551             /* FIXME: try to get the right i_bytes value from p_overlay */
552             p_pic->planes[ Y_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[0];
553             p_pic->planes[ Y_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 );
554             p_pic->planes[ U_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[2];
555             p_pic->planes[ U_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
556             p_pic->planes[ V_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[1];
557             p_pic->planes[ V_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
558
559             p_pic->i_planes = 3;
560
561             return 0;
562
563         default:
564             /* Unknown chroma, tell the guy to get lost */
565             p_pic->i_planes = 0;
566
567             return 0;
568     }
569 }
570