Merge svn changes up to r30448
[mplayer/glamo.git] / libvo / vo_sdl.c
blob617ffec0c5c7a588f2740e163e1a65b397245a3f
1 /*
2 * vo_sdl.c
4 * (was video_out_sdl.c from OMS project/mpeg2dec -> http://linuxvideo.org)
6 * Copyright (C) Ryan C. Gordon <icculus@lokigames.com> - April 22, 2000
8 * Copyright (C) Felix Buenemann <atmosfear@users.sourceforge.net> - 2001
10 * (for extensive code enhancements)
12 * Current maintainer for MPlayer project (report bugs to that address):
13 * Felix Buenemann <atmosfear@users.sourceforge.net>
15 * This file is a video out driver using the SDL library (http://libsdl.org/),
16 * to be used with MPlayer, further info from http://www.mplayerhq.hu
18 * -- old disclaimer --
20 * A mpeg2dec display driver that does output through the
21 * Simple DirectMedia Layer (SDL) library. This effectively gives us all
22 * sorts of output options: X11, SVGAlib, fbcon, AAlib, GGI. Win32, MacOS
23 * and BeOS support, too. Yay. SDL info, source, and binaries can be found
24 * at http://slouken.devolution.com/SDL/
26 * -- end old disclaimer --
28 * This file is part of MPlayer.
30 * MPlayer is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
35 * MPlayer is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License along
41 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
45 /* define to force software-surface (video surface stored in system memory)*/
46 #undef SDL_NOHWSURFACE
48 /* define to enable surface locks, this might be needed on SMP machines */
49 #undef SDL_ENABLE_LOCKS
51 //#define BUGGY_SDL //defined by configure
53 /* MONITOR_ASPECT MUST BE FLOAT */
54 #define MONITOR_ASPECT 4.0/3.0
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <inttypes.h>
61 #include "config.h"
62 #include "mp_msg.h"
63 #include "mp_msg.h"
64 #include "help_mp.h"
65 #include "video_out.h"
66 #include "video_out_internal.h"
68 #include "fastmemcpy.h"
69 #include "sub.h"
70 #include "aspect.h"
71 #include "libmpcodecs/vfcap.h"
73 #ifdef CONFIG_X11
74 #include <X11/Xlib.h>
75 #include "x11_common.h"
76 #endif
78 #include "input/input.h"
79 #include "input/mouse.h"
80 #include "subopt-helper.h"
81 #include "mp_fifo.h"
83 static const vo_info_t info =
85 "SDL YUV/RGB/BGR renderer (SDL v1.1.7+ only!)",
86 "sdl",
87 "Ryan C. Gordon <icculus@lokigames.com>, Felix Buenemann <atmosfear@users.sourceforge.net>",
91 const LIBVO_EXTERN(sdl)
93 #ifdef CONFIG_SDL_SDL_H
94 #include <SDL/SDL.h>
95 #else
96 #include <SDL.h>
97 #endif
98 //#include <SDL/SDL_syswm.h>
101 #ifdef SDL_ENABLE_LOCKS
102 #define SDL_OVR_LOCK(x) if (SDL_LockYUVOverlay (priv->overlay)) { \
103 mp_msg(MSGT_VO,MSGL_V, "SDL: Couldn't lock YUV overlay\n"); \
104 return x; \
106 #define SDL_OVR_UNLOCK SDL_UnlockYUVOverlay (priv->overlay);
108 #define SDL_SRF_LOCK(srf, x) if(SDL_MUSTLOCK(srf)) { \
109 if(SDL_LockSurface (srf)) { \
110 mp_msg(MSGT_VO,MSGL_V, "SDL: Couldn't lock RGB surface\n"); \
111 return x; \
115 #define SDL_SRF_UNLOCK(srf) if(SDL_MUSTLOCK(srf)) \
116 SDL_UnlockSurface (srf);
117 #else
118 #define SDL_OVR_LOCK(x)
119 #define SDL_OVR_UNLOCK
120 #define SDL_SRF_LOCK(srf, x)
121 #define SDL_SRF_UNLOCK(srf)
122 #endif
124 /** Private SDL Data structure **/
126 static struct sdl_priv_s {
128 /* output driver used by sdl */
129 char driver[8];
131 /* SDL display surface */
132 SDL_Surface *surface;
134 /* SDL RGB surface */
135 SDL_Surface *rgbsurface;
137 /* SDL YUV overlay */
138 SDL_Overlay *overlay;
140 /* available fullscreen modes */
141 SDL_Rect **fullmodes;
143 /* surface attributes for fullscreen and windowed mode */
144 Uint32 sdlflags, sdlfullflags;
146 /* save the windowed output extents */
147 SDL_Rect windowsize;
149 /* Bits per Pixel */
150 Uint8 bpp;
152 /* RGB or YUV? */
153 Uint8 mode;
154 #define YUV 0
155 #define RGB 1
156 #define BGR 2
158 /* use direct blitting to surface */
159 int dblit;
161 /* current fullscreen mode, 0 = highest available fullscreen mode */
162 int fullmode;
164 /* YUV ints */
165 int framePlaneY, framePlaneUV, framePlaneYUY;
166 int stridePlaneY, stridePlaneUV, stridePlaneYUY;
168 /* RGB ints */
169 int framePlaneRGB;
170 int stridePlaneRGB;
172 /* Flip image */
173 int flip;
175 /* fullscreen behaviour; see init */
176 int fulltype;
178 /* is X running (0/1) */
179 int X;
181 /* X11 Resolution */
182 int XWidth, XHeight;
184 /* original image dimensions */
185 int width, height;
187 /* destination dimensions */
188 int dstwidth, dstheight;
190 /* Draw image at coordinate y on the SDL surfaces */
191 int y;
193 /* The image is displayed between those y coordinates in priv->surface */
194 int y_screen_top, y_screen_bottom;
196 /* 1 if the OSD has changed otherwise 0 */
197 int osd_has_changed;
199 /* source image format (YUV/RGB/...) */
200 uint32_t format;
202 /* dirty_off_frame[0] contains a bounding box around the osd contents drawn above the image
203 dirty_off_frame[1] is the corresponding thing for OSD contents drawn below the image
205 SDL_Rect dirty_off_frame[2];
206 } sdl_priv;
208 static void erase_area_4(int x_start, int width, int height, int pitch, uint32_t color, uint8_t* pixels);
209 static void erase_area_1(int x_start, int width, int height, int pitch, uint8_t color, uint8_t* pixels);
210 static int setup_surfaces(void);
211 static void set_video_mode(int width, int height, int bpp, uint32_t sdlflags);
212 static void erase_rectangle(int x, int y, int w, int h);
214 /* Expand 'rect' to contain the rectangle specified by x, y, w and h */
215 static void expand_rect(SDL_Rect* rect, int x, int y, int w, int h)
217 if(rect->x < 0 || rect->y < 0) {
218 rect->x = x;
219 rect->y = y;
220 rect->w = w;
221 rect->h = h;
222 return;
225 if(rect->x > x)
226 rect->x = x;
228 if(rect->y > y)
229 rect->y = y;
231 if(rect->x + rect->w < x + w)
232 rect->w = x + w - rect->x;
234 if(rect->y + rect->h < y + h)
235 rect->h = y + h - rect->y;
238 /** libvo Plugin functions **/
241 * draw_alpha is used for osd and subtitle display.
245 static void draw_alpha(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
246 struct sdl_priv_s *priv = &sdl_priv;
248 if(priv->osd_has_changed) {
249 /* OSD did change. Store a bounding box of everything drawn into the OSD */
250 if(priv->y >= y0) {
251 /* Make sure we don't mark part of the frame area dirty */
252 if(h + y0 > priv->y)
253 expand_rect(&priv->dirty_off_frame[0], x0, y0, w, priv->y - y0);
254 else
255 expand_rect(&priv->dirty_off_frame[0], x0, y0, w, h);
257 else if(priv->y + priv->height <= y0 + h) {
258 /* Make sure we don't mark part of the frame area dirty */
259 if(y0 < priv->y + priv->height)
260 expand_rect(&priv->dirty_off_frame[1], x0,
261 priv->y + priv->height,
262 w, h - ((priv->y + priv->height) - y0));
263 else
264 expand_rect(&priv->dirty_off_frame[1], x0, y0, w, h);
267 else { /* OSD contents didn't change only draw parts that was erased by the frame */
268 if(priv->y >= y0) {
269 src = src + (priv->y - y0) * stride;
270 srca = srca + (priv->y - y0) * stride;
271 h -= priv->y - y0;
272 y0 = priv->y;
275 if(priv->y + priv->height <= y0 + h)
276 h = priv->y + priv->height - y0;
278 if(h <= 0)
279 return;
282 switch(priv->format) {
283 case IMGFMT_YV12:
284 case IMGFMT_I420:
285 case IMGFMT_IYUV:
286 vo_draw_alpha_yv12(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
287 break;
288 case IMGFMT_YUY2:
289 case IMGFMT_YVYU:
290 x0 *= 2;
291 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
292 break;
293 case IMGFMT_UYVY:
294 x0 *= 2;
295 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
296 break;
298 default:
299 if(priv->dblit) {
300 x0 *= priv->surface->format->BytesPerPixel;
301 switch(priv->format) {
302 case IMGFMT_RGB15:
303 case IMGFMT_BGR15:
304 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
305 break;
306 case IMGFMT_RGB16:
307 case IMGFMT_BGR16:
308 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
309 break;
310 case IMGFMT_RGB24:
311 case IMGFMT_BGR24:
312 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
313 break;
314 case IMGFMT_RGB32:
315 case IMGFMT_BGR32:
316 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
317 break;
320 else {
321 x0 *= priv->rgbsurface->format->BytesPerPixel;
322 switch(priv->format) {
323 case IMGFMT_RGB15:
324 case IMGFMT_BGR15:
325 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
326 break;
327 case IMGFMT_RGB16:
328 case IMGFMT_BGR16:
329 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
330 break;
331 case IMGFMT_RGB24:
332 case IMGFMT_BGR24:
333 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
334 break;
335 case IMGFMT_RGB32:
336 case IMGFMT_BGR32:
337 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
338 break;
347 * Take a null-terminated array of pointers, and find the last element.
349 * params : array == array of which we want to find the last element.
350 * returns : index of last NON-NULL element.
353 static inline int findArrayEnd (SDL_Rect **array)
355 int i = 0;
356 while ( array[i++] ); /* keep loopin' ... */
358 /* return the index of the last array element */
359 return i - 1;
364 * Open and prepare SDL output.
366 * params : *plugin ==
367 * *name ==
368 * returns : 0 on success, -1 on failure
371 static int sdl_open (void *plugin, void *name)
373 struct sdl_priv_s *priv = &sdl_priv;
374 const SDL_VideoInfo *vidInfo = NULL;
375 /*static int opened = 0;
377 if (opened)
378 return 0;
379 opened = 1;*/
382 /* other default values */
383 #ifdef SDL_NOHWSURFACE
384 mp_msg(MSGT_VO,MSGL_V, "SDL: using software-surface\n");
385 priv->sdlflags = SDL_SWSURFACE|SDL_RESIZABLE|SDL_ANYFORMAT;
386 priv->sdlfullflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ANYFORMAT;
387 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
388 #else
389 /*if((strcmp(priv->driver, "dga") == 0) && (priv->mode)) {
390 if( mp_msg_test(MSGT_VO,MSGL_V) ) {
391 printf("SDL: using software-surface\n"); }
392 priv->sdlflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
393 priv->sdlfullflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
395 else { */
396 mp_msg(MSGT_VO,MSGL_V, "SDL: using hardware-surface\n");
397 priv->sdlflags = SDL_HWSURFACE|SDL_RESIZABLE/*|SDL_ANYFORMAT*/;
398 priv->sdlfullflags = SDL_HWSURFACE|SDL_FULLSCREEN/*|SDL_ANYFORMAT*/;
399 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
401 #endif
403 #if !defined( __AMIGAOS4__ ) && !defined( __APPLE__ )
404 priv->sdlfullflags |= SDL_DOUBLEBUF;
405 if (vo_doublebuffering)
406 priv->sdlflags |= SDL_DOUBLEBUF;
407 #endif
409 /* Setup Keyrepeats (500/30 are defaults) */
410 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, 100 /*SDL_DEFAULT_REPEAT_INTERVAL*/);
412 /* get information about the graphics adapter */
413 vidInfo = SDL_GetVideoInfo ();
415 /* collect all fullscreen & hardware modes available */
416 if (!(priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags))) {
418 /* non hardware accelerated fullscreen modes */
419 priv->sdlfullflags &= ~SDL_HWSURFACE;
420 priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags);
423 /* test for normal resizeable & windowed hardware accellerated surfaces */
424 if (!SDL_ListModes (vidInfo->vfmt, priv->sdlflags)) {
426 /* test for NON hardware accelerated resizeable surfaces - poor you.
427 * That's all we have. If this fails there's nothing left.
428 * Theoretically there could be Fullscreenmodes left - we ignore this for now.
430 priv->sdlflags &= ~SDL_HWSURFACE;
431 if ((!SDL_ListModes (vidInfo->vfmt, priv->sdlflags)) && (!priv->fullmodes)) {
432 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_SDL] Couldn't get any acceptable SDL Mode for output.\n");
433 return -1;
438 /* YUV overlays need at least 16-bit color depth, but the
439 * display might less. The SDL AAlib target says it can only do
440 * 8-bits, for example. So, if the display is less than 16-bits,
441 * we'll force the BPP to 16, and pray that SDL can emulate for us.
443 priv->bpp = vidInfo->vfmt->BitsPerPixel;
444 if (priv->mode == YUV && priv->bpp < 16) {
446 mp_msg(MSGT_VO,MSGL_V, "SDL: Your SDL display target wants to be at a color "
447 "depth of (%d), but we need it to be at least 16 "
448 "bits, so we need to emulate 16-bit color. This is "
449 "going to slow things down; you might want to "
450 "increase your display's color depth, if possible.\n",
451 priv->bpp);
453 priv->bpp = 16;
456 /* We don't want those in our event queue.
457 * We use SDL_KEYUP cause SDL_KEYDOWN seems to cause problems
458 * with keys need to be pressed twice, to be recognized.
460 #ifndef BUGGY_SDL
461 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
462 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
463 // SDL_EventState(SDL_QUIT, SDL_IGNORE);
464 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
465 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
466 #endif
468 /* Success! */
469 return 0;
474 * Close SDL, Cleanups, Free Memory
476 * params : *plugin
477 * returns : non-zero on success, zero on error.
480 static int sdl_close (void)
482 struct sdl_priv_s *priv = &sdl_priv;
484 if (priv->fullmode)
485 SDL_ShowCursor(1);
487 /* Cleanup YUV Overlay structure */
488 if (priv->overlay) {
489 SDL_FreeYUVOverlay(priv->overlay);
490 priv->overlay=NULL;
493 /* Free RGB Surface */
494 if (priv->rgbsurface) {
495 SDL_FreeSurface(priv->rgbsurface);
496 priv->rgbsurface=NULL;
499 /* Free our blitting surface */
500 if (priv->surface) {
501 SDL_FreeSurface(priv->surface);
502 priv->surface=NULL;
505 /* DON'T attempt to free the fullscreen modes array. SDL_Quit* does this for us */
507 return 0;
511 * Do aspect ratio calculations
513 * params : srcw == sourcewidth
514 * srch == sourceheight
515 * dstw == destinationwidth
516 * dsth == destinationheight
518 * returns : SDL_Rect structure with new x and y, w and h
521 #if 0
522 static SDL_Rect aspect(int srcw, int srch, int dstw, int dsth) {
523 SDL_Rect newres;
524 mp_msg(MSGT_VO,MSGL_V, "SDL Aspect-Destinationres: %ix%i (x: %i, y: %i)\n", newres.w, newres.h, newres.x, newres.y);
525 newres.h = ((float)dstw / (float)srcw * (float)srch) * ((float)dsth/((float)dstw/(MONITOR_ASPECT)));
526 if(newres.h > dsth) {
527 newres.w = ((float)dsth / (float)newres.h) * dstw;
528 newres.h = dsth;
529 newres.x = (dstw - newres.w) / 2;
530 newres.y = 0;
532 else {
533 newres.w = dstw;
534 newres.x = 0;
535 newres.y = (dsth - newres.h) / 2;
538 mp_msg(MSGT_VO,MSGL_V, "SDL Mode: %d: %d x %d\n", i, priv->fullmodes[i]->w, priv->fullmodes[i]->h);
540 return newres;
542 #endif
545 * Sets the specified fullscreen mode.
547 * params : mode == index of the desired fullscreen mode
548 * returns : doesn't return
551 #if 0
552 static void set_fullmode (int mode)
554 struct sdl_priv_s *priv = &sdl_priv;
555 SDL_Surface *newsurface = NULL;
556 int haspect, waspect = 0;
558 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
559 if (mode < 0)
560 mode = priv->fullmode = findArrayEnd(priv->fullmodes) - 1;
562 /* Calculate proper aspect ratio for fullscreen
563 * Height smaller than expected: add horizontal black bars (haspect)*/
564 haspect = (priv->width * (float) ((float) priv->fullmodes[mode]->h / (float) priv->fullmodes[mode]->w) - priv->height) * (float) ((float) priv->fullmodes[mode]->w / (float) priv->width);
565 /* Height bigger than expected: add vertical black bars (waspect)*/
566 if (haspect < 0) {
567 haspect = 0; /* set haspect to zero because image will be scaled horizontal instead of vertical */
568 waspect = priv->fullmodes[mode]->w - ((float) ((float) priv->fullmodes[mode]->h / (float) priv->height) * (float) priv->width);
570 // printf ("W-Aspect: %i H-Aspect: %i\n", waspect, haspect);
572 /* change to given fullscreen mode and hide the mouse cursor */
573 newsurface = SDL_SetVideoMode(priv->fullmodes[mode]->w - waspect, priv->fullmodes[mode]->h - haspect, priv->bpp, priv->sdlfullflags);
575 /* if we were successful hide the mouse cursor and save the mode */
576 if (newsurface) {
577 if (priv->surface)
578 SDL_FreeSurface(priv->surface);
579 priv->surface = newsurface;
580 SDL_ShowCursor(0);
583 #endif
585 /* Set video mode. Not fullscreen */
586 static void set_video_mode(int width, int height, int bpp, uint32_t sdlflags)
588 struct sdl_priv_s *priv = &sdl_priv;
589 SDL_Surface* newsurface;
591 if(priv->rgbsurface)
592 SDL_FreeSurface(priv->rgbsurface);
593 else if(priv->overlay)
594 SDL_FreeYUVOverlay(priv->overlay);
596 priv->rgbsurface = NULL;
597 priv->overlay = NULL;
599 newsurface = SDL_SetVideoMode(width, height, bpp, sdlflags);
601 if(newsurface) {
603 /* priv->surface will be NULL the first time this function is called. */
604 if(priv->surface)
605 SDL_FreeSurface(priv->surface);
607 priv->surface = newsurface;
608 priv->dstwidth = width;
609 priv->dstheight = height;
611 setup_surfaces();
613 else
614 mp_msg(MSGT_VO,MSGL_WARN, "set_video_mode: SDL_SetVideoMode failed: %s\n", SDL_GetError());
617 static void set_fullmode (int mode) {
618 struct sdl_priv_s *priv = &sdl_priv;
619 SDL_Surface *newsurface = NULL;
620 int screen_surface_w, screen_surface_h;
622 if(priv->rgbsurface)
623 SDL_FreeSurface(priv->rgbsurface);
624 else if(priv->overlay)
625 SDL_FreeYUVOverlay(priv->overlay);
627 priv->rgbsurface = NULL;
628 priv->overlay = NULL;
630 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
631 /* But select a mode where the full video enter */
632 if(priv->X && priv->fulltype & VOFLAG_FULLSCREEN) {
633 screen_surface_w = priv->XWidth;
634 screen_surface_h = priv->XHeight;
636 else if (mode < 0) {
637 int i,j,imax;
638 mode = 0; // Default to the biggest mode avaible
639 if ( mp_msg_test(MSGT_VO,MSGL_V) ) for(i=0;priv->fullmodes[i];++i)
640 mp_msg(MSGT_VO,MSGL_V, "SDL Mode: %d: %d x %d\n", i, priv->fullmodes[i]->w, priv->fullmodes[i]->h);
641 for(i = findArrayEnd(priv->fullmodes) - 1; i >=0; i--) {
642 if( (priv->fullmodes[i]->w >= priv->dstwidth) &&
643 (priv->fullmodes[i]->h >= priv->dstheight) ) {
644 imax = i;
645 for (j = findArrayEnd(priv->fullmodes) - 1; j >=0; j--) {
646 if (priv->fullmodes[j]->w > priv->fullmodes[imax]->w
647 && priv->fullmodes[j]->h == priv->fullmodes[imax]->h)
648 imax = j;
650 mode = imax;
651 break;
654 mp_msg(MSGT_VO,MSGL_V, "SET SDL Mode: %d: %d x %d\n", mode, priv->fullmodes[mode]->w, priv->fullmodes[mode]->h);
655 priv->fullmode = mode;
656 screen_surface_h = priv->fullmodes[mode]->h;
657 screen_surface_w = priv->fullmodes[mode]->w;
659 else {
660 screen_surface_h = priv->fullmodes[mode]->h;
661 screen_surface_w = priv->fullmodes[mode]->w;
664 aspect_save_screenres(screen_surface_w, screen_surface_h);
666 /* calculate new video size/aspect */
667 if(priv->mode == YUV) {
668 if(priv->fulltype&VOFLAG_FULLSCREEN)
669 aspect_save_screenres(priv->XWidth, priv->XHeight);
671 aspect(&priv->dstwidth, &priv->dstheight, A_ZOOM);
674 /* try to change to given fullscreenmode */
675 newsurface = SDL_SetVideoMode(priv->dstwidth, screen_surface_h, priv->bpp,
676 priv->sdlfullflags);
679 * In Mac OS X (and possibly others?) SDL_SetVideoMode() appears to
680 * destroy the datastructure previously retrived, so we need to
681 * re-assign it. The comment in sdl_close() seems to imply that we
682 * should not free() anything.
684 #ifdef __APPLE__
686 const SDL_VideoInfo *vidInfo = NULL;
687 vidInfo = SDL_GetVideoInfo ();
689 /* collect all fullscreen & hardware modes available */
690 if (!(priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags))) {
692 /* non hardware accelerated fullscreen modes */
693 priv->sdlfullflags &= ~SDL_HWSURFACE;
694 priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags);
697 #endif
701 /* if creation of new surface was successful, save it and hide mouse cursor */
702 if(newsurface) {
703 if (priv->surface)
704 SDL_FreeSurface(priv->surface);
705 priv->surface = newsurface;
706 SDL_ShowCursor(0);
707 SDL_SRF_LOCK(priv->surface, -1)
708 SDL_FillRect(priv->surface, NULL, 0);
709 SDL_SRF_UNLOCK(priv->surface)
710 setup_surfaces();
712 else
713 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Set_fullmode: SDL_SetVideoMode failed: %s.\n", SDL_GetError());
718 * Initialize an SDL surface and an SDL YUV overlay.
720 * params : width == width of video we'll be displaying.
721 * height == height of video we'll be displaying.
722 * fullscreen == want to be fullscreen?
723 * title == Title for window titlebar.
724 * returns : non-zero on success, zero on error.
727 static int
728 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
729 //static int sdl_setup (int width, int height)
731 struct sdl_priv_s *priv = &sdl_priv;
733 switch(format){
734 case IMGFMT_I420:
735 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Mapping I420 to IYUV.\n");
736 format = SDL_IYUV_OVERLAY;
737 case IMGFMT_YV12:
738 case IMGFMT_IYUV:
739 case IMGFMT_YUY2:
740 case IMGFMT_UYVY:
741 case IMGFMT_YVYU:
742 priv->mode = YUV;
743 break;
744 case IMGFMT_BGR15:
745 case IMGFMT_BGR16:
746 case IMGFMT_BGR24:
747 case IMGFMT_BGR32:
748 priv->mode = BGR;
749 break;
750 case IMGFMT_RGB15:
751 case IMGFMT_RGB16:
752 case IMGFMT_RGB24:
753 case IMGFMT_RGB32:
754 priv->mode = RGB;
755 break;
756 default:
757 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Unsupported image format (0x%X).\n",format);
758 return -1;
761 if ( vo_config_count ) sdl_close();
763 mp_msg(MSGT_VO,MSGL_V, "SDL: Using 0x%X (%s) image format\n", format, vo_format_name(format));
765 if(priv->mode != YUV) {
766 priv->sdlflags |= SDL_ANYFORMAT;
767 priv->sdlfullflags |= SDL_ANYFORMAT;
770 /* SDL can only scale YUV data */
771 if(priv->mode == RGB || priv->mode == BGR) {
772 d_width = width;
773 d_height = height;
776 aspect_save_orig(width,height);
777 aspect_save_prescale(d_width ? d_width : width, d_height ? d_height : height);
779 /* Save the original Image size */
780 priv->width = width;
781 priv->height = height;
782 priv->dstwidth = d_width ? d_width : width;
783 priv->dstheight = d_height ? d_height : height;
785 priv->format = format;
787 if (sdl_open(NULL, NULL) != 0)
788 return -1;
790 /* Set output window title */
791 SDL_WM_SetCaption (".: MPlayer : F = Fullscreen/Windowed : C = Cycle Fullscreen Resolutions :.", title);
792 //SDL_WM_SetCaption (title, title);
794 if(priv->X) {
795 aspect_save_screenres(priv->XWidth,priv->XHeight);
796 aspect(&priv->dstwidth,&priv->dstheight,A_NOZOOM);
799 priv->windowsize.w = priv->dstwidth;
800 priv->windowsize.h = priv->dstheight;
802 /* bit 0 (0x01) means fullscreen (-fs)
803 * bit 1 (0x02) means mode switching (-vm)
804 * bit 2 (0x04) enables software scaling (-zoom)
805 * bit 3 (0x08) enables flipping (-flip)
807 // printf("SDL: flags are set to: %i\n", flags);
808 // printf("SDL: Width: %i Height: %i D_Width %i D_Height: %i\n", width, height, d_width, d_height);
809 if(flags&VOFLAG_FLIPPING) {
810 mp_msg(MSGT_VO,MSGL_V, "SDL: using flipped video (only with RGB/BGR/packed YUV)\n");
811 priv->flip = 1;
813 if(flags&VOFLAG_FULLSCREEN) {
814 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen without modeswitching\n");
815 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Info - please use -vm or -zoom to switch to the best resolution.\n");
816 priv->fulltype = VOFLAG_FULLSCREEN;
817 set_fullmode(priv->fullmode);
818 /*if((priv->surface = SDL_SetVideoMode (d_width, d_height, priv->bpp, priv->sdlfullflags)))
819 SDL_ShowCursor(0);*/
820 } else
821 if(flags&VOFLAG_MODESWITCHING) {
822 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
823 priv->fulltype = VOFLAG_MODESWITCHING;
824 set_fullmode(priv->fullmode);
825 /*if((priv->surface = SDL_SetVideoMode (d_width ? d_width : width, d_height ? d_height : height, priv->bpp, priv->sdlfullflags)))
826 SDL_ShowCursor(0);*/
827 } else
828 if(flags&VOFLAG_SWSCALE) {
829 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
830 priv->fulltype = VOFLAG_SWSCALE;
831 set_fullmode(priv->fullmode);
833 else {
834 if((strcmp(priv->driver, "x11") == 0)
835 ||(strcmp(priv->driver, "windib") == 0)
836 ||(strcmp(priv->driver, "directx") == 0)
837 ||(strcmp(priv->driver, "Quartz") == 0)
838 ||(strcmp(priv->driver, "cgx") == 0)
839 ||(strcmp(priv->driver, "os4video") == 0)
840 ||((strcmp(priv->driver, "aalib") == 0) && priv->X)){
841 mp_msg(MSGT_VO,MSGL_V, "SDL: setting windowed mode\n");
842 set_video_mode(priv->dstwidth, priv->dstheight, priv->bpp, priv->sdlflags);
844 else {
845 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
846 priv->fulltype = VOFLAG_SWSCALE;
847 set_fullmode(priv->fullmode);
851 if(!priv->surface) { // cannot SetVideoMode
852 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Failed to set video mode: %s.\n", SDL_GetError());
853 return -1;
856 return 0;
859 /* Free priv->rgbsurface or priv->overlay if they are != NULL.
860 * Setup priv->rgbsurface or priv->overlay depending on source format.
861 * The size of the created surface or overlay depends on the size of
862 * priv->surface, priv->width, priv->height, priv->dstwidth and priv->dstheight.
864 static int setup_surfaces(void)
866 struct sdl_priv_s *priv = &sdl_priv;
867 float v_scale = ((float) priv->dstheight) / priv->height;
868 int surfwidth, surfheight;
870 surfwidth = priv->width;
871 surfheight = priv->height + (priv->surface->h - priv->dstheight) / v_scale;
872 surfheight&= ~1;
873 /* Place the image in the middle of the screen */
874 priv->y = (surfheight - priv->height) / 2;
875 priv->y_screen_top = priv->y * v_scale;
876 priv->y_screen_bottom = priv->y_screen_top + priv->dstheight;
878 priv->dirty_off_frame[0].x = -1;
879 priv->dirty_off_frame[0].y = -1;
880 priv->dirty_off_frame[1].x = -1;
881 priv->dirty_off_frame[1].y = -1;
883 /* Make sure the entire screen is updated */
884 vo_osd_changed(1);
886 if(priv->rgbsurface)
887 SDL_FreeSurface(priv->rgbsurface);
888 else if(priv->overlay)
889 SDL_FreeYUVOverlay(priv->overlay);
891 priv->rgbsurface = NULL;
892 priv->overlay = NULL;
894 if(priv->mode != YUV && (priv->format&0xFF) == priv->bpp) {
895 if(strcmp(priv->driver, "x11") == 0) {
896 priv->dblit = 1;
897 priv->framePlaneRGB = priv->width * priv->height * priv->surface->format->BytesPerPixel;
898 priv->stridePlaneRGB = priv->width * priv->surface->format->BytesPerPixel;
899 erase_rectangle(0, 0, priv->surface->w, priv->surface->h);
900 return 0;
904 switch(priv->format) {
905 /* Initialize and create the RGB Surface used for video out in BGR/RGB mode */
906 //SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
907 // SDL_SWSURFACE,SDL_HWSURFACE,SDL_SRCCOLORKEY, priv->flags? guess: exchange Rmask and Bmask for BGR<->RGB
908 // 32 bit: a:ff000000 r:ff000 g:ff00 b:ff
909 // 24 bit: r:ff0000 g:ff00 b:ff
910 // 16 bit: r:1111100000000000b g:0000011111100000b b:0000000000011111b
911 // 15 bit: r:111110000000000b g:000001111100000b b:000000000011111b
912 // FIXME: colorkey detect based on bpp, FIXME static bpp value, FIXME alpha value correct?
913 case IMGFMT_RGB15:
914 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 15, 31, 992, 31744, 0);
915 break;
916 case IMGFMT_BGR15:
917 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 15, 31744, 992, 31, 0);
918 break;
919 case IMGFMT_RGB16:
920 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 16, 31, 2016, 63488, 0);
921 break;
922 case IMGFMT_BGR16:
923 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 16, 63488, 2016, 31, 0);
924 break;
925 case IMGFMT_RGB24:
926 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 24, 0x0000FF, 0x00FF00, 0xFF0000, 0);
927 break;
928 case IMGFMT_BGR24:
929 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 24, 0xFF0000, 0x00FF00, 0x0000FF, 0);
930 break;
931 case IMGFMT_RGB32:
932 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0/*0xFF000000*/);
933 break;
934 case IMGFMT_BGR32:
935 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0/*0xFF000000*/);
936 break;
937 default:
938 /* Initialize and create the YUV Overlay used for video out */
939 if (!(priv->overlay = SDL_CreateYUVOverlay (surfwidth, surfheight, priv->format, priv->surface))) {
940 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Couldn't create a YUV overlay: %s.\n", SDL_GetError());
941 return -1;
943 priv->framePlaneY = priv->width * priv->height;
944 priv->framePlaneUV = (priv->width * priv->height) >> 2;
945 priv->framePlaneYUY = priv->width * priv->height * 2;
946 priv->stridePlaneY = priv->width;
947 priv->stridePlaneUV = priv->width/2;
948 priv->stridePlaneYUY = priv->width * 2;
951 if(priv->mode != YUV) {
952 if(!priv->rgbsurface) {
953 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Couldn't create an RGB surface: %s.\n", SDL_GetError());
954 return -1;
957 priv->dblit = 0;
959 if((priv->format&0xFF) != priv->bpp)
960 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Using depth/colorspace conversion, this will slow things down (%ibpp -> %ibpp).\n", priv->format&0xFF, priv->bpp);
962 priv->framePlaneRGB = priv->width * priv->height * priv->rgbsurface->format->BytesPerPixel;
963 priv->stridePlaneRGB = priv->width * priv->rgbsurface->format->BytesPerPixel;
966 erase_rectangle(0, 0, surfwidth, surfheight);
968 return 0;
973 * Draw a frame to the SDL YUV overlay.
975 * params : *src[] == the Y, U, and V planes that make up the frame.
976 * returns : non-zero on success, zero on error.
979 //static int sdl_draw_frame (frame_t *frame)
980 static int draw_frame(uint8_t *src[])
982 struct sdl_priv_s *priv = &sdl_priv;
983 uint8_t *dst;
984 int i;
985 uint8_t *mysrc = src[0];
987 switch(priv->format){
988 case IMGFMT_YUY2:
989 case IMGFMT_UYVY:
990 case IMGFMT_YVYU:
991 SDL_OVR_LOCK(-1)
992 dst = (uint8_t *) *(priv->overlay->pixels) + priv->overlay->pitches[0]*priv->y;
993 if(priv->flip) {
994 mysrc+=priv->framePlaneYUY;
995 for(i = 0; i < priv->height; i++) {
996 mysrc-=priv->stridePlaneYUY;
997 fast_memcpy (dst, mysrc, priv->stridePlaneYUY);
998 dst+=priv->overlay->pitches[0];
1001 else fast_memcpy (dst, src[0], priv->framePlaneYUY);
1002 SDL_OVR_UNLOCK
1003 break;
1005 case IMGFMT_RGB15:
1006 case IMGFMT_BGR15:
1007 case IMGFMT_RGB16:
1008 case IMGFMT_BGR16:
1009 case IMGFMT_RGB24:
1010 case IMGFMT_BGR24:
1011 case IMGFMT_RGB32:
1012 case IMGFMT_BGR32:
1013 if(priv->dblit) {
1014 SDL_SRF_LOCK(priv->surface, -1)
1015 dst = (uint8_t *) priv->surface->pixels + priv->y*priv->surface->pitch;
1016 if(priv->flip) {
1017 mysrc+=priv->framePlaneRGB;
1018 for(i = 0; i < priv->height; i++) {
1019 mysrc-=priv->stridePlaneRGB;
1020 fast_memcpy (dst, mysrc, priv->stridePlaneRGB);
1021 dst += priv->surface->pitch;
1024 else fast_memcpy (dst, src[0], priv->framePlaneRGB);
1025 SDL_SRF_UNLOCK(priv->surface)
1026 } else {
1027 SDL_SRF_LOCK(priv->rgbsurface, -1)
1028 dst = (uint8_t *) priv->rgbsurface->pixels + priv->y*priv->rgbsurface->pitch;
1029 if(priv->flip) {
1030 mysrc+=priv->framePlaneRGB;
1031 for(i = 0; i < priv->height; i++) {
1032 mysrc-=priv->stridePlaneRGB;
1033 fast_memcpy (dst, mysrc, priv->stridePlaneRGB);
1034 dst += priv->rgbsurface->pitch;
1037 else fast_memcpy (dst, src[0], priv->framePlaneRGB);
1038 SDL_SRF_UNLOCK(priv->rgbsurface)
1040 break;
1044 return 0;
1049 * Draw a slice (16 rows of image) to the SDL YUV overlay.
1051 * params : *src[] == the Y, U, and V planes that make up the slice.
1052 * returns : non-zero on error, zero on success.
1055 //static uint32_t draw_slice(uint8_t *src[], uint32_t slice_num)
1056 static int draw_slice(uint8_t *image[], int stride[], int w,int h,int x,int y)
1058 struct sdl_priv_s *priv = &sdl_priv;
1059 uint8_t *dst;
1061 SDL_OVR_LOCK(-1)
1063 y += priv->y;
1065 dst = priv->overlay->pixels[0] + priv->overlay->pitches[0]*y + x;
1066 memcpy_pic(dst, image[0], w, h, priv->overlay->pitches[0], stride[0]);
1067 x/=2;y/=2;w/=2;h/=2;
1069 switch(priv->format) {
1070 case IMGFMT_YV12:
1071 dst = priv->overlay->pixels[2] + priv->overlay->pitches[2]*y + x;
1072 memcpy_pic(dst, image[1], w, h, priv->overlay->pitches[2], stride[1]);
1074 dst = priv->overlay->pixels[1] + priv->overlay->pitches[1]*y + x;
1075 memcpy_pic(dst, image[2], w, h, priv->overlay->pitches[1], stride[2]);
1077 break;
1078 case IMGFMT_I420:
1079 case IMGFMT_IYUV:
1080 dst = priv->overlay->pixels[1] + priv->overlay->pitches[1]*y + x;
1081 memcpy_pic(dst, image[1], w, h, priv->overlay->pitches[1], stride[1]);
1083 dst = priv->overlay->pixels[2] + priv->overlay->pitches[2]*y + x;
1084 memcpy_pic(dst, image[2], w, h, priv->overlay->pitches[2], stride[2]);
1086 break;
1087 default:
1088 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Unsupported image format in draw_slice, contact MPlayer developers!\n");
1091 SDL_OVR_UNLOCK
1093 return 0;
1099 * Checks for SDL keypress and window resize events
1101 * params : none
1102 * returns : doesn't return
1105 #include "osdep/keycodes.h"
1107 #define shift_key (event.key.keysym.mod==(KMOD_LSHIFT||KMOD_RSHIFT))
1108 static void check_events (void)
1110 struct sdl_priv_s *priv = &sdl_priv;
1111 SDL_Event event;
1112 SDLKey keypressed = SDLK_UNKNOWN;
1114 /* Poll the waiting SDL Events */
1115 while ( SDL_PollEvent(&event) ) {
1116 switch (event.type) {
1118 /* capture window resize events */
1119 case SDL_VIDEORESIZE:
1120 if(!priv->dblit)
1121 set_video_mode(event.resize.w, event.resize.h, priv->bpp, priv->sdlflags);
1123 /* save video extents, to restore them after going fullscreen */
1124 //if(!(priv->surface->flags & SDL_FULLSCREEN)) {
1125 priv->windowsize.w = priv->surface->w;
1126 priv->windowsize.h = priv->surface->h;
1128 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Window resize\n");
1129 break;
1131 case SDL_MOUSEBUTTONDOWN:
1132 if(vo_nomouse_input)
1133 break;
1134 mplayer_put_key((MOUSE_BTN0+event.button.button-1) | MP_KEY_DOWN);
1135 break;
1137 case SDL_MOUSEBUTTONUP:
1138 if(vo_nomouse_input)
1139 break;
1140 mplayer_put_key(MOUSE_BTN0+event.button.button-1);
1141 break;
1143 /* graphics mode selection shortcuts */
1144 #ifdef BUGGY_SDL
1145 case SDL_KEYDOWN:
1146 switch(event.key.keysym.sym) {
1147 case SDLK_UP: mplayer_put_key(KEY_UP); break;
1148 case SDLK_DOWN: mplayer_put_key(KEY_DOWN); break;
1149 case SDLK_LEFT: mplayer_put_key(KEY_LEFT); break;
1150 case SDLK_RIGHT: mplayer_put_key(KEY_RIGHT); break;
1151 case SDLK_LESS: mplayer_put_key(shift_key?'>':'<'); break;
1152 case SDLK_GREATER: mplayer_put_key('>'); break;
1153 case SDLK_ASTERISK:
1154 case SDLK_KP_MULTIPLY:
1155 case SDLK_SLASH:
1156 case SDLK_KP_DIVIDE:
1157 default: break;
1159 break;
1160 case SDL_KEYUP:
1161 #else
1162 case SDL_KEYDOWN:
1163 #endif
1164 keypressed = event.key.keysym.sym;
1165 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Key pressed: '%i'\n", keypressed);
1167 /* c key pressed. c cycles through available fullscreenmodes, if we have some */
1168 if ( ((keypressed == SDLK_c)) && (priv->fullmodes) ) {
1169 /* select next fullscreen mode */
1170 priv->fullmode++;
1171 if (priv->fullmode > (findArrayEnd(priv->fullmodes) - 1)) priv->fullmode = 0;
1172 set_fullmode(priv->fullmode);
1174 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Set next available fullscreen mode.\n");
1177 else if ( keypressed == SDLK_n ) {
1178 #ifdef CONFIG_X11
1179 aspect(&priv->dstwidth, &priv->dstheight,A_NOZOOM);
1180 #endif
1181 if (priv->surface->w != priv->dstwidth || priv->surface->h != priv->dstheight) {
1182 set_video_mode(priv->dstwidth, priv->dstheight, priv->bpp, priv->sdlflags);
1183 priv->windowsize.w = priv->surface->w;
1184 priv->windowsize.h = priv->surface->h;
1185 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Normal size\n");
1186 } else
1187 if (priv->surface->w != priv->dstwidth * 2 || priv->surface->h != priv->dstheight * 2) {
1188 set_video_mode(priv->dstwidth * 2, priv->dstheight * 2, priv->bpp, priv->sdlflags);
1189 priv->windowsize.w = priv->surface->w;
1190 priv->windowsize.h = priv->surface->h;
1191 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Double size\n");
1195 else switch(keypressed){
1196 case SDLK_RETURN: mplayer_put_key(KEY_ENTER);break;
1197 case SDLK_ESCAPE: mplayer_put_key(KEY_ESC);break;
1198 case SDLK_q: mplayer_put_key('q');break;
1199 case SDLK_F1: mplayer_put_key(KEY_F+1);break;
1200 case SDLK_F2: mplayer_put_key(KEY_F+2);break;
1201 case SDLK_F3: mplayer_put_key(KEY_F+3);break;
1202 case SDLK_F4: mplayer_put_key(KEY_F+4);break;
1203 case SDLK_F5: mplayer_put_key(KEY_F+5);break;
1204 case SDLK_F6: mplayer_put_key(KEY_F+6);break;
1205 case SDLK_F7: mplayer_put_key(KEY_F+7);break;
1206 case SDLK_F8: mplayer_put_key(KEY_F+8);break;
1207 case SDLK_F9: mplayer_put_key(KEY_F+9);break;
1208 case SDLK_F10: mplayer_put_key(KEY_F+10);break;
1209 case SDLK_F11: mplayer_put_key(KEY_F+11);break;
1210 case SDLK_F12: mplayer_put_key(KEY_F+12);break;
1211 /*case SDLK_o: mplayer_put_key('o');break;
1212 case SDLK_SPACE: mplayer_put_key(' ');break;
1213 case SDLK_p: mplayer_put_key('p');break;*/
1214 case SDLK_7: mplayer_put_key(shift_key?'/':'7');break;
1215 case SDLK_PLUS: mplayer_put_key(shift_key?'*':'+');break;
1216 case SDLK_KP_PLUS: mplayer_put_key('+');break;
1217 case SDLK_MINUS:
1218 case SDLK_KP_MINUS: mplayer_put_key('-');break;
1219 case SDLK_TAB: mplayer_put_key('\t');break;
1220 case SDLK_PAGEUP: mplayer_put_key(KEY_PAGE_UP);break;
1221 case SDLK_PAGEDOWN: mplayer_put_key(KEY_PAGE_DOWN);break;
1222 #ifdef BUGGY_SDL
1223 case SDLK_UP:
1224 case SDLK_DOWN:
1225 case SDLK_LEFT:
1226 case SDLK_RIGHT:
1227 case SDLK_ASTERISK:
1228 case SDLK_KP_MULTIPLY:
1229 case SDLK_SLASH:
1230 case SDLK_KP_DIVIDE:
1231 break;
1232 #else
1233 case SDLK_UP: mplayer_put_key(KEY_UP);break;
1234 case SDLK_DOWN: mplayer_put_key(KEY_DOWN);break;
1235 case SDLK_LEFT: mplayer_put_key(KEY_LEFT);break;
1236 case SDLK_RIGHT: mplayer_put_key(KEY_RIGHT);break;
1237 case SDLK_LESS: mplayer_put_key(shift_key?'>':'<'); break;
1238 case SDLK_GREATER: mplayer_put_key('>'); break;
1239 case SDLK_ASTERISK:
1240 case SDLK_KP_MULTIPLY: mplayer_put_key('*'); break;
1241 case SDLK_SLASH:
1242 case SDLK_KP_DIVIDE: mplayer_put_key('/'); break;
1243 #endif
1244 case SDLK_KP0: mplayer_put_key(KEY_KP0); break;
1245 case SDLK_KP1: mplayer_put_key(KEY_KP1); break;
1246 case SDLK_KP2: mplayer_put_key(KEY_KP2); break;
1247 case SDLK_KP3: mplayer_put_key(KEY_KP3); break;
1248 case SDLK_KP4: mplayer_put_key(KEY_KP4); break;
1249 case SDLK_KP5: mplayer_put_key(KEY_KP5); break;
1250 case SDLK_KP6: mplayer_put_key(KEY_KP6); break;
1251 case SDLK_KP7: mplayer_put_key(KEY_KP7); break;
1252 case SDLK_KP8: mplayer_put_key(KEY_KP8); break;
1253 case SDLK_KP9: mplayer_put_key(KEY_KP9); break;
1254 case SDLK_KP_PERIOD: mplayer_put_key(KEY_KPDEC); break;
1255 case SDLK_KP_ENTER: mplayer_put_key(KEY_KPENTER); break;
1256 default:
1257 //printf("got scancode: %d keysym: %d mod: %d %d\n", event.key.keysym.scancode, keypressed, event.key.keysym.mod);
1258 mplayer_put_key(keypressed);
1261 break;
1262 case SDL_QUIT: mplayer_put_key(KEY_CLOSE_WIN);break;
1266 #undef shift_key
1268 /* Erase (paint it black) the rectangle specified by x, y, w and h in the surface
1269 or overlay which is used for OSD
1271 static void erase_rectangle(int x, int y, int w, int h)
1273 struct sdl_priv_s *priv = &sdl_priv;
1275 switch(priv->format) {
1276 case IMGFMT_YV12:
1277 case IMGFMT_I420:
1278 case IMGFMT_IYUV:
1280 SDL_OVR_LOCK((void) 0)
1282 /* Erase Y plane */
1283 erase_area_1(x, w, h,
1284 priv->overlay->pitches[0], 0,
1285 priv->overlay->pixels[0] +
1286 priv->overlay->pitches[0]*y);
1288 /* Erase U and V planes */
1289 w /= 2;
1290 x /= 2;
1291 h /= 2;
1292 y /= 2;
1294 erase_area_1(x, w, h,
1295 priv->overlay->pitches[1], 128,
1296 priv->overlay->pixels[1] +
1297 priv->overlay->pitches[1]*y);
1299 erase_area_1(x, w, h,
1300 priv->overlay->pitches[2], 128,
1301 priv->overlay->pixels[2] +
1302 priv->overlay->pitches[2]*y);
1303 SDL_OVR_UNLOCK
1304 break;
1307 case IMGFMT_YUY2:
1308 case IMGFMT_YVYU:
1310 /* yuy2 and yvyu represent black the same way */
1311 uint8_t yuy2_black[] = {0, 128, 0, 128};
1313 SDL_OVR_LOCK((void) 0)
1314 erase_area_4(x*2, w*2, h,
1315 priv->overlay->pitches[0],
1316 *((uint32_t*) yuy2_black),
1317 priv->overlay->pixels[0] +
1318 priv->overlay->pitches[0]*y);
1319 SDL_OVR_UNLOCK
1320 break;
1323 case IMGFMT_UYVY:
1325 uint8_t uyvy_black[] = {128, 0, 128, 0};
1327 SDL_OVR_LOCK((void) 0)
1328 erase_area_4(x*2, w*2, h,
1329 priv->overlay->pitches[0],
1330 *((uint32_t*) uyvy_black),
1331 priv->overlay->pixels[0] +
1332 priv->overlay->pitches[0]*y);
1333 SDL_OVR_UNLOCK
1334 break;
1337 case IMGFMT_RGB15:
1338 case IMGFMT_BGR15:
1339 case IMGFMT_RGB16:
1340 case IMGFMT_BGR16:
1341 case IMGFMT_RGB24:
1342 case IMGFMT_BGR24:
1343 case IMGFMT_RGB32:
1344 case IMGFMT_BGR32:
1346 SDL_Rect rect;
1347 rect.w = w; rect.h = h;
1348 rect.x = x; rect.y = y;
1350 if(priv->dblit) {
1351 SDL_SRF_LOCK(priv->surface, (void) 0)
1352 SDL_FillRect(priv->surface, &rect, 0);
1353 SDL_SRF_UNLOCK(priv->surface)
1355 else {
1356 SDL_SRF_LOCK(priv->rgbsurface, (void) 0)
1357 SDL_FillRect(priv->rgbsurface, &rect, 0);
1358 SDL_SRF_UNLOCK(priv->rgbsurface)
1360 break;
1365 static void draw_osd(void)
1366 { struct sdl_priv_s *priv = &sdl_priv;
1368 priv->osd_has_changed = vo_osd_changed(0);
1370 if(priv->osd_has_changed)
1372 int i;
1374 for(i = 0; i < 2; i++) {
1375 if(priv->dirty_off_frame[i].x < 0 || priv->dirty_off_frame[i].y < 0)
1376 continue;
1378 erase_rectangle(priv->dirty_off_frame[i].x, priv->dirty_off_frame[i].y,
1379 priv->dirty_off_frame[i].w, priv->dirty_off_frame[i].h);
1381 priv->dirty_off_frame[i].x = -1;
1382 priv->dirty_off_frame[i].y = -1;
1386 /* update osd/subtitles */
1387 if(priv->mode == YUV)
1388 vo_draw_text(priv->overlay->w, priv->overlay->h, draw_alpha);
1389 else {
1390 if(priv->dblit)
1391 vo_draw_text(priv->surface->w, priv->surface->h, draw_alpha);
1392 else
1393 vo_draw_text(priv->rgbsurface->w, priv->rgbsurface->h, draw_alpha);
1397 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1398 * are given in bytes. 4 bytes at a time.
1400 static void erase_area_4(int x_start, int width, int height, int pitch, uint32_t color, uint8_t* pixels)
1402 int x_end = x_start/4 + width/4;
1403 int x, y;
1404 uint32_t* data = (uint32_t*) pixels;
1406 x_start /= 4;
1407 pitch /= 4;
1409 for(y = 0; y < height; y++) {
1410 for(x = x_start; x < x_end; x++)
1411 data[y*pitch + x] = color;
1415 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1416 * are given in bytes. 1 byte at a time.
1418 static void erase_area_1(int x_start, int width, int height, int pitch, uint8_t color, uint8_t* pixels)
1420 int y;
1422 for(y = 0; y < height; y++) {
1423 memset(&pixels[y*pitch + x_start], color, width);
1428 * Display the surface we have written our data to
1430 * params : mode == index of the desired fullscreen mode
1431 * returns : doesn't return
1434 static void flip_page (void)
1436 struct sdl_priv_s *priv = &sdl_priv;
1438 switch(priv->format) {
1439 case IMGFMT_RGB15:
1440 case IMGFMT_BGR15:
1441 case IMGFMT_RGB16:
1442 case IMGFMT_BGR16:
1443 case IMGFMT_RGB24:
1444 case IMGFMT_BGR24:
1445 case IMGFMT_RGB32:
1446 case IMGFMT_BGR32:
1447 if(!priv->dblit) {
1448 /* blit to the RGB surface */
1449 if(SDL_BlitSurface (priv->rgbsurface, NULL, priv->surface, NULL))
1450 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Blit failed: %s.\n", SDL_GetError());
1453 /* update screen */
1454 //SDL_UpdateRect(priv->surface, 0, 0, priv->surface->clip_rect.w, priv->surface->clip_rect.h);
1455 if(priv->osd_has_changed) {
1456 priv->osd_has_changed = 0;
1457 SDL_UpdateRects(priv->surface, 1, &priv->surface->clip_rect);
1459 else
1460 SDL_UpdateRect(priv->surface, 0, priv->y_screen_top,
1461 priv->surface->clip_rect.w, priv->y_screen_bottom);
1463 /* check if we have a double buffered surface and flip() if we do. */
1464 if ( priv->surface->flags & SDL_DOUBLEBUF )
1465 SDL_Flip(priv->surface);
1467 break;
1468 default:
1469 /* blit to the YUV overlay */
1470 SDL_DisplayYUVOverlay (priv->overlay, &priv->surface->clip_rect);
1472 /* check if we have a double buffered surface and flip() if we do. */
1473 if ( priv->surface->flags & SDL_DOUBLEBUF )
1474 SDL_Flip(priv->surface);
1476 //SDL_LockYUVOverlay (priv->overlay); // removed because unused!?
1480 static int
1481 query_format(uint32_t format)
1483 switch(format){
1484 case IMGFMT_YV12:
1485 // it seems buggy (not hw accelerated), so just use YV12 instead!
1486 // case IMGFMT_I420:
1487 // case IMGFMT_IYUV:
1488 case IMGFMT_YUY2:
1489 case IMGFMT_UYVY:
1490 case IMGFMT_YVYU:
1491 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD |
1492 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
1493 case IMGFMT_RGB15:
1494 case IMGFMT_BGR15:
1495 case IMGFMT_RGB16:
1496 case IMGFMT_BGR16:
1497 case IMGFMT_RGB24:
1498 case IMGFMT_BGR24:
1499 case IMGFMT_RGB32:
1500 case IMGFMT_BGR32:
1501 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_FLIP;
1503 return 0;
1507 static void
1508 uninit(void)
1510 #ifdef CONFIG_X11
1511 struct sdl_priv_s *priv = &sdl_priv;
1512 if(priv->X) {
1513 mp_msg(MSGT_VO,MSGL_V, "SDL: activating XScreensaver/DPMS\n");
1514 vo_x11_uninit();
1516 #endif
1517 sdl_close();
1519 /* Cleanup SDL */
1520 if(SDL_WasInit(SDL_INIT_VIDEO))
1521 SDL_QuitSubSystem(SDL_INIT_VIDEO);
1523 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Closed Plugin\n");
1527 static int preinit(const char *arg)
1529 struct sdl_priv_s *priv = &sdl_priv;
1530 char * sdl_driver = NULL;
1531 int sdl_hwaccel;
1532 int sdl_forcexv;
1533 const opt_t subopts[] = {
1534 {"forcexv", OPT_ARG_BOOL, &sdl_forcexv, NULL},
1535 {"hwaccel", OPT_ARG_BOOL, &sdl_hwaccel, NULL},
1536 {"driver", OPT_ARG_MSTRZ, &sdl_driver, NULL},
1537 {NULL, 0, NULL, NULL}
1540 sdl_forcexv = 1;
1541 sdl_hwaccel = 1;
1543 if (subopt_parse(arg, subopts) != 0) return -1;
1545 priv->rgbsurface = NULL;
1546 priv->overlay = NULL;
1547 priv->surface = NULL;
1549 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Opening Plugin\n");
1551 if(sdl_driver) {
1552 setenv("SDL_VIDEODRIVER", sdl_driver, 1);
1553 free(sdl_driver);
1556 /* does the user want SDL to try and force Xv */
1557 if(sdl_forcexv) setenv("SDL_VIDEO_X11_NODIRECTCOLOR", "1", 1);
1558 else setenv("SDL_VIDEO_X11_NODIRECTCOLOR", "0", 1);
1560 /* does the user want to disable Xv and use software scaling instead */
1561 if(sdl_hwaccel) setenv("SDL_VIDEO_YUV_HWACCEL", "1", 1);
1562 else setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
1564 /* default to no fullscreen mode, we'll set this as soon we have the avail. modes */
1565 priv->fullmode = -2;
1567 priv->fullmodes = NULL;
1568 priv->bpp = 0;
1570 /* initialize the SDL Video system */
1571 if (!SDL_WasInit(SDL_INIT_VIDEO)) {
1572 if (SDL_Init (SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE)) {
1573 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_SDL] SDL initialization failed: %s.\n", SDL_GetError());
1575 return -1;
1579 SDL_VideoDriverName(priv->driver, 8);
1580 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Using driver: %s.\n", priv->driver);
1582 priv->X = 0;
1583 #ifdef CONFIG_X11
1584 if(vo_init()) {
1585 mp_msg(MSGT_VO,MSGL_V, "SDL: deactivating XScreensaver/DPMS\n");
1586 priv->XWidth = vo_screenwidth;
1587 priv->XHeight = vo_screenheight;
1588 priv->X = 1;
1589 mp_msg(MSGT_VO,MSGL_V, "SDL: X11 Resolution %ix%i\n", priv->XWidth, priv->XHeight);
1591 #endif
1593 return 0;
1596 static uint32_t get_image(mp_image_t *mpi)
1598 struct sdl_priv_s *priv = &sdl_priv;
1600 if(priv->format != mpi->imgfmt) return VO_FALSE;
1601 if(mpi->type == MP_IMGTYPE_STATIC || mpi->type == MP_IMGTYPE_TEMP) {
1602 if(mpi->flags&MP_IMGFLAG_PLANAR) {
1603 mpi->planes[0] = priv->overlay->pixels[0] + priv->y*priv->overlay->pitches[0];
1604 mpi->stride[0] = priv->overlay->pitches[0];
1605 if(mpi->flags&MP_IMGFLAG_SWAPPED) {
1606 mpi->planes[1] = priv->overlay->pixels[1] + priv->y*priv->overlay->pitches[1]/2;
1607 mpi->stride[1] = priv->overlay->pitches[1];
1608 mpi->planes[2] = priv->overlay->pixels[2] + priv->y*priv->overlay->pitches[2]/2;
1609 mpi->stride[2] = priv->overlay->pitches[2];
1610 } else {
1611 mpi->planes[2] = priv->overlay->pixels[1] + priv->y*priv->overlay->pitches[1]/2;
1612 mpi->stride[2] = priv->overlay->pitches[1];
1613 mpi->planes[1] = priv->overlay->pixels[2] + priv->y*priv->overlay->pitches[2]/2;
1614 mpi->stride[1] = priv->overlay->pitches[2];
1617 else if(IMGFMT_IS_RGB(priv->format) || IMGFMT_IS_BGR(priv->format)) {
1618 if(priv->dblit) {
1619 if(mpi->type == MP_IMGTYPE_STATIC && (priv->surface->flags & SDL_DOUBLEBUF))
1620 return VO_FALSE;
1622 mpi->planes[0] = (uint8_t *)priv->surface->pixels + priv->y*priv->surface->pitch;
1623 mpi->stride[0] = priv->surface->pitch;
1625 else {
1626 mpi->planes[0] = (uint8_t *)priv->rgbsurface->pixels + priv->y*priv->rgbsurface->pitch;
1627 mpi->stride[0] = priv->rgbsurface->pitch;
1630 else {
1631 mpi->planes[0] = priv->overlay->pixels[0] + priv->y*priv->overlay->pitches[0];
1632 mpi->stride[0] = priv->overlay->pitches[0];
1635 mpi->flags|=MP_IMGFLAG_DIRECT;
1636 return VO_TRUE;
1639 return VO_FALSE;
1642 static int control(uint32_t request, void *data)
1644 struct sdl_priv_s *priv = &sdl_priv;
1645 switch (request) {
1646 case VOCTRL_GET_IMAGE:
1647 return get_image(data);
1648 case VOCTRL_QUERY_FORMAT:
1649 return query_format(*((uint32_t*)data));
1650 case VOCTRL_FULLSCREEN:
1651 if (priv->surface->flags & SDL_FULLSCREEN) {
1652 set_video_mode(priv->windowsize.w, priv->windowsize.h, priv->bpp, priv->sdlflags);
1653 SDL_ShowCursor(1);
1654 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Windowed mode\n");
1655 } else if (priv->fullmodes) {
1656 set_fullmode(priv->fullmode);
1657 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Set fullscreen mode\n");
1659 return VO_TRUE;
1662 return VO_NOTIMPL;