cocoa_common: fix problems with alt-tab window changes
[mplayer.git] / libvo / vo_sdl.c
blob71cf75323a2da594ec4b349195e6466928a64770
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 /* MONITOR_ASPECT MUST BE FLOAT */
52 #define MONITOR_ASPECT 4.0/3.0
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <inttypes.h>
59 #include "config.h"
60 #include "mp_msg.h"
61 #include "mp_msg.h"
62 #include "video_out.h"
63 #include "video_out_internal.h"
65 #include "fastmemcpy.h"
66 #include "sub/sub.h"
67 #include "aspect.h"
68 #include "libmpcodecs/vfcap.h"
69 #include "mpbswap.h"
71 #ifdef CONFIG_X11
72 #include <X11/Xlib.h>
73 #include "x11_common.h"
74 #endif
76 #include "subopt-helper.h"
78 static const vo_info_t info =
80 "SDL YUV/RGB/BGR renderer (SDL v1.1.7+ only!)",
81 "sdl",
82 "Ryan C. Gordon <icculus@lokigames.com>, Felix Buenemann <atmosfear@users.sourceforge.net>",
86 const LIBVO_EXTERN(sdl)
88 #include "sdl_common.h"
89 //#include <SDL/SDL_syswm.h>
92 #ifdef SDL_ENABLE_LOCKS
93 #define SDL_OVR_LOCK(x) if (SDL_LockYUVOverlay (priv->overlay)) { \
94 mp_msg(MSGT_VO,MSGL_V, "SDL: Couldn't lock YUV overlay\n"); \
95 return x; \
97 #define SDL_OVR_UNLOCK SDL_UnlockYUVOverlay (priv->overlay);
99 #define SDL_SRF_LOCK(srf, x) if(SDL_MUSTLOCK(srf)) { \
100 if(SDL_LockSurface (srf)) { \
101 mp_msg(MSGT_VO,MSGL_V, "SDL: Couldn't lock RGB surface\n"); \
102 return x; \
106 #define SDL_SRF_UNLOCK(srf) if(SDL_MUSTLOCK(srf)) \
107 SDL_UnlockSurface (srf);
108 #else
109 #define SDL_OVR_LOCK(x)
110 #define SDL_OVR_UNLOCK
111 #define SDL_SRF_LOCK(srf, x)
112 #define SDL_SRF_UNLOCK(srf)
113 #endif
115 /** Private SDL Data structure **/
117 static struct sdl_priv_s {
119 /* output driver used by sdl */
120 char driver[8];
122 /* SDL display surface */
123 SDL_Surface *surface;
125 /* SDL RGB surface */
126 SDL_Surface *rgbsurface;
128 /* SDL YUV overlay */
129 SDL_Overlay *overlay;
131 /* available fullscreen modes */
132 SDL_Rect **fullmodes;
134 /* surface attributes for fullscreen and windowed mode */
135 Uint32 sdlflags, sdlfullflags;
137 /* save the windowed output extents */
138 SDL_Rect windowsize;
140 /* Bits per Pixel */
141 Uint8 bpp;
143 /* RGB or YUV? */
144 Uint8 mode;
145 #define YUV 0
146 #define RGB 1
147 #define BGR 2
149 /* use direct blitting to surface */
150 int dblit;
152 /* current fullscreen mode, 0 = highest available fullscreen mode */
153 int fullmode;
155 /* YUV ints */
156 int framePlaneY, framePlaneUV, framePlaneYUY;
157 int stridePlaneY, stridePlaneUV, stridePlaneYUY;
159 /* RGB ints */
160 int framePlaneRGB;
161 int stridePlaneRGB;
163 /* Flip image */
164 int flip;
166 /* fullscreen behaviour; see init */
167 int fulltype;
169 /* is X running (0/1) */
170 int X;
172 /* X11 Resolution */
173 int XWidth, XHeight;
175 /* original image dimensions */
176 int width, height;
178 /* destination dimensions */
179 int dstwidth, dstheight;
181 /* Draw image at coordinate y on the SDL surfaces */
182 int y;
184 /* The image is displayed between those y coordinates in priv->surface */
185 int y_screen_top, y_screen_bottom;
187 /* 1 if the OSD has changed otherwise 0 */
188 int osd_has_changed;
190 /* source image format (YUV/RGB/...) */
191 uint32_t format;
193 /* dirty_off_frame[0] contains a bounding box around the osd contents drawn above the image
194 dirty_off_frame[1] is the corresponding thing for OSD contents drawn below the image
196 SDL_Rect dirty_off_frame[2];
197 } sdl_priv;
199 static void erase_area_4(int x_start, int width, int height, int pitch, uint32_t color, uint8_t* pixels);
200 static void erase_area_1(int x_start, int width, int height, int pitch, uint8_t color, uint8_t* pixels);
201 static int setup_surfaces(void);
202 static void set_video_mode(int width, int height, int bpp, uint32_t sdlflags);
203 static void erase_rectangle(int x, int y, int w, int h);
205 /* Expand 'rect' to contain the rectangle specified by x, y, w and h */
206 static void expand_rect(SDL_Rect* rect, int x, int y, int w, int h)
208 if(rect->x < 0 || rect->y < 0) {
209 rect->x = x;
210 rect->y = y;
211 rect->w = w;
212 rect->h = h;
213 return;
216 if(rect->x > x)
217 rect->x = x;
219 if(rect->y > y)
220 rect->y = y;
222 if(rect->x + rect->w < x + w)
223 rect->w = x + w - rect->x;
225 if(rect->y + rect->h < y + h)
226 rect->h = y + h - rect->y;
229 /** libvo Plugin functions **/
232 * draw_alpha is used for osd and subtitle display.
236 static void draw_alpha(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
237 struct sdl_priv_s *priv = &sdl_priv;
239 if(priv->osd_has_changed) {
240 /* OSD did change. Store a bounding box of everything drawn into the OSD */
241 if(priv->y >= y0) {
242 /* Make sure we don't mark part of the frame area dirty */
243 if(h + y0 > priv->y)
244 expand_rect(&priv->dirty_off_frame[0], x0, y0, w, priv->y - y0);
245 else
246 expand_rect(&priv->dirty_off_frame[0], x0, y0, w, h);
248 else if(priv->y + priv->height <= y0 + h) {
249 /* Make sure we don't mark part of the frame area dirty */
250 if(y0 < priv->y + priv->height)
251 expand_rect(&priv->dirty_off_frame[1], x0,
252 priv->y + priv->height,
253 w, h - ((priv->y + priv->height) - y0));
254 else
255 expand_rect(&priv->dirty_off_frame[1], x0, y0, w, h);
258 else { /* OSD contents didn't change only draw parts that was erased by the frame */
259 if(priv->y >= y0) {
260 src = src + (priv->y - y0) * stride;
261 srca = srca + (priv->y - y0) * stride;
262 h -= priv->y - y0;
263 y0 = priv->y;
266 if(priv->y + priv->height <= y0 + h)
267 h = priv->y + priv->height - y0;
269 if(h <= 0)
270 return;
273 switch(priv->format) {
274 case IMGFMT_YV12:
275 case IMGFMT_I420:
276 case IMGFMT_IYUV:
277 vo_draw_alpha_yv12(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
278 break;
279 case IMGFMT_YUY2:
280 case IMGFMT_YVYU:
281 x0 *= 2;
282 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
283 break;
284 case IMGFMT_UYVY:
285 x0 *= 2;
286 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
287 break;
289 default:
290 if(priv->dblit) {
291 x0 *= priv->surface->format->BytesPerPixel;
292 switch(priv->format) {
293 case IMGFMT_RGB15:
294 case IMGFMT_BGR15:
295 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
296 break;
297 case IMGFMT_RGB16:
298 case IMGFMT_BGR16:
299 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
300 break;
301 case IMGFMT_RGB24:
302 case IMGFMT_BGR24:
303 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
304 break;
305 case IMGFMT_RGB32:
306 case IMGFMT_BGR32:
307 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
308 break;
311 else {
312 x0 *= priv->rgbsurface->format->BytesPerPixel;
313 switch(priv->format) {
314 case IMGFMT_RGB15:
315 case IMGFMT_BGR15:
316 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
317 break;
318 case IMGFMT_RGB16:
319 case IMGFMT_BGR16:
320 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
321 break;
322 case IMGFMT_RGB24:
323 case IMGFMT_BGR24:
324 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
325 break;
326 case IMGFMT_RGB32:
327 case IMGFMT_BGR32:
328 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
329 break;
338 * Take a null-terminated array of pointers, and find the last element.
340 * params : array == array of which we want to find the last element.
341 * returns : index of last NON-NULL element.
344 static inline int findArrayEnd (SDL_Rect **array)
346 int i = 0;
347 while ( array[i++] ); /* keep loopin' ... */
349 /* return the index of the last array element */
350 return i - 1;
355 * Open and prepare SDL output.
357 * params : *plugin ==
358 * *name ==
359 * returns : 0 on success, -1 on failure
362 static int sdl_open (void *plugin, void *name)
364 struct sdl_priv_s *priv = &sdl_priv;
365 const SDL_VideoInfo *vidInfo = NULL;
366 /*static int opened = 0;
368 if (opened)
369 return 0;
370 opened = 1;*/
373 /* other default values */
374 #ifdef SDL_NOHWSURFACE
375 mp_msg(MSGT_VO,MSGL_V, "SDL: using software-surface\n");
376 priv->sdlflags = SDL_SWSURFACE|SDL_RESIZABLE|SDL_ANYFORMAT;
377 priv->sdlfullflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ANYFORMAT;
378 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
379 #else
380 /*if((strcmp(priv->driver, "dga") == 0) && (priv->mode)) {
381 if( mp_msg_test(MSGT_VO,MSGL_V) ) {
382 printf("SDL: using software-surface\n"); }
383 priv->sdlflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
384 priv->sdlfullflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
386 else { */
387 mp_msg(MSGT_VO,MSGL_V, "SDL: using hardware-surface\n");
388 priv->sdlflags = SDL_HWSURFACE|SDL_RESIZABLE/*|SDL_ANYFORMAT*/;
389 priv->sdlfullflags = SDL_HWSURFACE|SDL_FULLSCREEN/*|SDL_ANYFORMAT*/;
390 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
392 #endif
394 #if !defined( __AMIGAOS4__ ) && !defined( __APPLE__ )
395 priv->sdlfullflags |= SDL_DOUBLEBUF;
396 if (vo_doublebuffering)
397 priv->sdlflags |= SDL_DOUBLEBUF;
398 #endif
400 /* get information about the graphics adapter */
401 vidInfo = SDL_GetVideoInfo ();
403 /* collect all fullscreen & hardware modes available */
404 if (!(priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags))) {
406 /* non hardware accelerated fullscreen modes */
407 priv->sdlfullflags &= ~SDL_HWSURFACE;
408 priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags);
411 /* test for normal resizeable & windowed hardware accellerated surfaces */
412 if (!SDL_ListModes (vidInfo->vfmt, priv->sdlflags)) {
414 /* test for NON hardware accelerated resizeable surfaces - poor you.
415 * That's all we have. If this fails there's nothing left.
416 * Theoretically there could be Fullscreenmodes left - we ignore this for now.
418 priv->sdlflags &= ~SDL_HWSURFACE;
419 if ((!SDL_ListModes (vidInfo->vfmt, priv->sdlflags)) && (!priv->fullmodes)) {
420 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_SDL] Couldn't get any acceptable SDL Mode for output.\n");
421 return -1;
426 /* YUV overlays need at least 16-bit color depth, but the
427 * display might less. The SDL AAlib target says it can only do
428 * 8-bits, for example. So, if the display is less than 16-bits,
429 * we'll force the BPP to 16, and pray that SDL can emulate for us.
431 priv->bpp = vidInfo->vfmt->BitsPerPixel;
432 if (priv->mode == YUV && priv->bpp < 16) {
434 mp_msg(MSGT_VO,MSGL_V, "SDL: Your SDL display target wants to be at a color "
435 "depth of (%d), but we need it to be at least 16 "
436 "bits, so we need to emulate 16-bit color. This is "
437 "going to slow things down; you might want to "
438 "increase your display's color depth, if possible.\n",
439 priv->bpp);
441 priv->bpp = 16;
444 /* Success! */
445 return 0;
450 * Close SDL, Cleanups, Free Memory
452 * params : *plugin
453 * returns : non-zero on success, zero on error.
456 static int sdl_close (void)
458 struct sdl_priv_s *priv = &sdl_priv;
460 if (priv->fullmode)
461 SDL_ShowCursor(1);
463 /* Cleanup YUV Overlay structure */
464 if (priv->overlay) {
465 SDL_FreeYUVOverlay(priv->overlay);
466 priv->overlay=NULL;
469 /* Free RGB Surface */
470 if (priv->rgbsurface) {
471 SDL_FreeSurface(priv->rgbsurface);
472 priv->rgbsurface=NULL;
475 /* Free our blitting surface */
476 if (priv->surface) {
477 SDL_FreeSurface(priv->surface);
478 priv->surface=NULL;
481 /* DON'T attempt to free the fullscreen modes array. SDL_Quit* does this for us */
483 return 0;
487 * Do aspect ratio calculations
489 * params : srcw == sourcewidth
490 * srch == sourceheight
491 * dstw == destinationwidth
492 * dsth == destinationheight
494 * returns : SDL_Rect structure with new x and y, w and h
497 #if 0
498 static SDL_Rect aspect(int srcw, int srch, int dstw, int dsth) {
499 SDL_Rect newres;
500 mp_msg(MSGT_VO,MSGL_V, "SDL Aspect-Destinationres: %ix%i (x: %i, y: %i)\n", newres.w, newres.h, newres.x, newres.y);
501 newres.h = ((float)dstw / (float)srcw * (float)srch) * ((float)dsth/((float)dstw/(MONITOR_ASPECT)));
502 if(newres.h > dsth) {
503 newres.w = ((float)dsth / (float)newres.h) * dstw;
504 newres.h = dsth;
505 newres.x = (dstw - newres.w) / 2;
506 newres.y = 0;
508 else {
509 newres.w = dstw;
510 newres.x = 0;
511 newres.y = (dsth - newres.h) / 2;
514 mp_msg(MSGT_VO,MSGL_V, "SDL Mode: %d: %d x %d\n", i, priv->fullmodes[i]->w, priv->fullmodes[i]->h);
516 return newres;
518 #endif
521 * Sets the specified fullscreen mode.
523 * params : mode == index of the desired fullscreen mode
524 * returns : doesn't return
527 #if 0
528 static void set_fullmode (int mode)
530 struct sdl_priv_s *priv = &sdl_priv;
531 SDL_Surface *newsurface = NULL;
532 int haspect, waspect = 0;
534 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
535 if (mode < 0)
536 mode = priv->fullmode = findArrayEnd(priv->fullmodes) - 1;
538 /* Calculate proper aspect ratio for fullscreen
539 * Height smaller than expected: add horizontal black bars (haspect)*/
540 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);
541 /* Height bigger than expected: add vertical black bars (waspect)*/
542 if (haspect < 0) {
543 haspect = 0; /* set haspect to zero because image will be scaled horizontal instead of vertical */
544 waspect = priv->fullmodes[mode]->w - ((float) ((float) priv->fullmodes[mode]->h / (float) priv->height) * (float) priv->width);
546 // printf ("W-Aspect: %i H-Aspect: %i\n", waspect, haspect);
548 /* change to given fullscreen mode and hide the mouse cursor */
549 newsurface = SDL_SetVideoMode(priv->fullmodes[mode]->w - waspect, priv->fullmodes[mode]->h - haspect, priv->bpp, priv->sdlfullflags);
551 /* if we were successful hide the mouse cursor and save the mode */
552 if (newsurface) {
553 if (priv->surface)
554 SDL_FreeSurface(priv->surface);
555 priv->surface = newsurface;
556 SDL_ShowCursor(0);
559 #endif
561 /* Set video mode. Not fullscreen */
562 static void set_video_mode(int width, int height, int bpp, uint32_t sdlflags)
564 struct sdl_priv_s *priv = &sdl_priv;
565 SDL_Surface* newsurface;
567 if(priv->rgbsurface)
568 SDL_FreeSurface(priv->rgbsurface);
569 else if(priv->overlay)
570 SDL_FreeYUVOverlay(priv->overlay);
572 priv->rgbsurface = NULL;
573 priv->overlay = NULL;
575 newsurface = SDL_SetVideoMode(width, height, bpp, sdlflags);
577 if(newsurface) {
579 /* priv->surface will be NULL the first time this function is called. */
580 if(priv->surface)
581 SDL_FreeSurface(priv->surface);
583 priv->surface = newsurface;
584 priv->dstwidth = width;
585 priv->dstheight = height;
586 vo_dwidth = width;
587 vo_dheight = height;
589 setup_surfaces();
591 else
592 mp_msg(MSGT_VO,MSGL_WARN, "set_video_mode: SDL_SetVideoMode failed: %s\n", SDL_GetError());
595 static void set_fullmode (int mode) {
596 struct sdl_priv_s *priv = &sdl_priv;
597 SDL_Surface *newsurface = NULL;
598 int screen_surface_w, screen_surface_h;
600 if(priv->rgbsurface)
601 SDL_FreeSurface(priv->rgbsurface);
602 else if(priv->overlay)
603 SDL_FreeYUVOverlay(priv->overlay);
605 priv->rgbsurface = NULL;
606 priv->overlay = NULL;
608 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
609 /* But select a mode where the full video enter */
610 if(priv->X && priv->fulltype & VOFLAG_FULLSCREEN) {
611 screen_surface_w = priv->XWidth;
612 screen_surface_h = priv->XHeight;
614 else if (mode < 0) {
615 int i,j,imax;
616 mode = 0; // Default to the biggest mode avaible
617 if ( mp_msg_test(MSGT_VO,MSGL_V) ) for(i=0;priv->fullmodes[i];++i)
618 mp_msg(MSGT_VO,MSGL_V, "SDL Mode: %d: %d x %d\n", i, priv->fullmodes[i]->w, priv->fullmodes[i]->h);
619 for(i = findArrayEnd(priv->fullmodes) - 1; i >=0; i--) {
620 if( (priv->fullmodes[i]->w >= priv->dstwidth) &&
621 (priv->fullmodes[i]->h >= priv->dstheight) ) {
622 imax = i;
623 for (j = findArrayEnd(priv->fullmodes) - 1; j >=0; j--) {
624 if (priv->fullmodes[j]->w > priv->fullmodes[imax]->w
625 && priv->fullmodes[j]->h == priv->fullmodes[imax]->h)
626 imax = j;
628 mode = imax;
629 break;
632 mp_msg(MSGT_VO,MSGL_V, "SET SDL Mode: %d: %d x %d\n", mode, priv->fullmodes[mode]->w, priv->fullmodes[mode]->h);
633 priv->fullmode = mode;
634 screen_surface_h = priv->fullmodes[mode]->h;
635 screen_surface_w = priv->fullmodes[mode]->w;
637 else {
638 screen_surface_h = priv->fullmodes[mode]->h;
639 screen_surface_w = priv->fullmodes[mode]->w;
642 aspect_save_screenres(screen_surface_w, screen_surface_h);
644 /* calculate new video size/aspect */
645 if(priv->mode == YUV) {
646 if(priv->fulltype&VOFLAG_FULLSCREEN)
647 aspect_save_screenres(priv->XWidth, priv->XHeight);
649 aspect(&priv->dstwidth, &priv->dstheight, A_ZOOM);
652 /* try to change to given fullscreenmode */
653 newsurface = SDL_SetVideoMode(priv->dstwidth, screen_surface_h, priv->bpp,
654 priv->sdlfullflags);
657 * In Mac OS X (and possibly others?) SDL_SetVideoMode() appears to
658 * destroy the datastructure previously retrived, so we need to
659 * re-assign it. The comment in sdl_close() seems to imply that we
660 * should not free() anything.
662 #ifdef __APPLE__
664 const SDL_VideoInfo *vidInfo = NULL;
665 vidInfo = SDL_GetVideoInfo ();
667 /* collect all fullscreen & hardware modes available */
668 if (!(priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags))) {
670 /* non hardware accelerated fullscreen modes */
671 priv->sdlfullflags &= ~SDL_HWSURFACE;
672 priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags);
675 #endif
679 /* if creation of new surface was successful, save it and hide mouse cursor */
680 if(newsurface) {
681 if (priv->surface)
682 SDL_FreeSurface(priv->surface);
683 priv->surface = newsurface;
684 SDL_ShowCursor(0);
685 SDL_SRF_LOCK(priv->surface, -1)
686 SDL_FillRect(priv->surface, NULL, 0);
687 SDL_SRF_UNLOCK(priv->surface)
688 setup_surfaces();
690 else
691 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Set_fullmode: SDL_SetVideoMode failed: %s.\n", SDL_GetError());
696 * Initialize an SDL surface and an SDL YUV overlay.
698 * params : width == width of video we'll be displaying.
699 * height == height of video we'll be displaying.
700 * fullscreen == want to be fullscreen?
701 * title == Title for window titlebar.
702 * returns : non-zero on success, zero on error.
705 static int
706 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
707 //static int sdl_setup (int width, int height)
709 struct sdl_priv_s *priv = &sdl_priv;
711 switch(format){
712 case IMGFMT_I420:
713 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Mapping I420 to IYUV.\n");
714 format = SDL_IYUV_OVERLAY;
715 case IMGFMT_YV12:
716 case IMGFMT_IYUV:
717 case IMGFMT_YUY2:
718 case IMGFMT_UYVY:
719 case IMGFMT_YVYU:
720 priv->mode = YUV;
721 break;
722 case IMGFMT_BGR15:
723 case IMGFMT_BGR16:
724 case IMGFMT_BGR24:
725 case IMGFMT_BGR32:
726 priv->mode = BGR;
727 break;
728 case IMGFMT_RGB15:
729 case IMGFMT_RGB16:
730 case IMGFMT_RGB24:
731 case IMGFMT_RGB32:
732 priv->mode = RGB;
733 break;
734 default:
735 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Unsupported image format (0x%X).\n",format);
736 return -1;
739 if ( vo_config_count ) sdl_close();
741 mp_msg(MSGT_VO,MSGL_V, "SDL: Using 0x%X (%s) image format\n", format, vo_format_name(format));
743 if(priv->mode != YUV) {
744 priv->sdlflags |= SDL_ANYFORMAT;
745 priv->sdlfullflags |= SDL_ANYFORMAT;
748 /* SDL can only scale YUV data */
749 if(priv->mode == RGB || priv->mode == BGR) {
750 d_width = width;
751 d_height = height;
754 aspect_save_orig(width,height);
755 aspect_save_prescale(d_width ? d_width : width, d_height ? d_height : height);
757 /* Save the original Image size */
758 priv->width = width;
759 priv->height = height;
760 priv->dstwidth = d_width ? d_width : width;
761 priv->dstheight = d_height ? d_height : height;
763 priv->format = format;
765 if (sdl_open(NULL, NULL) != 0)
766 return -1;
768 /* Set output window title */
769 SDL_WM_SetCaption (".: MPlayer : F = Fullscreen/Windowed : C = Cycle Fullscreen Resolutions :.", title);
770 //SDL_WM_SetCaption (title, title);
772 if(priv->X) {
773 aspect_save_screenres(priv->XWidth,priv->XHeight);
774 aspect(&priv->dstwidth,&priv->dstheight,A_NOZOOM);
777 priv->windowsize.w = priv->dstwidth;
778 priv->windowsize.h = priv->dstheight;
780 /* bit 0 (0x01) means fullscreen (-fs)
781 * bit 1 (0x02) means mode switching (-vm)
782 * bit 2 (0x04) enables software scaling (-zoom)
783 * bit 3 (0x08) enables flipping (-flip)
785 // printf("SDL: flags are set to: %i\n", flags);
786 // printf("SDL: Width: %i Height: %i D_Width %i D_Height: %i\n", width, height, d_width, d_height);
787 if(flags&VOFLAG_FLIPPING) {
788 mp_msg(MSGT_VO,MSGL_V, "SDL: using flipped video (only with RGB/BGR/packed YUV)\n");
789 priv->flip = 1;
791 if(flags&VOFLAG_FULLSCREEN) {
792 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen without modeswitching\n");
793 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Info - please use -vm or -zoom to switch to the best resolution.\n");
794 priv->fulltype = VOFLAG_FULLSCREEN;
795 set_fullmode(priv->fullmode);
796 /*if((priv->surface = SDL_SetVideoMode (d_width, d_height, priv->bpp, priv->sdlfullflags)))
797 SDL_ShowCursor(0);*/
798 } else
799 if(flags&VOFLAG_MODESWITCHING) {
800 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
801 priv->fulltype = VOFLAG_MODESWITCHING;
802 set_fullmode(priv->fullmode);
803 /*if((priv->surface = SDL_SetVideoMode (d_width ? d_width : width, d_height ? d_height : height, priv->bpp, priv->sdlfullflags)))
804 SDL_ShowCursor(0);*/
805 } else
806 if(flags&VOFLAG_SWSCALE) {
807 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
808 priv->fulltype = VOFLAG_SWSCALE;
809 set_fullmode(priv->fullmode);
811 else {
812 if((strcmp(priv->driver, "x11") == 0)
813 ||(strcmp(priv->driver, "windib") == 0)
814 ||(strcmp(priv->driver, "directx") == 0)
815 ||(strcmp(priv->driver, "Quartz") == 0)
816 ||(strcmp(priv->driver, "cgx") == 0)
817 ||(strcmp(priv->driver, "os4video") == 0)
818 ||((strcmp(priv->driver, "aalib") == 0) && priv->X)){
819 mp_msg(MSGT_VO,MSGL_V, "SDL: setting windowed mode\n");
820 set_video_mode(priv->dstwidth, priv->dstheight, priv->bpp, priv->sdlflags);
822 else {
823 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
824 priv->fulltype = VOFLAG_SWSCALE;
825 set_fullmode(priv->fullmode);
829 if(!priv->surface) { // cannot SetVideoMode
830 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Failed to set video mode: %s.\n", SDL_GetError());
831 return -1;
834 return 0;
837 /* Free priv->rgbsurface or priv->overlay if they are != NULL.
838 * Setup priv->rgbsurface or priv->overlay depending on source format.
839 * The size of the created surface or overlay depends on the size of
840 * priv->surface, priv->width, priv->height, priv->dstwidth and priv->dstheight.
842 static int setup_surfaces(void)
844 struct sdl_priv_s *priv = &sdl_priv;
845 float v_scale = ((float) priv->dstheight) / priv->height;
846 int surfwidth, surfheight;
848 surfwidth = priv->width;
849 surfheight = priv->height + (priv->surface->h - priv->dstheight) / v_scale;
850 surfheight&= ~1;
851 /* Place the image in the middle of the screen */
852 priv->y = (surfheight - priv->height) / 2;
853 priv->y_screen_top = priv->y * v_scale;
854 priv->y_screen_bottom = priv->y_screen_top + priv->dstheight;
856 priv->dirty_off_frame[0].x = -1;
857 priv->dirty_off_frame[0].y = -1;
858 priv->dirty_off_frame[1].x = -1;
859 priv->dirty_off_frame[1].y = -1;
861 /* Make sure the entire screen is updated */
862 vo_osd_changed(1);
864 if(priv->rgbsurface)
865 SDL_FreeSurface(priv->rgbsurface);
866 else if(priv->overlay)
867 SDL_FreeYUVOverlay(priv->overlay);
869 priv->rgbsurface = NULL;
870 priv->overlay = NULL;
872 if(priv->mode != YUV && (priv->format&0xFF) == priv->bpp) {
873 if(strcmp(priv->driver, "x11") == 0) {
874 priv->dblit = 1;
875 priv->framePlaneRGB = priv->width * priv->height * priv->surface->format->BytesPerPixel;
876 priv->stridePlaneRGB = priv->width * priv->surface->format->BytesPerPixel;
877 erase_rectangle(0, 0, priv->surface->w, priv->surface->h);
878 return 0;
882 switch(priv->format) {
883 /* Initialize and create the RGB Surface used for video out in BGR/RGB mode */
884 //SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
885 // SDL_SWSURFACE,SDL_HWSURFACE,SDL_SRCCOLORKEY, priv->flags? guess: exchange Rmask and Bmask for BGR<->RGB
886 // 32 bit: a:ff000000 r:ff000 g:ff00 b:ff
887 // 24 bit: r:ff0000 g:ff00 b:ff
888 // 16 bit: r:1111100000000000b g:0000011111100000b b:0000000000011111b
889 // 15 bit: r:111110000000000b g:000001111100000b b:000000000011111b
890 // FIXME: colorkey detect based on bpp, FIXME static bpp value, FIXME alpha value correct?
891 case IMGFMT_RGB15:
892 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 15, 31, 992, 31744, 0);
893 break;
894 case IMGFMT_BGR15:
895 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 15, 31744, 992, 31, 0);
896 break;
897 case IMGFMT_RGB16:
898 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 16, 31, 2016, 63488, 0);
899 break;
900 case IMGFMT_BGR16:
901 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 16, 63488, 2016, 31, 0);
902 break;
903 case IMGFMT_RGB24:
904 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 24, 0x0000FF, 0x00FF00, 0xFF0000, 0);
905 break;
906 case IMGFMT_BGR24:
907 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 24, 0xFF0000, 0x00FF00, 0x0000FF, 0);
908 break;
909 case IMGFMT_RGB32:
910 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0/*0xFF000000*/);
911 break;
912 case IMGFMT_BGR32:
913 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0/*0xFF000000*/);
914 break;
915 default:
916 /* Initialize and create the YUV Overlay used for video out */
917 if (!(priv->overlay = SDL_CreateYUVOverlay (surfwidth, surfheight, priv->format, priv->surface))) {
918 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Couldn't create a YUV overlay: %s.\n", SDL_GetError());
919 return -1;
921 priv->framePlaneY = priv->width * priv->height;
922 priv->framePlaneUV = (priv->width * priv->height) >> 2;
923 priv->framePlaneYUY = priv->width * priv->height * 2;
924 priv->stridePlaneY = priv->width;
925 priv->stridePlaneUV = priv->width/2;
926 priv->stridePlaneYUY = priv->width * 2;
929 if(priv->mode != YUV) {
930 if(!priv->rgbsurface) {
931 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Couldn't create an RGB surface: %s.\n", SDL_GetError());
932 return -1;
935 priv->dblit = 0;
937 if((priv->format&0xFF) != priv->bpp)
938 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);
940 priv->framePlaneRGB = priv->width * priv->height * priv->rgbsurface->format->BytesPerPixel;
941 priv->stridePlaneRGB = priv->width * priv->rgbsurface->format->BytesPerPixel;
944 erase_rectangle(0, 0, surfwidth, surfheight);
946 return 0;
951 * Draw a frame to the SDL YUV overlay.
953 * params : *src[] == the Y, U, and V planes that make up the frame.
954 * returns : non-zero on success, zero on error.
957 //static int sdl_draw_frame (frame_t *frame)
958 static int draw_frame(uint8_t *src[])
960 struct sdl_priv_s *priv = &sdl_priv;
961 uint8_t *dst;
962 int i;
963 uint8_t *mysrc = src[0];
965 switch(priv->format){
966 case IMGFMT_YUY2:
967 case IMGFMT_UYVY:
968 case IMGFMT_YVYU:
969 SDL_OVR_LOCK(-1)
970 dst = (uint8_t *) *(priv->overlay->pixels) + priv->overlay->pitches[0]*priv->y;
971 if(priv->flip) {
972 mysrc+=priv->framePlaneYUY;
973 for(i = 0; i < priv->height; i++) {
974 mysrc-=priv->stridePlaneYUY;
975 fast_memcpy (dst, mysrc, priv->stridePlaneYUY);
976 dst+=priv->overlay->pitches[0];
979 else fast_memcpy (dst, src[0], priv->framePlaneYUY);
980 SDL_OVR_UNLOCK
981 break;
983 case IMGFMT_RGB15:
984 case IMGFMT_BGR15:
985 case IMGFMT_RGB16:
986 case IMGFMT_BGR16:
987 case IMGFMT_RGB24:
988 case IMGFMT_BGR24:
989 case IMGFMT_RGB32:
990 case IMGFMT_BGR32:
991 if(priv->dblit) {
992 SDL_SRF_LOCK(priv->surface, -1)
993 dst = (uint8_t *) priv->surface->pixels + priv->y*priv->surface->pitch;
994 if(priv->flip) {
995 mysrc+=priv->framePlaneRGB;
996 for(i = 0; i < priv->height; i++) {
997 mysrc-=priv->stridePlaneRGB;
998 fast_memcpy (dst, mysrc, priv->stridePlaneRGB);
999 dst += priv->surface->pitch;
1002 else fast_memcpy (dst, src[0], priv->framePlaneRGB);
1003 SDL_SRF_UNLOCK(priv->surface)
1004 } else {
1005 SDL_SRF_LOCK(priv->rgbsurface, -1)
1006 dst = (uint8_t *) priv->rgbsurface->pixels + priv->y*priv->rgbsurface->pitch;
1007 if(priv->flip) {
1008 mysrc+=priv->framePlaneRGB;
1009 for(i = 0; i < priv->height; i++) {
1010 mysrc-=priv->stridePlaneRGB;
1011 fast_memcpy (dst, mysrc, priv->stridePlaneRGB);
1012 dst += priv->rgbsurface->pitch;
1015 else fast_memcpy (dst, src[0], priv->framePlaneRGB);
1016 SDL_SRF_UNLOCK(priv->rgbsurface)
1018 break;
1022 return 0;
1027 * Draw a slice (16 rows of image) to the SDL YUV overlay.
1029 * params : *src[] == the Y, U, and V planes that make up the slice.
1030 * returns : non-zero on error, zero on success.
1033 //static uint32_t draw_slice(uint8_t *src[], uint32_t slice_num)
1034 static int draw_slice(uint8_t *image[], int stride[], int w,int h,int x,int y)
1036 struct sdl_priv_s *priv = &sdl_priv;
1037 uint8_t *dst;
1039 SDL_OVR_LOCK(-1)
1041 y += priv->y;
1043 dst = priv->overlay->pixels[0] + priv->overlay->pitches[0]*y + x;
1044 memcpy_pic(dst, image[0], w, h, priv->overlay->pitches[0], stride[0]);
1045 x/=2;y/=2;w/=2;h/=2;
1047 switch(priv->format) {
1048 case IMGFMT_YV12:
1049 dst = priv->overlay->pixels[2] + priv->overlay->pitches[2]*y + x;
1050 memcpy_pic(dst, image[1], w, h, priv->overlay->pitches[2], stride[1]);
1052 dst = priv->overlay->pixels[1] + priv->overlay->pitches[1]*y + x;
1053 memcpy_pic(dst, image[2], w, h, priv->overlay->pitches[1], stride[2]);
1055 break;
1056 case IMGFMT_I420:
1057 case IMGFMT_IYUV:
1058 dst = priv->overlay->pixels[1] + priv->overlay->pitches[1]*y + x;
1059 memcpy_pic(dst, image[1], w, h, priv->overlay->pitches[1], stride[1]);
1061 dst = priv->overlay->pixels[2] + priv->overlay->pitches[2]*y + x;
1062 memcpy_pic(dst, image[2], w, h, priv->overlay->pitches[2], stride[2]);
1064 break;
1065 default:
1066 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Unsupported image format in draw_slice, contact MPlayer developers!\n");
1069 SDL_OVR_UNLOCK
1071 return 0;
1077 * Checks for SDL keypress and window resize events
1079 * params : none
1080 * returns : doesn't return
1083 static void check_events (void)
1085 struct sdl_priv_s *priv = &sdl_priv;
1086 SDL_Event event;
1087 SDLKey keypressed = SDLK_UNKNOWN;
1089 /* Poll the waiting SDL Events */
1090 while ( SDL_PollEvent(&event) ) {
1091 switch (event.type) {
1093 /* capture window resize events */
1094 case SDL_VIDEORESIZE:
1095 if(!priv->dblit)
1096 set_video_mode(event.resize.w, event.resize.h, priv->bpp, priv->sdlflags);
1098 /* save video extents, to restore them after going fullscreen */
1099 //if(!(priv->surface->flags & SDL_FULLSCREEN)) {
1100 priv->windowsize.w = priv->surface->w;
1101 priv->windowsize.h = priv->surface->h;
1103 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Window resize\n");
1104 break;
1106 /* graphics mode selection shortcuts */
1107 case SDL_KEYDOWN:
1108 keypressed = event.key.keysym.sym;
1109 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Key pressed: '%i'\n", keypressed);
1111 /* c key pressed. c cycles through available fullscreenmodes, if we have some */
1112 if ( ((keypressed == SDLK_c)) && (priv->fullmodes) ) {
1113 /* select next fullscreen mode */
1114 priv->fullmode++;
1115 if (priv->fullmode > (findArrayEnd(priv->fullmodes) - 1)) priv->fullmode = 0;
1116 set_fullmode(priv->fullmode);
1118 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Set next available fullscreen mode.\n");
1121 else if ( keypressed == SDLK_n ) {
1122 #ifdef CONFIG_X11
1123 aspect(&priv->dstwidth, &priv->dstheight,A_NOZOOM);
1124 #endif
1125 if (priv->surface->w != priv->dstwidth || priv->surface->h != priv->dstheight) {
1126 set_video_mode(priv->dstwidth, priv->dstheight, priv->bpp, priv->sdlflags);
1127 priv->windowsize.w = priv->surface->w;
1128 priv->windowsize.h = priv->surface->h;
1129 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Normal size\n");
1130 } else
1131 if (priv->surface->w != priv->dstwidth * 2 || priv->surface->h != priv->dstheight * 2) {
1132 set_video_mode(priv->dstwidth * 2, priv->dstheight * 2, priv->bpp, priv->sdlflags);
1133 priv->windowsize.w = priv->surface->w;
1134 priv->windowsize.h = priv->surface->h;
1135 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Double size\n");
1139 else sdl_default_handle_event(&event);
1141 break;
1142 default: sdl_default_handle_event(&event); break;
1147 /* Erase (paint it black) the rectangle specified by x, y, w and h in the surface
1148 or overlay which is used for OSD
1150 static void erase_rectangle(int x, int y, int w, int h)
1152 struct sdl_priv_s *priv = &sdl_priv;
1154 switch(priv->format) {
1155 case IMGFMT_YV12:
1156 case IMGFMT_I420:
1157 case IMGFMT_IYUV:
1159 SDL_OVR_LOCK((void) 0)
1161 /* Erase Y plane */
1162 erase_area_1(x, w, h,
1163 priv->overlay->pitches[0], 0,
1164 priv->overlay->pixels[0] +
1165 priv->overlay->pitches[0]*y);
1167 /* Erase U and V planes */
1168 w /= 2;
1169 x /= 2;
1170 h /= 2;
1171 y /= 2;
1173 erase_area_1(x, w, h,
1174 priv->overlay->pitches[1], 128,
1175 priv->overlay->pixels[1] +
1176 priv->overlay->pitches[1]*y);
1178 erase_area_1(x, w, h,
1179 priv->overlay->pitches[2], 128,
1180 priv->overlay->pixels[2] +
1181 priv->overlay->pitches[2]*y);
1182 SDL_OVR_UNLOCK
1183 break;
1186 case IMGFMT_YUY2:
1187 case IMGFMT_YVYU:
1189 SDL_OVR_LOCK((void) 0)
1190 erase_area_4(x*2, w*2, h,
1191 priv->overlay->pitches[0],
1192 be2me_32(0x00800080), /* yuy2 and yvyu represent black the same way */
1193 priv->overlay->pixels[0] +
1194 priv->overlay->pitches[0]*y);
1195 SDL_OVR_UNLOCK
1196 break;
1199 case IMGFMT_UYVY:
1201 SDL_OVR_LOCK((void) 0)
1202 erase_area_4(x*2, w*2, h,
1203 priv->overlay->pitches[0],
1204 be2me_32(0x80008000),
1205 priv->overlay->pixels[0] +
1206 priv->overlay->pitches[0]*y);
1207 SDL_OVR_UNLOCK
1208 break;
1211 case IMGFMT_RGB15:
1212 case IMGFMT_BGR15:
1213 case IMGFMT_RGB16:
1214 case IMGFMT_BGR16:
1215 case IMGFMT_RGB24:
1216 case IMGFMT_BGR24:
1217 case IMGFMT_RGB32:
1218 case IMGFMT_BGR32:
1220 SDL_Rect rect;
1221 rect.w = w; rect.h = h;
1222 rect.x = x; rect.y = y;
1224 if(priv->dblit) {
1225 SDL_SRF_LOCK(priv->surface, (void) 0)
1226 SDL_FillRect(priv->surface, &rect, 0);
1227 SDL_SRF_UNLOCK(priv->surface)
1229 else {
1230 SDL_SRF_LOCK(priv->rgbsurface, (void) 0)
1231 SDL_FillRect(priv->rgbsurface, &rect, 0);
1232 SDL_SRF_UNLOCK(priv->rgbsurface)
1234 break;
1239 static void draw_osd(void)
1240 { struct sdl_priv_s *priv = &sdl_priv;
1242 priv->osd_has_changed = vo_osd_changed(0);
1244 if(priv->osd_has_changed)
1246 int i;
1248 for(i = 0; i < 2; i++) {
1249 if(priv->dirty_off_frame[i].x < 0 || priv->dirty_off_frame[i].y < 0)
1250 continue;
1252 erase_rectangle(priv->dirty_off_frame[i].x, priv->dirty_off_frame[i].y,
1253 priv->dirty_off_frame[i].w, priv->dirty_off_frame[i].h);
1255 priv->dirty_off_frame[i].x = -1;
1256 priv->dirty_off_frame[i].y = -1;
1260 /* update osd/subtitles */
1261 if(priv->mode == YUV)
1262 vo_draw_text(priv->overlay->w, priv->overlay->h, draw_alpha);
1263 else {
1264 if(priv->dblit)
1265 vo_draw_text(priv->surface->w, priv->surface->h, draw_alpha);
1266 else
1267 vo_draw_text(priv->rgbsurface->w, priv->rgbsurface->h, draw_alpha);
1271 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1272 * are given in bytes. 4 bytes at a time.
1274 static void erase_area_4(int x_start, int width, int height, int pitch, uint32_t color, uint8_t* pixels)
1276 int x_end = x_start/4 + width/4;
1277 int x, y;
1278 uint32_t* data = (uint32_t*) pixels;
1280 x_start /= 4;
1281 pitch /= 4;
1283 for(y = 0; y < height; y++) {
1284 for(x = x_start; x < x_end; x++)
1285 data[y*pitch + x] = color;
1289 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1290 * are given in bytes. 1 byte at a time.
1292 static void erase_area_1(int x_start, int width, int height, int pitch, uint8_t color, uint8_t* pixels)
1294 int y;
1296 for(y = 0; y < height; y++) {
1297 memset(&pixels[y*pitch + x_start], color, width);
1302 * Display the surface we have written our data to
1304 * params : mode == index of the desired fullscreen mode
1305 * returns : doesn't return
1308 static void flip_page (void)
1310 struct sdl_priv_s *priv = &sdl_priv;
1312 switch(priv->format) {
1313 case IMGFMT_RGB15:
1314 case IMGFMT_BGR15:
1315 case IMGFMT_RGB16:
1316 case IMGFMT_BGR16:
1317 case IMGFMT_RGB24:
1318 case IMGFMT_BGR24:
1319 case IMGFMT_RGB32:
1320 case IMGFMT_BGR32:
1321 if(!priv->dblit) {
1322 /* blit to the RGB surface */
1323 if(SDL_BlitSurface (priv->rgbsurface, NULL, priv->surface, NULL))
1324 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Blit failed: %s.\n", SDL_GetError());
1327 /* update screen */
1328 //SDL_UpdateRect(priv->surface, 0, 0, priv->surface->clip_rect.w, priv->surface->clip_rect.h);
1329 if(priv->osd_has_changed) {
1330 priv->osd_has_changed = 0;
1331 SDL_UpdateRects(priv->surface, 1, &priv->surface->clip_rect);
1333 else
1334 SDL_UpdateRect(priv->surface, 0, priv->y_screen_top,
1335 priv->surface->clip_rect.w, priv->y_screen_bottom);
1337 /* check if we have a double buffered surface and flip() if we do. */
1338 if ( priv->surface->flags & SDL_DOUBLEBUF )
1339 SDL_Flip(priv->surface);
1341 break;
1342 default:
1343 /* blit to the YUV overlay */
1344 SDL_DisplayYUVOverlay (priv->overlay, &priv->surface->clip_rect);
1346 /* check if we have a double buffered surface and flip() if we do. */
1347 if ( priv->surface->flags & SDL_DOUBLEBUF )
1348 SDL_Flip(priv->surface);
1350 //SDL_LockYUVOverlay (priv->overlay); // removed because unused!?
1354 static int
1355 query_format(uint32_t format)
1357 switch(format){
1358 case IMGFMT_YV12:
1359 // it seems buggy (not hw accelerated), so just use YV12 instead!
1360 // case IMGFMT_I420:
1361 // case IMGFMT_IYUV:
1362 case IMGFMT_YUY2:
1363 case IMGFMT_UYVY:
1364 case IMGFMT_YVYU:
1365 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD |
1366 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
1367 case IMGFMT_RGB15:
1368 case IMGFMT_BGR15:
1369 case IMGFMT_RGB16:
1370 case IMGFMT_BGR16:
1371 case IMGFMT_RGB24:
1372 case IMGFMT_BGR24:
1373 case IMGFMT_RGB32:
1374 case IMGFMT_BGR32:
1375 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_FLIP;
1377 return 0;
1381 static void
1382 uninit(void)
1384 #ifdef CONFIG_X11
1385 struct sdl_priv_s *priv = &sdl_priv;
1386 if(priv->X) {
1387 mp_msg(MSGT_VO,MSGL_V, "SDL: activating XScreensaver/DPMS\n");
1388 vo_x11_uninit();
1390 #endif
1391 sdl_close();
1393 /* Cleanup SDL */
1394 vo_sdl_uninit();
1396 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Closed Plugin\n");
1400 static int preinit(const char *arg)
1402 struct sdl_priv_s *priv = &sdl_priv;
1403 char * sdl_driver = NULL;
1404 int sdl_hwaccel;
1405 int sdl_forcexv;
1406 const opt_t subopts[] = {
1407 {"forcexv", OPT_ARG_BOOL, &sdl_forcexv, NULL},
1408 {"hwaccel", OPT_ARG_BOOL, &sdl_hwaccel, NULL},
1409 {"driver", OPT_ARG_MSTRZ, &sdl_driver, NULL},
1410 {NULL, 0, NULL, NULL}
1413 sdl_forcexv = 1;
1414 sdl_hwaccel = 1;
1416 if (subopt_parse(arg, subopts) != 0) return -1;
1418 priv->rgbsurface = NULL;
1419 priv->overlay = NULL;
1420 priv->surface = NULL;
1422 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Opening Plugin\n");
1424 if(sdl_driver) {
1425 setenv("SDL_VIDEODRIVER", sdl_driver, 1);
1426 free(sdl_driver);
1429 /* does the user want SDL to try and force Xv */
1430 if(sdl_forcexv) setenv("SDL_VIDEO_X11_NODIRECTCOLOR", "1", 1);
1431 else setenv("SDL_VIDEO_X11_NODIRECTCOLOR", "0", 1);
1433 /* does the user want to disable Xv and use software scaling instead */
1434 if(sdl_hwaccel) setenv("SDL_VIDEO_YUV_HWACCEL", "1", 1);
1435 else setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
1437 /* default to no fullscreen mode, we'll set this as soon we have the avail. modes */
1438 priv->fullmode = -2;
1440 priv->fullmodes = NULL;
1441 priv->bpp = 0;
1443 /* initialize the SDL Video system */
1444 if (!vo_sdl_init()) {
1445 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_SDL] SDL initialization failed: %s.\n", SDL_GetError());
1447 return -1;
1450 SDL_VideoDriverName(priv->driver, 8);
1451 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Using driver: %s.\n", priv->driver);
1453 priv->X = 0;
1454 #ifdef CONFIG_X11
1455 if(vo_init()) {
1456 mp_msg(MSGT_VO,MSGL_V, "SDL: deactivating XScreensaver/DPMS\n");
1457 priv->XWidth = vo_screenwidth;
1458 priv->XHeight = vo_screenheight;
1459 priv->X = 1;
1460 mp_msg(MSGT_VO,MSGL_V, "SDL: X11 Resolution %ix%i\n", priv->XWidth, priv->XHeight);
1462 #endif
1464 return 0;
1467 static uint32_t get_image(mp_image_t *mpi)
1469 struct sdl_priv_s *priv = &sdl_priv;
1471 if(priv->format != mpi->imgfmt) return VO_FALSE;
1472 if(mpi->type == MP_IMGTYPE_STATIC || mpi->type == MP_IMGTYPE_TEMP) {
1473 if(mpi->flags&MP_IMGFLAG_PLANAR) {
1474 mpi->planes[0] = priv->overlay->pixels[0] + priv->y*priv->overlay->pitches[0];
1475 mpi->stride[0] = priv->overlay->pitches[0];
1476 if(mpi->flags&MP_IMGFLAG_SWAPPED) {
1477 mpi->planes[1] = priv->overlay->pixels[1] + priv->y*priv->overlay->pitches[1]/2;
1478 mpi->stride[1] = priv->overlay->pitches[1];
1479 mpi->planes[2] = priv->overlay->pixels[2] + priv->y*priv->overlay->pitches[2]/2;
1480 mpi->stride[2] = priv->overlay->pitches[2];
1481 } else {
1482 mpi->planes[2] = priv->overlay->pixels[1] + priv->y*priv->overlay->pitches[1]/2;
1483 mpi->stride[2] = priv->overlay->pitches[1];
1484 mpi->planes[1] = priv->overlay->pixels[2] + priv->y*priv->overlay->pitches[2]/2;
1485 mpi->stride[1] = priv->overlay->pitches[2];
1488 else if(IMGFMT_IS_RGB(priv->format) || IMGFMT_IS_BGR(priv->format)) {
1489 if(priv->dblit) {
1490 if(mpi->type == MP_IMGTYPE_STATIC && (priv->surface->flags & SDL_DOUBLEBUF))
1491 return VO_FALSE;
1493 mpi->planes[0] = (uint8_t *)priv->surface->pixels + priv->y*priv->surface->pitch;
1494 mpi->stride[0] = priv->surface->pitch;
1496 else {
1497 mpi->planes[0] = (uint8_t *)priv->rgbsurface->pixels + priv->y*priv->rgbsurface->pitch;
1498 mpi->stride[0] = priv->rgbsurface->pitch;
1501 else {
1502 mpi->planes[0] = priv->overlay->pixels[0] + priv->y*priv->overlay->pitches[0];
1503 mpi->stride[0] = priv->overlay->pitches[0];
1506 mpi->flags|=MP_IMGFLAG_DIRECT;
1507 return VO_TRUE;
1510 return VO_FALSE;
1513 static int control(uint32_t request, void *data)
1515 struct sdl_priv_s *priv = &sdl_priv;
1516 switch (request) {
1517 case VOCTRL_GET_IMAGE:
1518 return get_image(data);
1519 case VOCTRL_QUERY_FORMAT:
1520 return query_format(*((uint32_t*)data));
1521 case VOCTRL_FULLSCREEN:
1522 if (priv->surface->flags & SDL_FULLSCREEN) {
1523 set_video_mode(priv->windowsize.w, priv->windowsize.h, priv->bpp, priv->sdlflags);
1524 SDL_ShowCursor(1);
1525 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Windowed mode\n");
1526 } else if (priv->fullmodes) {
1527 set_fullmode(priv->fullmode);
1528 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Set fullscreen mode\n");
1530 return VO_TRUE;
1533 return VO_NOTIMPL;