1 /*****************************************************************************
2 * vout_sdl.c: SDL video output display method
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: vout_sdl.c,v 1.73 2001/12/20 15:43:15 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Pierre Baillet <oct@zoy.org>
9 * Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
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.
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.
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 *****************************************************************************/
26 #define MODULE_NAME sdl
27 #include "modules_inner.h"
29 /*****************************************************************************
31 *****************************************************************************/
34 #include <errno.h> /* ENOMEM */
35 #include <stdlib.h> /* free() */
36 #include <string.h> /* strerror() */
38 #include <sys/types.h>
40 # include <netinet/in.h> /* BSD: struct in_addr */
43 #include SDL_INCLUDE_FILE
53 #include "video_output.h"
55 #include "interface.h"
57 #include "stream_control.h" /* needed by input_ext-intf.h... */
58 #include "input_ext-intf.h"
61 #include "modules_export.h"
63 #define SDL_MAX_DIRECTBUFFERS 5
64 #define SDL_DEFAULT_BPP 16
66 /*****************************************************************************
67 * vout_sys_t: video output SDL method descriptor
68 *****************************************************************************
69 * This structure is part of the video output thread descriptor.
70 * It describes the SDL specific properties of an output thread.
71 *****************************************************************************/
72 typedef struct vout_sys_s
74 SDL_Surface * p_display; /* display device */
75 SDL_Overlay * p_overlay; /* An overlay we keep to grab the XVideo port */
81 boolean_t b_cursor_autohidden;
86 /*****************************************************************************
87 * picture_sys_t: direct buffer method descriptor
88 *****************************************************************************
89 * This structure is part of the picture descriptor, it describes the
90 * SDL specific properties of a direct buffer.
91 *****************************************************************************/
92 typedef struct picture_sys_s
94 SDL_Overlay *p_overlay;
98 /*****************************************************************************
100 *****************************************************************************/
101 static int vout_Probe ( probedata_t *p_data );
102 static int vout_Create ( struct vout_thread_s * );
103 static int vout_Init ( struct vout_thread_s * );
104 static void vout_End ( struct vout_thread_s * );
105 static void vout_Destroy ( struct vout_thread_s * );
106 static int vout_Manage ( struct vout_thread_s * );
107 static void vout_Display ( struct vout_thread_s *, struct picture_s * );
109 static int SDLOpenDisplay ( vout_thread_t *p_vout );
110 static void SDLCloseDisplay ( vout_thread_t *p_vout );
111 static int SDLNewPicture ( vout_thread_t *p_vout, picture_t *p_pic );
113 /*****************************************************************************
114 * Functions exported as capabilities. They are declared as static so that
115 * we don't pollute the namespace too much.
116 *****************************************************************************/
117 void _M( vout_getfunctions )( function_list_t * p_function_list )
119 p_function_list->pf_probe = vout_Probe;
120 p_function_list->functions.vout.pf_create = vout_Create;
121 p_function_list->functions.vout.pf_init = vout_Init;
122 p_function_list->functions.vout.pf_end = vout_End;
123 p_function_list->functions.vout.pf_destroy = vout_Destroy;
124 p_function_list->functions.vout.pf_manage = vout_Manage;
125 p_function_list->functions.vout.pf_display = vout_Display;
126 p_function_list->functions.vout.pf_setpalette = NULL;
129 /*****************************************************************************
130 * vout_Probe: probe the video driver and return a score
131 *****************************************************************************
132 * This function tries to initialize SDL and returns a score to the
133 * plugin manager so that it can select the best plugin.
134 *****************************************************************************/
135 static int vout_Probe( probedata_t *p_data )
137 if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
142 if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
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 )
159 /* Allocate structure */
160 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
161 if( p_vout->p_sys == NULL )
163 intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
167 /* Initialize library */
168 if( SDL_Init( SDL_INIT_VIDEO
170 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
171 | SDL_INIT_EVENTTHREAD
174 /* In debug mode you may want vlc to dump a core instead of staying
176 | SDL_INIT_NOPARACHUTE
180 intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
181 free( p_vout->p_sys );
185 p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
186 p_vout->p_sys->b_cursor_autohidden = 0;
187 p_vout->p_sys->i_lastmoved = mdate();
189 if( p_vout->render.i_height * p_vout->render.i_aspect
190 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
192 p_vout->p_sys->i_width = p_vout->render.i_height
193 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
194 p_vout->p_sys->i_height = p_vout->render.i_height;
198 p_vout->p_sys->i_width = p_vout->render.i_width;
199 p_vout->p_sys->i_height = p_vout->render.i_width
200 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
203 if( p_vout->p_sys->i_width <= 300 && p_vout->p_sys->i_height <= 300 )
205 p_vout->p_sys->i_width <<= 1;
206 p_vout->p_sys->i_height <<= 1;
208 else if( p_vout->p_sys->i_width <= 400 && p_vout->p_sys->i_height <= 400 )
210 p_vout->p_sys->i_width += p_vout->p_sys->i_width >> 1;
211 p_vout->p_sys->i_height += p_vout->p_sys->i_height >> 1;
214 if( SDLOpenDisplay( p_vout ) )
216 intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
217 SDL_QuitSubSystem( SDL_INIT_VIDEO );
218 free( p_vout->p_sys );
225 /*****************************************************************************
226 * vout_Init: initialize SDL video thread output method
227 *****************************************************************************
228 * This function initialize the SDL display device.
229 *****************************************************************************/
230 static int vout_Init( vout_thread_t *p_vout )
235 I_OUTPUTPICTURES = 0;
237 /* Initialize the output structure */
238 switch( p_vout->render.i_chroma )
240 case YUV_420_PICTURE:
241 p_vout->output.i_chroma = p_vout->render.i_chroma;
242 p_vout->output.i_width = p_vout->render.i_width;
243 p_vout->output.i_height = p_vout->render.i_height;
244 p_vout->output.i_aspect = p_vout->render.i_aspect;
251 /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
252 while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
256 /* Find an empty picture slot */
257 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
259 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
261 p_pic = p_vout->p_picture + i_index;
266 /* Allocate the picture if we found one */
267 if( p_pic == NULL || SDLNewPicture( p_vout, p_pic ) )
272 p_pic->i_status = DESTROYED_PICTURE;
273 p_pic->i_type = DIRECT_PICTURE;
275 p_pic->i_left_margin =
276 p_pic->i_right_margin =
277 p_pic->i_top_margin =
278 p_pic->i_bottom_margin = 0;
280 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
288 /*****************************************************************************
289 * vout_End: terminate Sys video thread output method
290 *****************************************************************************
291 * Terminate an output method created by vout_SDLCreate
292 *****************************************************************************/
293 static void vout_End( vout_thread_t *p_vout )
297 /* Free the output buffers we allocated */
298 for( i_index = I_OUTPUTPICTURES ; i_index ; )
301 SDL_UnlockYUVOverlay( PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
302 SDL_FreeYUVOverlay( PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
303 free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
307 /*****************************************************************************
308 * vout_Destroy: destroy Sys video thread output method
309 *****************************************************************************
310 * Terminate an output method created by vout_SDLCreate
311 *****************************************************************************/
312 static void vout_Destroy( vout_thread_t *p_vout )
314 SDLCloseDisplay( p_vout );
316 SDL_QuitSubSystem( SDL_INIT_VIDEO );
318 free( p_vout->p_sys );
321 static __inline__ void vout_Seek( off_t i_seek )
323 #define area p_main->p_intf->p_input->stream.p_selected_area
324 off_t i_tell = area->i_tell;
326 i_tell += i_seek * (off_t)50 * p_main->p_intf->p_input->stream.i_mux_rate;
328 i_tell = ( i_tell <= area->i_start ) ? area->i_start
329 : ( i_tell >= area->i_size ) ? area->i_size
332 input_Seek( p_main->p_intf->p_input, i_tell );
336 /*****************************************************************************
337 * vout_Manage: handle Sys events
338 *****************************************************************************
339 * This function should be called regularly by video output thread. It returns
340 * a non null value if an error occured.
341 *****************************************************************************/
342 static int vout_Manage( vout_thread_t *p_vout )
344 SDL_Event event; /* SDL event */
347 while( SDL_PollEvent(&event) )
351 case SDL_VIDEORESIZE: /* Resizing of window */
352 p_vout->p_sys->i_width = event.resize.w;
353 p_vout->p_sys->i_height = event.resize.h;
354 SDLCloseDisplay( p_vout );
355 SDLOpenDisplay( p_vout );
358 case SDL_MOUSEMOTION:
359 if( p_vout->p_sys->b_cursor &&
360 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
362 if( p_vout->p_sys->b_cursor_autohidden )
364 p_vout->p_sys->b_cursor_autohidden = 0;
369 p_vout->p_sys->i_lastmoved = mdate();
374 case SDL_MOUSEBUTTONUP:
375 switch( event.button.button )
377 case SDL_BUTTON_RIGHT:
378 p_main->p_intf->b_menu_change = 1;
383 case SDL_MOUSEBUTTONDOWN:
384 switch( event.button.button )
386 case SDL_BUTTON_LEFT:
387 /* In this part we will eventually manage
388 * clicks for DVD navigation for instance. For the
389 * moment just pause the stream. */
390 input_SetStatus( p_main->p_intf->p_input, INPUT_STATUS_PAUSE );
404 p_main->p_intf->b_die = 1;
407 case SDL_KEYDOWN: /* if a key is pressed */
409 switch( event.key.keysym.sym )
411 case SDLK_q: /* quit */
413 p_main->p_intf->b_die = 1;
416 case SDLK_f: /* switch to fullscreen */
417 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
420 case SDLK_c: /* toggle grayscale */
421 p_vout->b_grayscale = ! p_vout->b_grayscale;
422 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
425 case SDLK_i: /* toggle info */
426 p_vout->b_info = ! p_vout->b_info;
427 p_vout->i_changes |= VOUT_INFO_CHANGE;
430 case SDLK_s: /* toggle scaling */
431 p_vout->b_scale = ! p_vout->b_scale;
432 p_vout->i_changes |= VOUT_SCALE_CHANGE;
435 case SDLK_SPACE: /* toggle interface */
436 p_vout->b_interface = ! p_vout->b_interface;
437 p_vout->i_changes |= VOUT_INTF_CHANGE;
441 p_main->p_intf->b_menu_change = 1;
460 case SDLK_F10: network_ChannelJoin( 0 ); break;
461 case SDLK_F1: network_ChannelJoin( 1 ); break;
462 case SDLK_F2: network_ChannelJoin( 2 ); break;
463 case SDLK_F3: network_ChannelJoin( 3 ); break;
464 case SDLK_F4: network_ChannelJoin( 4 ); break;
465 case SDLK_F5: network_ChannelJoin( 5 ); break;
466 case SDLK_F6: network_ChannelJoin( 6 ); break;
467 case SDLK_F7: network_ChannelJoin( 7 ); break;
468 case SDLK_F8: network_ChannelJoin( 8 ); break;
469 case SDLK_F9: network_ChannelJoin( 9 ); break;
472 intf_DbgMsg( "unhandled key %i", event.key.keysym.sym );
482 /* Fullscreen change */
483 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
485 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
487 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
489 p_vout->p_sys->b_cursor_autohidden = 0;
490 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
491 ! p_vout->p_sys->b_cursor_autohidden );
493 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
497 if( ! p_vout->p_sys->b_cursor_autohidden &&
498 ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
500 /* Hide the mouse automatically */
501 p_vout->p_sys->b_cursor_autohidden = 1;
508 /*****************************************************************************
509 * vout_Display: displays previously rendered output
510 *****************************************************************************
511 * This function sends the currently rendered image to the display.
512 *****************************************************************************/
513 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
518 vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
525 SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
526 SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
527 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
530 /* following functions are local */
532 /*****************************************************************************
533 * SDLOpenDisplay: open and initialize SDL device
534 *****************************************************************************
535 * Open and initialize display according to preferences specified in the vout
537 *****************************************************************************/
538 static int SDLOpenDisplay( vout_thread_t *p_vout )
543 /* Initialize flags and cursor */
544 i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE;
545 i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
547 i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
548 SDL_DEFAULT_BPP, i_flags );
551 intf_ErrMsg( "vout error: no video mode available" );
555 p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
556 p_vout->p_sys->i_height,
559 if( p_vout->p_sys->p_display == NULL )
561 intf_ErrMsg( "vout error: cannot set video mode" );
565 SDL_LockSurface( p_vout->p_sys->p_display );
567 p_vout->p_sys->p_overlay =
568 SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
569 p_vout->p_sys->p_display );
571 if( p_vout->p_sys->p_overlay == NULL )
573 intf_ErrMsg( "vout error: cannot set overlay" );
574 SDL_UnlockSurface( p_vout->p_sys->p_display );
575 SDL_FreeSurface( p_vout->p_sys->p_display );
579 if( p_vout->p_sys->p_overlay->hw_overlay )
581 SDL_WM_SetCaption( VOUT_TITLE " (hardware SDL output)",
582 VOUT_TITLE " (hardware SDL output)" );
586 SDL_WM_SetCaption( VOUT_TITLE " (software SDL output)",
587 VOUT_TITLE " (software SDL output)" );
590 SDL_EventState( SDL_KEYUP, SDL_IGNORE ); /* ignore keys up */
595 /*****************************************************************************
596 * SDLCloseDisplay: close and reset SDL device
597 *****************************************************************************
598 * This function returns all resources allocated by SDLOpenDisplay and restore
599 * the original state of the device.
600 *****************************************************************************/
601 static void SDLCloseDisplay( vout_thread_t *p_vout )
603 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
604 SDL_UnlockSurface ( p_vout->p_sys->p_display );
605 SDL_FreeSurface( p_vout->p_sys->p_display );
608 /*****************************************************************************
609 * SDLNewPicture: allocate a picture
610 *****************************************************************************
611 * Returns 0 on success, -1 otherwise
612 *****************************************************************************/
613 static int SDLNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
615 int i_width = p_vout->output.i_width;
616 int i_height = p_vout->output.i_height;
618 switch( p_vout->output.i_chroma )
620 case YUV_420_PICTURE:
621 /* We know this chroma, allocate a buffer which will be used
622 * directly by the decoder */
623 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
625 if( p_pic->p_sys == NULL )
630 p_pic->p_sys->p_overlay =
631 SDL_CreateYUVOverlay( i_width, i_height,
633 p_vout->p_sys->p_display );
635 if( p_pic->p_sys->p_overlay == NULL )
637 free( p_pic->p_sys );
641 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
643 /* Precalculate some values */
644 p_pic->i_size = i_width * i_height;
645 p_pic->i_chroma_width = i_width / 2;
646 p_pic->i_chroma_size = i_height * ( i_width / 2 );
648 /* FIXME: try to get the right i_bytes value from p_overlay */
649 p_pic->planes[ Y_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 0 ];
650 p_pic->planes[ Y_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 );
651 p_pic->planes[ Y_PLANE ].i_line_bytes = i_width * sizeof( u8 );
653 p_pic->planes[ U_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 2 ];
654 p_pic->planes[ U_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
655 p_pic->planes[ U_PLANE ].i_line_bytes = p_pic->i_chroma_width * sizeof( u8 );
657 p_pic->planes[ V_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 1 ];
658 p_pic->planes[ V_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
659 p_pic->planes[ V_PLANE ].i_line_bytes = p_pic->i_chroma_width * sizeof( u8 );
666 /* Unknown chroma, tell the guy to get lost */