1 /*****************************************************************************
2 * vout_sdl.c: SDL video output display method
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Pierre Baillet <oct@zoy.org>
8 * Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
44 #include "video_output.h"
47 #include "interface.h"
48 /* FIXME: get rid of this */
49 #include "keystrokes.h"
52 /*****************************************************************************
53 * FIXME: this file is ... *
55 * XXX XXX FIXME XXX XXX XXX XXX *
56 * XXX XXX XXX XXX XXX XXX XXX XXX *
57 * XXX XXX XXX XXX FIXME XXX *
58 * XXX XXX XXX TODO XXX XXX XXX *
59 * XXX XXX XXX XXX XXX XXX *
60 * FIXME FIXME FIXME XXX XXX *
62 *****************************************************************************/
64 /*****************************************************************************
65 * vout_sys_t: video output SDL method descriptor
66 *****************************************************************************
67 * This structure is part of the video output thread descriptor.
68 * It describes the SDL specific properties of an output thread.
69 *****************************************************************************/
70 typedef struct vout_sys_s
74 SDL_Surface * p_display; /* display device */
75 SDL_Overlay * p_overlay; /* overlay device */
76 boolean_t b_fullscreen;
79 boolean_t b_reopen_display;
80 Uint8 * p_sdl_buf[2]; /* Buffer information */
83 /*****************************************************************************
85 *****************************************************************************/
86 static int vout_Probe ( probedata_t *p_data );
87 static int vout_Create ( struct vout_thread_s * );
88 static int vout_Init ( struct vout_thread_s * );
89 static void vout_End ( struct vout_thread_s * );
90 static void vout_Destroy ( struct vout_thread_s * );
91 static int vout_Manage ( struct vout_thread_s * );
92 static void vout_Display ( struct vout_thread_s * );
93 static void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
94 u16 *blue, u16 *transp );
96 static int SDLOpenDisplay ( vout_thread_t *p_vout );
97 static void SDLCloseDisplay ( vout_thread_t *p_vout );
99 /*****************************************************************************
100 * Functions exported as capabilities. They are declared as static so that
101 * we don't pollute the namespace too much.
102 *****************************************************************************/
103 void vout_getfunctions( function_list_t * p_function_list )
105 p_function_list->pf_probe = vout_Probe;
106 p_function_list->functions.vout.pf_create = vout_Create;
107 p_function_list->functions.vout.pf_init = vout_Init;
108 p_function_list->functions.vout.pf_end = vout_End;
109 p_function_list->functions.vout.pf_destroy = vout_Destroy;
110 p_function_list->functions.vout.pf_manage = vout_Manage;
111 p_function_list->functions.vout.pf_display = vout_Display;
112 p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
115 /*****************************************************************************
116 * vout_Probe: probe the video driver and return a score
117 *****************************************************************************
118 * This function tries to initialize SDL and returns a score to the
119 * plugin manager so that it can select the best plugin.
120 *****************************************************************************/
121 static int vout_Probe( probedata_t *p_data )
123 if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
131 /*****************************************************************************
132 * vout_Create: allocate SDL video thread output method
133 *****************************************************************************
134 * This function allocate and initialize a SDL vout method. It uses some of the
135 * vout properties to choose the correct mode, and change them according to the
136 * mode actually used.
137 *****************************************************************************/
138 static int vout_Create( vout_thread_t *p_vout )
140 /* Allocate structure */
141 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
142 if( p_vout->p_sys == NULL )
144 intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
148 /* Initialize library */
149 if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE)
152 intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
153 free( p_vout->p_sys );
157 p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
158 p_vout->p_sys->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
159 VOUT_FULLSCREEN_DEFAULT );
160 p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
161 VOUT_OVERLAY_DEFAULT );
162 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
163 VOUT_WIDTH_DEFAULT );
164 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
165 VOUT_HEIGHT_DEFAULT );
167 p_vout->p_sys->p_display = NULL;
168 p_vout->p_sys->p_overlay = NULL;
170 if( SDLOpenDisplay(p_vout) )
172 intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
173 free( p_vout->p_sys );
177 /* FIXME: get rid of this ASAP, it's FUCKING UGLY */
178 { intf_thread_t * p_intf = p_main->p_intf;
179 intf_AssignKey(p_intf, SDLK_q, INTF_KEY_QUIT, 0);
180 intf_AssignKey(p_intf, SDLK_ESCAPE, INTF_KEY_QUIT, 0);
181 /* intf_AssignKey(p_intf,3,'Q'); */
182 intf_AssignKey(p_intf, SDLK_0, INTF_KEY_SET_CHANNEL,0);
183 intf_AssignKey(p_intf, SDLK_1, INTF_KEY_SET_CHANNEL,1);
184 intf_AssignKey(p_intf, SDLK_2, INTF_KEY_SET_CHANNEL,2);
185 intf_AssignKey(p_intf, SDLK_3, INTF_KEY_SET_CHANNEL,3);
186 intf_AssignKey(p_intf, SDLK_4, INTF_KEY_SET_CHANNEL,4);
187 intf_AssignKey(p_intf, SDLK_5, INTF_KEY_SET_CHANNEL,5);
188 intf_AssignKey(p_intf, SDLK_6, INTF_KEY_SET_CHANNEL,6);
189 intf_AssignKey(p_intf, SDLK_7, INTF_KEY_SET_CHANNEL,7);
190 intf_AssignKey(p_intf, SDLK_8, INTF_KEY_SET_CHANNEL,8);
191 intf_AssignKey(p_intf, SDLK_9, INTF_KEY_SET_CHANNEL,9);
192 intf_AssignKey(p_intf, SDLK_PLUS, INTF_KEY_INC_VOLUME, 0);
193 intf_AssignKey(p_intf, SDLK_MINUS, INTF_KEY_DEC_VOLUME, 0);
194 intf_AssignKey(p_intf, SDLK_m, INTF_KEY_TOGGLE_VOLUME, 0);
195 /* intf_AssignKey(p_intf,'M','M'); */
196 intf_AssignKey(p_intf, SDLK_g, INTF_KEY_DEC_GAMMA, 0);
197 /* intf_AssignKey(p_intf,'G','G'); */
198 intf_AssignKey(p_intf, SDLK_c, INTF_KEY_TOGGLE_GRAYSCALE, 0);
199 intf_AssignKey(p_intf, SDLK_SPACE, INTF_KEY_TOGGLE_INTERFACE, 0);
200 intf_AssignKey(p_intf, SDLK_i, INTF_KEY_TOGGLE_INFO, 0);
201 intf_AssignKey(p_intf, SDLK_s, INTF_KEY_TOGGLE_SCALING, 0); }
206 /*****************************************************************************
207 * vout_Init: initialize SDL video thread output method
208 *****************************************************************************
209 * This function initialize the SDL display device.
210 *****************************************************************************/
211 static int vout_Init( vout_thread_t *p_vout )
213 /* This hack is hugly, but hey, you are, too. */
215 SDL_Overlay * p_overlay;
217 p_overlay = SDL_CreateYUVOverlay( VOUT_WIDTH_DEFAULT, VOUT_HEIGHT_DEFAULT,
219 p_vout->p_sys->p_display );
220 intf_Msg( "vout: YUV acceleration %s",
221 p_overlay->hw_overlay ? "activated" : "unavailable !" );
222 p_vout->b_need_render = !p_overlay->hw_overlay;
224 SDL_FreeYUVOverlay( p_overlay );
229 /*****************************************************************************
230 * vout_End: terminate Sys video thread output method
231 *****************************************************************************
232 * Terminate an output method created by vout_SDLCreate
233 *****************************************************************************/
234 static void vout_End( vout_thread_t *p_vout )
236 SDLCloseDisplay( p_vout );
240 /*****************************************************************************
241 * vout_Destroy: destroy Sys video thread output method
242 *****************************************************************************
243 * Terminate an output method created by vout_SDLCreate
244 *****************************************************************************/
245 static void vout_Destroy( vout_thread_t *p_vout )
247 free( p_vout->p_sys );
250 /*****************************************************************************
251 * vout_Manage: handle Sys events
252 *****************************************************************************
253 * This function should be called regularly by video output thread. It returns
254 * a non null value if an error occured.
255 *****************************************************************************/
256 static int vout_Manage( vout_thread_t *p_vout )
258 SDL_Event event; /* SDL event */
262 while( SDL_PollEvent(&event) )
266 case SDL_VIDEORESIZE: /* Resizing of window */
267 p_vout->i_width = event.resize.w;
268 p_vout->i_height = event.resize.h;
269 p_vout->i_changes |= VOUT_SIZE_CHANGE;
272 case SDL_MOUSEBUTTONUP:
273 switch( event.button.button )
275 case SDL_BUTTON_RIGHT:
276 p_main->p_intf->b_menu_change = 1;
281 case SDL_MOUSEBUTTONDOWN:
282 switch( event.button.button )
284 case SDL_BUTTON_MIDDLE:
285 p_vout->i_changes |= VOUT_CURSOR_CHANGE;
291 intf_ProcessKey( p_main->p_intf, SDLK_q );
294 case SDL_KEYDOWN: /* if a key is pressed */
295 i_key = event.key.keysym.sym;
299 case SDLK_f: /* switch to fullscreen */
300 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
303 case SDLK_y: /* switch to hard YUV */
304 p_vout->i_changes |= VOUT_YUV_CHANGE;
307 case SDLK_c: /* toggle grayscale */
308 p_vout->b_grayscale = ! p_vout->b_grayscale;
309 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
312 case SDLK_i: /* toggle info */
313 p_vout->b_info = ! p_vout->b_info;
314 p_vout->i_changes |= VOUT_INFO_CHANGE;
317 case SDLK_s: /* toggle scaling */
318 p_vout->b_scale = ! p_vout->b_scale;
319 p_vout->i_changes |= VOUT_SCALE_CHANGE;
322 case SDLK_SPACE: /* toggle interface */
323 p_vout->b_interface = ! p_vout->b_interface;
324 p_vout->i_changes |= VOUT_INTF_CHANGE;
328 if( intf_ProcessKey( p_main->p_intf, (char )i_key ) )
330 intf_DbgMsg( "unhandled key '%c' (%i)", (char)i_key, i_key ); }
343 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
345 p_vout->p_sys->i_width = p_vout->i_width;
346 p_vout->p_sys->i_height = p_vout->i_height;
348 /* Need to reopen display */
349 SDLCloseDisplay( p_vout );
350 if( SDLOpenDisplay( p_vout ) )
352 intf_ErrMsg( "vout error: can't reopen display after resize" );
355 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
361 if( p_vout->i_changes & VOUT_YUV_CHANGE )
363 p_vout->b_need_render = ! p_vout->b_need_render;
365 /* Need to reopen display */
366 SDLCloseDisplay( p_vout );
367 if( SDLOpenDisplay( p_vout ) )
369 intf_ErrMsg( "error: can't reopen display after YUV change" );
372 p_vout->i_changes &= ~VOUT_YUV_CHANGE;
378 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
380 p_vout->p_sys->b_fullscreen = ! p_vout->p_sys->b_fullscreen;
382 if( p_vout->p_sys->b_fullscreen )
384 p_vout->p_sys->b_fullscreen = 0;
385 p_vout->p_sys->b_cursor = 1;
390 p_vout->p_sys->b_fullscreen = 1;
391 p_vout->p_sys->b_cursor = 0;
395 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
397 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
403 if( p_vout->i_changes & VOUT_CURSOR_CHANGE )
405 if( p_vout->p_sys->b_cursor )
408 p_vout->p_sys->b_cursor = 0;
413 p_vout->p_sys->b_cursor = 1;
415 p_vout->i_changes &= ~VOUT_CURSOR_CHANGE;
421 /*****************************************************************************
422 * vout_SetPalette: sets an 8 bpp palette
423 *****************************************************************************
424 * This function sets the palette given as an argument. It does not return
425 * anything, but could later send information on which colors it was unable
427 *****************************************************************************/
428 static void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
429 u16 *blue, u16 *transp)
431 /* Create a display surface with a grayscale palette */
432 SDL_Color colors[256];
435 /* Fill colors with color information */
436 for( i = 0; i < 256; i++ )
438 colors[ i ].r = red[ i ] >> 8;
439 colors[ i ].g = green[ i ] >> 8;
440 colors[ i ].b = blue[ i ] >> 8;
444 if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
446 intf_ErrMsg( "vout error: failed setting palette" );
451 /*****************************************************************************
452 * vout_Display: displays previously rendered output
453 *****************************************************************************
454 * This function send the currently rendered image to the display, wait until
455 * it is displayed and switch the two rendering buffer, preparing next frame.
456 *****************************************************************************/
457 static void vout_Display( vout_thread_t *p_vout )
460 if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
462 if( p_vout->b_need_render )
464 /* Change display frame */
465 SDL_Flip( p_vout->p_sys->p_display );
471 * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
474 /* TODO: support for streams other than 4:2:0 */
475 /* create the overlay if necessary */
476 if( p_vout->p_sys->p_overlay == NULL )
478 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay(
479 p_vout->p_rendered_pic->i_width,
480 p_vout->p_rendered_pic->i_height,
482 p_vout->p_sys->p_display
484 intf_Msg("vout: YUV acceleration %s",
485 p_vout->p_sys->p_overlay->hw_overlay
486 ? "activated" : "unavailable !" );
489 SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
490 /* copy the data into video buffers */
492 memcpy(p_vout->p_sys->p_overlay->pixels[0],
493 p_vout->p_rendered_pic->p_y,
494 p_vout->p_sys->p_overlay->h *
495 p_vout->p_sys->p_overlay->pitches[0]);
497 memcpy(p_vout->p_sys->p_overlay->pixels[1],
498 p_vout->p_rendered_pic->p_v,
499 p_vout->p_sys->p_overlay->h *
500 p_vout->p_sys->p_overlay->pitches[1] / 2);
502 memcpy(p_vout->p_sys->p_overlay->pixels[2],
503 p_vout->p_rendered_pic->p_u,
504 p_vout->p_sys->p_overlay->h *
505 p_vout->p_sys->p_overlay->pitches[2] / 2);
507 disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
508 disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
509 disp.x = (p_vout->i_width - disp.w)/2;
510 disp.y = (p_vout->i_height - disp.h)/2;
512 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
513 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
518 /* following functions are local */
520 /*****************************************************************************
521 * SDLOpenDisplay: open and initialize SDL device
522 *****************************************************************************
523 * Open and initialize display according to preferences specified in the vout
525 *****************************************************************************/
526 static int SDLOpenDisplay( vout_thread_t *p_vout )
528 SDL_Rect clipping_rect;
532 * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
535 /* init flags and cursor */
536 flags = SDL_ANYFORMAT | SDL_HWPALETTE;
538 if( p_vout->p_sys->b_fullscreen )
539 flags |= SDL_FULLSCREEN;
541 flags |= SDL_RESIZABLE;
543 if( p_vout->b_need_render )
544 flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
546 flags |= SDL_SWSURFACE; /* save video memory */
548 bpp = SDL_VideoModeOK( p_vout->p_sys->i_width,
549 p_vout->p_sys->i_height,
550 p_vout->i_screen_depth, flags );
554 intf_ErrMsg( "vout error: no video mode available" );
558 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
559 p_vout->p_sys->i_height,
562 if( p_vout->p_sys->p_display == NULL )
564 intf_ErrMsg( "vout error: cannot set video mode" );
568 SDL_LockSurface(p_vout->p_sys->p_display);
570 if( p_vout->p_sys->b_fullscreen )
575 SDL_WM_SetCaption( VOUT_TITLE " (SDL output)",
576 VOUT_TITLE " (SDL output)" );
577 SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
579 if( p_vout->b_need_render )
581 p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
582 SDL_Flip(p_vout->p_sys->p_display);
583 p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
584 SDL_Flip(p_vout->p_sys->p_display);
586 /* Set clipping for text */
589 clipping_rect.w = p_vout->p_sys->p_display->w;
590 clipping_rect.h = p_vout->p_sys->p_display->h;
591 SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
593 /* Set thread information */
594 p_vout->i_width = p_vout->p_sys->p_display->w;
595 p_vout->i_height = p_vout->p_sys->p_display->h;
596 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
598 p_vout->i_screen_depth =
599 p_vout->p_sys->p_display->format->BitsPerPixel;
600 p_vout->i_bytes_per_pixel =
601 p_vout->p_sys->p_display->format->BytesPerPixel;
603 p_vout->i_red_mask = p_vout->p_sys->p_display->format->Rmask;
604 p_vout->i_green_mask = p_vout->p_sys->p_display->format->Gmask;
605 p_vout->i_blue_mask = p_vout->p_sys->p_display->format->Bmask;
607 /* FIXME: palette in 8bpp ?? */
608 /* Set and initialize buffers */
609 vout_SetBuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
610 p_vout->p_sys->p_sdl_buf[ 1 ] );
614 p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
615 p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
617 /* Set thread information */
618 p_vout->i_width = p_vout->p_sys->p_display->w;
619 p_vout->i_height = p_vout->p_sys->p_display->h;
620 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
622 vout_SetBuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
623 p_vout->p_sys->p_sdl_buf[ 1 ] );
626 p_vout->p_sys->b_reopen_display = 0;
631 /*****************************************************************************
632 * SDLCloseDisplay: close and reset SDL device
633 *****************************************************************************
634 * This function returns all resources allocated by SDLOpenDisplay and restore
635 * the original state of the device.
636 *****************************************************************************/
637 static void SDLCloseDisplay( vout_thread_t *p_vout )
639 if( p_vout->p_sys->p_display != NULL )
641 if( p_vout->p_sys->p_overlay != NULL )
643 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
644 p_vout->p_sys->p_overlay = NULL;
647 SDL_UnlockSurface ( p_vout->p_sys->p_display );
648 SDL_FreeSurface( p_vout->p_sys->p_display );
649 p_vout->p_sys->p_display = NULL;