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
65 #include "video_out.h"
66 #include "video_out_internal.h"
68 #include "fastmemcpy.h"
71 #include "libmpcodecs/vfcap.h"
75 #include "x11_common.h"
78 #include "input/input.h"
79 #include "input/mouse.h"
80 #include "subopt-helper.h"
83 static const vo_info_t info
=
85 "SDL YUV/RGB/BGR renderer (SDL v1.1.7+ only!)",
87 "Ryan C. Gordon <icculus@lokigames.com>, Felix Buenemann <atmosfear@users.sourceforge.net>",
91 const LIBVO_EXTERN(sdl
)
93 #ifdef CONFIG_SDL_SDL_H
98 //#include <SDL/SDL_syswm.h>
101 #ifdef SDL_ENABLE_LOCKS
102 #define SDL_OVR_LOCK(x) if (SDL_LockYUVOverlay (priv->overlay)) { \
103 mp_msg(MSGT_VO,MSGL_V, "SDL: Couldn't lock YUV overlay\n"); \
106 #define SDL_OVR_UNLOCK SDL_UnlockYUVOverlay (priv->overlay);
108 #define SDL_SRF_LOCK(srf, x) if(SDL_MUSTLOCK(srf)) { \
109 if(SDL_LockSurface (srf)) { \
110 mp_msg(MSGT_VO,MSGL_V, "SDL: Couldn't lock RGB surface\n"); \
115 #define SDL_SRF_UNLOCK(srf) if(SDL_MUSTLOCK(srf)) \
116 SDL_UnlockSurface (srf);
118 #define SDL_OVR_LOCK(x)
119 #define SDL_OVR_UNLOCK
120 #define SDL_SRF_LOCK(srf, x)
121 #define SDL_SRF_UNLOCK(srf)
124 /** Private SDL Data structure **/
126 static struct sdl_priv_s
{
128 /* output driver used by sdl */
131 /* SDL display surface */
132 SDL_Surface
*surface
;
134 /* SDL RGB surface */
135 SDL_Surface
*rgbsurface
;
137 /* SDL YUV overlay */
138 SDL_Overlay
*overlay
;
140 /* available fullscreen modes */
141 SDL_Rect
**fullmodes
;
143 /* surface attributes for fullscreen and windowed mode */
144 Uint32 sdlflags
, sdlfullflags
;
146 /* save the windowed output extents */
158 /* use direct blitting to surface */
161 /* current fullscreen mode, 0 = highest available fullscreen mode */
165 int framePlaneY
, framePlaneUV
, framePlaneYUY
;
166 int stridePlaneY
, stridePlaneUV
, stridePlaneYUY
;
175 /* fullscreen behaviour; see init */
178 /* is X running (0/1) */
184 /* original image dimensions */
187 /* destination dimensions */
188 int dstwidth
, dstheight
;
190 /* Draw image at coordinate y on the SDL surfaces */
193 /* The image is displayed between those y coordinates in priv->surface */
194 int y_screen_top
, y_screen_bottom
;
196 /* 1 if the OSD has changed otherwise 0 */
199 /* source image format (YUV/RGB/...) */
202 /* dirty_off_frame[0] contains a bounding box around the osd contents drawn above the image
203 dirty_off_frame[1] is the corresponding thing for OSD contents drawn below the image
205 SDL_Rect dirty_off_frame
[2];
208 static void erase_area_4(int x_start
, int width
, int height
, int pitch
, uint32_t color
, uint8_t* pixels
);
209 static void erase_area_1(int x_start
, int width
, int height
, int pitch
, uint8_t color
, uint8_t* pixels
);
210 static int setup_surfaces(void);
211 static void set_video_mode(int width
, int height
, int bpp
, uint32_t sdlflags
);
212 static void erase_rectangle(int x
, int y
, int w
, int h
);
214 /* Expand 'rect' to contain the rectangle specified by x, y, w and h */
215 static void expand_rect(SDL_Rect
* rect
, int x
, int y
, int w
, int h
)
217 if(rect
->x
< 0 || rect
->y
< 0) {
231 if(rect
->x
+ rect
->w
< x
+ w
)
232 rect
->w
= x
+ w
- rect
->x
;
234 if(rect
->y
+ rect
->h
< y
+ h
)
235 rect
->h
= y
+ h
- rect
->y
;
238 /** libvo Plugin functions **/
241 * draw_alpha is used for osd and subtitle display.
245 static void draw_alpha(int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
){
246 struct sdl_priv_s
*priv
= &sdl_priv
;
248 if(priv
->osd_has_changed
) {
249 /* OSD did change. Store a bounding box of everything drawn into the OSD */
251 /* Make sure we don't mark part of the frame area dirty */
253 expand_rect(&priv
->dirty_off_frame
[0], x0
, y0
, w
, priv
->y
- y0
);
255 expand_rect(&priv
->dirty_off_frame
[0], x0
, y0
, w
, h
);
257 else if(priv
->y
+ priv
->height
<= y0
+ h
) {
258 /* Make sure we don't mark part of the frame area dirty */
259 if(y0
< priv
->y
+ priv
->height
)
260 expand_rect(&priv
->dirty_off_frame
[1], x0
,
261 priv
->y
+ priv
->height
,
262 w
, h
- ((priv
->y
+ priv
->height
) - y0
));
264 expand_rect(&priv
->dirty_off_frame
[1], x0
, y0
, w
, h
);
267 else { /* OSD contents didn't change only draw parts that was erased by the frame */
269 src
= src
+ (priv
->y
- y0
) * stride
;
270 srca
= srca
+ (priv
->y
- y0
) * stride
;
275 if(priv
->y
+ priv
->height
<= y0
+ h
)
276 h
= priv
->y
+ priv
->height
- y0
;
282 switch(priv
->format
) {
286 vo_draw_alpha_yv12(w
,h
,src
,srca
,stride
,((uint8_t *) *(priv
->overlay
->pixels
))+priv
->overlay
->pitches
[0]*y0
+x0
,priv
->overlay
->pitches
[0]);
291 vo_draw_alpha_yuy2(w
,h
,src
,srca
,stride
,((uint8_t *) *(priv
->overlay
->pixels
))+priv
->overlay
->pitches
[0]*y0
+x0
,priv
->overlay
->pitches
[0]);
295 vo_draw_alpha_yuy2(w
,h
,src
,srca
,stride
,((uint8_t *) *(priv
->overlay
->pixels
))+priv
->overlay
->pitches
[0]*y0
+x0
,priv
->overlay
->pitches
[0]);
300 x0
*= priv
->surface
->format
->BytesPerPixel
;
301 switch(priv
->format
) {
304 vo_draw_alpha_rgb15(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->surface
->pixels
)+y0
*priv
->surface
->pitch
+x0
,priv
->surface
->pitch
);
308 vo_draw_alpha_rgb16(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->surface
->pixels
)+y0
*priv
->surface
->pitch
+x0
,priv
->surface
->pitch
);
312 vo_draw_alpha_rgb24(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->surface
->pixels
)+y0
*priv
->surface
->pitch
+x0
,priv
->surface
->pitch
);
316 vo_draw_alpha_rgb32(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->surface
->pixels
)+y0
*priv
->surface
->pitch
+x0
,priv
->surface
->pitch
);
321 x0
*= priv
->rgbsurface
->format
->BytesPerPixel
;
322 switch(priv
->format
) {
325 vo_draw_alpha_rgb15(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->rgbsurface
->pixels
)+y0
*priv
->rgbsurface
->pitch
+x0
,priv
->rgbsurface
->pitch
);
329 vo_draw_alpha_rgb16(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->rgbsurface
->pixels
)+y0
*priv
->rgbsurface
->pitch
+x0
,priv
->rgbsurface
->pitch
);
333 vo_draw_alpha_rgb24(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->rgbsurface
->pixels
)+y0
*priv
->rgbsurface
->pitch
+x0
,priv
->rgbsurface
->pitch
);
337 vo_draw_alpha_rgb32(w
,h
,src
,srca
,stride
,((uint8_t *) priv
->rgbsurface
->pixels
)+y0
*priv
->rgbsurface
->pitch
+x0
,priv
->rgbsurface
->pitch
);
347 * Take a null-terminated array of pointers, and find the last element.
349 * params : array == array of which we want to find the last element.
350 * returns : index of last NON-NULL element.
353 static inline int findArrayEnd (SDL_Rect
**array
)
356 while ( array
[i
++] ); /* keep loopin' ... */
358 /* return the index of the last array element */
364 * Open and prepare SDL output.
366 * params : *plugin ==
368 * returns : 0 on success, -1 on failure
371 static int sdl_open (void *plugin
, void *name
)
373 struct sdl_priv_s
*priv
= &sdl_priv
;
374 const SDL_VideoInfo
*vidInfo
= NULL
;
375 /*static int opened = 0;
382 /* other default values */
383 #ifdef SDL_NOHWSURFACE
384 mp_msg(MSGT_VO
,MSGL_V
, "SDL: using software-surface\n");
385 priv
->sdlflags
= SDL_SWSURFACE
|SDL_RESIZABLE
|SDL_ANYFORMAT
;
386 priv
->sdlfullflags
= SDL_SWSURFACE
|SDL_FULLSCREEN
|SDL_ANYFORMAT
;
387 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
389 /*if((strcmp(priv->driver, "dga") == 0) && (priv->mode)) {
390 if( mp_msg_test(MSGT_VO,MSGL_V) ) {
391 printf("SDL: using software-surface\n"); }
392 priv->sdlflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
393 priv->sdlfullflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
396 mp_msg(MSGT_VO
,MSGL_V
, "SDL: using hardware-surface\n");
397 priv
->sdlflags
= SDL_HWSURFACE
|SDL_RESIZABLE
/*|SDL_ANYFORMAT*/;
398 priv
->sdlfullflags
= SDL_HWSURFACE
|SDL_FULLSCREEN
/*|SDL_ANYFORMAT*/;
399 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
403 #if !defined( __AMIGAOS4__ ) && !defined( __APPLE__ )
404 priv
->sdlfullflags
|= SDL_DOUBLEBUF
;
405 if (vo_doublebuffering
)
406 priv
->sdlflags
|= SDL_DOUBLEBUF
;
409 /* Setup Keyrepeats (500/30 are defaults) */
410 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, 100 /*SDL_DEFAULT_REPEAT_INTERVAL*/);
412 /* get information about the graphics adapter */
413 vidInfo
= SDL_GetVideoInfo ();
415 /* collect all fullscreen & hardware modes available */
416 if (!(priv
->fullmodes
= SDL_ListModes (vidInfo
->vfmt
, priv
->sdlfullflags
))) {
418 /* non hardware accelerated fullscreen modes */
419 priv
->sdlfullflags
&= ~SDL_HWSURFACE
;
420 priv
->fullmodes
= SDL_ListModes (vidInfo
->vfmt
, priv
->sdlfullflags
);
423 /* test for normal resizeable & windowed hardware accellerated surfaces */
424 if (!SDL_ListModes (vidInfo
->vfmt
, priv
->sdlflags
)) {
426 /* test for NON hardware accelerated resizeable surfaces - poor you.
427 * That's all we have. If this fails there's nothing left.
428 * Theoretically there could be Fullscreenmodes left - we ignore this for now.
430 priv
->sdlflags
&= ~SDL_HWSURFACE
;
431 if ((!SDL_ListModes (vidInfo
->vfmt
, priv
->sdlflags
)) && (!priv
->fullmodes
)) {
432 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_SDL_CouldntGetAnyAcceptableSDLModeForOutput
);
438 /* YUV overlays need at least 16-bit color depth, but the
439 * display might less. The SDL AAlib target says it can only do
440 * 8-bits, for example. So, if the display is less than 16-bits,
441 * we'll force the BPP to 16, and pray that SDL can emulate for us.
443 priv
->bpp
= vidInfo
->vfmt
->BitsPerPixel
;
444 if (priv
->mode
== YUV
&& priv
->bpp
< 16) {
446 mp_msg(MSGT_VO
,MSGL_V
, "SDL: Your SDL display target wants to be at a color "
447 "depth of (%d), but we need it to be at least 16 "
448 "bits, so we need to emulate 16-bit color. This is "
449 "going to slow things down; you might want to "
450 "increase your display's color depth, if possible.\n",
456 /* We don't want those in our event queue.
457 * We use SDL_KEYUP cause SDL_KEYDOWN seems to cause problems
458 * with keys need to be pressed twice, to be recognized.
461 SDL_EventState(SDL_ACTIVEEVENT
, SDL_IGNORE
);
462 SDL_EventState(SDL_MOUSEMOTION
, SDL_IGNORE
);
463 // SDL_EventState(SDL_QUIT, SDL_IGNORE);
464 SDL_EventState(SDL_SYSWMEVENT
, SDL_IGNORE
);
465 SDL_EventState(SDL_USEREVENT
, SDL_IGNORE
);
474 * Close SDL, Cleanups, Free Memory
477 * returns : non-zero on success, zero on error.
480 static int sdl_close (void)
482 struct sdl_priv_s
*priv
= &sdl_priv
;
487 /* Cleanup YUV Overlay structure */
489 SDL_FreeYUVOverlay(priv
->overlay
);
493 /* Free RGB Surface */
494 if (priv
->rgbsurface
) {
495 SDL_FreeSurface(priv
->rgbsurface
);
496 priv
->rgbsurface
=NULL
;
499 /* Free our blitting surface */
501 SDL_FreeSurface(priv
->surface
);
505 /* DON'T attempt to free the fullscreen modes array. SDL_Quit* does this for us */
511 * Do aspect ratio calculations
513 * params : srcw == sourcewidth
514 * srch == sourceheight
515 * dstw == destinationwidth
516 * dsth == destinationheight
518 * returns : SDL_Rect structure with new x and y, w and h
522 static SDL_Rect
aspect(int srcw
, int srch
, int dstw
, int dsth
) {
524 mp_msg(MSGT_VO
,MSGL_V
, "SDL Aspect-Destinationres: %ix%i (x: %i, y: %i)\n", newres
.w
, newres
.h
, newres
.x
, newres
.y
);
525 newres
.h
= ((float)dstw
/ (float)srcw
* (float)srch
) * ((float)dsth
/((float)dstw
/(MONITOR_ASPECT
)));
526 if(newres
.h
> dsth
) {
527 newres
.w
= ((float)dsth
/ (float)newres
.h
) * dstw
;
529 newres
.x
= (dstw
- newres
.w
) / 2;
535 newres
.y
= (dsth
- newres
.h
) / 2;
538 mp_msg(MSGT_VO
,MSGL_V
, "SDL Mode: %d: %d x %d\n", i
, priv
->fullmodes
[i
]->w
, priv
->fullmodes
[i
]->h
);
545 * Sets the specified fullscreen mode.
547 * params : mode == index of the desired fullscreen mode
548 * returns : doesn't return
552 static void set_fullmode (int mode
)
554 struct sdl_priv_s
*priv
= &sdl_priv
;
555 SDL_Surface
*newsurface
= NULL
;
556 int haspect
, waspect
= 0;
558 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
560 mode
= priv
->fullmode
= findArrayEnd(priv
->fullmodes
) - 1;
562 /* Calculate proper aspect ratio for fullscreen
563 * Height smaller than expected: add horizontal black bars (haspect)*/
564 haspect
= (priv
->width
* (float) ((float) priv
->fullmodes
[mode
]->h
/ (float) priv
->fullmodes
[mode
]->w
) - priv
->height
) * (float) ((float) priv
->fullmodes
[mode
]->w
/ (float) priv
->width
);
565 /* Height bigger than expected: add vertical black bars (waspect)*/
567 haspect
= 0; /* set haspect to zero because image will be scaled horizontal instead of vertical */
568 waspect
= priv
->fullmodes
[mode
]->w
- ((float) ((float) priv
->fullmodes
[mode
]->h
/ (float) priv
->height
) * (float) priv
->width
);
570 // printf ("W-Aspect: %i H-Aspect: %i\n", waspect, haspect);
572 /* change to given fullscreen mode and hide the mouse cursor */
573 newsurface
= SDL_SetVideoMode(priv
->fullmodes
[mode
]->w
- waspect
, priv
->fullmodes
[mode
]->h
- haspect
, priv
->bpp
, priv
->sdlfullflags
);
575 /* if we were successful hide the mouse cursor and save the mode */
578 SDL_FreeSurface(priv
->surface
);
579 priv
->surface
= newsurface
;
585 /* Set video mode. Not fullscreen */
586 static void set_video_mode(int width
, int height
, int bpp
, uint32_t sdlflags
)
588 struct sdl_priv_s
*priv
= &sdl_priv
;
589 SDL_Surface
* newsurface
;
592 SDL_FreeSurface(priv
->rgbsurface
);
593 else if(priv
->overlay
)
594 SDL_FreeYUVOverlay(priv
->overlay
);
596 priv
->rgbsurface
= NULL
;
597 priv
->overlay
= NULL
;
599 newsurface
= SDL_SetVideoMode(width
, height
, bpp
, sdlflags
);
603 /* priv->surface will be NULL the first time this function is called. */
605 SDL_FreeSurface(priv
->surface
);
607 priv
->surface
= newsurface
;
608 priv
->dstwidth
= width
;
609 priv
->dstheight
= height
;
614 mp_msg(MSGT_VO
,MSGL_WARN
, "set_video_mode: SDL_SetVideoMode failed: %s\n", SDL_GetError());
617 static void set_fullmode (int mode
) {
618 struct sdl_priv_s
*priv
= &sdl_priv
;
619 SDL_Surface
*newsurface
= NULL
;
620 int screen_surface_w
, screen_surface_h
;
623 SDL_FreeSurface(priv
->rgbsurface
);
624 else if(priv
->overlay
)
625 SDL_FreeYUVOverlay(priv
->overlay
);
627 priv
->rgbsurface
= NULL
;
628 priv
->overlay
= NULL
;
630 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
631 /* But select a mode where the full video enter */
632 if(priv
->X
&& priv
->fulltype
& VOFLAG_FULLSCREEN
) {
633 screen_surface_w
= priv
->XWidth
;
634 screen_surface_h
= priv
->XHeight
;
638 mode
= 0; // Default to the biggest mode avaible
639 if ( mp_msg_test(MSGT_VO
,MSGL_V
) ) for(i
=0;priv
->fullmodes
[i
];++i
)
640 mp_msg(MSGT_VO
,MSGL_V
, "SDL Mode: %d: %d x %d\n", i
, priv
->fullmodes
[i
]->w
, priv
->fullmodes
[i
]->h
);
641 for(i
= findArrayEnd(priv
->fullmodes
) - 1; i
>=0; i
--) {
642 if( (priv
->fullmodes
[i
]->w
>= priv
->dstwidth
) &&
643 (priv
->fullmodes
[i
]->h
>= priv
->dstheight
) ) {
645 for (j
= findArrayEnd(priv
->fullmodes
) - 1; j
>=0; j
--) {
646 if (priv
->fullmodes
[j
]->w
> priv
->fullmodes
[imax
]->w
647 && priv
->fullmodes
[j
]->h
== priv
->fullmodes
[imax
]->h
)
654 mp_msg(MSGT_VO
,MSGL_V
, "SET SDL Mode: %d: %d x %d\n", mode
, priv
->fullmodes
[mode
]->w
, priv
->fullmodes
[mode
]->h
);
655 priv
->fullmode
= mode
;
656 screen_surface_h
= priv
->fullmodes
[mode
]->h
;
657 screen_surface_w
= priv
->fullmodes
[mode
]->w
;
660 screen_surface_h
= priv
->fullmodes
[mode
]->h
;
661 screen_surface_w
= priv
->fullmodes
[mode
]->w
;
664 aspect_save_screenres(screen_surface_w
, screen_surface_h
);
666 /* calculate new video size/aspect */
667 if(priv
->mode
== YUV
) {
668 if(priv
->fulltype
&VOFLAG_FULLSCREEN
)
669 aspect_save_screenres(priv
->XWidth
, priv
->XHeight
);
671 aspect(&priv
->dstwidth
, &priv
->dstheight
, A_ZOOM
);
674 /* try to change to given fullscreenmode */
675 newsurface
= SDL_SetVideoMode(priv
->dstwidth
, screen_surface_h
, priv
->bpp
,
679 * In Mac OS X (and possibly others?) SDL_SetVideoMode() appears to
680 * destroy the datastructure previously retrived, so we need to
681 * re-assign it. The comment in sdl_close() seems to imply that we
682 * should not free() anything.
686 const SDL_VideoInfo
*vidInfo
= NULL
;
687 vidInfo
= SDL_GetVideoInfo ();
689 /* collect all fullscreen & hardware modes available */
690 if (!(priv
->fullmodes
= SDL_ListModes (vidInfo
->vfmt
, priv
->sdlfullflags
))) {
692 /* non hardware accelerated fullscreen modes */
693 priv
->sdlfullflags
&= ~SDL_HWSURFACE
;
694 priv
->fullmodes
= SDL_ListModes (vidInfo
->vfmt
, priv
->sdlfullflags
);
701 /* if creation of new surface was successful, save it and hide mouse cursor */
704 SDL_FreeSurface(priv
->surface
);
705 priv
->surface
= newsurface
;
707 SDL_SRF_LOCK(priv
->surface
, -1)
708 SDL_FillRect(priv
->surface
, NULL
, 0);
709 SDL_SRF_UNLOCK(priv
->surface
)
713 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_SDL_SetVideoModeFailedFull
, SDL_GetError());
718 * Initialize an SDL surface and an SDL YUV overlay.
720 * params : width == width of video we'll be displaying.
721 * height == height of video we'll be displaying.
722 * fullscreen == want to be fullscreen?
723 * title == Title for window titlebar.
724 * returns : non-zero on success, zero on error.
728 config(uint32_t width
, uint32_t height
, uint32_t d_width
, uint32_t d_height
, uint32_t flags
, char *title
, uint32_t format
)
729 //static int sdl_setup (int width, int height)
731 struct sdl_priv_s
*priv
= &sdl_priv
;
735 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_SDL_MappingI420ToIYUV
);
736 format
= SDL_IYUV_OVERLAY
;
757 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_SDL_UnsupportedImageFormat
,format
);
761 if ( vo_config_count
) sdl_close();
763 mp_msg(MSGT_VO
,MSGL_V
, "SDL: Using 0x%X (%s) image format\n", format
, vo_format_name(format
));
765 if(priv
->mode
!= YUV
) {
766 priv
->sdlflags
|= SDL_ANYFORMAT
;
767 priv
->sdlfullflags
|= SDL_ANYFORMAT
;
770 /* SDL can only scale YUV data */
771 if(priv
->mode
== RGB
|| priv
->mode
== BGR
) {
776 aspect_save_orig(width
,height
);
777 aspect_save_prescale(d_width
? d_width
: width
, d_height
? d_height
: height
);
779 /* Save the original Image size */
781 priv
->height
= height
;
782 priv
->dstwidth
= d_width
? d_width
: width
;
783 priv
->dstheight
= d_height
? d_height
: height
;
785 priv
->format
= format
;
787 if (sdl_open(NULL
, NULL
) != 0)
790 /* Set output window title */
791 SDL_WM_SetCaption (".: MPlayer : F = Fullscreen/Windowed : C = Cycle Fullscreen Resolutions :.", title
);
792 //SDL_WM_SetCaption (title, title);
795 aspect_save_screenres(priv
->XWidth
,priv
->XHeight
);
796 aspect(&priv
->dstwidth
,&priv
->dstheight
,A_NOZOOM
);
799 priv
->windowsize
.w
= priv
->dstwidth
;
800 priv
->windowsize
.h
= priv
->dstheight
;
802 /* bit 0 (0x01) means fullscreen (-fs)
803 * bit 1 (0x02) means mode switching (-vm)
804 * bit 2 (0x04) enables software scaling (-zoom)
805 * bit 3 (0x08) enables flipping (-flip)
807 // printf("SDL: flags are set to: %i\n", flags);
808 // printf("SDL: Width: %i Height: %i D_Width %i D_Height: %i\n", width, height, d_width, d_height);
809 if(flags
&VOFLAG_FLIPPING
) {
810 mp_msg(MSGT_VO
,MSGL_V
, "SDL: using flipped video (only with RGB/BGR/packed YUV)\n");
813 if(flags
&VOFLAG_FULLSCREEN
) {
814 mp_msg(MSGT_VO
,MSGL_V
, "SDL: setting zoomed fullscreen without modeswitching\n");
815 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_SDL_InfoPleaseUseVmOrZoom
);
816 priv
->fulltype
= VOFLAG_FULLSCREEN
;
817 set_fullmode(priv
->fullmode
);
818 /*if((priv->surface = SDL_SetVideoMode (d_width, d_height, priv->bpp, priv->sdlfullflags)))
821 if(flags
&VOFLAG_MODESWITCHING
) {
822 mp_msg(MSGT_VO
,MSGL_V
, "SDL: setting zoomed fullscreen with modeswitching\n");
823 priv
->fulltype
= VOFLAG_MODESWITCHING
;
824 set_fullmode(priv
->fullmode
);
825 /*if((priv->surface = SDL_SetVideoMode (d_width ? d_width : width, d_height ? d_height : height, priv->bpp, priv->sdlfullflags)))
828 if(flags
&VOFLAG_SWSCALE
) {
829 mp_msg(MSGT_VO
,MSGL_V
, "SDL: setting zoomed fullscreen with modeswitching\n");
830 priv
->fulltype
= VOFLAG_SWSCALE
;
831 set_fullmode(priv
->fullmode
);
834 if((strcmp(priv
->driver
, "x11") == 0)
835 ||(strcmp(priv
->driver
, "windib") == 0)
836 ||(strcmp(priv
->driver
, "directx") == 0)
837 ||(strcmp(priv
->driver
, "Quartz") == 0)
838 ||(strcmp(priv
->driver
, "cgx") == 0)
839 ||(strcmp(priv
->driver
, "os4video") == 0)
840 ||((strcmp(priv
->driver
, "aalib") == 0) && priv
->X
)){
841 mp_msg(MSGT_VO
,MSGL_V
, "SDL: setting windowed mode\n");
842 set_video_mode(priv
->dstwidth
, priv
->dstheight
, priv
->bpp
, priv
->sdlflags
);
845 mp_msg(MSGT_VO
,MSGL_V
, "SDL: setting zoomed fullscreen with modeswitching\n");
846 priv
->fulltype
= VOFLAG_SWSCALE
;
847 set_fullmode(priv
->fullmode
);
851 if(!priv
->surface
) { // cannot SetVideoMode
852 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_SDL_FailedToSetVideoMode
, SDL_GetError());
859 /* Free priv->rgbsurface or priv->overlay if they are != NULL.
860 * Setup priv->rgbsurface or priv->overlay depending on source format.
861 * The size of the created surface or overlay depends on the size of
862 * priv->surface, priv->width, priv->height, priv->dstwidth and priv->dstheight.
864 static int setup_surfaces(void)
866 struct sdl_priv_s
*priv
= &sdl_priv
;
867 float v_scale
= ((float) priv
->dstheight
) / priv
->height
;
868 int surfwidth
, surfheight
;
870 surfwidth
= priv
->width
;
871 surfheight
= priv
->height
+ (priv
->surface
->h
- priv
->dstheight
) / v_scale
;
873 /* Place the image in the middle of the screen */
874 priv
->y
= (surfheight
- priv
->height
) / 2;
875 priv
->y_screen_top
= priv
->y
* v_scale
;
876 priv
->y_screen_bottom
= priv
->y_screen_top
+ priv
->dstheight
;
878 priv
->dirty_off_frame
[0].x
= -1;
879 priv
->dirty_off_frame
[0].y
= -1;
880 priv
->dirty_off_frame
[1].x
= -1;
881 priv
->dirty_off_frame
[1].y
= -1;
883 /* Make sure the entire screen is updated */
887 SDL_FreeSurface(priv
->rgbsurface
);
888 else if(priv
->overlay
)
889 SDL_FreeYUVOverlay(priv
->overlay
);
891 priv
->rgbsurface
= NULL
;
892 priv
->overlay
= NULL
;
894 if(priv
->mode
!= YUV
&& (priv
->format
&0xFF) == priv
->bpp
) {
895 if(strcmp(priv
->driver
, "x11") == 0) {
897 priv
->framePlaneRGB
= priv
->width
* priv
->height
* priv
->surface
->format
->BytesPerPixel
;
898 priv
->stridePlaneRGB
= priv
->width
* priv
->surface
->format
->BytesPerPixel
;
899 erase_rectangle(0, 0, priv
->surface
->w
, priv
->surface
->h
);
904 switch(priv
->format
) {
905 /* Initialize and create the RGB Surface used for video out in BGR/RGB mode */
906 //SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
907 // SDL_SWSURFACE,SDL_HWSURFACE,SDL_SRCCOLORKEY, priv->flags? guess: exchange Rmask and Bmask for BGR<->RGB
908 // 32 bit: a:ff000000 r:ff000 g:ff00 b:ff
909 // 24 bit: r:ff0000 g:ff00 b:ff
910 // 16 bit: r:1111100000000000b g:0000011111100000b b:0000000000011111b
911 // 15 bit: r:111110000000000b g:000001111100000b b:000000000011111b
912 // FIXME: colorkey detect based on bpp, FIXME static bpp value, FIXME alpha value correct?
914 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 15, 31, 992, 31744, 0);
917 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 15, 31744, 992, 31, 0);
920 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 16, 31, 2016, 63488, 0);
923 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 16, 63488, 2016, 31, 0);
926 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 24, 0x0000FF, 0x00FF00, 0xFF0000, 0);
929 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 24, 0xFF0000, 0x00FF00, 0x0000FF, 0);
932 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0/*0xFF000000*/);
935 priv
->rgbsurface
= SDL_CreateRGBSurface (SDL_SRCCOLORKEY
, surfwidth
, surfheight
, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0/*0xFF000000*/);
938 /* Initialize and create the YUV Overlay used for video out */
939 if (!(priv
->overlay
= SDL_CreateYUVOverlay (surfwidth
, surfheight
, priv
->format
, priv
->surface
))) {
940 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_SDL_CouldntCreateAYUVOverlay
, SDL_GetError());
943 priv
->framePlaneY
= priv
->width
* priv
->height
;
944 priv
->framePlaneUV
= (priv
->width
* priv
->height
) >> 2;
945 priv
->framePlaneYUY
= priv
->width
* priv
->height
* 2;
946 priv
->stridePlaneY
= priv
->width
;
947 priv
->stridePlaneUV
= priv
->width
/2;
948 priv
->stridePlaneYUY
= priv
->width
* 2;
951 if(priv
->mode
!= YUV
) {
952 if(!priv
->rgbsurface
) {
953 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_SDL_CouldntCreateARGBSurface
, SDL_GetError());
959 if((priv
->format
&0xFF) != priv
->bpp
)
960 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_SDL_UsingDepthColorspaceConversion
, priv
->format
&0xFF, priv
->bpp
);
962 priv
->framePlaneRGB
= priv
->width
* priv
->height
* priv
->rgbsurface
->format
->BytesPerPixel
;
963 priv
->stridePlaneRGB
= priv
->width
* priv
->rgbsurface
->format
->BytesPerPixel
;
966 erase_rectangle(0, 0, surfwidth
, surfheight
);
973 * Draw a frame to the SDL YUV overlay.
975 * params : *src[] == the Y, U, and V planes that make up the frame.
976 * returns : non-zero on success, zero on error.
979 //static int sdl_draw_frame (frame_t *frame)
980 static int draw_frame(uint8_t *src
[])
982 struct sdl_priv_s
*priv
= &sdl_priv
;
985 uint8_t *mysrc
= src
[0];
987 switch(priv
->format
){
992 dst
= (uint8_t *) *(priv
->overlay
->pixels
) + priv
->overlay
->pitches
[0]*priv
->y
;
994 mysrc
+=priv
->framePlaneYUY
;
995 for(i
= 0; i
< priv
->height
; i
++) {
996 mysrc
-=priv
->stridePlaneYUY
;
997 fast_memcpy (dst
, mysrc
, priv
->stridePlaneYUY
);
998 dst
+=priv
->overlay
->pitches
[0];
1001 else fast_memcpy (dst
, src
[0], priv
->framePlaneYUY
);
1014 SDL_SRF_LOCK(priv
->surface
, -1)
1015 dst
= (uint8_t *) priv
->surface
->pixels
+ priv
->y
*priv
->surface
->pitch
;
1017 mysrc
+=priv
->framePlaneRGB
;
1018 for(i
= 0; i
< priv
->height
; i
++) {
1019 mysrc
-=priv
->stridePlaneRGB
;
1020 fast_memcpy (dst
, mysrc
, priv
->stridePlaneRGB
);
1021 dst
+= priv
->surface
->pitch
;
1024 else fast_memcpy (dst
, src
[0], priv
->framePlaneRGB
);
1025 SDL_SRF_UNLOCK(priv
->surface
)
1027 SDL_SRF_LOCK(priv
->rgbsurface
, -1)
1028 dst
= (uint8_t *) priv
->rgbsurface
->pixels
+ priv
->y
*priv
->rgbsurface
->pitch
;
1030 mysrc
+=priv
->framePlaneRGB
;
1031 for(i
= 0; i
< priv
->height
; i
++) {
1032 mysrc
-=priv
->stridePlaneRGB
;
1033 fast_memcpy (dst
, mysrc
, priv
->stridePlaneRGB
);
1034 dst
+= priv
->rgbsurface
->pitch
;
1037 else fast_memcpy (dst
, src
[0], priv
->framePlaneRGB
);
1038 SDL_SRF_UNLOCK(priv
->rgbsurface
)
1049 * Draw a slice (16 rows of image) to the SDL YUV overlay.
1051 * params : *src[] == the Y, U, and V planes that make up the slice.
1052 * returns : non-zero on error, zero on success.
1055 //static uint32_t draw_slice(uint8_t *src[], uint32_t slice_num)
1056 static int draw_slice(uint8_t *image
[], int stride
[], int w
,int h
,int x
,int y
)
1058 struct sdl_priv_s
*priv
= &sdl_priv
;
1065 dst
= priv
->overlay
->pixels
[0] + priv
->overlay
->pitches
[0]*y
+ x
;
1066 memcpy_pic(dst
, image
[0], w
, h
, priv
->overlay
->pitches
[0], stride
[0]);
1067 x
/=2;y
/=2;w
/=2;h
/=2;
1069 switch(priv
->format
) {
1071 dst
= priv
->overlay
->pixels
[2] + priv
->overlay
->pitches
[2]*y
+ x
;
1072 memcpy_pic(dst
, image
[1], w
, h
, priv
->overlay
->pitches
[2], stride
[1]);
1074 dst
= priv
->overlay
->pixels
[1] + priv
->overlay
->pitches
[1]*y
+ x
;
1075 memcpy_pic(dst
, image
[2], w
, h
, priv
->overlay
->pitches
[1], stride
[2]);
1080 dst
= priv
->overlay
->pixels
[1] + priv
->overlay
->pitches
[1]*y
+ x
;
1081 memcpy_pic(dst
, image
[1], w
, h
, priv
->overlay
->pitches
[1], stride
[1]);
1083 dst
= priv
->overlay
->pixels
[2] + priv
->overlay
->pitches
[2]*y
+ x
;
1084 memcpy_pic(dst
, image
[2], w
, h
, priv
->overlay
->pitches
[2], stride
[2]);
1088 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_SDL_UnsupportedImageFormatInDrawslice
);
1099 * Checks for SDL keypress and window resize events
1102 * returns : doesn't return
1105 #include "osdep/keycodes.h"
1107 #define shift_key (event.key.keysym.mod==(KMOD_LSHIFT||KMOD_RSHIFT))
1108 static void check_events (void)
1110 struct sdl_priv_s
*priv
= &sdl_priv
;
1112 SDLKey keypressed
= SDLK_UNKNOWN
;
1114 /* Poll the waiting SDL Events */
1115 while ( SDL_PollEvent(&event
) ) {
1116 switch (event
.type
) {
1118 /* capture window resize events */
1119 case SDL_VIDEORESIZE
:
1121 set_video_mode(event
.resize
.w
, event
.resize
.h
, priv
->bpp
, priv
->sdlflags
);
1123 /* save video extents, to restore them after going fullscreen */
1124 //if(!(priv->surface->flags & SDL_FULLSCREEN)) {
1125 priv
->windowsize
.w
= priv
->surface
->w
;
1126 priv
->windowsize
.h
= priv
->surface
->h
;
1128 mp_msg(MSGT_VO
,MSGL_DBG3
, "SDL: Window resize\n");
1131 case SDL_MOUSEBUTTONDOWN
:
1132 if(vo_nomouse_input
)
1134 mplayer_put_key((MOUSE_BTN0
+event
.button
.button
-1) | MP_KEY_DOWN
);
1137 case SDL_MOUSEBUTTONUP
:
1138 if(vo_nomouse_input
)
1140 mplayer_put_key(MOUSE_BTN0
+event
.button
.button
-1);
1143 /* graphics mode selection shortcuts */
1146 switch(event
.key
.keysym
.sym
) {
1147 case SDLK_UP
: mplayer_put_key(KEY_UP
); break;
1148 case SDLK_DOWN
: mplayer_put_key(KEY_DOWN
); break;
1149 case SDLK_LEFT
: mplayer_put_key(KEY_LEFT
); break;
1150 case SDLK_RIGHT
: mplayer_put_key(KEY_RIGHT
); break;
1151 case SDLK_LESS
: mplayer_put_key(shift_key
?'>':'<'); break;
1152 case SDLK_GREATER
: mplayer_put_key('>'); break;
1154 case SDLK_KP_MULTIPLY
:
1156 case SDLK_KP_DIVIDE
:
1164 keypressed
= event
.key
.keysym
.sym
;
1165 mp_msg(MSGT_VO
,MSGL_DBG2
, "SDL: Key pressed: '%i'\n", keypressed
);
1167 /* c key pressed. c cycles through available fullscreenmodes, if we have some */
1168 if ( ((keypressed
== SDLK_c
)) && (priv
->fullmodes
) ) {
1169 /* select next fullscreen mode */
1171 if (priv
->fullmode
> (findArrayEnd(priv
->fullmodes
) - 1)) priv
->fullmode
= 0;
1172 set_fullmode(priv
->fullmode
);
1174 mp_msg(MSGT_VO
,MSGL_DBG2
, "SDL: Set next available fullscreen mode.\n");
1177 else if ( keypressed
== SDLK_n
) {
1179 aspect(&priv
->dstwidth
, &priv
->dstheight
,A_NOZOOM
);
1181 if (priv
->surface
->w
!= priv
->dstwidth
|| priv
->surface
->h
!= priv
->dstheight
) {
1182 set_video_mode(priv
->dstwidth
, priv
->dstheight
, priv
->bpp
, priv
->sdlflags
);
1183 priv
->windowsize
.w
= priv
->surface
->w
;
1184 priv
->windowsize
.h
= priv
->surface
->h
;
1185 mp_msg(MSGT_VO
,MSGL_DBG2
, "SDL: Normal size\n");
1187 if (priv
->surface
->w
!= priv
->dstwidth
* 2 || priv
->surface
->h
!= priv
->dstheight
* 2) {
1188 set_video_mode(priv
->dstwidth
* 2, priv
->dstheight
* 2, priv
->bpp
, priv
->sdlflags
);
1189 priv
->windowsize
.w
= priv
->surface
->w
;
1190 priv
->windowsize
.h
= priv
->surface
->h
;
1191 mp_msg(MSGT_VO
,MSGL_DBG2
, "SDL: Double size\n");
1195 else switch(keypressed
){
1196 case SDLK_RETURN
: mplayer_put_key(KEY_ENTER
);break;
1197 case SDLK_ESCAPE
: mplayer_put_key(KEY_ESC
);break;
1198 case SDLK_q
: mplayer_put_key('q');break;
1199 case SDLK_F1
: mplayer_put_key(KEY_F
+1);break;
1200 case SDLK_F2
: mplayer_put_key(KEY_F
+2);break;
1201 case SDLK_F3
: mplayer_put_key(KEY_F
+3);break;
1202 case SDLK_F4
: mplayer_put_key(KEY_F
+4);break;
1203 case SDLK_F5
: mplayer_put_key(KEY_F
+5);break;
1204 case SDLK_F6
: mplayer_put_key(KEY_F
+6);break;
1205 case SDLK_F7
: mplayer_put_key(KEY_F
+7);break;
1206 case SDLK_F8
: mplayer_put_key(KEY_F
+8);break;
1207 case SDLK_F9
: mplayer_put_key(KEY_F
+9);break;
1208 case SDLK_F10
: mplayer_put_key(KEY_F
+10);break;
1209 case SDLK_F11
: mplayer_put_key(KEY_F
+11);break;
1210 case SDLK_F12
: mplayer_put_key(KEY_F
+12);break;
1211 /*case SDLK_o: mplayer_put_key('o');break;
1212 case SDLK_SPACE: mplayer_put_key(' ');break;
1213 case SDLK_p: mplayer_put_key('p');break;*/
1214 case SDLK_7
: mplayer_put_key(shift_key
?'/':'7');break;
1215 case SDLK_PLUS
: mplayer_put_key(shift_key
?'*':'+');break;
1216 case SDLK_KP_PLUS
: mplayer_put_key('+');break;
1218 case SDLK_KP_MINUS
: mplayer_put_key('-');break;
1219 case SDLK_TAB
: mplayer_put_key('\t');break;
1220 case SDLK_PAGEUP
: mplayer_put_key(KEY_PAGE_UP
);break;
1221 case SDLK_PAGEDOWN
: mplayer_put_key(KEY_PAGE_DOWN
);break;
1228 case SDLK_KP_MULTIPLY
:
1230 case SDLK_KP_DIVIDE
:
1233 case SDLK_UP
: mplayer_put_key(KEY_UP
);break;
1234 case SDLK_DOWN
: mplayer_put_key(KEY_DOWN
);break;
1235 case SDLK_LEFT
: mplayer_put_key(KEY_LEFT
);break;
1236 case SDLK_RIGHT
: mplayer_put_key(KEY_RIGHT
);break;
1237 case SDLK_LESS
: mplayer_put_key(shift_key
?'>':'<'); break;
1238 case SDLK_GREATER
: mplayer_put_key('>'); break;
1240 case SDLK_KP_MULTIPLY
: mplayer_put_key('*'); break;
1242 case SDLK_KP_DIVIDE
: mplayer_put_key('/'); break;
1244 case SDLK_KP0
: mplayer_put_key(KEY_KP0
); break;
1245 case SDLK_KP1
: mplayer_put_key(KEY_KP1
); break;
1246 case SDLK_KP2
: mplayer_put_key(KEY_KP2
); break;
1247 case SDLK_KP3
: mplayer_put_key(KEY_KP3
); break;
1248 case SDLK_KP4
: mplayer_put_key(KEY_KP4
); break;
1249 case SDLK_KP5
: mplayer_put_key(KEY_KP5
); break;
1250 case SDLK_KP6
: mplayer_put_key(KEY_KP6
); break;
1251 case SDLK_KP7
: mplayer_put_key(KEY_KP7
); break;
1252 case SDLK_KP8
: mplayer_put_key(KEY_KP8
); break;
1253 case SDLK_KP9
: mplayer_put_key(KEY_KP9
); break;
1254 case SDLK_KP_PERIOD
: mplayer_put_key(KEY_KPDEC
); break;
1255 case SDLK_KP_ENTER
: mplayer_put_key(KEY_KPENTER
); break;
1257 //printf("got scancode: %d keysym: %d mod: %d %d\n", event.key.keysym.scancode, keypressed, event.key.keysym.mod);
1258 mplayer_put_key(keypressed
);
1262 case SDL_QUIT
: mplayer_put_key(KEY_CLOSE_WIN
);break;
1268 /* Erase (paint it black) the rectangle specified by x, y, w and h in the surface
1269 or overlay which is used for OSD
1271 static void erase_rectangle(int x
, int y
, int w
, int h
)
1273 struct sdl_priv_s
*priv
= &sdl_priv
;
1275 switch(priv
->format
) {
1280 SDL_OVR_LOCK((void) 0)
1283 erase_area_1(x
, w
, h
,
1284 priv
->overlay
->pitches
[0], 0,
1285 priv
->overlay
->pixels
[0] +
1286 priv
->overlay
->pitches
[0]*y
);
1288 /* Erase U and V planes */
1294 erase_area_1(x
, w
, h
,
1295 priv
->overlay
->pitches
[1], 128,
1296 priv
->overlay
->pixels
[1] +
1297 priv
->overlay
->pitches
[1]*y
);
1299 erase_area_1(x
, w
, h
,
1300 priv
->overlay
->pitches
[2], 128,
1301 priv
->overlay
->pixels
[2] +
1302 priv
->overlay
->pitches
[2]*y
);
1310 /* yuy2 and yvyu represent black the same way */
1311 uint8_t yuy2_black
[] = {0, 128, 0, 128};
1313 SDL_OVR_LOCK((void) 0)
1314 erase_area_4(x
*2, w
*2, h
,
1315 priv
->overlay
->pitches
[0],
1316 *((uint32_t*) yuy2_black
),
1317 priv
->overlay
->pixels
[0] +
1318 priv
->overlay
->pitches
[0]*y
);
1325 uint8_t uyvy_black
[] = {128, 0, 128, 0};
1327 SDL_OVR_LOCK((void) 0)
1328 erase_area_4(x
*2, w
*2, h
,
1329 priv
->overlay
->pitches
[0],
1330 *((uint32_t*) uyvy_black
),
1331 priv
->overlay
->pixels
[0] +
1332 priv
->overlay
->pitches
[0]*y
);
1347 rect
.w
= w
; rect
.h
= h
;
1348 rect
.x
= x
; rect
.y
= y
;
1351 SDL_SRF_LOCK(priv
->surface
, (void) 0)
1352 SDL_FillRect(priv
->surface
, &rect
, 0);
1353 SDL_SRF_UNLOCK(priv
->surface
)
1356 SDL_SRF_LOCK(priv
->rgbsurface
, (void) 0)
1357 SDL_FillRect(priv
->rgbsurface
, &rect
, 0);
1358 SDL_SRF_UNLOCK(priv
->rgbsurface
)
1365 static void draw_osd(void)
1366 { struct sdl_priv_s
*priv
= &sdl_priv
;
1368 priv
->osd_has_changed
= vo_osd_changed(0);
1370 if(priv
->osd_has_changed
)
1374 for(i
= 0; i
< 2; i
++) {
1375 if(priv
->dirty_off_frame
[i
].x
< 0 || priv
->dirty_off_frame
[i
].y
< 0)
1378 erase_rectangle(priv
->dirty_off_frame
[i
].x
, priv
->dirty_off_frame
[i
].y
,
1379 priv
->dirty_off_frame
[i
].w
, priv
->dirty_off_frame
[i
].h
);
1381 priv
->dirty_off_frame
[i
].x
= -1;
1382 priv
->dirty_off_frame
[i
].y
= -1;
1386 /* update osd/subtitles */
1387 if(priv
->mode
== YUV
)
1388 vo_draw_text(priv
->overlay
->w
, priv
->overlay
->h
, draw_alpha
);
1391 vo_draw_text(priv
->surface
->w
, priv
->surface
->h
, draw_alpha
);
1393 vo_draw_text(priv
->rgbsurface
->w
, priv
->rgbsurface
->h
, draw_alpha
);
1397 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1398 * are given in bytes. 4 bytes at a time.
1400 static void erase_area_4(int x_start
, int width
, int height
, int pitch
, uint32_t color
, uint8_t* pixels
)
1402 int x_end
= x_start
/4 + width
/4;
1404 uint32_t* data
= (uint32_t*) pixels
;
1409 for(y
= 0; y
< height
; y
++) {
1410 for(x
= x_start
; x
< x_end
; x
++)
1411 data
[y
*pitch
+ x
] = color
;
1415 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1416 * are given in bytes. 1 byte at a time.
1418 static void erase_area_1(int x_start
, int width
, int height
, int pitch
, uint8_t color
, uint8_t* pixels
)
1422 for(y
= 0; y
< height
; y
++) {
1423 memset(&pixels
[y
*pitch
+ x_start
], color
, width
);
1428 * Display the surface we have written our data to
1430 * params : mode == index of the desired fullscreen mode
1431 * returns : doesn't return
1434 static void flip_page (void)
1436 struct sdl_priv_s
*priv
= &sdl_priv
;
1438 switch(priv
->format
) {
1448 /* blit to the RGB surface */
1449 if(SDL_BlitSurface (priv
->rgbsurface
, NULL
, priv
->surface
, NULL
))
1450 mp_msg(MSGT_VO
,MSGL_WARN
, MSGTR_LIBVO_SDL_BlitFailed
, SDL_GetError());
1454 //SDL_UpdateRect(priv->surface, 0, 0, priv->surface->clip_rect.w, priv->surface->clip_rect.h);
1455 if(priv
->osd_has_changed
) {
1456 priv
->osd_has_changed
= 0;
1457 SDL_UpdateRects(priv
->surface
, 1, &priv
->surface
->clip_rect
);
1460 SDL_UpdateRect(priv
->surface
, 0, priv
->y_screen_top
,
1461 priv
->surface
->clip_rect
.w
, priv
->y_screen_bottom
);
1463 /* check if we have a double buffered surface and flip() if we do. */
1464 if ( priv
->surface
->flags
& SDL_DOUBLEBUF
)
1465 SDL_Flip(priv
->surface
);
1469 /* blit to the YUV overlay */
1470 SDL_DisplayYUVOverlay (priv
->overlay
, &priv
->surface
->clip_rect
);
1472 /* check if we have a double buffered surface and flip() if we do. */
1473 if ( priv
->surface
->flags
& SDL_DOUBLEBUF
)
1474 SDL_Flip(priv
->surface
);
1476 //SDL_LockYUVOverlay (priv->overlay); // removed because unused!?
1481 query_format(uint32_t format
)
1485 // it seems buggy (not hw accelerated), so just use YV12 instead!
1486 // case IMGFMT_I420:
1487 // case IMGFMT_IYUV:
1491 return VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_OSD
|
1492 VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
;
1501 return VFCAP_CSP_SUPPORTED
| VFCAP_OSD
| VFCAP_FLIP
;
1511 struct sdl_priv_s
*priv
= &sdl_priv
;
1513 mp_msg(MSGT_VO
,MSGL_V
, "SDL: activating XScreensaver/DPMS\n");
1520 if(SDL_WasInit(SDL_INIT_VIDEO
))
1521 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
1523 mp_msg(MSGT_VO
,MSGL_DBG3
, "SDL: Closed Plugin\n");
1527 static int preinit(const char *arg
)
1529 struct sdl_priv_s
*priv
= &sdl_priv
;
1530 char * sdl_driver
= NULL
;
1533 const opt_t subopts
[] = {
1534 {"forcexv", OPT_ARG_BOOL
, &sdl_forcexv
, NULL
},
1535 {"hwaccel", OPT_ARG_BOOL
, &sdl_hwaccel
, NULL
},
1536 {"driver", OPT_ARG_MSTRZ
, &sdl_driver
, NULL
},
1537 {NULL
, 0, NULL
, NULL
}
1543 if (subopt_parse(arg
, subopts
) != 0) return -1;
1545 priv
->rgbsurface
= NULL
;
1546 priv
->overlay
= NULL
;
1547 priv
->surface
= NULL
;
1549 mp_msg(MSGT_VO
,MSGL_DBG3
, "SDL: Opening Plugin\n");
1552 setenv("SDL_VIDEODRIVER", sdl_driver
, 1);
1556 /* does the user want SDL to try and force Xv */
1557 if(sdl_forcexv
) setenv("SDL_VIDEO_X11_NODIRECTCOLOR", "1", 1);
1558 else setenv("SDL_VIDEO_X11_NODIRECTCOLOR", "0", 1);
1560 /* does the user want to disable Xv and use software scaling instead */
1561 if(sdl_hwaccel
) setenv("SDL_VIDEO_YUV_HWACCEL", "1", 1);
1562 else setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
1564 /* default to no fullscreen mode, we'll set this as soon we have the avail. modes */
1565 priv
->fullmode
= -2;
1567 priv
->fullmodes
= NULL
;
1570 /* initialize the SDL Video system */
1571 if (!SDL_WasInit(SDL_INIT_VIDEO
)) {
1572 if (SDL_Init (SDL_INIT_VIDEO
|SDL_INIT_NOPARACHUTE
)) {
1573 mp_msg(MSGT_VO
,MSGL_ERR
, MSGTR_LIBVO_SDL_InitializationFailed
, SDL_GetError());
1579 SDL_VideoDriverName(priv
->driver
, 8);
1580 mp_msg(MSGT_VO
,MSGL_INFO
, MSGTR_LIBVO_SDL_UsingDriver
, priv
->driver
);
1585 mp_msg(MSGT_VO
,MSGL_V
, "SDL: deactivating XScreensaver/DPMS\n");
1586 priv
->XWidth
= vo_screenwidth
;
1587 priv
->XHeight
= vo_screenheight
;
1589 mp_msg(MSGT_VO
,MSGL_V
, "SDL: X11 Resolution %ix%i\n", priv
->XWidth
, priv
->XHeight
);
1596 static uint32_t get_image(mp_image_t
*mpi
)
1598 struct sdl_priv_s
*priv
= &sdl_priv
;
1600 if(priv
->format
!= mpi
->imgfmt
) return VO_FALSE
;
1601 if(mpi
->type
== MP_IMGTYPE_STATIC
|| mpi
->type
== MP_IMGTYPE_TEMP
) {
1602 if(mpi
->flags
&MP_IMGFLAG_PLANAR
) {
1603 mpi
->planes
[0] = priv
->overlay
->pixels
[0] + priv
->y
*priv
->overlay
->pitches
[0];
1604 mpi
->stride
[0] = priv
->overlay
->pitches
[0];
1605 if(mpi
->flags
&MP_IMGFLAG_SWAPPED
) {
1606 mpi
->planes
[1] = priv
->overlay
->pixels
[1] + priv
->y
*priv
->overlay
->pitches
[1]/2;
1607 mpi
->stride
[1] = priv
->overlay
->pitches
[1];
1608 mpi
->planes
[2] = priv
->overlay
->pixels
[2] + priv
->y
*priv
->overlay
->pitches
[2]/2;
1609 mpi
->stride
[2] = priv
->overlay
->pitches
[2];
1611 mpi
->planes
[2] = priv
->overlay
->pixels
[1] + priv
->y
*priv
->overlay
->pitches
[1]/2;
1612 mpi
->stride
[2] = priv
->overlay
->pitches
[1];
1613 mpi
->planes
[1] = priv
->overlay
->pixels
[2] + priv
->y
*priv
->overlay
->pitches
[2]/2;
1614 mpi
->stride
[1] = priv
->overlay
->pitches
[2];
1617 else if(IMGFMT_IS_RGB(priv
->format
) || IMGFMT_IS_BGR(priv
->format
)) {
1619 if(mpi
->type
== MP_IMGTYPE_STATIC
&& (priv
->surface
->flags
& SDL_DOUBLEBUF
))
1622 mpi
->planes
[0] = (uint8_t *)priv
->surface
->pixels
+ priv
->y
*priv
->surface
->pitch
;
1623 mpi
->stride
[0] = priv
->surface
->pitch
;
1626 mpi
->planes
[0] = (uint8_t *)priv
->rgbsurface
->pixels
+ priv
->y
*priv
->rgbsurface
->pitch
;
1627 mpi
->stride
[0] = priv
->rgbsurface
->pitch
;
1631 mpi
->planes
[0] = priv
->overlay
->pixels
[0] + priv
->y
*priv
->overlay
->pitches
[0];
1632 mpi
->stride
[0] = priv
->overlay
->pitches
[0];
1635 mpi
->flags
|=MP_IMGFLAG_DIRECT
;
1642 static int control(uint32_t request
, void *data
, ...)
1644 struct sdl_priv_s
*priv
= &sdl_priv
;
1646 case VOCTRL_GET_IMAGE
:
1647 return get_image(data
);
1648 case VOCTRL_QUERY_FORMAT
:
1649 return query_format(*((uint32_t*)data
));
1650 case VOCTRL_FULLSCREEN
:
1651 if (priv
->surface
->flags
& SDL_FULLSCREEN
) {
1652 set_video_mode(priv
->windowsize
.w
, priv
->windowsize
.h
, priv
->bpp
, priv
->sdlflags
);
1654 mp_msg(MSGT_VO
,MSGL_DBG2
, "SDL: Windowed mode\n");
1655 } else if (priv
->fullmodes
) {
1656 set_fullmode(priv
->fullmode
);
1657 mp_msg(MSGT_VO
,MSGL_DBG2
, "SDL: Set fullscreen mode\n");