1 /*****************************************************************************
2 * vout_sdl.c: SDL video output display method
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: vout_sdl.c,v 1.74 2001/12/30 07:09:56 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 )
230 case YUV_420_PICTURE:
231 p_vout->output.i_chroma = p_vout->render.i_chroma;
232 p_vout->output.i_width = p_vout->render.i_width;
233 p_vout->output.i_height = p_vout->render.i_height;
234 p_vout->output.i_aspect = p_vout->render.i_aspect;
238 /* All we have is a 16bpp image with square pixels */
239 /* FIXME: and if screen depth != 16 ?! */
240 p_vout->output.i_chroma = RGB_16BPP_PICTURE;
241 p_vout->output.i_width = p_vout->p_sys->i_width;
242 p_vout->output.i_height = p_vout->p_sys->i_height;
243 p_vout->output.i_aspect = p_vout->p_sys->i_width
244 * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
248 /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
249 while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
253 /* Find an empty picture slot */
254 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
256 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
258 p_pic = p_vout->p_picture + i_index;
263 /* Allocate the picture if we found one */
264 if( p_pic == NULL || SDLNewPicture( p_vout, p_pic ) )
269 p_pic->i_status = DESTROYED_PICTURE;
270 p_pic->i_type = DIRECT_PICTURE;
272 p_pic->i_left_margin =
273 p_pic->i_right_margin =
274 p_pic->i_top_margin =
275 p_pic->i_bottom_margin = 0;
277 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
285 /*****************************************************************************
286 * vout_End: terminate Sys video thread output method
287 *****************************************************************************
288 * Terminate an output method created by vout_SDLCreate
289 *****************************************************************************/
290 static void vout_End( vout_thread_t *p_vout )
294 /* Free the output buffers we allocated */
295 for( i_index = I_OUTPUTPICTURES ; i_index ; )
298 switch( p_vout->output.i_chroma )
300 case YUV_420_PICTURE:
301 SDL_UnlockYUVOverlay(
302 PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
304 PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
307 case RGB_16BPP_PICTURE:
313 free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
317 /*****************************************************************************
318 * vout_Destroy: destroy Sys video thread output method
319 *****************************************************************************
320 * Terminate an output method created by vout_SDLCreate
321 *****************************************************************************/
322 static void vout_Destroy( vout_thread_t *p_vout )
324 SDLCloseDisplay( p_vout );
326 SDL_QuitSubSystem( SDL_INIT_VIDEO );
328 free( p_vout->p_sys );
331 static __inline__ void vout_Seek( off_t i_seek )
333 #define area p_main->p_intf->p_input->stream.p_selected_area
334 off_t i_tell = area->i_tell;
336 i_tell += i_seek * (off_t)50 * p_main->p_intf->p_input->stream.i_mux_rate;
338 i_tell = ( i_tell <= area->i_start ) ? area->i_start
339 : ( i_tell >= area->i_size ) ? area->i_size
342 input_Seek( p_main->p_intf->p_input, i_tell );
346 /*****************************************************************************
347 * vout_Manage: handle Sys events
348 *****************************************************************************
349 * This function should be called regularly by video output thread. It returns
350 * a non null value if an error occured.
351 *****************************************************************************/
352 static int vout_Manage( vout_thread_t *p_vout )
354 SDL_Event event; /* SDL event */
357 while( SDL_PollEvent(&event) )
361 case SDL_VIDEORESIZE: /* Resizing of window */
362 p_vout->p_sys->i_width = event.resize.w;
363 p_vout->p_sys->i_height = event.resize.h;
364 SDLCloseDisplay( p_vout );
365 SDLOpenDisplay( p_vout );
368 case SDL_MOUSEMOTION:
369 if( p_vout->p_sys->b_cursor &&
370 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
372 if( p_vout->p_sys->b_cursor_autohidden )
374 p_vout->p_sys->b_cursor_autohidden = 0;
379 p_vout->p_sys->i_lastmoved = mdate();
384 case SDL_MOUSEBUTTONUP:
385 switch( event.button.button )
387 case SDL_BUTTON_RIGHT:
388 p_main->p_intf->b_menu_change = 1;
393 case SDL_MOUSEBUTTONDOWN:
394 switch( event.button.button )
396 case SDL_BUTTON_LEFT:
397 /* In this part we will eventually manage
398 * clicks for DVD navigation for instance. For the
399 * moment just pause the stream. */
400 input_SetStatus( p_main->p_intf->p_input, INPUT_STATUS_PAUSE );
414 p_main->p_intf->b_die = 1;
417 case SDL_KEYDOWN: /* if a key is pressed */
419 switch( event.key.keysym.sym )
421 case SDLK_q: /* quit */
423 p_main->p_intf->b_die = 1;
426 case SDLK_f: /* switch to fullscreen */
427 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
430 case SDLK_c: /* toggle grayscale */
431 p_vout->b_grayscale = ! p_vout->b_grayscale;
432 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
435 case SDLK_i: /* toggle info */
436 p_vout->b_info = ! p_vout->b_info;
437 p_vout->i_changes |= VOUT_INFO_CHANGE;
440 case SDLK_s: /* toggle scaling */
441 p_vout->b_scale = ! p_vout->b_scale;
442 p_vout->i_changes |= VOUT_SCALE_CHANGE;
445 case SDLK_SPACE: /* toggle interface */
446 p_vout->b_interface = ! p_vout->b_interface;
447 p_vout->i_changes |= VOUT_INTF_CHANGE;
451 p_main->p_intf->b_menu_change = 1;
470 case SDLK_F10: network_ChannelJoin( 0 ); break;
471 case SDLK_F1: network_ChannelJoin( 1 ); break;
472 case SDLK_F2: network_ChannelJoin( 2 ); break;
473 case SDLK_F3: network_ChannelJoin( 3 ); break;
474 case SDLK_F4: network_ChannelJoin( 4 ); break;
475 case SDLK_F5: network_ChannelJoin( 5 ); break;
476 case SDLK_F6: network_ChannelJoin( 6 ); break;
477 case SDLK_F7: network_ChannelJoin( 7 ); break;
478 case SDLK_F8: network_ChannelJoin( 8 ); break;
479 case SDLK_F9: network_ChannelJoin( 9 ); break;
482 intf_DbgMsg( "unhandled key %i", event.key.keysym.sym );
492 /* Fullscreen change */
493 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
495 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
497 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
499 p_vout->p_sys->b_cursor_autohidden = 0;
500 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
501 ! p_vout->p_sys->b_cursor_autohidden );
503 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
507 if( ! p_vout->p_sys->b_cursor_autohidden &&
508 ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
510 /* Hide the mouse automatically */
511 p_vout->p_sys->b_cursor_autohidden = 1;
518 /*****************************************************************************
519 * vout_Display: displays previously rendered output
520 *****************************************************************************
521 * This function sends the currently rendered image to the display.
522 *****************************************************************************/
523 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
528 vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
535 switch( p_vout->output.i_chroma )
537 case RGB_16BPP_PICTURE:
538 SDL_Flip(p_vout->p_sys->p_display);
541 case YUV_420_PICTURE:
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);
552 /* following functions are local */
554 /*****************************************************************************
555 * SDLOpenDisplay: open and initialize SDL device
556 *****************************************************************************
557 * Open and initialize display according to preferences specified in the vout
559 *****************************************************************************/
560 static int SDLOpenDisplay( vout_thread_t *p_vout )
565 /* Initialize flags and cursor */
566 i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
567 i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
569 i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
570 SDL_DEFAULT_BPP, i_flags );
573 intf_ErrMsg( "vout error: no video mode available" );
577 p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
578 p_vout->p_sys->i_height,
581 if( p_vout->p_sys->p_display == NULL )
583 intf_ErrMsg( "vout error: cannot set video mode" );
587 SDL_LockSurface( p_vout->p_sys->p_display );
589 p_vout->p_sys->p_overlay =
590 SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
591 p_vout->p_sys->p_display );
593 if( p_vout->p_sys->p_overlay == NULL )
595 intf_ErrMsg( "vout error: cannot set overlay" );
596 SDL_UnlockSurface( p_vout->p_sys->p_display );
597 SDL_FreeSurface( p_vout->p_sys->p_display );
601 if( p_vout->p_sys->p_overlay->hw_overlay )
603 SDL_WM_SetCaption( VOUT_TITLE " (hardware SDL output)",
604 VOUT_TITLE " (hardware SDL output)" );
608 SDL_WM_SetCaption( VOUT_TITLE " (software SDL output)",
609 VOUT_TITLE " (software SDL output)" );
612 SDL_EventState( SDL_KEYUP, SDL_IGNORE ); /* ignore keys up */
617 /*****************************************************************************
618 * SDLCloseDisplay: close and reset SDL device
619 *****************************************************************************
620 * This function returns all resources allocated by SDLOpenDisplay and restore
621 * the original state of the device.
622 *****************************************************************************/
623 static void SDLCloseDisplay( vout_thread_t *p_vout )
625 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
626 SDL_UnlockSurface ( p_vout->p_sys->p_display );
627 SDL_FreeSurface( p_vout->p_sys->p_display );
630 /*****************************************************************************
631 * SDLNewPicture: allocate a picture
632 *****************************************************************************
633 * Returns 0 on success, -1 otherwise
634 *****************************************************************************/
635 static int SDLNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
637 #define P p_pic->planes
638 int i_width = p_vout->output.i_width;
639 int i_height = p_vout->output.i_height;
641 switch( p_vout->output.i_chroma )
643 case RGB_16BPP_PICTURE:
644 if( p_vout->p_sys->i_surfaces )
646 /* We already allocated this surface, return */
650 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
652 if( p_pic->p_sys == NULL )
657 P[ RGB_PLANE ].p_data = p_vout->p_sys->p_display->pixels;
658 P[ RGB_PLANE ].i_bytes = 2 * i_width * i_height;
659 P[ RGB_PLANE ].i_line_bytes = 2 * i_width;
661 p_vout->p_sys->i_surfaces++;
667 case YUV_420_PICTURE:
668 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
670 if( p_pic->p_sys == NULL )
675 p_pic->p_sys->p_overlay =
676 SDL_CreateYUVOverlay( i_width, i_height,
678 p_vout->p_sys->p_display );
680 if( p_pic->p_sys->p_overlay == NULL )
682 free( p_pic->p_sys );
686 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
688 /* FIXME: try to get the right i_bytes value from p_overlay */
689 P[ Y_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 0 ];
690 P[ Y_PLANE ].i_bytes = i_width * i_height;
691 P[ Y_PLANE ].i_line_bytes = i_width;
693 P[ U_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 2 ];
694 P[ U_PLANE ].i_bytes = i_width * i_height / 4;
695 P[ U_PLANE ].i_line_bytes = i_width / 2;
697 P[ V_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 1 ];
698 P[ V_PLANE ].i_bytes = i_width * i_height / 4;
699 P[ V_PLANE ].i_line_bytes = i_width / 2;
706 /* Unknown chroma, tell the guy to get lost */