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
64 #include "video_out.h"
65 #include "video_out_internal.h"
67 #include "fastmemcpy.h"
70 #include "libmpcodecs/vfcap.h"
74 #include "x11_common.h"
77 #include "input/input.h"
78 #include "input/mouse.h"
79 #include "subopt-helper.h"
82 static const vo_info_t info
=
84 "SDL YUV/RGB/BGR renderer (SDL v1.1.7+ only!)",
86 "Ryan C. Gordon <icculus@lokigames.com>, Felix Buenemann <atmosfear@users.sourceforge.net>",
90 const LIBVO_EXTERN(sdl
)
92 #ifdef CONFIG_SDL_SDL_H
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"); \
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"); \
114 #define SDL_SRF_UNLOCK(srf) if(SDL_MUSTLOCK(srf)) \
115 SDL_UnlockSurface (srf);
117 #define SDL_OVR_LOCK(x)
118 #define SDL_OVR_UNLOCK
119 #define SDL_SRF_LOCK(srf, x)
120 #define SDL_SRF_UNLOCK(srf)
123 /** Private SDL Data structure **/
125 static struct sdl_priv_s
{
127 /* output driver used by sdl */
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 */
157 /* use direct blitting to surface */
160 /* current fullscreen mode, 0 = highest available fullscreen mode */
164 int framePlaneY
, framePlaneUV
, framePlaneYUY
;
165 int stridePlaneY
, stridePlaneUV
, stridePlaneYUY
;
174 /* fullscreen behaviour; see init */
177 /* is X running (0/1) */
183 /* original image dimensions */
186 /* destination dimensions */
187 int dstwidth
, dstheight
;
189 /* Draw image at coordinate y on the SDL surfaces */
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 */
198 /* source image format (YUV/RGB/...) */
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];
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) {
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 */
250 /* Make sure we don't mark part of the frame area dirty */
252 expand_rect(&priv
->dirty_off_frame
[0], x0
, y0
, w
, priv
->y
- y0
);
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
));
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 */
268 src
= src
+ (priv
->y
- y0
) * stride
;
269 srca
= srca
+ (priv
->y
- y0
) * stride
;
274 if(priv
->y
+ priv
->height
<= y0
+ h
)
275 h
= priv
->y
+ priv
->height
- y0
;
281 switch(priv
->format
) {
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]);
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]);
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]);
299 x0
*= priv
->surface
->format
->BytesPerPixel
;
300 switch(priv
->format
) {
303 vo_draw_alpha_rgb15(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->surface
->pixels
)+y0
*priv
->surface
->pitch
+x0
,priv
->surface
->pitch
);
307 vo_draw_alpha_rgb16(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->surface
->pixels
)+y0
*priv
->surface
->pitch
+x0
,priv
->surface
->pitch
);
311 vo_draw_alpha_rgb24(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->surface
->pixels
)+y0
*priv
->surface
->pitch
+x0
,priv
->surface
->pitch
);
315 vo_draw_alpha_rgb32(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->surface
->pixels
)+y0
*priv
->surface
->pitch
+x0
,priv
->surface
->pitch
);
320 x0
*= priv
->rgbsurface
->format
->BytesPerPixel
;
321 switch(priv
->format
) {
324 vo_draw_alpha_rgb15(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->rgbsurface
->pixels
)+y0
*priv
->rgbsurface
->pitch
+x0
,priv
->rgbsurface
->pitch
);
328 vo_draw_alpha_rgb16(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->rgbsurface
->pixels
)+y0
*priv
->rgbsurface
->pitch
+x0
,priv
->rgbsurface
->pitch
);
332 vo_draw_alpha_rgb24(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->rgbsurface
->pixels
)+y0
*priv
->rgbsurface
->pitch
+x0
,priv
->rgbsurface
->pitch
);
336 vo_draw_alpha_rgb32(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->rgbsurface
->pixels
)+y0
*priv
->rgbsurface
->pitch
+x0
,priv
->rgbsurface
->pitch
);
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
)
355 while ( array
[i
++] ); /* keep loopin' ... */
357 /* return the index of the last array element */
363 * Open and prepare SDL output.
365 * params : *plugin ==
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;
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
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;
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
402 #if !defined( __AMIGAOS4__ ) && !defined( __APPLE__ )
403 priv
->sdlfullflags
|= SDL_DOUBLEBUF
;
404 if (vo_doublebuffering
)
405 priv
->sdlflags
|= SDL_DOUBLEBUF
;
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");
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",
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.
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
);
473 * Close SDL, Cleanups, Free Memory
476 * returns : non-zero on success, zero on error.
479 static int sdl_close (void)
481 struct sdl_priv_s
*priv
= &sdl_priv
;
486 /* Cleanup YUV Overlay structure */
488 SDL_FreeYUVOverlay(priv
->overlay
);
492 /* Free RGB Surface */
493 if (priv
->rgbsurface
) {
494 SDL_FreeSurface(priv
->rgbsurface
);
495 priv
->rgbsurface
=NULL
;
498 /* Free our blitting surface */
500 SDL_FreeSurface(priv
->surface
);
504 /* DON'T attempt to free the fullscreen modes array. SDL_Quit* does this for us */
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
521 static SDL_Rect
aspect(int srcw
, int srch
, int dstw
, int dsth
) {
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
;
528 newres
.x
= (dstw
- newres
.w
) / 2;
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
);
544 * Sets the specified fullscreen mode.
546 * params : mode == index of the desired fullscreen mode
547 * returns : doesn't return
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 */
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)*/
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 */
577 SDL_FreeSurface(priv
->surface
);
578 priv
->surface
= newsurface
;
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
;
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
);
602 /* priv->surface will be NULL the first time this function is called. */
604 SDL_FreeSurface(priv
->surface
);
606 priv
->surface
= newsurface
;
607 priv
->dstwidth
= width
;
608 priv
->dstheight
= height
;
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
;
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
;
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
) ) {
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
)
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
;
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
,
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.
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
);
700 /* if creation of new surface was successful, save it and hide mouse cursor */
703 SDL_FreeSurface(priv
->surface
);
704 priv
->surface
= newsurface
;
706 SDL_SRF_LOCK(priv
->surface
, -1)
707 SDL_FillRect(priv
->surface
, NULL
, 0);
708 SDL_SRF_UNLOCK(priv
->surface
)
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.
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
;
734 mp_tmsg(MSGT_VO
,MSGL_INFO
, "[VO_SDL] Mapping I420 to IYUV.\n");
735 format
= SDL_IYUV_OVERLAY
;
756 mp_tmsg(MSGT_VO
,MSGL_WARN
, "[VO_SDL] Unsupported image format (0x%X).\n",format
);
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
) {
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 */
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)
789 /* Set output window title */
790 SDL_WM_SetCaption (".: MPlayer : F = Fullscreen/Windowed : C = Cycle Fullscreen Resolutions :.", title
);
791 //SDL_WM_SetCaption (title, title);
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");
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)))
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)))
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
);
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
);
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());
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
;
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 */
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) {
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
);
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?
913 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 15, 31, 992, 31744, 0);
916 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 15, 31744, 992, 31, 0);
919 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 16, 31, 2016, 63488, 0);
922 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 16, 63488, 2016, 31, 0);
925 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 24, 0x0000FF, 0x00FF00, 0xFF0000, 0);
928 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 24, 0xFF0000, 0x00FF00, 0x0000FF, 0);
931 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0/*0xFF000000*/);
934 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0/*0xFF000000*/);
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());
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());
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
);
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
;
984 uint8_t *mysrc
= src
[0];
986 switch(priv
->format
){
991 dst
= (uint8_t *) *(priv
->overlay
->pixels
) + priv
->overlay
->pitches
[0]*priv
->y
;
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
);
1013 SDL_SRF_LOCK(priv
->surface
, -1)
1014 dst
= (uint8_t *) priv
->surface
->pixels
+ priv
->y
*priv
->surface
->pitch
;
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
)
1026 SDL_SRF_LOCK(priv
->rgbsurface
, -1)
1027 dst
= (uint8_t *) priv
->rgbsurface
->pixels
+ priv
->y
*priv
->rgbsurface
->pitch
;
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
)
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
;
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
) {
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]);
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]);
1087 mp_tmsg(MSGT_VO
,MSGL_WARN
, "[VO_SDL] Unsupported image format in draw_slice, contact MPlayer developers!\n");
1098 * Checks for SDL keypress and window resize events
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
;
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
:
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");
1130 case SDL_MOUSEBUTTONDOWN
:
1131 if(vo_nomouse_input
)
1133 mplayer_put_key((MOUSE_BTN0
+event
.button
.button
-1) | MP_KEY_DOWN
);
1136 case SDL_MOUSEBUTTONUP
:
1137 if(vo_nomouse_input
)
1139 mplayer_put_key(MOUSE_BTN0
+event
.button
.button
-1);
1142 /* graphics mode selection shortcuts */
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;
1153 case SDLK_KP_MULTIPLY
:
1155 case SDLK_KP_DIVIDE
:
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 */
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
) {
1178 aspect(&priv
->dstwidth
, &priv
->dstheight
,A_NOZOOM
);
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");
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;
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;
1227 case SDLK_KP_MULTIPLY
:
1229 case SDLK_KP_DIVIDE
:
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;
1239 case SDLK_KP_MULTIPLY
: mplayer_put_key('*'); break;
1241 case SDLK_KP_DIVIDE
: mplayer_put_key('/'); break;
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;
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
);
1261 case SDL_QUIT
: mplayer_put_key(KEY_CLOSE_WIN
);break;
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
) {
1279 SDL_OVR_LOCK((void) 0)
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 */
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
);
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
);
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
);
1346 rect
.w
= w
; rect
.h
= h
;
1347 rect
.x
= x
; rect
.y
= y
;
1350 SDL_SRF_LOCK(priv
->surface
, (void) 0)
1351 SDL_FillRect(priv
->surface
, &rect
, 0);
1352 SDL_SRF_UNLOCK(priv
->surface
)
1355 SDL_SRF_LOCK(priv
->rgbsurface
, (void) 0)
1356 SDL_FillRect(priv
->rgbsurface
, &rect
, 0);
1357 SDL_SRF_UNLOCK(priv
->rgbsurface
)
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
)
1373 for(i
= 0; i
< 2; i
++) {
1374 if(priv
->dirty_off_frame
[i
].x
< 0 || priv
->dirty_off_frame
[i
].y
< 0)
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
);
1390 vo_draw_text(priv
->surface
->w
, priv
->surface
->h
, draw_alpha
);
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;
1403 uint32_t* data
= (uint32_t*) pixels
;
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
)
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
) {
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());
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
);
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
);
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!?
1480 query_format(uint32_t format
)
1484 // it seems buggy (not hw accelerated), so just use YV12 instead!
1485 // case IMGFMT_I420:
1486 // case IMGFMT_IYUV:
1490 return VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_OSD
|
1491 VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
;
1500 return VFCAP_CSP_SUPPORTED
| VFCAP_OSD
| VFCAP_FLIP
;
1510 struct sdl_priv_s
*priv
= &sdl_priv
;
1512 mp_msg(MSGT_VO
,MSGL_V
, "SDL: activating XScreensaver/DPMS\n");
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
;
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
}
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");
1551 setenv("SDL_VIDEODRIVER", sdl_driver
, 1);
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
;
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());
1578 SDL_VideoDriverName(priv
->driver
, 8);
1579 mp_tmsg(MSGT_VO
,MSGL_INFO
, "[VO_SDL] Using driver: %s.\n", priv
->driver
);
1584 mp_msg(MSGT_VO
,MSGL_V
, "SDL: deactivating XScreensaver/DPMS\n");
1585 priv
->XWidth
= vo_screenwidth
;
1586 priv
->XHeight
= vo_screenheight
;
1588 mp_msg(MSGT_VO
,MSGL_V
, "SDL: X11 Resolution %ix%i\n", priv
->XWidth
, priv
->XHeight
);
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];
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
)) {
1618 if(mpi
->type
== MP_IMGTYPE_STATIC
&& (priv
->surface
->flags
& SDL_DOUBLEBUF
))
1621 mpi
->planes
[0] = (uint8_t *)priv
->surface
->pixels
+ priv
->y
*priv
->surface
->pitch
;
1622 mpi
->stride
[0] = priv
->surface
->pitch
;
1625 mpi
->planes
[0] = (uint8_t *)priv
->rgbsurface
->pixels
+ priv
->y
*priv
->rgbsurface
->pitch
;
1626 mpi
->stride
[0] = priv
->rgbsurface
->pitch
;
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
;
1641 static int control(uint32_t request
, void *data
)
1643 struct sdl_priv_s
*priv
= &sdl_priv
;
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
);
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");