cosmetics: av_sub
[mplayer/greg.git] / libvo / vo_sdl.c
blob9715dd98fb1f418e144ea7f5e9136ef9a0ed51b0
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.h"
67 #include "aspect.h"
68 #include "libmpcodecs/vfcap.h"
70 #ifdef CONFIG_X11
71 #include <X11/Xlib.h>
72 #include "x11_common.h"
73 #endif
75 #include "subopt-helper.h"
77 static const vo_info_t info =
79 "SDL YUV/RGB/BGR renderer (SDL v1.1.7+ only!)",
80 "sdl",
81 "Ryan C. Gordon <icculus@lokigames.com>, Felix Buenemann <atmosfear@users.sourceforge.net>",
85 const LIBVO_EXTERN(sdl)
87 #include "sdl_common.h"
88 //#include <SDL/SDL_syswm.h>
91 #ifdef SDL_ENABLE_LOCKS
92 #define SDL_OVR_LOCK(x) if (SDL_LockYUVOverlay (priv->overlay)) { \
93 mp_msg(MSGT_VO,MSGL_V, "SDL: Couldn't lock YUV overlay\n"); \
94 return x; \
96 #define SDL_OVR_UNLOCK SDL_UnlockYUVOverlay (priv->overlay);
98 #define SDL_SRF_LOCK(srf, x) if(SDL_MUSTLOCK(srf)) { \
99 if(SDL_LockSurface (srf)) { \
100 mp_msg(MSGT_VO,MSGL_V, "SDL: Couldn't lock RGB surface\n"); \
101 return x; \
105 #define SDL_SRF_UNLOCK(srf) if(SDL_MUSTLOCK(srf)) \
106 SDL_UnlockSurface (srf);
107 #else
108 #define SDL_OVR_LOCK(x)
109 #define SDL_OVR_UNLOCK
110 #define SDL_SRF_LOCK(srf, x)
111 #define SDL_SRF_UNLOCK(srf)
112 #endif
114 /** Private SDL Data structure **/
116 static struct sdl_priv_s {
118 /* output driver used by sdl */
119 char driver[8];
121 /* SDL display surface */
122 SDL_Surface *surface;
124 /* SDL RGB surface */
125 SDL_Surface *rgbsurface;
127 /* SDL YUV overlay */
128 SDL_Overlay *overlay;
130 /* available fullscreen modes */
131 SDL_Rect **fullmodes;
133 /* surface attributes for fullscreen and windowed mode */
134 Uint32 sdlflags, sdlfullflags;
136 /* save the windowed output extents */
137 SDL_Rect windowsize;
139 /* Bits per Pixel */
140 Uint8 bpp;
142 /* RGB or YUV? */
143 Uint8 mode;
144 #define YUV 0
145 #define RGB 1
146 #define BGR 2
148 /* use direct blitting to surface */
149 int dblit;
151 /* current fullscreen mode, 0 = highest available fullscreen mode */
152 int fullmode;
154 /* YUV ints */
155 int framePlaneY, framePlaneUV, framePlaneYUY;
156 int stridePlaneY, stridePlaneUV, stridePlaneYUY;
158 /* RGB ints */
159 int framePlaneRGB;
160 int stridePlaneRGB;
162 /* Flip image */
163 int flip;
165 /* fullscreen behaviour; see init */
166 int fulltype;
168 /* is X running (0/1) */
169 int X;
171 /* X11 Resolution */
172 int XWidth, XHeight;
174 /* original image dimensions */
175 int width, height;
177 /* destination dimensions */
178 int dstwidth, dstheight;
180 /* Draw image at coordinate y on the SDL surfaces */
181 int y;
183 /* The image is displayed between those y coordinates in priv->surface */
184 int y_screen_top, y_screen_bottom;
186 /* 1 if the OSD has changed otherwise 0 */
187 int osd_has_changed;
189 /* source image format (YUV/RGB/...) */
190 uint32_t format;
192 /* dirty_off_frame[0] contains a bounding box around the osd contents drawn above the image
193 dirty_off_frame[1] is the corresponding thing for OSD contents drawn below the image
195 SDL_Rect dirty_off_frame[2];
196 } sdl_priv;
198 static void erase_area_4(int x_start, int width, int height, int pitch, uint32_t color, uint8_t* pixels);
199 static void erase_area_1(int x_start, int width, int height, int pitch, uint8_t color, uint8_t* pixels);
200 static int setup_surfaces(void);
201 static void set_video_mode(int width, int height, int bpp, uint32_t sdlflags);
202 static void erase_rectangle(int x, int y, int w, int h);
204 /* Expand 'rect' to contain the rectangle specified by x, y, w and h */
205 static void expand_rect(SDL_Rect* rect, int x, int y, int w, int h)
207 if(rect->x < 0 || rect->y < 0) {
208 rect->x = x;
209 rect->y = y;
210 rect->w = w;
211 rect->h = h;
212 return;
215 if(rect->x > x)
216 rect->x = x;
218 if(rect->y > y)
219 rect->y = y;
221 if(rect->x + rect->w < x + w)
222 rect->w = x + w - rect->x;
224 if(rect->y + rect->h < y + h)
225 rect->h = y + h - rect->y;
228 /** libvo Plugin functions **/
231 * draw_alpha is used for osd and subtitle display.
235 static void draw_alpha(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
236 struct sdl_priv_s *priv = &sdl_priv;
238 if(priv->osd_has_changed) {
239 /* OSD did change. Store a bounding box of everything drawn into the OSD */
240 if(priv->y >= y0) {
241 /* Make sure we don't mark part of the frame area dirty */
242 if(h + y0 > priv->y)
243 expand_rect(&priv->dirty_off_frame[0], x0, y0, w, priv->y - y0);
244 else
245 expand_rect(&priv->dirty_off_frame[0], x0, y0, w, h);
247 else if(priv->y + priv->height <= y0 + h) {
248 /* Make sure we don't mark part of the frame area dirty */
249 if(y0 < priv->y + priv->height)
250 expand_rect(&priv->dirty_off_frame[1], x0,
251 priv->y + priv->height,
252 w, h - ((priv->y + priv->height) - y0));
253 else
254 expand_rect(&priv->dirty_off_frame[1], x0, y0, w, h);
257 else { /* OSD contents didn't change only draw parts that was erased by the frame */
258 if(priv->y >= y0) {
259 src = src + (priv->y - y0) * stride;
260 srca = srca + (priv->y - y0) * stride;
261 h -= priv->y - y0;
262 y0 = priv->y;
265 if(priv->y + priv->height <= y0 + h)
266 h = priv->y + priv->height - y0;
268 if(h <= 0)
269 return;
272 switch(priv->format) {
273 case IMGFMT_YV12:
274 case IMGFMT_I420:
275 case IMGFMT_IYUV:
276 vo_draw_alpha_yv12(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
277 break;
278 case IMGFMT_YUY2:
279 case IMGFMT_YVYU:
280 x0 *= 2;
281 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
282 break;
283 case IMGFMT_UYVY:
284 x0 *= 2;
285 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
286 break;
288 default:
289 if(priv->dblit) {
290 x0 *= priv->surface->format->BytesPerPixel;
291 switch(priv->format) {
292 case IMGFMT_RGB15:
293 case IMGFMT_BGR15:
294 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
295 break;
296 case IMGFMT_RGB16:
297 case IMGFMT_BGR16:
298 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
299 break;
300 case IMGFMT_RGB24:
301 case IMGFMT_BGR24:
302 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
303 break;
304 case IMGFMT_RGB32:
305 case IMGFMT_BGR32:
306 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
307 break;
310 else {
311 x0 *= priv->rgbsurface->format->BytesPerPixel;
312 switch(priv->format) {
313 case IMGFMT_RGB15:
314 case IMGFMT_BGR15:
315 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
316 break;
317 case IMGFMT_RGB16:
318 case IMGFMT_BGR16:
319 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
320 break;
321 case IMGFMT_RGB24:
322 case IMGFMT_BGR24:
323 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
324 break;
325 case IMGFMT_RGB32:
326 case IMGFMT_BGR32:
327 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
328 break;
337 * Take a null-terminated array of pointers, and find the last element.
339 * params : array == array of which we want to find the last element.
340 * returns : index of last NON-NULL element.
343 static inline int findArrayEnd (SDL_Rect **array)
345 int i = 0;
346 while ( array[i++] ); /* keep loopin' ... */
348 /* return the index of the last array element */
349 return i - 1;
354 * Open and prepare SDL output.
356 * params : *plugin ==
357 * *name ==
358 * returns : 0 on success, -1 on failure
361 static int sdl_open (void *plugin, void *name)
363 struct sdl_priv_s *priv = &sdl_priv;
364 const SDL_VideoInfo *vidInfo = NULL;
365 /*static int opened = 0;
367 if (opened)
368 return 0;
369 opened = 1;*/
372 /* other default values */
373 #ifdef SDL_NOHWSURFACE
374 mp_msg(MSGT_VO,MSGL_V, "SDL: using software-surface\n");
375 priv->sdlflags = SDL_SWSURFACE|SDL_RESIZABLE|SDL_ANYFORMAT;
376 priv->sdlfullflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ANYFORMAT;
377 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
378 #else
379 /*if((strcmp(priv->driver, "dga") == 0) && (priv->mode)) {
380 if( mp_msg_test(MSGT_VO,MSGL_V) ) {
381 printf("SDL: using software-surface\n"); }
382 priv->sdlflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
383 priv->sdlfullflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
385 else { */
386 mp_msg(MSGT_VO,MSGL_V, "SDL: using hardware-surface\n");
387 priv->sdlflags = SDL_HWSURFACE|SDL_RESIZABLE/*|SDL_ANYFORMAT*/;
388 priv->sdlfullflags = SDL_HWSURFACE|SDL_FULLSCREEN/*|SDL_ANYFORMAT*/;
389 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
391 #endif
393 #if !defined( __AMIGAOS4__ ) && !defined( __APPLE__ )
394 priv->sdlfullflags |= SDL_DOUBLEBUF;
395 if (vo_doublebuffering)
396 priv->sdlflags |= SDL_DOUBLEBUF;
397 #endif
399 /* get information about the graphics adapter */
400 vidInfo = SDL_GetVideoInfo ();
402 /* collect all fullscreen & hardware modes available */
403 if (!(priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags))) {
405 /* non hardware accelerated fullscreen modes */
406 priv->sdlfullflags &= ~SDL_HWSURFACE;
407 priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags);
410 /* test for normal resizeable & windowed hardware accellerated surfaces */
411 if (!SDL_ListModes (vidInfo->vfmt, priv->sdlflags)) {
413 /* test for NON hardware accelerated resizeable surfaces - poor you.
414 * That's all we have. If this fails there's nothing left.
415 * Theoretically there could be Fullscreenmodes left - we ignore this for now.
417 priv->sdlflags &= ~SDL_HWSURFACE;
418 if ((!SDL_ListModes (vidInfo->vfmt, priv->sdlflags)) && (!priv->fullmodes)) {
419 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_SDL] Couldn't get any acceptable SDL Mode for output.\n");
420 return -1;
425 /* YUV overlays need at least 16-bit color depth, but the
426 * display might less. The SDL AAlib target says it can only do
427 * 8-bits, for example. So, if the display is less than 16-bits,
428 * we'll force the BPP to 16, and pray that SDL can emulate for us.
430 priv->bpp = vidInfo->vfmt->BitsPerPixel;
431 if (priv->mode == YUV && priv->bpp < 16) {
433 mp_msg(MSGT_VO,MSGL_V, "SDL: Your SDL display target wants to be at a color "
434 "depth of (%d), but we need it to be at least 16 "
435 "bits, so we need to emulate 16-bit color. This is "
436 "going to slow things down; you might want to "
437 "increase your display's color depth, if possible.\n",
438 priv->bpp);
440 priv->bpp = 16;
443 /* Success! */
444 return 0;
449 * Close SDL, Cleanups, Free Memory
451 * params : *plugin
452 * returns : non-zero on success, zero on error.
455 static int sdl_close (void)
457 struct sdl_priv_s *priv = &sdl_priv;
459 if (priv->fullmode)
460 SDL_ShowCursor(1);
462 /* Cleanup YUV Overlay structure */
463 if (priv->overlay) {
464 SDL_FreeYUVOverlay(priv->overlay);
465 priv->overlay=NULL;
468 /* Free RGB Surface */
469 if (priv->rgbsurface) {
470 SDL_FreeSurface(priv->rgbsurface);
471 priv->rgbsurface=NULL;
474 /* Free our blitting surface */
475 if (priv->surface) {
476 SDL_FreeSurface(priv->surface);
477 priv->surface=NULL;
480 /* DON'T attempt to free the fullscreen modes array. SDL_Quit* does this for us */
482 return 0;
486 * Do aspect ratio calculations
488 * params : srcw == sourcewidth
489 * srch == sourceheight
490 * dstw == destinationwidth
491 * dsth == destinationheight
493 * returns : SDL_Rect structure with new x and y, w and h
496 #if 0
497 static SDL_Rect aspect(int srcw, int srch, int dstw, int dsth) {
498 SDL_Rect newres;
499 mp_msg(MSGT_VO,MSGL_V, "SDL Aspect-Destinationres: %ix%i (x: %i, y: %i)\n", newres.w, newres.h, newres.x, newres.y);
500 newres.h = ((float)dstw / (float)srcw * (float)srch) * ((float)dsth/((float)dstw/(MONITOR_ASPECT)));
501 if(newres.h > dsth) {
502 newres.w = ((float)dsth / (float)newres.h) * dstw;
503 newres.h = dsth;
504 newres.x = (dstw - newres.w) / 2;
505 newres.y = 0;
507 else {
508 newres.w = dstw;
509 newres.x = 0;
510 newres.y = (dsth - newres.h) / 2;
513 mp_msg(MSGT_VO,MSGL_V, "SDL Mode: %d: %d x %d\n", i, priv->fullmodes[i]->w, priv->fullmodes[i]->h);
515 return newres;
517 #endif
520 * Sets the specified fullscreen mode.
522 * params : mode == index of the desired fullscreen mode
523 * returns : doesn't return
526 #if 0
527 static void set_fullmode (int mode)
529 struct sdl_priv_s *priv = &sdl_priv;
530 SDL_Surface *newsurface = NULL;
531 int haspect, waspect = 0;
533 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
534 if (mode < 0)
535 mode = priv->fullmode = findArrayEnd(priv->fullmodes) - 1;
537 /* Calculate proper aspect ratio for fullscreen
538 * Height smaller than expected: add horizontal black bars (haspect)*/
539 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);
540 /* Height bigger than expected: add vertical black bars (waspect)*/
541 if (haspect < 0) {
542 haspect = 0; /* set haspect to zero because image will be scaled horizontal instead of vertical */
543 waspect = priv->fullmodes[mode]->w - ((float) ((float) priv->fullmodes[mode]->h / (float) priv->height) * (float) priv->width);
545 // printf ("W-Aspect: %i H-Aspect: %i\n", waspect, haspect);
547 /* change to given fullscreen mode and hide the mouse cursor */
548 newsurface = SDL_SetVideoMode(priv->fullmodes[mode]->w - waspect, priv->fullmodes[mode]->h - haspect, priv->bpp, priv->sdlfullflags);
550 /* if we were successful hide the mouse cursor and save the mode */
551 if (newsurface) {
552 if (priv->surface)
553 SDL_FreeSurface(priv->surface);
554 priv->surface = newsurface;
555 SDL_ShowCursor(0);
558 #endif
560 /* Set video mode. Not fullscreen */
561 static void set_video_mode(int width, int height, int bpp, uint32_t sdlflags)
563 struct sdl_priv_s *priv = &sdl_priv;
564 SDL_Surface* newsurface;
566 if(priv->rgbsurface)
567 SDL_FreeSurface(priv->rgbsurface);
568 else if(priv->overlay)
569 SDL_FreeYUVOverlay(priv->overlay);
571 priv->rgbsurface = NULL;
572 priv->overlay = NULL;
574 newsurface = SDL_SetVideoMode(width, height, bpp, sdlflags);
576 if(newsurface) {
578 /* priv->surface will be NULL the first time this function is called. */
579 if(priv->surface)
580 SDL_FreeSurface(priv->surface);
582 priv->surface = newsurface;
583 priv->dstwidth = width;
584 priv->dstheight = height;
585 vo_dwidth = width;
586 vo_dheight = height;
588 setup_surfaces();
590 else
591 mp_msg(MSGT_VO,MSGL_WARN, "set_video_mode: SDL_SetVideoMode failed: %s\n", SDL_GetError());
594 static void set_fullmode (int mode) {
595 struct sdl_priv_s *priv = &sdl_priv;
596 SDL_Surface *newsurface = NULL;
597 int screen_surface_w, screen_surface_h;
599 if(priv->rgbsurface)
600 SDL_FreeSurface(priv->rgbsurface);
601 else if(priv->overlay)
602 SDL_FreeYUVOverlay(priv->overlay);
604 priv->rgbsurface = NULL;
605 priv->overlay = NULL;
607 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
608 /* But select a mode where the full video enter */
609 if(priv->X && priv->fulltype & VOFLAG_FULLSCREEN) {
610 screen_surface_w = priv->XWidth;
611 screen_surface_h = priv->XHeight;
613 else if (mode < 0) {
614 int i,j,imax;
615 mode = 0; // Default to the biggest mode avaible
616 if ( mp_msg_test(MSGT_VO,MSGL_V) ) for(i=0;priv->fullmodes[i];++i)
617 mp_msg(MSGT_VO,MSGL_V, "SDL Mode: %d: %d x %d\n", i, priv->fullmodes[i]->w, priv->fullmodes[i]->h);
618 for(i = findArrayEnd(priv->fullmodes) - 1; i >=0; i--) {
619 if( (priv->fullmodes[i]->w >= priv->dstwidth) &&
620 (priv->fullmodes[i]->h >= priv->dstheight) ) {
621 imax = i;
622 for (j = findArrayEnd(priv->fullmodes) - 1; j >=0; j--) {
623 if (priv->fullmodes[j]->w > priv->fullmodes[imax]->w
624 && priv->fullmodes[j]->h == priv->fullmodes[imax]->h)
625 imax = j;
627 mode = imax;
628 break;
631 mp_msg(MSGT_VO,MSGL_V, "SET SDL Mode: %d: %d x %d\n", mode, priv->fullmodes[mode]->w, priv->fullmodes[mode]->h);
632 priv->fullmode = mode;
633 screen_surface_h = priv->fullmodes[mode]->h;
634 screen_surface_w = priv->fullmodes[mode]->w;
636 else {
637 screen_surface_h = priv->fullmodes[mode]->h;
638 screen_surface_w = priv->fullmodes[mode]->w;
641 aspect_save_screenres(screen_surface_w, screen_surface_h);
643 /* calculate new video size/aspect */
644 if(priv->mode == YUV) {
645 if(priv->fulltype&VOFLAG_FULLSCREEN)
646 aspect_save_screenres(priv->XWidth, priv->XHeight);
648 aspect(&priv->dstwidth, &priv->dstheight, A_ZOOM);
651 /* try to change to given fullscreenmode */
652 newsurface = SDL_SetVideoMode(priv->dstwidth, screen_surface_h, priv->bpp,
653 priv->sdlfullflags);
656 * In Mac OS X (and possibly others?) SDL_SetVideoMode() appears to
657 * destroy the datastructure previously retrived, so we need to
658 * re-assign it. The comment in sdl_close() seems to imply that we
659 * should not free() anything.
661 #ifdef __APPLE__
663 const SDL_VideoInfo *vidInfo = NULL;
664 vidInfo = SDL_GetVideoInfo ();
666 /* collect all fullscreen & hardware modes available */
667 if (!(priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags))) {
669 /* non hardware accelerated fullscreen modes */
670 priv->sdlfullflags &= ~SDL_HWSURFACE;
671 priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags);
674 #endif
678 /* if creation of new surface was successful, save it and hide mouse cursor */
679 if(newsurface) {
680 if (priv->surface)
681 SDL_FreeSurface(priv->surface);
682 priv->surface = newsurface;
683 SDL_ShowCursor(0);
684 SDL_SRF_LOCK(priv->surface, -1)
685 SDL_FillRect(priv->surface, NULL, 0);
686 SDL_SRF_UNLOCK(priv->surface)
687 setup_surfaces();
689 else
690 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Set_fullmode: SDL_SetVideoMode failed: %s.\n", SDL_GetError());
695 * Initialize an SDL surface and an SDL YUV overlay.
697 * params : width == width of video we'll be displaying.
698 * height == height of video we'll be displaying.
699 * fullscreen == want to be fullscreen?
700 * title == Title for window titlebar.
701 * returns : non-zero on success, zero on error.
704 static int
705 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
706 //static int sdl_setup (int width, int height)
708 struct sdl_priv_s *priv = &sdl_priv;
710 switch(format){
711 case IMGFMT_I420:
712 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Mapping I420 to IYUV.\n");
713 format = SDL_IYUV_OVERLAY;
714 case IMGFMT_YV12:
715 case IMGFMT_IYUV:
716 case IMGFMT_YUY2:
717 case IMGFMT_UYVY:
718 case IMGFMT_YVYU:
719 priv->mode = YUV;
720 break;
721 case IMGFMT_BGR15:
722 case IMGFMT_BGR16:
723 case IMGFMT_BGR24:
724 case IMGFMT_BGR32:
725 priv->mode = BGR;
726 break;
727 case IMGFMT_RGB15:
728 case IMGFMT_RGB16:
729 case IMGFMT_RGB24:
730 case IMGFMT_RGB32:
731 priv->mode = RGB;
732 break;
733 default:
734 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Unsupported image format (0x%X).\n",format);
735 return -1;
738 if ( vo_config_count ) sdl_close();
740 mp_msg(MSGT_VO,MSGL_V, "SDL: Using 0x%X (%s) image format\n", format, vo_format_name(format));
742 if(priv->mode != YUV) {
743 priv->sdlflags |= SDL_ANYFORMAT;
744 priv->sdlfullflags |= SDL_ANYFORMAT;
747 /* SDL can only scale YUV data */
748 if(priv->mode == RGB || priv->mode == BGR) {
749 d_width = width;
750 d_height = height;
753 aspect_save_orig(width,height);
754 aspect_save_prescale(d_width ? d_width : width, d_height ? d_height : height);
756 /* Save the original Image size */
757 priv->width = width;
758 priv->height = height;
759 priv->dstwidth = d_width ? d_width : width;
760 priv->dstheight = d_height ? d_height : height;
762 priv->format = format;
764 if (sdl_open(NULL, NULL) != 0)
765 return -1;
767 /* Set output window title */
768 SDL_WM_SetCaption (".: MPlayer : F = Fullscreen/Windowed : C = Cycle Fullscreen Resolutions :.", title);
769 //SDL_WM_SetCaption (title, title);
771 if(priv->X) {
772 aspect_save_screenres(priv->XWidth,priv->XHeight);
773 aspect(&priv->dstwidth,&priv->dstheight,A_NOZOOM);
776 priv->windowsize.w = priv->dstwidth;
777 priv->windowsize.h = priv->dstheight;
779 /* bit 0 (0x01) means fullscreen (-fs)
780 * bit 1 (0x02) means mode switching (-vm)
781 * bit 2 (0x04) enables software scaling (-zoom)
782 * bit 3 (0x08) enables flipping (-flip)
784 // printf("SDL: flags are set to: %i\n", flags);
785 // printf("SDL: Width: %i Height: %i D_Width %i D_Height: %i\n", width, height, d_width, d_height);
786 if(flags&VOFLAG_FLIPPING) {
787 mp_msg(MSGT_VO,MSGL_V, "SDL: using flipped video (only with RGB/BGR/packed YUV)\n");
788 priv->flip = 1;
790 if(flags&VOFLAG_FULLSCREEN) {
791 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen without modeswitching\n");
792 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Info - please use -vm or -zoom to switch to the best resolution.\n");
793 priv->fulltype = VOFLAG_FULLSCREEN;
794 set_fullmode(priv->fullmode);
795 /*if((priv->surface = SDL_SetVideoMode (d_width, d_height, priv->bpp, priv->sdlfullflags)))
796 SDL_ShowCursor(0);*/
797 } else
798 if(flags&VOFLAG_MODESWITCHING) {
799 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
800 priv->fulltype = VOFLAG_MODESWITCHING;
801 set_fullmode(priv->fullmode);
802 /*if((priv->surface = SDL_SetVideoMode (d_width ? d_width : width, d_height ? d_height : height, priv->bpp, priv->sdlfullflags)))
803 SDL_ShowCursor(0);*/
804 } else
805 if(flags&VOFLAG_SWSCALE) {
806 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
807 priv->fulltype = VOFLAG_SWSCALE;
808 set_fullmode(priv->fullmode);
810 else {
811 if((strcmp(priv->driver, "x11") == 0)
812 ||(strcmp(priv->driver, "windib") == 0)
813 ||(strcmp(priv->driver, "directx") == 0)
814 ||(strcmp(priv->driver, "Quartz") == 0)
815 ||(strcmp(priv->driver, "cgx") == 0)
816 ||(strcmp(priv->driver, "os4video") == 0)
817 ||((strcmp(priv->driver, "aalib") == 0) && priv->X)){
818 mp_msg(MSGT_VO,MSGL_V, "SDL: setting windowed mode\n");
819 set_video_mode(priv->dstwidth, priv->dstheight, priv->bpp, priv->sdlflags);
821 else {
822 mp_msg(MSGT_VO,MSGL_V, "SDL: setting zoomed fullscreen with modeswitching\n");
823 priv->fulltype = VOFLAG_SWSCALE;
824 set_fullmode(priv->fullmode);
828 if(!priv->surface) { // cannot SetVideoMode
829 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Failed to set video mode: %s.\n", SDL_GetError());
830 return -1;
833 return 0;
836 /* Free priv->rgbsurface or priv->overlay if they are != NULL.
837 * Setup priv->rgbsurface or priv->overlay depending on source format.
838 * The size of the created surface or overlay depends on the size of
839 * priv->surface, priv->width, priv->height, priv->dstwidth and priv->dstheight.
841 static int setup_surfaces(void)
843 struct sdl_priv_s *priv = &sdl_priv;
844 float v_scale = ((float) priv->dstheight) / priv->height;
845 int surfwidth, surfheight;
847 surfwidth = priv->width;
848 surfheight = priv->height + (priv->surface->h - priv->dstheight) / v_scale;
849 surfheight&= ~1;
850 /* Place the image in the middle of the screen */
851 priv->y = (surfheight - priv->height) / 2;
852 priv->y_screen_top = priv->y * v_scale;
853 priv->y_screen_bottom = priv->y_screen_top + priv->dstheight;
855 priv->dirty_off_frame[0].x = -1;
856 priv->dirty_off_frame[0].y = -1;
857 priv->dirty_off_frame[1].x = -1;
858 priv->dirty_off_frame[1].y = -1;
860 /* Make sure the entire screen is updated */
861 vo_osd_changed(1);
863 if(priv->rgbsurface)
864 SDL_FreeSurface(priv->rgbsurface);
865 else if(priv->overlay)
866 SDL_FreeYUVOverlay(priv->overlay);
868 priv->rgbsurface = NULL;
869 priv->overlay = NULL;
871 if(priv->mode != YUV && (priv->format&0xFF) == priv->bpp) {
872 if(strcmp(priv->driver, "x11") == 0) {
873 priv->dblit = 1;
874 priv->framePlaneRGB = priv->width * priv->height * priv->surface->format->BytesPerPixel;
875 priv->stridePlaneRGB = priv->width * priv->surface->format->BytesPerPixel;
876 erase_rectangle(0, 0, priv->surface->w, priv->surface->h);
877 return 0;
881 switch(priv->format) {
882 /* Initialize and create the RGB Surface used for video out in BGR/RGB mode */
883 //SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
884 // SDL_SWSURFACE,SDL_HWSURFACE,SDL_SRCCOLORKEY, priv->flags? guess: exchange Rmask and Bmask for BGR<->RGB
885 // 32 bit: a:ff000000 r:ff000 g:ff00 b:ff
886 // 24 bit: r:ff0000 g:ff00 b:ff
887 // 16 bit: r:1111100000000000b g:0000011111100000b b:0000000000011111b
888 // 15 bit: r:111110000000000b g:000001111100000b b:000000000011111b
889 // FIXME: colorkey detect based on bpp, FIXME static bpp value, FIXME alpha value correct?
890 case IMGFMT_RGB15:
891 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 15, 31, 992, 31744, 0);
892 break;
893 case IMGFMT_BGR15:
894 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 15, 31744, 992, 31, 0);
895 break;
896 case IMGFMT_RGB16:
897 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 16, 31, 2016, 63488, 0);
898 break;
899 case IMGFMT_BGR16:
900 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 16, 63488, 2016, 31, 0);
901 break;
902 case IMGFMT_RGB24:
903 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 24, 0x0000FF, 0x00FF00, 0xFF0000, 0);
904 break;
905 case IMGFMT_BGR24:
906 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 24, 0xFF0000, 0x00FF00, 0x0000FF, 0);
907 break;
908 case IMGFMT_RGB32:
909 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0/*0xFF000000*/);
910 break;
911 case IMGFMT_BGR32:
912 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0/*0xFF000000*/);
913 break;
914 default:
915 /* Initialize and create the YUV Overlay used for video out */
916 if (!(priv->overlay = SDL_CreateYUVOverlay (surfwidth, surfheight, priv->format, priv->surface))) {
917 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Couldn't create a YUV overlay: %s.\n", SDL_GetError());
918 return -1;
920 priv->framePlaneY = priv->width * priv->height;
921 priv->framePlaneUV = (priv->width * priv->height) >> 2;
922 priv->framePlaneYUY = priv->width * priv->height * 2;
923 priv->stridePlaneY = priv->width;
924 priv->stridePlaneUV = priv->width/2;
925 priv->stridePlaneYUY = priv->width * 2;
928 if(priv->mode != YUV) {
929 if(!priv->rgbsurface) {
930 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Couldn't create an RGB surface: %s.\n", SDL_GetError());
931 return -1;
934 priv->dblit = 0;
936 if((priv->format&0xFF) != priv->bpp)
937 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);
939 priv->framePlaneRGB = priv->width * priv->height * priv->rgbsurface->format->BytesPerPixel;
940 priv->stridePlaneRGB = priv->width * priv->rgbsurface->format->BytesPerPixel;
943 erase_rectangle(0, 0, surfwidth, surfheight);
945 return 0;
950 * Draw a frame to the SDL YUV overlay.
952 * params : *src[] == the Y, U, and V planes that make up the frame.
953 * returns : non-zero on success, zero on error.
956 //static int sdl_draw_frame (frame_t *frame)
957 static int draw_frame(uint8_t *src[])
959 struct sdl_priv_s *priv = &sdl_priv;
960 uint8_t *dst;
961 int i;
962 uint8_t *mysrc = src[0];
964 switch(priv->format){
965 case IMGFMT_YUY2:
966 case IMGFMT_UYVY:
967 case IMGFMT_YVYU:
968 SDL_OVR_LOCK(-1)
969 dst = (uint8_t *) *(priv->overlay->pixels) + priv->overlay->pitches[0]*priv->y;
970 if(priv->flip) {
971 mysrc+=priv->framePlaneYUY;
972 for(i = 0; i < priv->height; i++) {
973 mysrc-=priv->stridePlaneYUY;
974 fast_memcpy (dst, mysrc, priv->stridePlaneYUY);
975 dst+=priv->overlay->pitches[0];
978 else fast_memcpy (dst, src[0], priv->framePlaneYUY);
979 SDL_OVR_UNLOCK
980 break;
982 case IMGFMT_RGB15:
983 case IMGFMT_BGR15:
984 case IMGFMT_RGB16:
985 case IMGFMT_BGR16:
986 case IMGFMT_RGB24:
987 case IMGFMT_BGR24:
988 case IMGFMT_RGB32:
989 case IMGFMT_BGR32:
990 if(priv->dblit) {
991 SDL_SRF_LOCK(priv->surface, -1)
992 dst = (uint8_t *) priv->surface->pixels + priv->y*priv->surface->pitch;
993 if(priv->flip) {
994 mysrc+=priv->framePlaneRGB;
995 for(i = 0; i < priv->height; i++) {
996 mysrc-=priv->stridePlaneRGB;
997 fast_memcpy (dst, mysrc, priv->stridePlaneRGB);
998 dst += priv->surface->pitch;
1001 else fast_memcpy (dst, src[0], priv->framePlaneRGB);
1002 SDL_SRF_UNLOCK(priv->surface)
1003 } else {
1004 SDL_SRF_LOCK(priv->rgbsurface, -1)
1005 dst = (uint8_t *) priv->rgbsurface->pixels + priv->y*priv->rgbsurface->pitch;
1006 if(priv->flip) {
1007 mysrc+=priv->framePlaneRGB;
1008 for(i = 0; i < priv->height; i++) {
1009 mysrc-=priv->stridePlaneRGB;
1010 fast_memcpy (dst, mysrc, priv->stridePlaneRGB);
1011 dst += priv->rgbsurface->pitch;
1014 else fast_memcpy (dst, src[0], priv->framePlaneRGB);
1015 SDL_SRF_UNLOCK(priv->rgbsurface)
1017 break;
1021 return 0;
1026 * Draw a slice (16 rows of image) to the SDL YUV overlay.
1028 * params : *src[] == the Y, U, and V planes that make up the slice.
1029 * returns : non-zero on error, zero on success.
1032 //static uint32_t draw_slice(uint8_t *src[], uint32_t slice_num)
1033 static int draw_slice(uint8_t *image[], int stride[], int w,int h,int x,int y)
1035 struct sdl_priv_s *priv = &sdl_priv;
1036 uint8_t *dst;
1038 SDL_OVR_LOCK(-1)
1040 y += priv->y;
1042 dst = priv->overlay->pixels[0] + priv->overlay->pitches[0]*y + x;
1043 memcpy_pic(dst, image[0], w, h, priv->overlay->pitches[0], stride[0]);
1044 x/=2;y/=2;w/=2;h/=2;
1046 switch(priv->format) {
1047 case IMGFMT_YV12:
1048 dst = priv->overlay->pixels[2] + priv->overlay->pitches[2]*y + x;
1049 memcpy_pic(dst, image[1], w, h, priv->overlay->pitches[2], stride[1]);
1051 dst = priv->overlay->pixels[1] + priv->overlay->pitches[1]*y + x;
1052 memcpy_pic(dst, image[2], w, h, priv->overlay->pitches[1], stride[2]);
1054 break;
1055 case IMGFMT_I420:
1056 case IMGFMT_IYUV:
1057 dst = priv->overlay->pixels[1] + priv->overlay->pitches[1]*y + x;
1058 memcpy_pic(dst, image[1], w, h, priv->overlay->pitches[1], stride[1]);
1060 dst = priv->overlay->pixels[2] + priv->overlay->pitches[2]*y + x;
1061 memcpy_pic(dst, image[2], w, h, priv->overlay->pitches[2], stride[2]);
1063 break;
1064 default:
1065 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Unsupported image format in draw_slice, contact MPlayer developers!\n");
1068 SDL_OVR_UNLOCK
1070 return 0;
1076 * Checks for SDL keypress and window resize events
1078 * params : none
1079 * returns : doesn't return
1082 static void check_events (void)
1084 struct sdl_priv_s *priv = &sdl_priv;
1085 SDL_Event event;
1086 SDLKey keypressed = SDLK_UNKNOWN;
1088 /* Poll the waiting SDL Events */
1089 while ( SDL_PollEvent(&event) ) {
1090 switch (event.type) {
1092 /* capture window resize events */
1093 case SDL_VIDEORESIZE:
1094 if(!priv->dblit)
1095 set_video_mode(event.resize.w, event.resize.h, priv->bpp, priv->sdlflags);
1097 /* save video extents, to restore them after going fullscreen */
1098 //if(!(priv->surface->flags & SDL_FULLSCREEN)) {
1099 priv->windowsize.w = priv->surface->w;
1100 priv->windowsize.h = priv->surface->h;
1102 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Window resize\n");
1103 break;
1105 /* graphics mode selection shortcuts */
1106 case SDL_KEYDOWN:
1107 keypressed = event.key.keysym.sym;
1108 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Key pressed: '%i'\n", keypressed);
1110 /* c key pressed. c cycles through available fullscreenmodes, if we have some */
1111 if ( ((keypressed == SDLK_c)) && (priv->fullmodes) ) {
1112 /* select next fullscreen mode */
1113 priv->fullmode++;
1114 if (priv->fullmode > (findArrayEnd(priv->fullmodes) - 1)) priv->fullmode = 0;
1115 set_fullmode(priv->fullmode);
1117 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Set next available fullscreen mode.\n");
1120 else if ( keypressed == SDLK_n ) {
1121 #ifdef CONFIG_X11
1122 aspect(&priv->dstwidth, &priv->dstheight,A_NOZOOM);
1123 #endif
1124 if (priv->surface->w != priv->dstwidth || priv->surface->h != priv->dstheight) {
1125 set_video_mode(priv->dstwidth, priv->dstheight, priv->bpp, priv->sdlflags);
1126 priv->windowsize.w = priv->surface->w;
1127 priv->windowsize.h = priv->surface->h;
1128 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Normal size\n");
1129 } else
1130 if (priv->surface->w != priv->dstwidth * 2 || priv->surface->h != priv->dstheight * 2) {
1131 set_video_mode(priv->dstwidth * 2, priv->dstheight * 2, priv->bpp, priv->sdlflags);
1132 priv->windowsize.w = priv->surface->w;
1133 priv->windowsize.h = priv->surface->h;
1134 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Double size\n");
1138 else sdl_default_handle_event(&event);
1140 break;
1141 default: sdl_default_handle_event(&event); break;
1146 /* Erase (paint it black) the rectangle specified by x, y, w and h in the surface
1147 or overlay which is used for OSD
1149 static void erase_rectangle(int x, int y, int w, int h)
1151 struct sdl_priv_s *priv = &sdl_priv;
1153 switch(priv->format) {
1154 case IMGFMT_YV12:
1155 case IMGFMT_I420:
1156 case IMGFMT_IYUV:
1158 SDL_OVR_LOCK((void) 0)
1160 /* Erase Y plane */
1161 erase_area_1(x, w, h,
1162 priv->overlay->pitches[0], 0,
1163 priv->overlay->pixels[0] +
1164 priv->overlay->pitches[0]*y);
1166 /* Erase U and V planes */
1167 w /= 2;
1168 x /= 2;
1169 h /= 2;
1170 y /= 2;
1172 erase_area_1(x, w, h,
1173 priv->overlay->pitches[1], 128,
1174 priv->overlay->pixels[1] +
1175 priv->overlay->pitches[1]*y);
1177 erase_area_1(x, w, h,
1178 priv->overlay->pitches[2], 128,
1179 priv->overlay->pixels[2] +
1180 priv->overlay->pitches[2]*y);
1181 SDL_OVR_UNLOCK
1182 break;
1185 case IMGFMT_YUY2:
1186 case IMGFMT_YVYU:
1188 /* yuy2 and yvyu represent black the same way */
1189 uint8_t yuy2_black[] = {0, 128, 0, 128};
1191 SDL_OVR_LOCK((void) 0)
1192 erase_area_4(x*2, w*2, h,
1193 priv->overlay->pitches[0],
1194 *((uint32_t*) yuy2_black),
1195 priv->overlay->pixels[0] +
1196 priv->overlay->pitches[0]*y);
1197 SDL_OVR_UNLOCK
1198 break;
1201 case IMGFMT_UYVY:
1203 uint8_t uyvy_black[] = {128, 0, 128, 0};
1205 SDL_OVR_LOCK((void) 0)
1206 erase_area_4(x*2, w*2, h,
1207 priv->overlay->pitches[0],
1208 *((uint32_t*) uyvy_black),
1209 priv->overlay->pixels[0] +
1210 priv->overlay->pitches[0]*y);
1211 SDL_OVR_UNLOCK
1212 break;
1215 case IMGFMT_RGB15:
1216 case IMGFMT_BGR15:
1217 case IMGFMT_RGB16:
1218 case IMGFMT_BGR16:
1219 case IMGFMT_RGB24:
1220 case IMGFMT_BGR24:
1221 case IMGFMT_RGB32:
1222 case IMGFMT_BGR32:
1224 SDL_Rect rect;
1225 rect.w = w; rect.h = h;
1226 rect.x = x; rect.y = y;
1228 if(priv->dblit) {
1229 SDL_SRF_LOCK(priv->surface, (void) 0)
1230 SDL_FillRect(priv->surface, &rect, 0);
1231 SDL_SRF_UNLOCK(priv->surface)
1233 else {
1234 SDL_SRF_LOCK(priv->rgbsurface, (void) 0)
1235 SDL_FillRect(priv->rgbsurface, &rect, 0);
1236 SDL_SRF_UNLOCK(priv->rgbsurface)
1238 break;
1243 static void draw_osd(void)
1244 { struct sdl_priv_s *priv = &sdl_priv;
1246 priv->osd_has_changed = vo_osd_changed(0);
1248 if(priv->osd_has_changed)
1250 int i;
1252 for(i = 0; i < 2; i++) {
1253 if(priv->dirty_off_frame[i].x < 0 || priv->dirty_off_frame[i].y < 0)
1254 continue;
1256 erase_rectangle(priv->dirty_off_frame[i].x, priv->dirty_off_frame[i].y,
1257 priv->dirty_off_frame[i].w, priv->dirty_off_frame[i].h);
1259 priv->dirty_off_frame[i].x = -1;
1260 priv->dirty_off_frame[i].y = -1;
1264 /* update osd/subtitles */
1265 if(priv->mode == YUV)
1266 vo_draw_text(priv->overlay->w, priv->overlay->h, draw_alpha);
1267 else {
1268 if(priv->dblit)
1269 vo_draw_text(priv->surface->w, priv->surface->h, draw_alpha);
1270 else
1271 vo_draw_text(priv->rgbsurface->w, priv->rgbsurface->h, draw_alpha);
1275 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1276 * are given in bytes. 4 bytes at a time.
1278 static void erase_area_4(int x_start, int width, int height, int pitch, uint32_t color, uint8_t* pixels)
1280 int x_end = x_start/4 + width/4;
1281 int x, y;
1282 uint32_t* data = (uint32_t*) pixels;
1284 x_start /= 4;
1285 pitch /= 4;
1287 for(y = 0; y < height; y++) {
1288 for(x = x_start; x < x_end; x++)
1289 data[y*pitch + x] = color;
1293 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1294 * are given in bytes. 1 byte at a time.
1296 static void erase_area_1(int x_start, int width, int height, int pitch, uint8_t color, uint8_t* pixels)
1298 int y;
1300 for(y = 0; y < height; y++) {
1301 memset(&pixels[y*pitch + x_start], color, width);
1306 * Display the surface we have written our data to
1308 * params : mode == index of the desired fullscreen mode
1309 * returns : doesn't return
1312 static void flip_page (void)
1314 struct sdl_priv_s *priv = &sdl_priv;
1316 switch(priv->format) {
1317 case IMGFMT_RGB15:
1318 case IMGFMT_BGR15:
1319 case IMGFMT_RGB16:
1320 case IMGFMT_BGR16:
1321 case IMGFMT_RGB24:
1322 case IMGFMT_BGR24:
1323 case IMGFMT_RGB32:
1324 case IMGFMT_BGR32:
1325 if(!priv->dblit) {
1326 /* blit to the RGB surface */
1327 if(SDL_BlitSurface (priv->rgbsurface, NULL, priv->surface, NULL))
1328 mp_tmsg(MSGT_VO,MSGL_WARN, "[VO_SDL] Blit failed: %s.\n", SDL_GetError());
1331 /* update screen */
1332 //SDL_UpdateRect(priv->surface, 0, 0, priv->surface->clip_rect.w, priv->surface->clip_rect.h);
1333 if(priv->osd_has_changed) {
1334 priv->osd_has_changed = 0;
1335 SDL_UpdateRects(priv->surface, 1, &priv->surface->clip_rect);
1337 else
1338 SDL_UpdateRect(priv->surface, 0, priv->y_screen_top,
1339 priv->surface->clip_rect.w, priv->y_screen_bottom);
1341 /* check if we have a double buffered surface and flip() if we do. */
1342 if ( priv->surface->flags & SDL_DOUBLEBUF )
1343 SDL_Flip(priv->surface);
1345 break;
1346 default:
1347 /* blit to the YUV overlay */
1348 SDL_DisplayYUVOverlay (priv->overlay, &priv->surface->clip_rect);
1350 /* check if we have a double buffered surface and flip() if we do. */
1351 if ( priv->surface->flags & SDL_DOUBLEBUF )
1352 SDL_Flip(priv->surface);
1354 //SDL_LockYUVOverlay (priv->overlay); // removed because unused!?
1358 static int
1359 query_format(uint32_t format)
1361 switch(format){
1362 case IMGFMT_YV12:
1363 // it seems buggy (not hw accelerated), so just use YV12 instead!
1364 // case IMGFMT_I420:
1365 // case IMGFMT_IYUV:
1366 case IMGFMT_YUY2:
1367 case IMGFMT_UYVY:
1368 case IMGFMT_YVYU:
1369 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD |
1370 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
1371 case IMGFMT_RGB15:
1372 case IMGFMT_BGR15:
1373 case IMGFMT_RGB16:
1374 case IMGFMT_BGR16:
1375 case IMGFMT_RGB24:
1376 case IMGFMT_BGR24:
1377 case IMGFMT_RGB32:
1378 case IMGFMT_BGR32:
1379 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_FLIP;
1381 return 0;
1385 static void
1386 uninit(void)
1388 #ifdef CONFIG_X11
1389 struct sdl_priv_s *priv = &sdl_priv;
1390 if(priv->X) {
1391 mp_msg(MSGT_VO,MSGL_V, "SDL: activating XScreensaver/DPMS\n");
1392 vo_x11_uninit();
1394 #endif
1395 sdl_close();
1397 /* Cleanup SDL */
1398 vo_sdl_uninit();
1400 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Closed Plugin\n");
1404 static int preinit(const char *arg)
1406 struct sdl_priv_s *priv = &sdl_priv;
1407 char * sdl_driver = NULL;
1408 int sdl_hwaccel;
1409 int sdl_forcexv;
1410 const opt_t subopts[] = {
1411 {"forcexv", OPT_ARG_BOOL, &sdl_forcexv, NULL},
1412 {"hwaccel", OPT_ARG_BOOL, &sdl_hwaccel, NULL},
1413 {"driver", OPT_ARG_MSTRZ, &sdl_driver, NULL},
1414 {NULL, 0, NULL, NULL}
1417 sdl_forcexv = 1;
1418 sdl_hwaccel = 1;
1420 if (subopt_parse(arg, subopts) != 0) return -1;
1422 priv->rgbsurface = NULL;
1423 priv->overlay = NULL;
1424 priv->surface = NULL;
1426 mp_msg(MSGT_VO,MSGL_DBG3, "SDL: Opening Plugin\n");
1428 if(sdl_driver) {
1429 setenv("SDL_VIDEODRIVER", sdl_driver, 1);
1430 free(sdl_driver);
1433 /* does the user want SDL to try and force Xv */
1434 if(sdl_forcexv) setenv("SDL_VIDEO_X11_NODIRECTCOLOR", "1", 1);
1435 else setenv("SDL_VIDEO_X11_NODIRECTCOLOR", "0", 1);
1437 /* does the user want to disable Xv and use software scaling instead */
1438 if(sdl_hwaccel) setenv("SDL_VIDEO_YUV_HWACCEL", "1", 1);
1439 else setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
1441 /* default to no fullscreen mode, we'll set this as soon we have the avail. modes */
1442 priv->fullmode = -2;
1444 priv->fullmodes = NULL;
1445 priv->bpp = 0;
1447 /* initialize the SDL Video system */
1448 if (!vo_sdl_init()) {
1449 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_SDL] SDL initialization failed: %s.\n", SDL_GetError());
1451 return -1;
1454 SDL_VideoDriverName(priv->driver, 8);
1455 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_SDL] Using driver: %s.\n", priv->driver);
1457 priv->X = 0;
1458 #ifdef CONFIG_X11
1459 if(vo_init()) {
1460 mp_msg(MSGT_VO,MSGL_V, "SDL: deactivating XScreensaver/DPMS\n");
1461 priv->XWidth = vo_screenwidth;
1462 priv->XHeight = vo_screenheight;
1463 priv->X = 1;
1464 mp_msg(MSGT_VO,MSGL_V, "SDL: X11 Resolution %ix%i\n", priv->XWidth, priv->XHeight);
1466 #endif
1468 return 0;
1471 static uint32_t get_image(mp_image_t *mpi)
1473 struct sdl_priv_s *priv = &sdl_priv;
1475 if(priv->format != mpi->imgfmt) return VO_FALSE;
1476 if(mpi->type == MP_IMGTYPE_STATIC || mpi->type == MP_IMGTYPE_TEMP) {
1477 if(mpi->flags&MP_IMGFLAG_PLANAR) {
1478 mpi->planes[0] = priv->overlay->pixels[0] + priv->y*priv->overlay->pitches[0];
1479 mpi->stride[0] = priv->overlay->pitches[0];
1480 if(mpi->flags&MP_IMGFLAG_SWAPPED) {
1481 mpi->planes[1] = priv->overlay->pixels[1] + priv->y*priv->overlay->pitches[1]/2;
1482 mpi->stride[1] = priv->overlay->pitches[1];
1483 mpi->planes[2] = priv->overlay->pixels[2] + priv->y*priv->overlay->pitches[2]/2;
1484 mpi->stride[2] = priv->overlay->pitches[2];
1485 } else {
1486 mpi->planes[2] = priv->overlay->pixels[1] + priv->y*priv->overlay->pitches[1]/2;
1487 mpi->stride[2] = priv->overlay->pitches[1];
1488 mpi->planes[1] = priv->overlay->pixels[2] + priv->y*priv->overlay->pitches[2]/2;
1489 mpi->stride[1] = priv->overlay->pitches[2];
1492 else if(IMGFMT_IS_RGB(priv->format) || IMGFMT_IS_BGR(priv->format)) {
1493 if(priv->dblit) {
1494 if(mpi->type == MP_IMGTYPE_STATIC && (priv->surface->flags & SDL_DOUBLEBUF))
1495 return VO_FALSE;
1497 mpi->planes[0] = (uint8_t *)priv->surface->pixels + priv->y*priv->surface->pitch;
1498 mpi->stride[0] = priv->surface->pitch;
1500 else {
1501 mpi->planes[0] = (uint8_t *)priv->rgbsurface->pixels + priv->y*priv->rgbsurface->pitch;
1502 mpi->stride[0] = priv->rgbsurface->pitch;
1505 else {
1506 mpi->planes[0] = priv->overlay->pixels[0] + priv->y*priv->overlay->pitches[0];
1507 mpi->stride[0] = priv->overlay->pitches[0];
1510 mpi->flags|=MP_IMGFLAG_DIRECT;
1511 return VO_TRUE;
1514 return VO_FALSE;
1517 static int control(uint32_t request, void *data)
1519 struct sdl_priv_s *priv = &sdl_priv;
1520 switch (request) {
1521 case VOCTRL_GET_IMAGE:
1522 return get_image(data);
1523 case VOCTRL_QUERY_FORMAT:
1524 return query_format(*((uint32_t*)data));
1525 case VOCTRL_FULLSCREEN:
1526 if (priv->surface->flags & SDL_FULLSCREEN) {
1527 set_video_mode(priv->windowsize.w, priv->windowsize.h, priv->bpp, priv->sdlflags);
1528 SDL_ShowCursor(1);
1529 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Windowed mode\n");
1530 } else if (priv->fullmodes) {
1531 set_fullmode(priv->fullmode);
1532 mp_msg(MSGT_VO,MSGL_DBG2, "SDL: Set fullscreen mode\n");
1534 return VO_TRUE;
1537 return VO_NOTIMPL;