1 /*****************************************************************************
2 * vout_sdl.c: SDL video output display method
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: vout_sdl.c,v 1.75 2002/01/02 14:37:42 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 /*****************************************************************************
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <stdlib.h> /* free() */
31 #include <string.h> /* strerror() */
33 #include <videolan/vlc.h>
35 #include <sys/types.h>
37 # include <netinet/in.h> /* BSD: struct in_addr */
40 #include SDL_INCLUDE_FILE
45 #include "video_output.h"
47 #include "interface.h"
49 #include "stream_control.h" /* needed by input_ext-intf.h... */
50 #include "input_ext-intf.h"
52 #define SDL_MAX_DIRECTBUFFERS 5
53 #define SDL_DEFAULT_BPP 16
55 /*****************************************************************************
56 * vout_sys_t: video output SDL method descriptor
57 *****************************************************************************
58 * This structure is part of the video output thread descriptor.
59 * It describes the SDL specific properties of an output thread.
60 *****************************************************************************/
61 typedef struct vout_sys_s
63 SDL_Surface * p_display; /* display device */
64 SDL_Overlay * p_overlay; /* An overlay we keep to grab the XVideo port */
72 boolean_t b_cursor_autohidden;
77 /*****************************************************************************
78 * picture_sys_t: direct buffer method descriptor
79 *****************************************************************************
80 * This structure is part of the picture descriptor, it describes the
81 * SDL specific properties of a direct buffer.
82 *****************************************************************************/
83 typedef struct picture_sys_s
85 SDL_Overlay *p_overlay;
89 /*****************************************************************************
91 *****************************************************************************/
92 static int vout_Probe ( probedata_t *p_data );
93 static int vout_Create ( struct vout_thread_s * );
94 static int vout_Init ( struct vout_thread_s * );
95 static void vout_End ( struct vout_thread_s * );
96 static void vout_Destroy ( struct vout_thread_s * );
97 static int vout_Manage ( struct vout_thread_s * );
98 static void vout_Display ( struct vout_thread_s *, struct picture_s * );
100 static int SDLOpenDisplay ( vout_thread_t *p_vout );
101 static void SDLCloseDisplay ( vout_thread_t *p_vout );
102 static int SDLNewPicture ( vout_thread_t *p_vout, picture_t *p_pic );
104 /*****************************************************************************
105 * Functions exported as capabilities. They are declared as static so that
106 * we don't pollute the namespace too much.
107 *****************************************************************************/
108 void _M( vout_getfunctions )( function_list_t * p_function_list )
110 p_function_list->pf_probe = vout_Probe;
111 p_function_list->functions.vout.pf_create = vout_Create;
112 p_function_list->functions.vout.pf_init = vout_Init;
113 p_function_list->functions.vout.pf_end = vout_End;
114 p_function_list->functions.vout.pf_destroy = vout_Destroy;
115 p_function_list->functions.vout.pf_manage = vout_Manage;
116 p_function_list->functions.vout.pf_display = vout_Display;
117 p_function_list->functions.vout.pf_setpalette = NULL;
120 /*****************************************************************************
121 * vout_Probe: probe the video driver and return a score
122 *****************************************************************************
123 * This function tries to initialize SDL and returns a score to the
124 * plugin manager so that it can select the best plugin.
125 *****************************************************************************/
126 static int vout_Probe( probedata_t *p_data )
128 if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
136 /*****************************************************************************
137 * vout_Create: allocate SDL video thread output method
138 *****************************************************************************
139 * This function allocate and initialize a SDL vout method. It uses some of the
140 * vout properties to choose the correct mode, and change them according to the
141 * mode actually used.
142 *****************************************************************************/
143 static int vout_Create( vout_thread_t *p_vout )
145 /* Allocate structure */
146 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
147 if( p_vout->p_sys == NULL )
149 intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
153 /* Initialize library */
154 if( SDL_Init( SDL_INIT_VIDEO
156 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
157 | SDL_INIT_EVENTTHREAD
160 /* In debug mode you may want vlc to dump a core instead of staying
162 | SDL_INIT_NOPARACHUTE
166 intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
167 free( p_vout->p_sys );
171 p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
172 p_vout->p_sys->b_cursor_autohidden = 0;
173 p_vout->p_sys->i_lastmoved = mdate();
175 if( p_vout->render.i_height * p_vout->render.i_aspect
176 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
178 p_vout->p_sys->i_width = p_vout->render.i_height
179 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
180 p_vout->p_sys->i_height = p_vout->render.i_height;
184 p_vout->p_sys->i_width = p_vout->render.i_width;
185 p_vout->p_sys->i_height = p_vout->render.i_width
186 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
190 if( p_vout->p_sys->i_width <= 300 && p_vout->p_sys->i_height <= 300 )
192 p_vout->p_sys->i_width <<= 1;
193 p_vout->p_sys->i_height <<= 1;
195 else if( p_vout->p_sys->i_width <= 400 && p_vout->p_sys->i_height <= 400 )
197 p_vout->p_sys->i_width += p_vout->p_sys->i_width >> 1;
198 p_vout->p_sys->i_height += p_vout->p_sys->i_height >> 1;
202 if( SDLOpenDisplay( p_vout ) )
204 intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
205 SDL_QuitSubSystem( SDL_INIT_VIDEO );
206 free( p_vout->p_sys );
213 /*****************************************************************************
214 * vout_Init: initialize SDL video thread output method
215 *****************************************************************************
216 * This function initialize the SDL display device.
217 *****************************************************************************/
218 static int vout_Init( vout_thread_t *p_vout )
223 p_vout->p_sys->i_surfaces = 0;
225 I_OUTPUTPICTURES = 0;
227 /* Initialize the output structure */
228 switch( p_vout->render.i_chroma )
233 p_vout->output.i_chroma = p_vout->render.i_chroma;
234 p_vout->output.i_width = p_vout->render.i_width;
235 p_vout->output.i_height = p_vout->render.i_height;
236 p_vout->output.i_aspect = p_vout->render.i_aspect;
240 /* All we have is a 16bpp image with square pixels */
241 /* FIXME: and if screen depth != 16 ?! */
242 p_vout->output.i_chroma = FOURCC_BI_BITFIELDS | DEPTH_16BPP;
243 p_vout->output.i_width = p_vout->p_sys->i_width;
244 p_vout->output.i_height = p_vout->p_sys->i_height;
245 p_vout->output.i_aspect = p_vout->p_sys->i_width
246 * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
250 /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
251 while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
255 /* Find an empty picture slot */
256 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
258 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
260 p_pic = p_vout->p_picture + i_index;
265 /* Allocate the picture if we found one */
266 if( p_pic == NULL || SDLNewPicture( p_vout, p_pic ) )
271 p_pic->i_status = DESTROYED_PICTURE;
272 p_pic->i_type = DIRECT_PICTURE;
274 p_pic->i_left_margin =
275 p_pic->i_right_margin =
276 p_pic->i_top_margin =
277 p_pic->i_bottom_margin = 0;
279 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
287 /*****************************************************************************
288 * vout_End: terminate Sys video thread output method
289 *****************************************************************************
290 * Terminate an output method created by vout_SDLCreate
291 *****************************************************************************/
292 static void vout_End( vout_thread_t *p_vout )
296 /* Free the output buffers we allocated */
297 for( i_index = I_OUTPUTPICTURES ; i_index ; )
300 switch( p_vout->output.i_chroma )
305 SDL_UnlockYUVOverlay(
306 PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
308 PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
315 free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
319 /*****************************************************************************
320 * vout_Destroy: destroy Sys video thread output method
321 *****************************************************************************
322 * Terminate an output method created by vout_SDLCreate
323 *****************************************************************************/
324 static void vout_Destroy( vout_thread_t *p_vout )
326 SDLCloseDisplay( p_vout );
328 SDL_QuitSubSystem( SDL_INIT_VIDEO );
330 free( p_vout->p_sys );
333 static __inline__ void vout_Seek( off_t i_seek )
335 #define area p_main->p_intf->p_input->stream.p_selected_area
336 off_t i_tell = area->i_tell;
338 i_tell += i_seek * (off_t)50 * p_main->p_intf->p_input->stream.i_mux_rate;
340 i_tell = ( i_tell <= area->i_start ) ? area->i_start
341 : ( i_tell >= area->i_size ) ? area->i_size
344 input_Seek( p_main->p_intf->p_input, i_tell );
348 /*****************************************************************************
349 * vout_Manage: handle Sys events
350 *****************************************************************************
351 * This function should be called regularly by video output thread. It returns
352 * a non null value if an error occured.
353 *****************************************************************************/
354 static int vout_Manage( vout_thread_t *p_vout )
356 SDL_Event event; /* SDL event */
359 while( SDL_PollEvent(&event) )
363 case SDL_VIDEORESIZE: /* Resizing of window */
364 p_vout->p_sys->i_width = event.resize.w;
365 p_vout->p_sys->i_height = event.resize.h;
366 SDLCloseDisplay( p_vout );
367 SDLOpenDisplay( p_vout );
370 case SDL_MOUSEMOTION:
371 if( p_vout->p_sys->b_cursor &&
372 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
374 if( p_vout->p_sys->b_cursor_autohidden )
376 p_vout->p_sys->b_cursor_autohidden = 0;
381 p_vout->p_sys->i_lastmoved = mdate();
386 case SDL_MOUSEBUTTONUP:
387 switch( event.button.button )
389 case SDL_BUTTON_RIGHT:
390 p_main->p_intf->b_menu_change = 1;
395 case SDL_MOUSEBUTTONDOWN:
396 switch( event.button.button )
398 case SDL_BUTTON_LEFT:
399 /* In this part we will eventually manage
400 * clicks for DVD navigation for instance. For the
401 * moment just pause the stream. */
402 input_SetStatus( p_main->p_intf->p_input, INPUT_STATUS_PAUSE );
416 p_main->p_intf->b_die = 1;
419 case SDL_KEYDOWN: /* if a key is pressed */
421 switch( event.key.keysym.sym )
423 case SDLK_q: /* quit */
425 p_main->p_intf->b_die = 1;
428 case SDLK_f: /* switch to fullscreen */
429 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
432 case SDLK_c: /* toggle grayscale */
433 p_vout->b_grayscale = ! p_vout->b_grayscale;
434 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
437 case SDLK_i: /* toggle info */
438 p_vout->b_info = ! p_vout->b_info;
439 p_vout->i_changes |= VOUT_INFO_CHANGE;
442 case SDLK_s: /* toggle scaling */
443 p_vout->b_scale = ! p_vout->b_scale;
444 p_vout->i_changes |= VOUT_SCALE_CHANGE;
447 case SDLK_SPACE: /* toggle interface */
448 p_vout->b_interface = ! p_vout->b_interface;
449 p_vout->i_changes |= VOUT_INTF_CHANGE;
453 p_main->p_intf->b_menu_change = 1;
472 case SDLK_F10: network_ChannelJoin( 0 ); break;
473 case SDLK_F1: network_ChannelJoin( 1 ); break;
474 case SDLK_F2: network_ChannelJoin( 2 ); break;
475 case SDLK_F3: network_ChannelJoin( 3 ); break;
476 case SDLK_F4: network_ChannelJoin( 4 ); break;
477 case SDLK_F5: network_ChannelJoin( 5 ); break;
478 case SDLK_F6: network_ChannelJoin( 6 ); break;
479 case SDLK_F7: network_ChannelJoin( 7 ); break;
480 case SDLK_F8: network_ChannelJoin( 8 ); break;
481 case SDLK_F9: network_ChannelJoin( 9 ); break;
484 intf_DbgMsg( "unhandled key %i", event.key.keysym.sym );
494 /* Fullscreen change */
495 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
497 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
499 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
501 p_vout->p_sys->b_cursor_autohidden = 0;
502 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
503 ! p_vout->p_sys->b_cursor_autohidden );
505 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
509 if( ! p_vout->p_sys->b_cursor_autohidden &&
510 ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
512 /* Hide the mouse automatically */
513 p_vout->p_sys->b_cursor_autohidden = 1;
520 /*****************************************************************************
521 * vout_Display: displays previously rendered output
522 *****************************************************************************
523 * This function sends the currently rendered image to the display.
524 *****************************************************************************/
525 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
530 vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
537 switch( p_vout->output.i_chroma )
542 SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
543 SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
544 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
549 SDL_Flip(p_vout->p_sys->p_display);
554 /* following functions are local */
556 /*****************************************************************************
557 * SDLOpenDisplay: open and initialize SDL device
558 *****************************************************************************
559 * Open and initialize display according to preferences specified in the vout
561 *****************************************************************************/
562 static int SDLOpenDisplay( vout_thread_t *p_vout )
567 /* Initialize flags and cursor */
568 i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
569 i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
571 i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
572 SDL_DEFAULT_BPP, i_flags );
575 intf_ErrMsg( "vout error: no video mode available" );
579 p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
580 p_vout->p_sys->i_height,
583 if( p_vout->p_sys->p_display == NULL )
585 intf_ErrMsg( "vout error: cannot set video mode" );
589 SDL_LockSurface( p_vout->p_sys->p_display );
591 p_vout->p_sys->p_overlay =
592 SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
593 p_vout->p_sys->p_display );
595 if( p_vout->p_sys->p_overlay == NULL )
597 intf_ErrMsg( "vout error: cannot set overlay" );
598 SDL_UnlockSurface( p_vout->p_sys->p_display );
599 SDL_FreeSurface( p_vout->p_sys->p_display );
603 if( p_vout->p_sys->p_overlay->hw_overlay )
605 SDL_WM_SetCaption( VOUT_TITLE " (hardware SDL output)",
606 VOUT_TITLE " (hardware SDL output)" );
610 SDL_WM_SetCaption( VOUT_TITLE " (software SDL output)",
611 VOUT_TITLE " (software SDL output)" );
614 SDL_EventState( SDL_KEYUP, SDL_IGNORE ); /* ignore keys up */
619 /*****************************************************************************
620 * SDLCloseDisplay: close and reset SDL device
621 *****************************************************************************
622 * This function returns all resources allocated by SDLOpenDisplay and restore
623 * the original state of the device.
624 *****************************************************************************/
625 static void SDLCloseDisplay( vout_thread_t *p_vout )
627 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
628 SDL_UnlockSurface ( p_vout->p_sys->p_display );
629 SDL_FreeSurface( p_vout->p_sys->p_display );
632 /*****************************************************************************
633 * SDLNewPicture: allocate a picture
634 *****************************************************************************
635 * Returns 0 on success, -1 otherwise
636 *****************************************************************************/
637 static int SDLNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
639 #define P p_pic->planes
640 int i_width = p_vout->output.i_width;
641 int i_height = p_vout->output.i_height;
643 switch( p_vout->output.i_chroma )
648 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
650 if( p_pic->p_sys == NULL )
655 p_pic->p_sys->p_overlay =
656 SDL_CreateYUVOverlay( i_width, i_height,
658 p_vout->p_sys->p_display );
660 if( p_pic->p_sys->p_overlay == NULL )
662 free( p_pic->p_sys );
666 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
668 /* FIXME: try to get the right i_bytes value from p_overlay */
669 P[ Y_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 0 ];
670 P[ Y_PLANE ].i_bytes = i_width * i_height;
671 P[ Y_PLANE ].i_line_bytes = i_width;
673 P[ U_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 2 ];
674 P[ U_PLANE ].i_bytes = i_width * i_height / 4;
675 P[ U_PLANE ].i_line_bytes = i_width / 2;
677 P[ V_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 1 ];
678 P[ V_PLANE ].i_bytes = i_width * i_height / 4;
679 P[ V_PLANE ].i_line_bytes = i_width / 2;
685 case FOURCC_BI_BITFIELDS | DEPTH_16BPP:
686 if( p_vout->p_sys->i_surfaces )
688 /* We already allocated this surface, return */
692 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
694 if( p_pic->p_sys == NULL )
699 P[ MAIN_PLANE ].p_data = p_vout->p_sys->p_display->pixels;
700 P[ MAIN_PLANE ].i_bytes = 2 * i_width * i_height;
701 P[ MAIN_PLANE ].i_line_bytes = 2 * i_width;
703 p_vout->p_sys->i_surfaces++;
710 /* Unknown chroma, tell the guy to get lost */