1 /*****************************************************************************
2 * vout_sdl.c: SDL video output display method
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
41 /* FIXME: it's up to the _interface_ to do this, not the video output */
42 #include "stream_control.h"
43 #include "input_ext-intf.h"
46 #include "video_output.h"
49 #include "interface.h"
50 /* FIXME: get rid of this */
51 #include "keystrokes.h"
54 /*****************************************************************************
55 * FIXME: this file is ... *
57 * XXX XXX XXX XXX XXX XXX XXX XXX *
58 * XXX XXX XXX XXX XXX XXX XXX *
59 * XXX XXX XXX XXX XXX XXX *
60 * XXX XXX XXX XXX XXX XXX XXX *
61 * XXX XXX XXX XXX XXX XXX *
62 * XXX XXX XXX XXX XXX XXX XXX *
64 *****************************************************************************/
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
76 SDL_Surface * p_display; /* display device */
77 SDL_Overlay * p_overlay; /* overlay device */
78 boolean_t b_fullscreen;
81 boolean_t b_reopen_display;
83 /* Buffers informations */
86 /*****************************************************************************
88 *****************************************************************************/
89 static int vout_Probe ( probedata_t *p_data );
90 static int vout_Create ( struct vout_thread_s * );
91 static int vout_Init ( struct vout_thread_s * );
92 static void vout_End ( struct vout_thread_s * );
93 static void vout_Destroy ( struct vout_thread_s * );
94 static int vout_Manage ( struct vout_thread_s * );
95 static void vout_Display ( struct vout_thread_s * );
97 static int SDLOpenDisplay ( vout_thread_t *p_vout );
98 static void SDLCloseDisplay ( vout_thread_t *p_vout );
100 /*****************************************************************************
101 * Functions exported as capabilities. They are declared as static so that
102 * we don't pollute the namespace too much.
103 *****************************************************************************/
104 void vout_getfunctions( function_list_t * p_function_list )
106 p_function_list->pf_probe = vout_Probe;
107 p_function_list->functions.vout.pf_create = vout_Create;
108 p_function_list->functions.vout.pf_init = vout_Init;
109 p_function_list->functions.vout.pf_end = vout_End;
110 p_function_list->functions.vout.pf_destroy = vout_Destroy;
111 p_function_list->functions.vout.pf_manage = vout_Manage;
112 p_function_list->functions.vout.pf_display = vout_Display;
113 p_function_list->functions.vout.pf_setpalette = NULL;
116 /*****************************************************************************
117 * intf_Probe: probe the video driver and return a score
118 *****************************************************************************
119 * This function tries to initialize SDL and returns a score to the
120 * plugin manager so that it can select the best plugin.
121 *****************************************************************************/
122 static int vout_Probe( probedata_t *p_data )
124 if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
132 /*****************************************************************************
133 * vout_Create: allocate SDL video thread output method
134 *****************************************************************************
135 * This function allocate and initialize a SDL vout method. It uses some of the
136 * vout properties to choose the correct mode, and change them according to the
137 * mode actually used.
138 *****************************************************************************/
139 int vout_Create( vout_thread_t *p_vout )
141 /* Allocate structure */
142 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
143 if( p_vout->p_sys == NULL )
145 intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
149 /* Initialize library */
150 if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE)
153 intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
154 free( p_vout->p_sys );
158 /* Force the software yuv even if it is not used */
159 /* If we don't do this, p_vout is not correctly initialized
160 and it's impossible to switch between soft/hard yuv */
161 /* FIXME: this is a broken way to do !! fix this !! */
162 p_vout->b_need_render = 1;
164 p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
165 p_vout->p_sys->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
166 VOUT_FULLSCREEN_DEFAULT );
167 p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
168 VOUT_OVERLAY_DEFAULT );
169 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
170 VOUT_WIDTH_DEFAULT );
171 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
172 VOUT_HEIGHT_DEFAULT );
174 p_vout->p_sys->p_display = NULL;
175 p_vout->p_sys->p_overlay = NULL;
177 if( SDLOpenDisplay(p_vout) )
179 intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
180 free( p_vout->p_sys );
184 /* FIXME: get rid of this ASAP, it's FUCKING UGLY */
185 { intf_thread_t * p_intf = p_main->p_intf;
186 /* p_intf->p_intf_getKey = intf_getKey; */
187 intf_AssignKey(p_intf, SDLK_q, INTF_KEY_QUIT, 0);
188 intf_AssignKey(p_intf, SDLK_ESCAPE, INTF_KEY_QUIT, 0);
189 /* intf_AssignKey(p_intf,3,'Q'); */
190 intf_AssignKey(p_intf, SDLK_0, INTF_KEY_SET_CHANNEL,0);
191 intf_AssignKey(p_intf, SDLK_1, INTF_KEY_SET_CHANNEL,1);
192 intf_AssignKey(p_intf, SDLK_2, INTF_KEY_SET_CHANNEL,2);
193 intf_AssignKey(p_intf, SDLK_3, INTF_KEY_SET_CHANNEL,3);
194 intf_AssignKey(p_intf, SDLK_4, INTF_KEY_SET_CHANNEL,4);
195 intf_AssignKey(p_intf, SDLK_5, INTF_KEY_SET_CHANNEL,5);
196 intf_AssignKey(p_intf, SDLK_6, INTF_KEY_SET_CHANNEL,6);
197 intf_AssignKey(p_intf, SDLK_7, INTF_KEY_SET_CHANNEL,7);
198 intf_AssignKey(p_intf, SDLK_8, INTF_KEY_SET_CHANNEL,8);
199 intf_AssignKey(p_intf, SDLK_9, INTF_KEY_SET_CHANNEL,9);
200 intf_AssignKey(p_intf, SDLK_PLUS, INTF_KEY_INC_VOLUME, 0);
201 intf_AssignKey(p_intf, SDLK_MINUS, INTF_KEY_DEC_VOLUME, 0);
202 intf_AssignKey(p_intf, SDLK_m, INTF_KEY_TOGGLE_VOLUME, 0);
203 /* intf_AssignKey(p_intf,'M','M'); */
204 intf_AssignKey(p_intf, SDLK_g, INTF_KEY_DEC_GAMMA, 0);
205 /* intf_AssignKey(p_intf,'G','G'); */
206 intf_AssignKey(p_intf, SDLK_c, INTF_KEY_TOGGLE_GRAYSCALE, 0);
207 intf_AssignKey(p_intf, SDLK_SPACE, INTF_KEY_TOGGLE_INTERFACE, 0);
208 intf_AssignKey(p_intf, SDLK_i, INTF_KEY_TOGGLE_INFO, 0);
209 intf_AssignKey(p_intf, SDLK_s, INTF_KEY_TOGGLE_SCALING, 0); }
214 /*****************************************************************************
215 * vout_Init: initialize SDL video thread output method
216 *****************************************************************************
217 * This function initialize the SDL display device.
218 *****************************************************************************/
219 int vout_Init( vout_thread_t *p_vout )
224 /*****************************************************************************
225 * vout_End: terminate Sys video thread output method
226 *****************************************************************************
227 * Terminate an output method created by vout_SDLCreate
228 *****************************************************************************/
229 void vout_End( vout_thread_t *p_vout )
231 SDLCloseDisplay( p_vout );
235 /*****************************************************************************
236 * vout_Destroy: destroy Sys video thread output method
237 *****************************************************************************
238 * Terminate an output method created by vout_SDLCreate
239 *****************************************************************************/
240 void vout_Destroy( vout_thread_t *p_vout )
242 free( p_vout->p_sys );
245 /*****************************************************************************
246 * vout_Manage: handle Sys events
247 *****************************************************************************
248 * This function should be called regularly by video output thread. It returns
249 * a non null value if an error occured.
250 *****************************************************************************/
251 int vout_Manage( vout_thread_t *p_vout )
253 SDL_Event event; /* SDL event */
257 /* FIXME: do this nicely */
258 input_thread_t * p_input = p_main->p_intf->p_input;
261 while( SDL_PollEvent(&event) )
265 case SDL_VIDEORESIZE: /* Resizing of window */
266 p_vout->i_width = event.resize.w;
267 p_vout->i_height = event.resize.h;
268 p_vout->i_changes |= VOUT_SIZE_CHANGE;
271 case SDL_MOUSEBUTTONDOWN:
272 switch( event.button.button )
274 case SDL_BUTTON_MIDDLE:
275 p_vout->i_changes |= VOUT_CURSOR_CHANGE;
278 case SDL_BUTTON_RIGHT:
279 p_main->p_intf->b_menu_change = 1;
285 intf_ProcessKey( p_main->p_intf, SDLK_q );
288 case SDL_KEYDOWN: /* if a key is pressed */
289 i_key = event.key.keysym.sym;
293 case SDLK_f: /* switch to fullscreen */
294 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
297 case SDLK_y: /* switch to hard YUV */
298 p_vout->i_changes |= VOUT_YUV_CHANGE;
301 case SDLK_c: /* toggle grayscale */
302 p_vout->b_grayscale = ! p_vout->b_grayscale;
303 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
306 case SDLK_i: /* toggle info */
307 p_vout->b_info = ! p_vout->b_info;
308 p_vout->i_changes |= VOUT_INFO_CHANGE;
311 case SDLK_s: /* toggle scaling */
312 p_vout->b_scale = ! p_vout->b_scale;
313 p_vout->i_changes |= VOUT_SCALE_CHANGE;
316 case SDLK_SPACE: /* toggle interface */
317 p_vout->b_interface = ! p_vout->b_interface;
318 p_vout->i_changes |= VOUT_INTF_CHANGE;
321 /* FIXME : this is temporary */
323 if( p_input->stream.control.i_status == PLAYING_S )
325 input_Pause( p_input );
329 input_Play( p_input );
334 i_rate = p_input->stream.control.i_rate/2;
335 if ( i_rate >= MINIMAL_RATE )
337 input_Forward( p_input, i_rate );
342 i_rate = p_input->stream.control.i_rate*2;
343 if ( i_rate <= MAXIMAL_RATE )
345 /* Compensation of int truncature */
346 if ( i_rate > 500 && i_rate < 1000 )
348 input_Forward( p_input, i_rate );
354 input_Seek( p_input, p_input->stream.i_tell
355 + p_input->stream.i_size / 20 );
361 input_Seek( p_input, p_input->stream.i_tell
362 - p_input->stream.i_size / 20 );
366 if( intf_ProcessKey( p_main->p_intf, (char )i_key ) )
368 intf_DbgMsg( "unhandled key '%c' (%i)", (char)i_key, i_key ); }
381 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
383 p_vout->p_sys->i_width = p_vout->i_width;
384 p_vout->p_sys->i_height = p_vout->i_height;
386 /* Need to reopen display */
387 SDLCloseDisplay( p_vout );
388 if( SDLOpenDisplay( p_vout ) )
390 intf_ErrMsg( "error: can't open DISPLAY default display" );
393 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
399 if( p_vout->i_changes & VOUT_YUV_CHANGE )
401 p_vout->b_need_render = ! p_vout->b_need_render;
403 /* Need to reopen display */
404 SDLCloseDisplay( p_vout );
405 if( SDLOpenDisplay( p_vout ) )
407 intf_ErrMsg( "error: can't open DISPLAY default display" );
410 p_vout->i_changes &= ~VOUT_YUV_CHANGE;
416 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
418 p_vout->p_sys->b_fullscreen = ! p_vout->p_sys->b_fullscreen;
420 if( p_vout->p_sys->b_fullscreen )
422 p_vout->p_sys->b_fullscreen = 0;
423 p_vout->p_sys->b_cursor = 1;
428 p_vout->p_sys->b_fullscreen = 1;
429 p_vout->p_sys->b_cursor = 0;
433 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
435 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
441 if( p_vout->i_changes & VOUT_CURSOR_CHANGE )
443 if( p_vout->p_sys->b_cursor )
446 p_vout->p_sys->b_cursor = 0;
451 p_vout->p_sys->b_cursor = 1;
453 p_vout->i_changes &= ~VOUT_CURSOR_CHANGE;
459 /*****************************************************************************
460 * vout_SetPalette: sets an 8 bpp palette
461 *****************************************************************************
462 * This function sets the palette given as an argument. It does not return
463 * anything, but could later send information on which colors it was unable
465 *****************************************************************************/
466 void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
467 u16 *blue, u16 *transp)
469 /* Create a display surface with a grayscale palette */
470 SDL_Color colors[256];
473 /* Fill colors with color information */
474 for( i = 0; i < 256; i++ )
476 colors[ i ].r = red[ i ] >> 8;
477 colors[ i ].g = green[ i ] >> 8;
478 colors[ i ].b = blue[ i ] >> 8;
482 if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
484 intf_ErrMsg( "vout error: failed setting palette" );
489 /*****************************************************************************
490 * vout_Display: displays previously rendered output
491 *****************************************************************************
492 * This function send the currently rendered image to the display, wait until
493 * it is displayed and switch the two rendering buffer, preparing next frame.
494 *****************************************************************************/
495 void vout_Display( vout_thread_t *p_vout )
498 if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
500 if( p_vout->b_need_render )
502 /* Change display frame */
503 SDL_Flip( p_vout->p_sys->p_display );
509 * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
512 /* TODO: support for streams other than 4:2:0 */
513 /* create the overlay if necessary */
514 if( p_vout->p_sys->p_overlay == NULL )
516 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay(
517 p_vout->p_rendered_pic->i_width,
518 p_vout->p_rendered_pic->i_height,
520 p_vout->p_sys->p_display
522 intf_Msg("vout: YUV acceleration %s",
523 p_vout->p_sys->p_overlay->hw_overlay
524 ? "activated" : "unavailable !" );
527 SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
528 /* copy the data into video buffers */
530 memcpy(p_vout->p_sys->p_overlay->pixels[0],
531 p_vout->p_rendered_pic->p_y,
532 p_vout->p_sys->p_overlay->h *
533 p_vout->p_sys->p_overlay->pitches[0]);
535 memcpy(p_vout->p_sys->p_overlay->pixels[1],
536 p_vout->p_rendered_pic->p_v,
537 p_vout->p_sys->p_overlay->h *
538 p_vout->p_sys->p_overlay->pitches[1] / 2);
540 memcpy(p_vout->p_sys->p_overlay->pixels[2],
541 p_vout->p_rendered_pic->p_u,
542 p_vout->p_sys->p_overlay->h *
543 p_vout->p_sys->p_overlay->pitches[2] / 2);
545 disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
546 disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
547 disp.x = (p_vout->i_width - disp.w)/2;
548 disp.y = (p_vout->i_height - disp.h)/2;
550 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
551 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
556 /* following functions are local */
558 /*****************************************************************************
559 * SDLOpenDisplay: open and initialize SDL device
560 *****************************************************************************
561 * Open and initialize display according to preferences specified in the vout
563 *****************************************************************************/
564 static int SDLOpenDisplay( vout_thread_t *p_vout )
566 SDL_Rect clipping_rect;
570 * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
573 /* init flags and cursor */
574 flags = SDL_ANYFORMAT | SDL_HWPALETTE;
576 if( p_vout->p_sys->b_fullscreen )
577 flags |= SDL_FULLSCREEN;
579 flags |= SDL_RESIZABLE;
581 if( p_vout->b_need_render )
582 flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
584 flags |= SDL_SWSURFACE; /* save video memory */
586 bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
587 p_vout->p_sys->i_height,
588 p_vout->i_screen_depth, flags);
592 intf_ErrMsg( "error: can't open DISPLAY default display" );
596 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
597 p_vout->p_sys->i_height,
600 if( p_vout->p_sys->p_display == NULL )
602 intf_ErrMsg( "error: can't open DISPLAY default display" );
606 SDL_LockSurface(p_vout->p_sys->p_display);
608 if( p_vout->p_sys->b_fullscreen )
613 SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
614 SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
615 SDL_EventState(SDL_MOUSEBUTTONUP, SDL_IGNORE);
617 if( p_vout->b_need_render )
619 p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
620 SDL_Flip(p_vout->p_sys->p_display);
621 p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
622 SDL_Flip(p_vout->p_sys->p_display);
624 /* Set clipping for text */
627 clipping_rect.w = p_vout->p_sys->p_display->w;
628 clipping_rect.h = p_vout->p_sys->p_display->h;
629 SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
631 /* Set thread information */
632 p_vout->i_width = p_vout->p_sys->p_display->w;
633 p_vout->i_height = p_vout->p_sys->p_display->h;
634 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
636 p_vout->i_screen_depth =
637 p_vout->p_sys->p_display->format->BitsPerPixel;
638 p_vout->i_bytes_per_pixel =
639 p_vout->p_sys->p_display->format->BytesPerPixel;
641 p_vout->i_red_mask = p_vout->p_sys->p_display->format->Rmask;
642 p_vout->i_green_mask = p_vout->p_sys->p_display->format->Gmask;
643 p_vout->i_blue_mask = p_vout->p_sys->p_display->format->Bmask;
645 /* FIXME: palette in 8bpp ?? */
646 /* Set and initialize buffers */
647 vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
648 p_vout->p_sys->p_buffer[ 1 ] );
652 p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
653 p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
655 /* Set thread information */
656 p_vout->i_width = p_vout->p_sys->p_display->w;
657 p_vout->i_height = p_vout->p_sys->p_display->h;
658 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
660 vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
661 p_vout->p_sys->p_buffer[ 1 ] );
664 p_vout->p_sys->b_reopen_display = 0;
669 /*****************************************************************************
670 * SDLCloseDisplay: close and reset SDL device
671 *****************************************************************************
672 * This function returns all resources allocated by SDLOpenDisplay and restore
673 * the original state of the device.
674 *****************************************************************************/
675 static void SDLCloseDisplay( vout_thread_t *p_vout )
677 if( p_vout->p_sys->p_display != NULL )
679 if( p_vout->p_sys->p_overlay != NULL )
681 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
682 p_vout->p_sys->p_overlay = NULL;
685 SDL_UnlockSurface ( p_vout->p_sys->p_display );
686 SDL_FreeSurface( p_vout->p_sys->p_display );
687 p_vout->p_sys->p_display = NULL;