Fix vf_tcdump's compilation
[mplayer/kovensky.git] / libvo / vo_sdl.c
blob9ae325b98a9fc3b515d69b97aeca80c4e3cb4cf3
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 "video_out.h"
65 #include "video_out_internal.h"
67 #include "fastmemcpy.h"
68 #include "sub.h"
69 #include "aspect.h"
70 #include "libmpcodecs/vfcap.h"
72 #ifdef CONFIG_X11
73 #include <X11/Xlib.h>
74 #include "x11_common.h"
75 #endif
77 #include "input/input.h"
78 #include "input/mouse.h"
79 #include "subopt-helper.h"
80 #include "mp_fifo.h"
82 static const vo_info_t info =
84 "SDL YUV/RGB/BGR renderer (SDL v1.1.7+ only!)",
85 "sdl",
86 "Ryan C. Gordon <icculus@lokigames.com>, Felix Buenemann <atmosfear@users.sourceforge.net>",
90 const LIBVO_EXTERN(sdl)
92 #ifdef CONFIG_SDL_SDL_H
93 #include <SDL/SDL.h>
94 #else
95 #include <SDL.h>
96 #endif
97 //#include <SDL/SDL_syswm.h>
100 #ifdef SDL_ENABLE_LOCKS
101 #define SDL_OVR_LOCK(x) if (SDL_LockYUVOverlay (priv->overlay)) { \
102 mp_msg(MSGT_VO,MSGL_V, "SDL: Couldn't lock YUV overlay\n"); \
103 return x; \
105 #define SDL_OVR_UNLOCK SDL_UnlockYUVOverlay (priv->overlay);
107 #define SDL_SRF_LOCK(srf, x) if(SDL_MUSTLOCK(srf)) { \
108 if(SDL_LockSurface (srf)) { \
109 mp_msg(MSGT_VO,MSGL_V, "SDL: Couldn't lock RGB surface\n"); \
110 return x; \
114 #define SDL_SRF_UNLOCK(srf) if(SDL_MUSTLOCK(srf)) \
115 SDL_UnlockSurface (srf);
116 #else
117 #define SDL_OVR_LOCK(x)
118 #define SDL_OVR_UNLOCK
119 #define SDL_SRF_LOCK(srf, x)
120 #define SDL_SRF_UNLOCK(srf)
121 #endif
123 /** Private SDL Data structure **/
125 static struct sdl_priv_s {
127 /* output driver used by sdl */
128 char driver[8];
130 /* SDL display surface */
131 SDL_Surface *surface;
133 /* SDL RGB surface */
134 SDL_Surface *rgbsurface;
136 /* SDL YUV overlay */
137 SDL_Overlay *overlay;
139 /* available fullscreen modes */
140 SDL_Rect **fullmodes;
142 /* surface attributes for fullscreen and windowed mode */
143 Uint32 sdlflags, sdlfullflags;
145 /* save the windowed output extents */
146 SDL_Rect windowsize;
148 /* Bits per Pixel */
149 Uint8 bpp;
151 /* RGB or YUV? */
152 Uint8 mode;
153 #define YUV 0
154 #define RGB 1
155 #define BGR 2
157 /* use direct blitting to surface */
158 int dblit;
160 /* current fullscreen mode, 0 = highest available fullscreen mode */
161 int fullmode;
163 /* YUV ints */
164 int framePlaneY, framePlaneUV, framePlaneYUY;
165 int stridePlaneY, stridePlaneUV, stridePlaneYUY;
167 /* RGB ints */
168 int framePlaneRGB;
169 int stridePlaneRGB;
171 /* Flip image */
172 int flip;
174 /* fullscreen behaviour; see init */
175 int fulltype;
177 /* is X running (0/1) */
178 int X;
180 /* X11 Resolution */
181 int XWidth, XHeight;
183 /* original image dimensions */
184 int width, height;
186 /* destination dimensions */
187 int dstwidth, dstheight;
189 /* Draw image at coordinate y on the SDL surfaces */
190 int y;
192 /* The image is displayed between those y coordinates in priv->surface */
193 int y_screen_top, y_screen_bottom;
195 /* 1 if the OSD has changed otherwise 0 */
196 int osd_has_changed;
198 /* source image format (YUV/RGB/...) */
199 uint32_t format;
201 /* dirty_off_frame[0] contains a bounding box around the osd contents drawn above the image
202 dirty_off_frame[1] is the corresponding thing for OSD contents drawn below the image
204 SDL_Rect dirty_off_frame[2];
205 } sdl_priv;
207 static void erase_area_4(int x_start, int width, int height, int pitch, uint32_t color, uint8_t* pixels);
208 static void erase_area_1(int x_start, int width, int height, int pitch, uint8_t color, uint8_t* pixels);
209 static int setup_surfaces(void);
210 static void set_video_mode(int width, int height, int bpp, uint32_t sdlflags);
211 static void erase_rectangle(int x, int y, int w, int h);
213 /* Expand 'rect' to contain the rectangle specified by x, y, w and h */
214 static void expand_rect(SDL_Rect* rect, int x, int y, int w, int h)
216 if(rect->x < 0 || rect->y < 0) {
217 rect->x = x;
218 rect->y = y;
219 rect->w = w;
220 rect->h = h;
221 return;
224 if(rect->x > x)
225 rect->x = x;
227 if(rect->y > y)
228 rect->y = y;
230 if(rect->x + rect->w < x + w)
231 rect->w = x + w - rect->x;
233 if(rect->y + rect->h < y + h)
234 rect->h = y + h - rect->y;
237 /** libvo Plugin functions **/
240 * draw_alpha is used for osd and subtitle display.
244 static void draw_alpha(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
245 struct sdl_priv_s *priv = &sdl_priv;
247 if(priv->osd_has_changed) {
248 /* OSD did change. Store a bounding box of everything drawn into the OSD */
249 if(priv->y >= y0) {
250 /* Make sure we don't mark part of the frame area dirty */
251 if(h + y0 > priv->y)
252 expand_rect(&priv->dirty_off_frame[0], x0, y0, w, priv->y - y0);
253 else
254 expand_rect(&priv->dirty_off_frame[0], x0, y0, w, h);
256 else if(priv->y + priv->height <= y0 + h) {
257 /* Make sure we don't mark part of the frame area dirty */
258 if(y0 < priv->y + priv->height)
259 expand_rect(&priv->dirty_off_frame[1], x0,
260 priv->y + priv->height,
261 w, h - ((priv->y + priv->height) - y0));
262 else
263 expand_rect(&priv->dirty_off_frame[1], x0, y0, w, h);
266 else { /* OSD contents didn't change only draw parts that was erased by the frame */
267 if(priv->y >= y0) {
268 src = src + (priv->y - y0) * stride;
269 srca = srca + (priv->y - y0) * stride;
270 h -= priv->y - y0;
271 y0 = priv->y;
274 if(priv->y + priv->height <= y0 + h)
275 h = priv->y + priv->height - y0;
277 if(h <= 0)
278 return;
281 switch(priv->format) {
282 case IMGFMT_YV12:
283 case IMGFMT_I420:
284 case IMGFMT_IYUV:
285 vo_draw_alpha_yv12(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
286 break;
287 case IMGFMT_YUY2:
288 case IMGFMT_YVYU:
289 x0 *= 2;
290 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
291 break;
292 case IMGFMT_UYVY:
293 x0 *= 2;
294 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
295 break;
297 default:
298 if(priv->dblit) {
299 x0 *= priv->surface->format->BytesPerPixel;
300 switch(priv->format) {
301 case IMGFMT_RGB15:
302 case IMGFMT_BGR15:
303 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
304 break;
305 case IMGFMT_RGB16:
306 case IMGFMT_BGR16:
307 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
308 break;
309 case IMGFMT_RGB24:
310 case IMGFMT_BGR24:
311 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
312 break;
313 case IMGFMT_RGB32:
314 case IMGFMT_BGR32:
315 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
316 break;
319 else {
320 x0 *= priv->rgbsurface->format->BytesPerPixel;
321 switch(priv->format) {
322 case IMGFMT_RGB15:
323 case IMGFMT_BGR15:
324 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
325 break;
326 case IMGFMT_RGB16:
327 case IMGFMT_BGR16:
328 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
329 break;
330 case IMGFMT_RGB24:
331 case IMGFMT_BGR24:
332 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
333 break;
334 case IMGFMT_RGB32:
335 case IMGFMT_BGR32:
336 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
337 break;
346 * Take a null-terminated array of pointers, and find the last element.
348 * params : array == array of which we want to find the last element.
349 * returns : index of last NON-NULL element.
352 static inline int findArrayEnd (SDL_Rect **array)
354 int i = 0;
355 while ( array[i++] ); /* keep loopin' ... */
357 /* return the index of the last array element */
358 return i - 1;
363 * Open and prepare SDL output.
365 * params : *plugin ==
366 * *name ==
367 * returns : 0 on success, -1 on failure
370 static int sdl_open (void *plugin, void *name)
372 struct sdl_priv_s *priv = &sdl_priv;
373 const SDL_VideoInfo *vidInfo = NULL;
374 /*static int opened = 0;
376 if (opened)
377 return 0;
378 opened = 1;*/
381 /* other default values */
382 #ifdef SDL_NOHWSURFACE
383 mp_msg(MSGT_VO,MSGL_V, "SDL: using software-surface\n");
384 priv->sdlflags = SDL_SWSURFACE|SDL_RESIZABLE|SDL_ANYFORMAT;
385 priv->sdlfullflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ANYFORMAT;
386 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
387 #else
388 /*if((strcmp(priv->driver, "dga") == 0) && (priv->mode)) {
389 if( mp_msg_test(MSGT_VO,MSGL_V) ) {
390 printf("SDL: using software-surface\n"); }
391 priv->sdlflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
392 priv->sdlfullflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
394 else { */
395 mp_msg(MSGT_VO,MSGL_V, "SDL: using hardware-surface\n");
396 priv->sdlflags = SDL_HWSURFACE|SDL_RESIZABLE/*|SDL_ANYFORMAT*/;
397 priv->sdlfullflags = SDL_HWSURFACE|SDL_FULLSCREEN/*|SDL_ANYFORMAT*/;
398 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
400 #endif
402 #if !defined( __AMIGAOS4__ ) && !defined( __APPLE__ )
403 priv->sdlfullflags |= SDL_DOUBLEBUF;
404 if (vo_doublebuffering)
405 priv->sdlflags |= SDL_DOUBLEBUF;
406 #endif
408 /* Setup Keyrepeats (500/30 are defaults) */
409 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, 100 /*SDL_DEFAULT_REPEAT_INTERVAL*/);
411 /* get information about the graphics adapter */
412 vidInfo = SDL_GetVideoInfo ();
414 /* collect all fullscreen & hardware modes available */
415 if (!(priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags))) {
417 /* non hardware accelerated fullscreen modes */
418 priv->sdlfullflags &= ~SDL_HWSURFACE;
419 priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags);
422 /* test for normal resizeable & windowed hardware accellerated surfaces */
423 if (!SDL_ListModes (vidInfo->vfmt, priv->sdlflags)) {
425 /* test for NON hardware accelerated resizeable surfaces - poor you.
426 * That's all we have. If this fails there's nothing left.
427 * Theoretically there could be Fullscreenmodes left - we ignore this for now.
429 priv->sdlflags &= ~SDL_HWSURFACE;
430 if ((!SDL_ListModes (vidInfo->vfmt, priv->sdlflags)) && (!priv->fullmodes)) {
431 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_SDL] Couldn't get any acceptable SDL Mode for output.\n");
432 return -1;
437 /* YUV overlays need at least 16-bit color depth, but the
438 * display might less. The SDL AAlib target says it can only do
439 * 8-bits, for example. So, if the display is less than 16-bits,
440 * we'll force the BPP to 16, and pray that SDL can emulate for us.
442 priv->bpp = vidInfo->vfmt->BitsPerPixel;
443 if (priv->mode == YUV && priv->bpp < 16) {
445 mp_msg(MSGT_VO,MSGL_V, "SDL: Your SDL display target wants to be at a color "
446 "depth of (%d), but we need it to be at least 16 "
447 "bits, so we need to emulate 16-bit color. This is "
448 "going to slow things down; you might want to "
449 "increase your display's color depth, if possible.\n",
450 priv->bpp);
452 priv->bpp = 16;
455 /* We don't want those in our event queue.
456 * We use SDL_KEYUP cause SDL_KEYDOWN seems to cause problems
457 * with keys need to be pressed twice, to be recognized.
459 #ifndef BUGGY_SDL
460 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
461 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
462 // SDL_EventState(SDL_QUIT, SDL_IGNORE);
463 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
464 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
465 #endif
467 /* Success! */
468 return 0;
473 * Close SDL, Cleanups, Free Memory
475 * params : *plugin
476 * returns : non-zero on success, zero on error.
479 static int sdl_close (void)
481 struct sdl_priv_s *priv = &sdl_priv;
483 if (priv->fullmode)
484 SDL_ShowCursor(1);
486 /* Cleanup YUV Overlay structure */
487 if (priv->overlay) {
488 SDL_FreeYUVOverlay(priv->overlay);
489 priv->overlay=NULL;
492 /* Free RGB Surface */
493 if (priv->rgbsurface) {
494 SDL_FreeSurface(priv->rgbsurface);
495 priv->rgbsurface=NULL;
498 /* Free our blitting surface */
499 if (priv->surface) {
500 SDL_FreeSurface(priv->surface);
501 priv->surface=NULL;
504 /* DON'T attempt to free the fullscreen modes array. SDL_Quit* does this for us */
506 return 0;
510 * Do aspect ratio calculations
512 * params : srcw == sourcewidth
513 * srch == sourceheight
514 * dstw == destinationwidth
515 * dsth == destinationheight
517 * returns : SDL_Rect structure with new x and y, w and h
520 #if 0
521 static SDL_Rect aspect(int srcw, int srch, int dstw, int dsth) {
522 SDL_Rect newres;
523 mp_msg(MSGT_VO,MSGL_V, "SDL Aspect-Destinationres: %ix%i (x: %i, y: %i)\n", newres.w, newres.h, newres.x, newres.y);
524 newres.h = ((float)dstw / (float)srcw * (float)srch) * ((float)dsth/((float)dstw/(MONITOR_ASPECT)));
525 if(newres.h > dsth) {
526 newres.w = ((float)dsth / (float)newres.h) * dstw;
527 newres.h = dsth;
528 newres.x = (dstw - newres.w) / 2;
529 newres.y = 0;
531 else {
532 newres.w = dstw;
533 newres.x = 0;
534 newres.y = (dsth - newres.h) / 2;
537 mp_msg(MSGT_VO,MSGL_V, "SDL Mode: %d: %d x %d\n", i, priv->fullmodes[i]->w, priv->fullmodes[i]->h);
539 return newres;
541 #endif
544 * Sets the specified fullscreen mode.
546 * params : mode == index of the desired fullscreen mode
547 * returns : doesn't return
550 #if 0
551 static void set_fullmode (int mode)
553 struct sdl_priv_s *priv = &sdl_priv;
554 SDL_Surface *newsurface = NULL;
555 int haspect, waspect = 0;
557 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
558 if (mode < 0)
559 mode = priv->fullmode = findArrayEnd(priv->fullmodes) - 1;
561 /* Calculate proper aspect ratio for fullscreen
562 * Height smaller than expected: add horizontal black bars (haspect)*/
563 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);
564 /* Height bigger than expected: add vertical black bars (waspect)*/
565 if (haspect < 0) {
566 haspect = 0; /* set haspect to zero because image will be scaled horizontal instead of vertical */
567 waspect = priv->fullmodes[mode]->w - ((float) ((float) priv->fullmodes[mode]->h / (float) priv->height) * (float) priv->width);
569 // printf ("W-Aspect: %i H-Aspect: %i\n", waspect, haspect);
571 /* change to given fullscreen mode and hide the mouse cursor */
572 newsurface = SDL_SetVideoMode(priv->fullmodes[mode]->w - waspect, priv->fullmodes[mode]->h - haspect, priv->bpp, priv->sdlfullflags);
574 /* if we were successful hide the mouse cursor and save the mode */
575 if (newsurface) {
576 if (priv->surface)
577 SDL_FreeSurface(priv->surface);
578 priv->surface = newsurface;
579 SDL_ShowCursor(0);
582 #endif
584 /* Set video mode. Not fullscreen */
585 static void set_video_mode(int width, int height, int bpp, uint32_t sdlflags)
587 struct sdl_priv_s *priv = &sdl_priv;
588 SDL_Surface* newsurface;
590 if(priv->rgbsurface)
591 SDL_FreeSurface(priv->rgbsurface);
592 else if(priv->overlay)
593 SDL_FreeYUVOverlay(priv->overlay);
595 priv->rgbsurface = NULL;
596 priv->overlay = NULL;
598 newsurface = SDL_SetVideoMode(width, height, bpp, sdlflags);
600 if(newsurface) {
602 /* priv->surface will be NULL the first time this function is called. */
603 if(priv->surface)
604 SDL_FreeSurface(priv->surface);
606 priv->surface = newsurface;
607 priv->dstwidth = width;
608 priv->dstheight = height;
610 setup_surfaces();
612 else
613 mp_msg(MSGT_VO,MSGL_WARN, "set_video_mode: SDL_SetVideoMode failed: %s\n", SDL_GetError());
616 static void set_fullmode (int mode) {
617 struct sdl_priv_s *priv = &sdl_priv;
618 SDL_Surface *newsurface = NULL;
619 int screen_surface_w, screen_surface_h;
621 if(priv->rgbsurface)
622 SDL_FreeSurface(priv->rgbsurface);
623 else if(priv->overlay)
624 SDL_FreeYUVOverlay(priv->overlay);
626 priv->rgbsurface = NULL;
627 priv->overlay = NULL;
629 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
630 /* But select a mode where the full video enter */
631 if(priv->X && priv->fulltype & VOFLAG_FULLSCREEN) {
632 screen_surface_w = priv->XWidth;
633 screen_surface_h = priv->XHeight;
635 else if (mode < 0) {
636 int i,j,imax;
637 mode = 0; // Default to the biggest mode avaible
638 if ( mp_msg_test(MSGT_VO,MSGL_V) ) for(i=0;priv->fullmodes[i];++i)
639 mp_msg(MSGT_VO,MSGL_V, "SDL Mode: %d: %d x %d\n", i, priv->fullmodes[i]->w, priv->fullmodes[i]->h);
640 for(i = findArrayEnd(priv->fullmodes) - 1; i >=0; i--) {
641 if( (priv->fullmodes[i]->w >= priv->dstwidth) &&
642 (priv->fullmodes[i]->h >= priv->dstheight) ) {
643 imax = i;
644 for (j = findArrayEnd(priv->fullmodes) - 1; j >=0; j--) {
645 if (priv->fullmodes[j]->w > priv->fullmodes[imax]->w
646 && priv->fullmodes[j]->h == priv->fullmodes[imax]->h)
647 imax = j;
649 mode = imax;
650 break;
653 mp_msg(MSGT_VO,MSGL_V, "SET SDL Mode: %d: %d x %d\n", mode, priv->fullmodes[mode]->w, priv->fullmodes[mode]->h);
654 priv->fullmode = mode;
655 screen_surface_h = priv->fullmodes[mode]->h;
656 screen_surface_w = priv->fullmodes[mode]->w;
658 else {
659 screen_surface_h = priv->fullmodes[mode]->h;
660 screen_surface_w = priv->fullmodes[mode]->w;
663 aspect_save_screenres(screen_surface_w, screen_surface_h);
665 /* calculate new video size/aspect */
666 if(priv->mode == YUV) {
667 if(priv->fulltype&VOFLAG_FULLSCREEN)
668 aspect_save_screenres(priv->XWidth, priv->XHeight);
670 aspect(&priv->dstwidth, &priv->dstheight, A_ZOOM);
673 /* try to change to given fullscreenmode */
674 newsurface = SDL_SetVideoMode(priv->dstwidth, screen_surface_h, priv->bpp,
675 priv->sdlfullflags);
678 * In Mac OS X (and possibly others?) SDL_SetVideoMode() appears to
679 * destroy the datastructure previously retrived, so we need to
680 * re-assign it. The comment in sdl_close() seems to imply that we
681 * should not free() anything.
683 #ifdef __APPLE__
685 const SDL_VideoInfo *vidInfo = NULL;
686 vidInfo = SDL_GetVideoInfo ();
688 /* collect all fullscreen & hardware modes available */
689 if (!(priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags))) {
691 /* non hardware accelerated fullscreen modes */
692 priv->sdlfullflags &= ~SDL_HWSURFACE;
693 priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags);
696 #endif
700 /* if creation of new surface was successful, save it and hide mouse cursor */
701 if(newsurface) {
702 if (priv->surface)
703 SDL_FreeSurface(priv->surface);
704 priv->surface = newsurface;
705 SDL_ShowCursor(0);
706 SDL_SRF_LOCK(priv->surface, -1)
707 SDL_FillRect(priv->surface, NULL, 0);
708 SDL_SRF_UNLOCK(priv->surface)
709 setup_surfaces();
711 else
712 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Set_fullmode: SDL_SetVideoMode failed: %s.\n", SDL_GetError());
717 * Initialize an SDL surface and an SDL YUV overlay.
719 * params : width == width of video we'll be displaying.
720 * height == height of video we'll be displaying.
721 * fullscreen == want to be fullscreen?
722 * title == Title for window titlebar.
723 * returns : non-zero on success, zero on error.
726 static int
727 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
728 //static int sdl_setup (int width, int height)
730 struct sdl_priv_s *priv = &sdl_priv;
732 switch(format){
733 case IMGFMT_I420:
734 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Mapping I420 to IYUV.\n");
735 format = SDL_IYUV_OVERLAY;
736 case IMGFMT_YV12:
737 case IMGFMT_IYUV:
738 case IMGFMT_YUY2:
739 case IMGFMT_UYVY:
740 case IMGFMT_YVYU:
741 priv->mode = YUV;
742 break;
743 case IMGFMT_BGR15:
744 case IMGFMT_BGR16:
745 case IMGFMT_BGR24:
746 case IMGFMT_BGR32:
747 priv->mode = BGR;
748 break;
749 case IMGFMT_RGB15:
750 case IMGFMT_RGB16:
751 case IMGFMT_RGB24:
752 case IMGFMT_RGB32:
753 priv->mode = RGB;
754 break;
755 default:
756 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Unsupported image format (0x%X).\n",format);
757 return -1;
760 if ( vo_config_count ) sdl_close();
762 mp_msg(MSGT_VO,MSGL_V, "SDL: Using 0x%X (%s) image format\n", format, vo_format_name(format));
764 if(priv->mode != YUV) {
765 priv->sdlflags |= SDL_ANYFORMAT;
766 priv->sdlfullflags |= SDL_ANYFORMAT;
769 /* SDL can only scale YUV data */
770 if(priv->mode == RGB || priv->mode == BGR) {
771 d_width = width;
772 d_height = height;
775 aspect_save_orig(width,height);
776 aspect_save_prescale(d_width ? d_width : width, d_height ? d_height : height);
778 /* Save the original Image size */
779 priv->width = width;
780 priv->height = height;
781 priv->dstwidth = d_width ? d_width : width;
782 priv->dstheight = d_height ? d_height : height;
784 priv->format = format;
786 if (sdl_open(NULL, NULL) != 0)
787 return -1;
789 /* Set output window title */
790 SDL_WM_SetCaption (".: MPlayer : F = Fullscreen/Windowed : C = Cycle Fullscreen Resolutions :.", title);
791 //SDL_WM_SetCaption (title, title);
793 if(priv->X) {
794 aspect_save_screenres(priv->XWidth,priv->XHeight);
795 aspect(&priv->dstwidth,&priv->dstheight,A_NOZOOM);
798 priv->windowsize.w = priv->dstwidth;
799 priv->windowsize.h = priv->dstheight;
801 /* bit 0 (0x01) means fullscreen (-fs)
802 * bit 1 (0x02) means mode switching (-vm)
803 * bit 2 (0x04) enables software scaling (-zoom)
804 * bit 3 (0x08) enables flipping (-flip)
806 // printf("SDL: flags are set to: %i\n", flags);
807 // printf("SDL: Width: %i Height: %i D_Width %i D_Height: %i\n", width, height, d_width, d_height);
808 if(flags&VOFLAG_FLIPPING) {
809 mp_msg(MSGT_VO,MSGL_V, "SDL: using flipped video (only with RGB/BGR/packed YUV)\n");
810 priv->flip = 1;
812 if(flags&VOFLAG_FULLSCREEN) {
813 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen without modeswitching\n");
814 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Info - please use -vm or -zoom to switch to the best resolution.\n");
815 priv->fulltype = VOFLAG_FULLSCREEN;
816 set_fullmode(priv->fullmode);
817 /*if((priv->surface = SDL_SetVideoMode (d_width, d_height, priv->bpp, priv->sdlfullflags)))
818 SDL_ShowCursor(0);*/
819 } else
820 if(flags&VOFLAG_MODESWITCHING) {
821 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
822 priv->fulltype = VOFLAG_MODESWITCHING;
823 set_fullmode(priv->fullmode);
824 /*if((priv->surface = SDL_SetVideoMode (d_width ? d_width : width, d_height ? d_height : height, priv->bpp, priv->sdlfullflags)))
825 SDL_ShowCursor(0);*/
826 } else
827 if(flags&VOFLAG_SWSCALE) {
828 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
829 priv->fulltype = VOFLAG_SWSCALE;
830 set_fullmode(priv->fullmode);
832 else {
833 if((strcmp(priv->driver, "x11") == 0)
834 ||(strcmp(priv->driver, "windib") == 0)
835 ||(strcmp(priv->driver, "directx") == 0)
836 ||(strcmp(priv->driver, "Quartz") == 0)
837 ||(strcmp(priv->driver, "cgx") == 0)
838 ||(strcmp(priv->driver, "os4video") == 0)
839 ||((strcmp(priv->driver, "aalib") == 0) && priv->X)){
840 mp_msg(MSGT_VO,MSGL_V, "SDL: setting windowed mode\n");
841 set_video_mode(priv->dstwidth, priv->dstheight, priv->bpp, priv->sdlflags);
843 else {
844 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
845 priv->fulltype = VOFLAG_SWSCALE;
846 set_fullmode(priv->fullmode);
850 if(!priv->surface) { // cannot SetVideoMode
851 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Failed to set video mode: %s.\n", SDL_GetError());
852 return -1;
855 return 0;
858 /* Free priv->rgbsurface or priv->overlay if they are != NULL.
859 * Setup priv->rgbsurface or priv->overlay depending on source format.
860 * The size of the created surface or overlay depends on the size of
861 * priv->surface, priv->width, priv->height, priv->dstwidth and priv->dstheight.
863 static int setup_surfaces(void)
865 struct sdl_priv_s *priv = &sdl_priv;
866 float v_scale = ((float) priv->dstheight) / priv->height;
867 int surfwidth, surfheight;
869 surfwidth = priv->width;
870 surfheight = priv->height + (priv->surface->h - priv->dstheight) / v_scale;
871 surfheight&= ~1;
872 /* Place the image in the middle of the screen */
873 priv->y = (surfheight - priv->height) / 2;
874 priv->y_screen_top = priv->y * v_scale;
875 priv->y_screen_bottom = priv->y_screen_top + priv->dstheight;
877 priv->dirty_off_frame[0].x = -1;
878 priv->dirty_off_frame[0].y = -1;
879 priv->dirty_off_frame[1].x = -1;
880 priv->dirty_off_frame[1].y = -1;
882 /* Make sure the entire screen is updated */
883 vo_osd_changed(1);
885 if(priv->rgbsurface)
886 SDL_FreeSurface(priv->rgbsurface);
887 else if(priv->overlay)
888 SDL_FreeYUVOverlay(priv->overlay);
890 priv->rgbsurface = NULL;
891 priv->overlay = NULL;
893 if(priv->mode != YUV && (priv->format&0xFF) == priv->bpp) {
894 if(strcmp(priv->driver, "x11") == 0) {
895 priv->dblit = 1;
896 priv->framePlaneRGB = priv->width * priv->height * priv->surface->format->BytesPerPixel;
897 priv->stridePlaneRGB = priv->width * priv->surface->format->BytesPerPixel;
898 erase_rectangle(0, 0, priv->surface->w, priv->surface->h);
899 return 0;
903 switch(priv->format) {
904 /* Initialize and create the RGB Surface used for video out in BGR/RGB mode */
905 //SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
906 // SDL_SWSURFACE,SDL_HWSURFACE,SDL_SRCCOLORKEY, priv->flags? guess: exchange Rmask and Bmask for BGR<->RGB
907 // 32 bit: a:ff000000 r:ff000 g:ff00 b:ff
908 // 24 bit: r:ff0000 g:ff00 b:ff
909 // 16 bit: r:1111100000000000b g:0000011111100000b b:0000000000011111b
910 // 15 bit: r:111110000000000b g:000001111100000b b:000000000011111b
911 // FIXME: colorkey detect based on bpp, FIXME static bpp value, FIXME alpha value correct?
912 case IMGFMT_RGB15:
913 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 15, 31, 992, 31744, 0);
914 break;
915 case IMGFMT_BGR15:
916 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 15, 31744, 992, 31, 0);
917 break;
918 case IMGFMT_RGB16:
919 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 16, 31, 2016, 63488, 0);
920 break;
921 case IMGFMT_BGR16:
922 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 16, 63488, 2016, 31, 0);
923 break;
924 case IMGFMT_RGB24:
925 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 24, 0x0000FF, 0x00FF00, 0xFF0000, 0);
926 break;
927 case IMGFMT_BGR24:
928 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 24, 0xFF0000, 0x00FF00, 0x0000FF, 0);
929 break;
930 case IMGFMT_RGB32:
931 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0/*0xFF000000*/);
932 break;
933 case IMGFMT_BGR32:
934 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0/*0xFF000000*/);
935 break;
936 default:
937 /* Initialize and create the YUV Overlay used for video out */
938 if (!(priv->overlay = SDL_CreateYUVOverlay (surfwidth, surfheight, priv->format, priv->surface))) {
939 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Couldn't create a YUV overlay: %s.\n", SDL_GetError());
940 return -1;
942 priv->framePlaneY = priv->width * priv->height;
943 priv->framePlaneUV = (priv->width * priv->height) >> 2;
944 priv->framePlaneYUY = priv->width * priv->height * 2;
945 priv->stridePlaneY = priv->width;
946 priv->stridePlaneUV = priv->width/2;
947 priv->stridePlaneYUY = priv->width * 2;
950 if(priv->mode != YUV) {
951 if(!priv->rgbsurface) {
952 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Couldn't create an RGB surface: %s.\n", SDL_GetError());
953 return -1;
956 priv->dblit = 0;
958 if((priv->format&0xFF) != priv->bpp)
959 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);
961 priv->framePlaneRGB = priv->width * priv->height * priv->rgbsurface->format->BytesPerPixel;
962 priv->stridePlaneRGB = priv->width * priv->rgbsurface->format->BytesPerPixel;
965 erase_rectangle(0, 0, surfwidth, surfheight);
967 return 0;
972 * Draw a frame to the SDL YUV overlay.
974 * params : *src[] == the Y, U, and V planes that make up the frame.
975 * returns : non-zero on success, zero on error.
978 //static int sdl_draw_frame (frame_t *frame)
979 static int draw_frame(uint8_t *src[])
981 struct sdl_priv_s *priv = &sdl_priv;
982 uint8_t *dst;
983 int i;
984 uint8_t *mysrc = src[0];
986 switch(priv->format){
987 case IMGFMT_YUY2:
988 case IMGFMT_UYVY:
989 case IMGFMT_YVYU:
990 SDL_OVR_LOCK(-1)
991 dst = (uint8_t *) *(priv->overlay->pixels) + priv->overlay->pitches[0]*priv->y;
992 if(priv->flip) {
993 mysrc+=priv->framePlaneYUY;
994 for(i = 0; i < priv->height; i++) {
995 mysrc-=priv->stridePlaneYUY;
996 fast_memcpy (dst, mysrc, priv->stridePlaneYUY);
997 dst+=priv->overlay->pitches[0];
1000 else fast_memcpy (dst, src[0], priv->framePlaneYUY);
1001 SDL_OVR_UNLOCK
1002 break;
1004 case IMGFMT_RGB15:
1005 case IMGFMT_BGR15:
1006 case IMGFMT_RGB16:
1007 case IMGFMT_BGR16:
1008 case IMGFMT_RGB24:
1009 case IMGFMT_BGR24:
1010 case IMGFMT_RGB32:
1011 case IMGFMT_BGR32:
1012 if(priv->dblit) {
1013 SDL_SRF_LOCK(priv->surface, -1)
1014 dst = (uint8_t *) priv->surface->pixels + priv->y*priv->surface->pitch;
1015 if(priv->flip) {
1016 mysrc+=priv->framePlaneRGB;
1017 for(i = 0; i < priv->height; i++) {
1018 mysrc-=priv->stridePlaneRGB;
1019 fast_memcpy (dst, mysrc, priv->stridePlaneRGB);
1020 dst += priv->surface->pitch;
1023 else fast_memcpy (dst, src[0], priv->framePlaneRGB);
1024 SDL_SRF_UNLOCK(priv->surface)
1025 } else {
1026 SDL_SRF_LOCK(priv->rgbsurface, -1)
1027 dst = (uint8_t *) priv->rgbsurface->pixels + priv->y*priv->rgbsurface->pitch;
1028 if(priv->flip) {
1029 mysrc+=priv->framePlaneRGB;
1030 for(i = 0; i < priv->height; i++) {
1031 mysrc-=priv->stridePlaneRGB;
1032 fast_memcpy (dst, mysrc, priv->stridePlaneRGB);
1033 dst += priv->rgbsurface->pitch;
1036 else fast_memcpy (dst, src[0], priv->framePlaneRGB);
1037 SDL_SRF_UNLOCK(priv->rgbsurface)
1039 break;
1043 return 0;
1048 * Draw a slice (16 rows of image) to the SDL YUV overlay.
1050 * params : *src[] == the Y, U, and V planes that make up the slice.
1051 * returns : non-zero on error, zero on success.
1054 //static uint32_t draw_slice(uint8_t *src[], uint32_t slice_num)
1055 static int draw_slice(uint8_t *image[], int stride[], int w,int h,int x,int y)
1057 struct sdl_priv_s *priv = &sdl_priv;
1058 uint8_t *dst;
1060 SDL_OVR_LOCK(-1)
1062 y += priv->y;
1064 dst = priv->overlay->pixels[0] + priv->overlay->pitches[0]*y + x;
1065 memcpy_pic(dst, image[0], w, h, priv->overlay->pitches[0], stride[0]);
1066 x/=2;y/=2;w/=2;h/=2;
1068 switch(priv->format) {
1069 case IMGFMT_YV12:
1070 dst = priv->overlay->pixels[2] + priv->overlay->pitches[2]*y + x;
1071 memcpy_pic(dst, image[1], w, h, priv->overlay->pitches[2], stride[1]);
1073 dst = priv->overlay->pixels[1] + priv->overlay->pitches[1]*y + x;
1074 memcpy_pic(dst, image[2], w, h, priv->overlay->pitches[1], stride[2]);
1076 break;
1077 case IMGFMT_I420:
1078 case IMGFMT_IYUV:
1079 dst = priv->overlay->pixels[1] + priv->overlay->pitches[1]*y + x;
1080 memcpy_pic(dst, image[1], w, h, priv->overlay->pitches[1], stride[1]);
1082 dst = priv->overlay->pixels[2] + priv->overlay->pitches[2]*y + x;
1083 memcpy_pic(dst, image[2], w, h, priv->overlay->pitches[2], stride[2]);
1085 break;
1086 default:
1087 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Unsupported image format in draw_slice, contact MPlayer developers!\n");
1090 SDL_OVR_UNLOCK
1092 return 0;
1098 * Checks for SDL keypress and window resize events
1100 * params : none
1101 * returns : doesn't return
1104 #include "osdep/keycodes.h"
1106 #define shift_key (event.key.keysym.mod==(KMOD_LSHIFT||KMOD_RSHIFT))
1107 static void check_events (void)
1109 struct sdl_priv_s *priv = &sdl_priv;
1110 SDL_Event event;
1111 SDLKey keypressed = SDLK_UNKNOWN;
1113 /* Poll the waiting SDL Events */
1114 while ( SDL_PollEvent(&event) ) {
1115 switch (event.type) {
1117 /* capture window resize events */
1118 case SDL_VIDEORESIZE:
1119 if(!priv->dblit)
1120 set_video_mode(event.resize.w, event.resize.h, priv->bpp, priv->sdlflags);
1122 /* save video extents, to restore them after going fullscreen */
1123 //if(!(priv->surface->flags & SDL_FULLSCREEN)) {
1124 priv->windowsize.w = priv->surface->w;
1125 priv->windowsize.h = priv->surface->h;
1127 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Window resize\n");
1128 break;
1130 case SDL_MOUSEBUTTONDOWN:
1131 if(vo_nomouse_input)
1132 break;
1133 mplayer_put_key((MOUSE_BTN0+event.button.button-1) | MP_KEY_DOWN);
1134 break;
1136 case SDL_MOUSEBUTTONUP:
1137 if(vo_nomouse_input)
1138 break;
1139 mplayer_put_key(MOUSE_BTN0+event.button.button-1);
1140 break;
1142 /* graphics mode selection shortcuts */
1143 #ifdef BUGGY_SDL
1144 case SDL_KEYDOWN:
1145 switch(event.key.keysym.sym) {
1146 case SDLK_UP: mplayer_put_key(KEY_UP); break;
1147 case SDLK_DOWN: mplayer_put_key(KEY_DOWN); break;
1148 case SDLK_LEFT: mplayer_put_key(KEY_LEFT); break;
1149 case SDLK_RIGHT: mplayer_put_key(KEY_RIGHT); break;
1150 case SDLK_LESS: mplayer_put_key(shift_key?'>':'<'); break;
1151 case SDLK_GREATER: mplayer_put_key('>'); break;
1152 case SDLK_ASTERISK:
1153 case SDLK_KP_MULTIPLY:
1154 case SDLK_SLASH:
1155 case SDLK_KP_DIVIDE:
1156 default: break;
1158 break;
1159 case SDL_KEYUP:
1160 #else
1161 case SDL_KEYDOWN:
1162 #endif
1163 keypressed = event.key.keysym.sym;
1164 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Key pressed: '%i'\n", keypressed);
1166 /* c key pressed. c cycles through available fullscreenmodes, if we have some */
1167 if ( ((keypressed == SDLK_c)) && (priv->fullmodes) ) {
1168 /* select next fullscreen mode */
1169 priv->fullmode++;
1170 if (priv->fullmode > (findArrayEnd(priv->fullmodes) - 1)) priv->fullmode = 0;
1171 set_fullmode(priv->fullmode);
1173 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Set next available fullscreen mode.\n");
1176 else if ( keypressed == SDLK_n ) {
1177 #ifdef CONFIG_X11
1178 aspect(&priv->dstwidth, &priv->dstheight,A_NOZOOM);
1179 #endif
1180 if (priv->surface->w != priv->dstwidth || priv->surface->h != priv->dstheight) {
1181 set_video_mode(priv->dstwidth, priv->dstheight, priv->bpp, priv->sdlflags);
1182 priv->windowsize.w = priv->surface->w;
1183 priv->windowsize.h = priv->surface->h;
1184 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Normal size\n");
1185 } else
1186 if (priv->surface->w != priv->dstwidth * 2 || priv->surface->h != priv->dstheight * 2) {
1187 set_video_mode(priv->dstwidth * 2, priv->dstheight * 2, priv->bpp, priv->sdlflags);
1188 priv->windowsize.w = priv->surface->w;
1189 priv->windowsize.h = priv->surface->h;
1190 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Double size\n");
1194 else switch(keypressed){
1195 case SDLK_RETURN: mplayer_put_key(KEY_ENTER);break;
1196 case SDLK_ESCAPE: mplayer_put_key(KEY_ESC);break;
1197 case SDLK_q: mplayer_put_key('q');break;
1198 case SDLK_F1: mplayer_put_key(KEY_F+1);break;
1199 case SDLK_F2: mplayer_put_key(KEY_F+2);break;
1200 case SDLK_F3: mplayer_put_key(KEY_F+3);break;
1201 case SDLK_F4: mplayer_put_key(KEY_F+4);break;
1202 case SDLK_F5: mplayer_put_key(KEY_F+5);break;
1203 case SDLK_F6: mplayer_put_key(KEY_F+6);break;
1204 case SDLK_F7: mplayer_put_key(KEY_F+7);break;
1205 case SDLK_F8: mplayer_put_key(KEY_F+8);break;
1206 case SDLK_F9: mplayer_put_key(KEY_F+9);break;
1207 case SDLK_F10: mplayer_put_key(KEY_F+10);break;
1208 case SDLK_F11: mplayer_put_key(KEY_F+11);break;
1209 case SDLK_F12: mplayer_put_key(KEY_F+12);break;
1210 /*case SDLK_o: mplayer_put_key('o');break;
1211 case SDLK_SPACE: mplayer_put_key(' ');break;
1212 case SDLK_p: mplayer_put_key('p');break;*/
1213 case SDLK_7: mplayer_put_key(shift_key?'/':'7');break;
1214 case SDLK_PLUS: mplayer_put_key(shift_key?'*':'+');break;
1215 case SDLK_KP_PLUS: mplayer_put_key('+');break;
1216 case SDLK_MINUS:
1217 case SDLK_KP_MINUS: mplayer_put_key('-');break;
1218 case SDLK_TAB: mplayer_put_key('\t');break;
1219 case SDLK_PAGEUP: mplayer_put_key(KEY_PAGE_UP);break;
1220 case SDLK_PAGEDOWN: mplayer_put_key(KEY_PAGE_DOWN);break;
1221 #ifdef BUGGY_SDL
1222 case SDLK_UP:
1223 case SDLK_DOWN:
1224 case SDLK_LEFT:
1225 case SDLK_RIGHT:
1226 case SDLK_ASTERISK:
1227 case SDLK_KP_MULTIPLY:
1228 case SDLK_SLASH:
1229 case SDLK_KP_DIVIDE:
1230 break;
1231 #else
1232 case SDLK_UP: mplayer_put_key(KEY_UP);break;
1233 case SDLK_DOWN: mplayer_put_key(KEY_DOWN);break;
1234 case SDLK_LEFT: mplayer_put_key(KEY_LEFT);break;
1235 case SDLK_RIGHT: mplayer_put_key(KEY_RIGHT);break;
1236 case SDLK_LESS: mplayer_put_key(shift_key?'>':'<'); break;
1237 case SDLK_GREATER: mplayer_put_key('>'); break;
1238 case SDLK_ASTERISK:
1239 case SDLK_KP_MULTIPLY: mplayer_put_key('*'); break;
1240 case SDLK_SLASH:
1241 case SDLK_KP_DIVIDE: mplayer_put_key('/'); break;
1242 #endif
1243 case SDLK_KP0: mplayer_put_key(KEY_KP0); break;
1244 case SDLK_KP1: mplayer_put_key(KEY_KP1); break;
1245 case SDLK_KP2: mplayer_put_key(KEY_KP2); break;
1246 case SDLK_KP3: mplayer_put_key(KEY_KP3); break;
1247 case SDLK_KP4: mplayer_put_key(KEY_KP4); break;
1248 case SDLK_KP5: mplayer_put_key(KEY_KP5); break;
1249 case SDLK_KP6: mplayer_put_key(KEY_KP6); break;
1250 case SDLK_KP7: mplayer_put_key(KEY_KP7); break;
1251 case SDLK_KP8: mplayer_put_key(KEY_KP8); break;
1252 case SDLK_KP9: mplayer_put_key(KEY_KP9); break;
1253 case SDLK_KP_PERIOD: mplayer_put_key(KEY_KPDEC); break;
1254 case SDLK_KP_ENTER: mplayer_put_key(KEY_KPENTER); break;
1255 default:
1256 //printf("got scancode: %d keysym: %d mod: %d %d\n", event.key.keysym.scancode, keypressed, event.key.keysym.mod);
1257 mplayer_put_key(keypressed);
1260 break;
1261 case SDL_QUIT: mplayer_put_key(KEY_CLOSE_WIN);break;
1265 #undef shift_key
1267 /* Erase (paint it black) the rectangle specified by x, y, w and h in the surface
1268 or overlay which is used for OSD
1270 static void erase_rectangle(int x, int y, int w, int h)
1272 struct sdl_priv_s *priv = &sdl_priv;
1274 switch(priv->format) {
1275 case IMGFMT_YV12:
1276 case IMGFMT_I420:
1277 case IMGFMT_IYUV:
1279 SDL_OVR_LOCK((void) 0)
1281 /* Erase Y plane */
1282 erase_area_1(x, w, h,
1283 priv->overlay->pitches[0], 0,
1284 priv->overlay->pixels[0] +
1285 priv->overlay->pitches[0]*y);
1287 /* Erase U and V planes */
1288 w /= 2;
1289 x /= 2;
1290 h /= 2;
1291 y /= 2;
1293 erase_area_1(x, w, h,
1294 priv->overlay->pitches[1], 128,
1295 priv->overlay->pixels[1] +
1296 priv->overlay->pitches[1]*y);
1298 erase_area_1(x, w, h,
1299 priv->overlay->pitches[2], 128,
1300 priv->overlay->pixels[2] +
1301 priv->overlay->pitches[2]*y);
1302 SDL_OVR_UNLOCK
1303 break;
1306 case IMGFMT_YUY2:
1307 case IMGFMT_YVYU:
1309 /* yuy2 and yvyu represent black the same way */
1310 uint8_t yuy2_black[] = {0, 128, 0, 128};
1312 SDL_OVR_LOCK((void) 0)
1313 erase_area_4(x*2, w*2, h,
1314 priv->overlay->pitches[0],
1315 *((uint32_t*) yuy2_black),
1316 priv->overlay->pixels[0] +
1317 priv->overlay->pitches[0]*y);
1318 SDL_OVR_UNLOCK
1319 break;
1322 case IMGFMT_UYVY:
1324 uint8_t uyvy_black[] = {128, 0, 128, 0};
1326 SDL_OVR_LOCK((void) 0)
1327 erase_area_4(x*2, w*2, h,
1328 priv->overlay->pitches[0],
1329 *((uint32_t*) uyvy_black),
1330 priv->overlay->pixels[0] +
1331 priv->overlay->pitches[0]*y);
1332 SDL_OVR_UNLOCK
1333 break;
1336 case IMGFMT_RGB15:
1337 case IMGFMT_BGR15:
1338 case IMGFMT_RGB16:
1339 case IMGFMT_BGR16:
1340 case IMGFMT_RGB24:
1341 case IMGFMT_BGR24:
1342 case IMGFMT_RGB32:
1343 case IMGFMT_BGR32:
1345 SDL_Rect rect;
1346 rect.w = w; rect.h = h;
1347 rect.x = x; rect.y = y;
1349 if(priv->dblit) {
1350 SDL_SRF_LOCK(priv->surface, (void) 0)
1351 SDL_FillRect(priv->surface, &rect, 0);
1352 SDL_SRF_UNLOCK(priv->surface)
1354 else {
1355 SDL_SRF_LOCK(priv->rgbsurface, (void) 0)
1356 SDL_FillRect(priv->rgbsurface, &rect, 0);
1357 SDL_SRF_UNLOCK(priv->rgbsurface)
1359 break;
1364 static void draw_osd(void)
1365 { struct sdl_priv_s *priv = &sdl_priv;
1367 priv->osd_has_changed = vo_osd_changed(0);
1369 if(priv->osd_has_changed)
1371 int i;
1373 for(i = 0; i < 2; i++) {
1374 if(priv->dirty_off_frame[i].x < 0 || priv->dirty_off_frame[i].y < 0)
1375 continue;
1377 erase_rectangle(priv->dirty_off_frame[i].x, priv->dirty_off_frame[i].y,
1378 priv->dirty_off_frame[i].w, priv->dirty_off_frame[i].h);
1380 priv->dirty_off_frame[i].x = -1;
1381 priv->dirty_off_frame[i].y = -1;
1385 /* update osd/subtitles */
1386 if(priv->mode == YUV)
1387 vo_draw_text(priv->overlay->w, priv->overlay->h, draw_alpha);
1388 else {
1389 if(priv->dblit)
1390 vo_draw_text(priv->surface->w, priv->surface->h, draw_alpha);
1391 else
1392 vo_draw_text(priv->rgbsurface->w, priv->rgbsurface->h, draw_alpha);
1396 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1397 * are given in bytes. 4 bytes at a time.
1399 static void erase_area_4(int x_start, int width, int height, int pitch, uint32_t color, uint8_t* pixels)
1401 int x_end = x_start/4 + width/4;
1402 int x, y;
1403 uint32_t* data = (uint32_t*) pixels;
1405 x_start /= 4;
1406 pitch /= 4;
1408 for(y = 0; y < height; y++) {
1409 for(x = x_start; x < x_end; x++)
1410 data[y*pitch + x] = color;
1414 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1415 * are given in bytes. 1 byte at a time.
1417 static void erase_area_1(int x_start, int width, int height, int pitch, uint8_t color, uint8_t* pixels)
1419 int y;
1421 for(y = 0; y < height; y++) {
1422 memset(&pixels[y*pitch + x_start], color, width);
1427 * Display the surface we have written our data to
1429 * params : mode == index of the desired fullscreen mode
1430 * returns : doesn't return
1433 static void flip_page (void)
1435 struct sdl_priv_s *priv = &sdl_priv;
1437 switch(priv->format) {
1438 case IMGFMT_RGB15:
1439 case IMGFMT_BGR15:
1440 case IMGFMT_RGB16:
1441 case IMGFMT_BGR16:
1442 case IMGFMT_RGB24:
1443 case IMGFMT_BGR24:
1444 case IMGFMT_RGB32:
1445 case IMGFMT_BGR32:
1446 if(!priv->dblit) {
1447 /* blit to the RGB surface */
1448 if(SDL_BlitSurface (priv->rgbsurface, NULL, priv->surface, NULL))
1449 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Blit failed: %s.\n", SDL_GetError());
1452 /* update screen */
1453 //SDL_UpdateRect(priv->surface, 0, 0, priv->surface->clip_rect.w, priv->surface->clip_rect.h);
1454 if(priv->osd_has_changed) {
1455 priv->osd_has_changed = 0;
1456 SDL_UpdateRects(priv->surface, 1, &priv->surface->clip_rect);
1458 else
1459 SDL_UpdateRect(priv->surface, 0, priv->y_screen_top,
1460 priv->surface->clip_rect.w, priv->y_screen_bottom);
1462 /* check if we have a double buffered surface and flip() if we do. */
1463 if ( priv->surface->flags & SDL_DOUBLEBUF )
1464 SDL_Flip(priv->surface);
1466 break;
1467 default:
1468 /* blit to the YUV overlay */
1469 SDL_DisplayYUVOverlay (priv->overlay, &priv->surface->clip_rect);
1471 /* check if we have a double buffered surface and flip() if we do. */
1472 if ( priv->surface->flags & SDL_DOUBLEBUF )
1473 SDL_Flip(priv->surface);
1475 //SDL_LockYUVOverlay (priv->overlay); // removed because unused!?
1479 static int
1480 query_format(uint32_t format)
1482 switch(format){
1483 case IMGFMT_YV12:
1484 // it seems buggy (not hw accelerated), so just use YV12 instead!
1485 // case IMGFMT_I420:
1486 // case IMGFMT_IYUV:
1487 case IMGFMT_YUY2:
1488 case IMGFMT_UYVY:
1489 case IMGFMT_YVYU:
1490 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD |
1491 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
1492 case IMGFMT_RGB15:
1493 case IMGFMT_BGR15:
1494 case IMGFMT_RGB16:
1495 case IMGFMT_BGR16:
1496 case IMGFMT_RGB24:
1497 case IMGFMT_BGR24:
1498 case IMGFMT_RGB32:
1499 case IMGFMT_BGR32:
1500 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_FLIP;
1502 return 0;
1506 static void
1507 uninit(void)
1509 #ifdef CONFIG_X11
1510 struct sdl_priv_s *priv = &sdl_priv;
1511 if(priv->X) {
1512 mp_msg(MSGT_VO,MSGL_V, "SDL: activating XScreensaver/DPMS\n");
1513 vo_x11_uninit();
1515 #endif
1516 sdl_close();
1518 /* Cleanup SDL */
1519 if(SDL_WasInit(SDL_INIT_VIDEO))
1520 SDL_QuitSubSystem(SDL_INIT_VIDEO);
1522 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Closed Plugin\n");
1526 static int preinit(const char *arg)
1528 struct sdl_priv_s *priv = &sdl_priv;
1529 char * sdl_driver = NULL;
1530 int sdl_hwaccel;
1531 int sdl_forcexv;
1532 const opt_t subopts[] = {
1533 {"forcexv", OPT_ARG_BOOL, &sdl_forcexv, NULL},
1534 {"hwaccel", OPT_ARG_BOOL, &sdl_hwaccel, NULL},
1535 {"driver", OPT_ARG_MSTRZ, &sdl_driver, NULL},
1536 {NULL, 0, NULL, NULL}
1539 sdl_forcexv = 1;
1540 sdl_hwaccel = 1;
1542 if (subopt_parse(arg, subopts) != 0) return -1;
1544 priv->rgbsurface = NULL;
1545 priv->overlay = NULL;
1546 priv->surface = NULL;
1548 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Opening Plugin\n");
1550 if(sdl_driver) {
1551 setenv("SDL_VIDEODRIVER", sdl_driver, 1);
1552 free(sdl_driver);
1555 /* does the user want SDL to try and force Xv */
1556 if(sdl_forcexv) setenv("SDL_VIDEO_X11_NODIRECTCOLOR", "1", 1);
1557 else setenv("SDL_VIDEO_X11_NODIRECTCOLOR", "0", 1);
1559 /* does the user want to disable Xv and use software scaling instead */
1560 if(sdl_hwaccel) setenv("SDL_VIDEO_YUV_HWACCEL", "1", 1);
1561 else setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
1563 /* default to no fullscreen mode, we'll set this as soon we have the avail. modes */
1564 priv->fullmode = -2;
1566 priv->fullmodes = NULL;
1567 priv->bpp = 0;
1569 /* initialize the SDL Video system */
1570 if (!SDL_WasInit(SDL_INIT_VIDEO)) {
1571 if (SDL_Init (SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE)) {
1572 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_SDL] SDL initialization failed: %s.\n", SDL_GetError());
1574 return -1;
1578 SDL_VideoDriverName(priv->driver, 8);
1579 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Using driver: %s.\n", priv->driver);
1581 priv->X = 0;
1582 #ifdef CONFIG_X11
1583 if(vo_init()) {
1584 mp_msg(MSGT_VO,MSGL_V, "SDL: deactivating XScreensaver/DPMS\n");
1585 priv->XWidth = vo_screenwidth;
1586 priv->XHeight = vo_screenheight;
1587 priv->X = 1;
1588 mp_msg(MSGT_VO,MSGL_V, "SDL: X11 Resolution %ix%i\n", priv->XWidth, priv->XHeight);
1590 #endif
1592 return 0;
1595 static uint32_t get_image(mp_image_t *mpi)
1597 struct sdl_priv_s *priv = &sdl_priv;
1599 if(priv->format != mpi->imgfmt) return VO_FALSE;
1600 if(mpi->type == MP_IMGTYPE_STATIC || mpi->type == MP_IMGTYPE_TEMP) {
1601 if(mpi->flags&MP_IMGFLAG_PLANAR) {
1602 mpi->planes[0] = priv->overlay->pixels[0] + priv->y*priv->overlay->pitches[0];
1603 mpi->stride[0] = priv->overlay->pitches[0];
1604 if(mpi->flags&MP_IMGFLAG_SWAPPED) {
1605 mpi->planes[1] = priv->overlay->pixels[1] + priv->y*priv->overlay->pitches[1]/2;
1606 mpi->stride[1] = priv->overlay->pitches[1];
1607 mpi->planes[2] = priv->overlay->pixels[2] + priv->y*priv->overlay->pitches[2]/2;
1608 mpi->stride[2] = priv->overlay->pitches[2];
1609 } else {
1610 mpi->planes[2] = priv->overlay->pixels[1] + priv->y*priv->overlay->pitches[1]/2;
1611 mpi->stride[2] = priv->overlay->pitches[1];
1612 mpi->planes[1] = priv->overlay->pixels[2] + priv->y*priv->overlay->pitches[2]/2;
1613 mpi->stride[1] = priv->overlay->pitches[2];
1616 else if(IMGFMT_IS_RGB(priv->format) || IMGFMT_IS_BGR(priv->format)) {
1617 if(priv->dblit) {
1618 if(mpi->type == MP_IMGTYPE_STATIC && (priv->surface->flags & SDL_DOUBLEBUF))
1619 return VO_FALSE;
1621 mpi->planes[0] = (uint8_t *)priv->surface->pixels + priv->y*priv->surface->pitch;
1622 mpi->stride[0] = priv->surface->pitch;
1624 else {
1625 mpi->planes[0] = (uint8_t *)priv->rgbsurface->pixels + priv->y*priv->rgbsurface->pitch;
1626 mpi->stride[0] = priv->rgbsurface->pitch;
1629 else {
1630 mpi->planes[0] = priv->overlay->pixels[0] + priv->y*priv->overlay->pitches[0];
1631 mpi->stride[0] = priv->overlay->pitches[0];
1634 mpi->flags|=MP_IMGFLAG_DIRECT;
1635 return VO_TRUE;
1638 return VO_FALSE;
1641 static int control(uint32_t request, void *data)
1643 struct sdl_priv_s *priv = &sdl_priv;
1644 switch (request) {
1645 case VOCTRL_GET_IMAGE:
1646 return get_image(data);
1647 case VOCTRL_QUERY_FORMAT:
1648 return query_format(*((uint32_t*)data));
1649 case VOCTRL_FULLSCREEN:
1650 if (priv->surface->flags & SDL_FULLSCREEN) {
1651 set_video_mode(priv->windowsize.w, priv->windowsize.h, priv->bpp, priv->sdlflags);
1652 SDL_ShowCursor(1);
1653 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Windowed mode\n");
1654 } else if (priv->fullmodes) {
1655 set_fullmode(priv->fullmode);
1656 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Set fullscreen mode\n");
1658 return VO_TRUE;
1661 return VO_NOTIMPL;