10l initial patch by Oded Shimon <ods15 at ods15.dyndns.org>
[mplayer/greg.git] / libvo / vo_sdl.c
blobe320709bb2d8d7a2d092774de0cb9be09dea1003
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 [The Movie Player for Linux] project, further info
17 * from http://www.mplayerhq.hu
19 * -- old disclaimer --
21 * A mpeg2dec display driver that does output through the
22 * Simple DirectMedia Layer (SDL) library. This effectively gives us all
23 * sorts of output options: X11, SVGAlib, fbcon, AAlib, GGI. Win32, MacOS
24 * and BeOS support, too. Yay. SDL info, source, and binaries can be found
25 * at http://slouken.devolution.com/SDL/
27 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
29 * mpeg2dec is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2, or (at your option)
32 * any later version.
34 * mpeg2dec is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with GNU Make; see the file COPYING. If not, write to
41 * the Free Software Foundation.
43 * -- end old disclaimer --
45 * Changes:
46 * Dominik Schnitzer <dominik@schnitzer.at> - November 08, 2000.
47 * - Added resizing support, fullscreen: changed the sdlmodes selection
48 * routine.
49 * - SDL bugfixes: removed the atexit(SLD_Quit), SDL_Quit now resides in
50 * the plugin_exit routine.
51 * - Commented the source :)
52 * - Shortcuts: for switching between Fullscreen/Windowed mode and for
53 * cycling between the different Fullscreen modes.
54 * - Small bugfixes: proper width/height of movie
55 * Dominik Schnitzer <dominik@schnitzer.at> - November 11, 2000.
56 * - Cleanup code, more comments
57 * - Better error handling
58 * Bruno Barreyra <barreyra@ufl.edu> - December 10, 2000.
59 * - Eliminated memcpy's for entire frames
60 * Felix Buenemann <Atmosfear@users.sourceforge.net> - March 11, 2001
61 * - Added aspect-ratio awareness for fullscreen
62 * Felix Buenemann <Atmosfear@users.sourceforge.net> - March 11, 2001
63 * - Fixed aspect-ratio awareness, did only vertical scaling (black bars above
64 * and below), now also does horizontal scaling (black bars left and right),
65 * so you get the biggest possible picture with correct aspect-ratio.
66 * Felix Buenemann <Atmosfear@users.sourceforge.net> - March 12, 2001
67 * - Minor bugfix to aspect-ratio for non-4:3-resolutions (like 1280x1024)
68 * - Bugfix to check_events() to reveal mouse cursor after 'q'-quit in
69 * fullscreen-mode
70 * Felix Buenemann <Atmosfear@users.sourceforge.net> - April 10, 2001
71 * - Changed keypress-detection from keydown to keyup, seems to fix keyrepeat
72 * bug (key had to be pressed twice to be detected)
73 * - Changed key-handling: 'f' cycles fullscreen/windowed, ESC/RETURN/'q' quits
74 * - Bugfix which avoids exit, because return is passed to sdl-output on startup,
75 * which caused the player to exit (keyboard-buffer problem? better solution
76 * recommed)
77 * Felix Buenemann <Atmosfear@users.sourceforge.net> - April 11, 2001
78 * - OSD and subtitle support added
79 * - some minor code-changes
80 * - added code to comply with new fullscreen meaning
81 * - changed fullscreen-mode-cycling from '+' to 'c' (interferred with audiosync
82 * adjustment)
83 * Felix Buenemann <Atmosfear@users.sourceforge.net> - April 13, 2001
84 * - added keymapping to toggle OSD ('o' key)
85 * - added some defines to modify some sdl-out internas (see comments)
87 * Felix Buenemann: further changes will be visible through cvs log, don't want
88 * to update this all the time (CVS info on http://mplayer.sourceforge.net)
92 /* define to force software-surface (video surface stored in system memory)*/
93 #undef SDL_NOHWSURFACE
95 /* define to enable surface locks, this might be needed on SMP machines */
96 #undef SDL_ENABLE_LOCKS
98 //#define BUGGY_SDL //defined by configure
100 /* MONITOR_ASPECT MUST BE FLOAT */
101 #define MONITOR_ASPECT 4.0/3.0
103 #include <stdio.h>
104 #include <stdlib.h>
105 #include <string.h>
106 #include <inttypes.h>
108 #include "config.h"
109 #include "video_out.h"
110 #include "video_out_internal.h"
112 #include "fastmemcpy.h"
113 #include "sub.h"
114 #include "aspect.h"
115 #include "libmpcodecs/vfcap.h"
117 #ifdef HAVE_X11
118 #include <X11/Xlib.h>
119 #include "x11_common.h"
120 #endif
122 #include "input/input.h"
123 #include "input/mouse.h"
125 extern int verbose;
126 int sdl_noxv;
127 int sdl_forcexv;
129 static vo_info_t info =
131 "SDL YUV/RGB/BGR renderer (SDL v1.1.7+ only!)",
132 "sdl",
133 "Ryan C. Gordon <icculus@lokigames.com>, Felix Buenemann <atmosfear@users.sourceforge.net>",
137 LIBVO_EXTERN(sdl)
139 #include <SDL.h>
140 //#include <SDL/SDL_syswm.h>
142 #if defined(__MINGW32__) || defined(HPUX) || defined(sgi) || (defined(sun) && defined(__svr4__))
143 /* setenv is missing on win32, solaris, IRIX and HPUX */
144 static void setenv(const char *name, const char *val, int _xx)
146 int len = strlen(name) + strlen(val) + 2;
147 char *env = malloc(len);
149 if (env != NULL) {
150 strcpy(env, name);
151 strcat(env, "=");
152 strcat(env, val);
153 putenv(env);
156 #endif
159 #define FS 0x01
160 #define VM 0x02
161 #define ZOOM 0x04
162 #define FLIP 0x08
164 #ifdef SDL_ENABLE_LOCKS
165 #define SDL_OVR_LOCK(x) if (SDL_LockYUVOverlay (priv->overlay)) { \
166 if(verbose) printf("SDL: Couldn't lock YUV overlay\n"); \
167 return x; \
169 #define SDL_OVR_UNLOCK SDL_UnlockYUVOverlay (priv->overlay);
171 #define SDL_SRF_LOCK(srf, x) if(SDL_MUSTLOCK(srf)) { \
172 if(SDL_LockSurface (srf)) { \
173 if(verbose) printf("SDL: Couldn't lock RGB surface\n"); \
174 return x; \
178 #define SDL_SRF_UNLOCK(srf) if(SDL_MUSTLOCK(srf)) \
179 SDL_UnlockSurface (srf);
180 #else
181 #define SDL_OVR_LOCK(x)
182 #define SDL_OVR_UNLOCK
183 #define SDL_SRF_LOCK(srf, x)
184 #define SDL_SRF_UNLOCK(srf)
185 #endif
187 /** Private SDL Data structure **/
189 static struct sdl_priv_s {
191 /* output driver used by sdl */
192 char driver[8];
194 /* SDL display surface */
195 SDL_Surface *surface;
197 /* SDL RGB surface */
198 SDL_Surface *rgbsurface;
200 /* SDL YUV overlay */
201 SDL_Overlay *overlay;
203 /* available fullscreen modes */
204 SDL_Rect **fullmodes;
206 /* surface attributes for fullscreen and windowed mode */
207 Uint32 sdlflags, sdlfullflags;
209 /* save the windowed output extents */
210 SDL_Rect windowsize;
212 /* Bits per Pixel */
213 Uint8 bpp;
215 /* RGB or YUV? */
216 Uint8 mode;
217 #define YUV 0
218 #define RGB 1
219 #define BGR 2
221 /* use direct blitting to surface */
222 int dblit;
224 /* current fullscreen mode, 0 = highest available fullscreen mode */
225 int fullmode;
227 /* YUV ints */
228 int framePlaneY, framePlaneUV, framePlaneYUY;
229 int stridePlaneY, stridePlaneUV, stridePlaneYUY;
231 /* RGB ints */
232 int framePlaneRGB;
233 int stridePlaneRGB;
235 /* Flip image */
236 int flip;
238 /* fullscreen behaviour; see init */
239 int fulltype;
241 /* is X running (0/1) */
242 int X;
244 /* X11 Resolution */
245 int XWidth, XHeight;
247 /* original image dimensions */
248 int width, height;
250 /* destination dimensions */
251 int dstwidth, dstheight;
253 /* Draw image at coordinate y on the SDL surfaces */
254 int y;
256 /* The image is displayed between those y coordinates in priv->surface */
257 int y_screen_top, y_screen_bottom;
259 /* 1 if the OSD has changed otherwise 0 */
260 int osd_has_changed;
262 /* source image format (YUV/RGB/...) */
263 uint32_t format;
265 /* dirty_off_frame[0] contains a bounding box around the osd contents drawn above the image
266 dirty_off_frame[1] is the corresponding thing for OSD contents drawn below the image
268 SDL_Rect dirty_off_frame[2];
269 } sdl_priv;
271 static void erase_area_4(int x_start, int width, int height, int pitch, uint32_t color, uint8_t* pixels);
272 static void erase_area_1(int x_start, int width, int height, int pitch, uint8_t color, uint8_t* pixels);
273 static int setup_surfaces(void);
274 static void set_video_mode(int width, int height, int bpp, uint32_t sdlflags);
275 static void erase_rectangle(int x, int y, int w, int h);
277 /* Expand 'rect' to contain the rectangle specified by x, y, w and h */
278 static void expand_rect(SDL_Rect* rect, int x, int y, int w, int h)
280 if(rect->x < 0 || rect->y < 0) {
281 rect->x = x;
282 rect->y = y;
283 rect->w = w;
284 rect->h = h;
285 return;
288 if(rect->x > x)
289 rect->x = x;
291 if(rect->y > y)
292 rect->y = y;
294 if(rect->x + rect->w < x + w)
295 rect->w = x + w - rect->x;
297 if(rect->y + rect->h < y + h)
298 rect->h = y + h - rect->y;
301 /** libvo Plugin functions **/
304 * draw_alpha is used for osd and subtitle display.
308 static void draw_alpha(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
309 struct sdl_priv_s *priv = &sdl_priv;
311 if(priv->osd_has_changed) {
312 /* OSD did change. Store a bounding box of everything drawn into the OSD */
313 if(priv->y >= y0) {
314 /* Make sure we don't mark part of the frame area dirty */
315 if(h + y0 > priv->y)
316 expand_rect(&priv->dirty_off_frame[0], x0, y0, w, priv->y - y0);
317 else
318 expand_rect(&priv->dirty_off_frame[0], x0, y0, w, h);
320 else if(priv->y + priv->height <= y0 + h) {
321 /* Make sure we don't mark part of the frame area dirty */
322 if(y0 < priv->y + priv->height)
323 expand_rect(&priv->dirty_off_frame[1], x0,
324 priv->y + priv->height,
325 w, h - ((priv->y + priv->height) - y0));
326 else
327 expand_rect(&priv->dirty_off_frame[1], x0, y0, w, h);
330 else { /* OSD contents didn't change only draw parts that was erased by the frame */
331 if(priv->y >= y0) {
332 src = src + (priv->y - y0) * stride;
333 srca = srca + (priv->y - y0) * stride;
334 h -= priv->y - y0;
335 y0 = priv->y;
338 if(priv->y + priv->height <= y0 + h)
339 h = priv->y + priv->height - y0;
341 if(h <= 0)
342 return;
345 switch(priv->format) {
346 case IMGFMT_YV12:
347 case IMGFMT_I420:
348 case IMGFMT_IYUV:
349 vo_draw_alpha_yv12(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
350 break;
351 case IMGFMT_YUY2:
352 case IMGFMT_YVYU:
353 x0 *= 2;
354 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
355 break;
356 case IMGFMT_UYVY:
357 x0 *= 2;
358 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) *(priv->overlay->pixels))+priv->overlay->pitches[0]*y0+x0,priv->overlay->pitches[0]);
359 break;
361 default:
362 if(priv->dblit) {
363 x0 *= priv->surface->format->BytesPerPixel;
364 switch(priv->format) {
365 case IMGFMT_RGB15:
366 case IMGFMT_BGR15:
367 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
368 break;
369 case IMGFMT_RGB16:
370 case IMGFMT_BGR16:
371 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
372 break;
373 case IMGFMT_RGB24:
374 case IMGFMT_BGR24:
375 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
376 break;
377 case IMGFMT_RGB32:
378 case IMGFMT_BGR32:
379 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) priv->surface->pixels)+y0*priv->surface->pitch+x0,priv->surface->pitch);
380 break;
383 else {
384 x0 *= priv->rgbsurface->format->BytesPerPixel;
385 switch(priv->format) {
386 case IMGFMT_RGB15:
387 case IMGFMT_BGR15:
388 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
389 break;
390 case IMGFMT_RGB16:
391 case IMGFMT_BGR16:
392 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
393 break;
394 case IMGFMT_RGB24:
395 case IMGFMT_BGR24:
396 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
397 break;
398 case IMGFMT_RGB32:
399 case IMGFMT_BGR32:
400 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) priv->rgbsurface->pixels)+y0*priv->rgbsurface->pitch+x0,priv->rgbsurface->pitch);
401 break;
410 * Take a null-terminated array of pointers, and find the last element.
412 * params : array == array of which we want to find the last element.
413 * returns : index of last NON-NULL element.
416 static inline int findArrayEnd (SDL_Rect **array)
418 int i = 0;
419 while ( array[i++] ); /* keep loopin' ... */
421 /* return the index of the last array element */
422 return i - 1;
427 * Open and prepare SDL output.
429 * params : *plugin ==
430 * *name ==
431 * returns : 0 on success, -1 on failure
434 static int sdl_open (void *plugin, void *name)
436 struct sdl_priv_s *priv = &sdl_priv;
437 const SDL_VideoInfo *vidInfo = NULL;
438 /*static int opened = 0;
440 if (opened)
441 return 0;
442 opened = 1;*/
445 /* other default values */
446 #ifdef SDL_NOHWSURFACE
447 if(verbose) printf("SDL: using software-surface\n");
448 priv->sdlflags = SDL_SWSURFACE|SDL_RESIZABLE|SDL_ANYFORMAT;
449 priv->sdlfullflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ANYFORMAT;
450 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
451 #else
452 /*if((strcmp(priv->driver, "dga") == 0) && (priv->mode)) {
453 if(verbose) printf("SDL: using software-surface\n");
454 priv->sdlflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
455 priv->sdlfullflags = SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_ANYFORMAT;
457 else { */
458 if(verbose) printf("SDL: using hardware-surface\n");
459 priv->sdlflags = SDL_HWSURFACE|SDL_RESIZABLE/*|SDL_ANYFORMAT*/;
460 priv->sdlfullflags = SDL_HWSURFACE|SDL_FULLSCREEN/*|SDL_ANYFORMAT*/;
461 // XXX:FIXME: ASYNCBLIT should be enabled for SMP systems
462 //}
463 #endif
465 #ifndef AMIGA
466 priv->sdlfullflags |= SDL_DOUBLEBUF;
467 if (vo_doublebuffering)
468 priv->sdlflags |= SDL_DOUBLEBUF;
469 #endif
471 /* Setup Keyrepeats (500/30 are defaults) */
472 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, 100 /*SDL_DEFAULT_REPEAT_INTERVAL*/);
474 /* get information about the graphics adapter */
475 vidInfo = SDL_GetVideoInfo ();
477 /* collect all fullscreen & hardware modes available */
478 if (!(priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags))) {
480 /* non hardware accelerated fullscreen modes */
481 priv->sdlfullflags &= ~SDL_HWSURFACE;
482 priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags);
485 /* test for normal resizeable & windowed hardware accellerated surfaces */
486 if (!SDL_ListModes (vidInfo->vfmt, priv->sdlflags)) {
488 /* test for NON hardware accelerated resizeable surfaces - poor you.
489 * That's all we have. If this fails there's nothing left.
490 * Theoretically there could be Fullscreenmodes left - we ignore this for now.
492 priv->sdlflags &= ~SDL_HWSURFACE;
493 if ((!SDL_ListModes (vidInfo->vfmt, priv->sdlflags)) && (!priv->fullmodes)) {
494 printf("SDL: Couldn't get any acceptable SDL Mode for output.\n");
495 return -1;
500 /* YUV overlays need at least 16-bit color depth, but the
501 * display might less. The SDL AAlib target says it can only do
502 * 8-bits, for example. So, if the display is less than 16-bits,
503 * we'll force the BPP to 16, and pray that SDL can emulate for us.
505 priv->bpp = vidInfo->vfmt->BitsPerPixel;
506 if (priv->mode == YUV && priv->bpp < 16) {
508 if(verbose) printf("SDL: Your SDL display target wants to be at a color "
509 "depth of (%d), but we need it to be at least 16 "
510 "bits, so we need to emulate 16-bit color. This is "
511 "going to slow things down; you might want to "
512 "increase your display's color depth, if possible.\n",
513 priv->bpp);
515 priv->bpp = 16;
518 /* We don't want those in our event queue.
519 * We use SDL_KEYUP cause SDL_KEYDOWN seems to cause problems
520 * with keys need to be pressed twice, to be recognized.
522 #ifndef BUGGY_SDL
523 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
524 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
525 // SDL_EventState(SDL_QUIT, SDL_IGNORE);
526 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
527 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
528 #endif
530 /* Success! */
531 return 0;
536 * Close SDL, Cleanups, Free Memory
538 * params : *plugin
539 * returns : non-zero on success, zero on error.
542 static int sdl_close (void)
544 struct sdl_priv_s *priv = &sdl_priv;
546 if (priv->fullmode)
547 SDL_ShowCursor(1);
549 /* Cleanup YUV Overlay structure */
550 if (priv->overlay) {
551 SDL_FreeYUVOverlay(priv->overlay);
552 priv->overlay=NULL;
555 /* Free RGB Surface */
556 if (priv->rgbsurface) {
557 SDL_FreeSurface(priv->rgbsurface);
558 priv->rgbsurface=NULL;
561 /* Free our blitting surface */
562 if (priv->surface) {
563 SDL_FreeSurface(priv->surface);
564 priv->surface=NULL;
567 /* DON'T attempt to free the fullscreen modes array. SDL_Quit* does this for us */
569 return 0;
573 * Do aspect ratio calculations
575 * params : srcw == sourcewidth
576 * srch == sourceheight
577 * dstw == destinationwidth
578 * dsth == destinationheight
580 * returns : SDL_Rect structure with new x and y, w and h
583 #if 0
584 static SDL_Rect aspect(int srcw, int srch, int dstw, int dsth) {
585 SDL_Rect newres;
586 if(verbose > 1) printf("SDL Aspect: src: %ix%i dst: %ix%i\n", srcw, srch, dstw, dsth);
587 newres.h = ((float)dstw / (float)srcw * (float)srch) * ((float)dsth/((float)dstw/(MONITOR_ASPECT)));
588 if(newres.h > dsth) {
589 newres.w = ((float)dsth / (float)newres.h) * dstw;
590 newres.h = dsth;
591 newres.x = (dstw - newres.w) / 2;
592 newres.y = 0;
594 else {
595 newres.w = dstw;
596 newres.x = 0;
597 newres.y = (dsth - newres.h) / 2;
600 if(verbose) printf("SDL Aspect-Destinationres: %ix%i (x: %i, y: %i)\n", newres.w, newres.h, newres.x, newres.y);
602 return newres;
604 #endif
607 * Sets the specified fullscreen mode.
609 * params : mode == index of the desired fullscreen mode
610 * returns : doesn't return
613 #if 0
614 static void set_fullmode (int mode)
616 struct sdl_priv_s *priv = &sdl_priv;
617 SDL_Surface *newsurface = NULL;
618 int haspect, waspect = 0;
620 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
621 if (mode < 0)
622 mode = priv->fullmode = findArrayEnd(priv->fullmodes) - 1;
624 /* Calculate proper aspect ratio for fullscreen
625 * Height smaller than expected: add horizontal black bars (haspect)*/
626 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);
627 /* Height bigger than expected: add vertical black bars (waspect)*/
628 if (haspect < 0) {
629 haspect = 0; /* set haspect to zero because image will be scaled horizontal instead of vertical */
630 waspect = priv->fullmodes[mode]->w - ((float) ((float) priv->fullmodes[mode]->h / (float) priv->height) * (float) priv->width);
632 // printf ("W-Aspect: %i H-Aspect: %i\n", waspect, haspect);
634 /* change to given fullscreen mode and hide the mouse cursor */
635 newsurface = SDL_SetVideoMode(priv->fullmodes[mode]->w - waspect, priv->fullmodes[mode]->h - haspect, priv->bpp, priv->sdlfullflags);
637 /* if we were successfull hide the mouse cursor and save the mode */
638 if (newsurface) {
639 if (priv->surface)
640 SDL_FreeSurface(priv->surface);
641 priv->surface = newsurface;
642 SDL_ShowCursor(0);
645 #endif
647 /* Set video mode. Not fullscreen */
648 static void set_video_mode(int width, int height, int bpp, uint32_t sdlflags)
650 struct sdl_priv_s *priv = &sdl_priv;
651 SDL_Surface* newsurface;
653 if(priv->rgbsurface)
654 SDL_FreeSurface(priv->rgbsurface);
655 else if(priv->overlay)
656 SDL_FreeYUVOverlay(priv->overlay);
658 priv->rgbsurface = NULL;
659 priv->overlay = NULL;
661 newsurface = SDL_SetVideoMode(width, height, bpp, sdlflags);
663 if(newsurface) {
665 /* priv->surface will be NULL the first time this function is called. */
666 if(priv->surface)
667 SDL_FreeSurface(priv->surface);
669 priv->surface = newsurface;
670 priv->dstwidth = width;
671 priv->dstheight = height;
673 setup_surfaces();
675 else
676 printf("set_video_mode: SDL_SetVideoMode failed: %s\n", SDL_GetError());
679 static void set_fullmode (int mode) {
680 struct sdl_priv_s *priv = &sdl_priv;
681 SDL_Surface *newsurface = NULL;
682 int screen_surface_w, screen_surface_h;
684 if(priv->rgbsurface)
685 SDL_FreeSurface(priv->rgbsurface);
686 else if(priv->overlay)
687 SDL_FreeYUVOverlay(priv->overlay);
689 priv->rgbsurface = NULL;
690 priv->overlay = NULL;
692 /* if we haven't set a fullmode yet, default to the lowest res fullmode first */
693 /* But select a mode where the full video enter */
694 if(priv->X && priv->fulltype & FS) {
695 screen_surface_w = priv->XWidth;
696 screen_surface_h = priv->XHeight;
698 else if (mode < 0) {
699 int i,j,imax;
700 mode = 0; // Default to the biggest mode avaible
701 if (verbose) for(i=0;priv->fullmodes[i];++i)
702 printf("SDL Mode: %d: %d x %d\n", i, priv->fullmodes[i]->w, priv->fullmodes[i]->h);
703 for(i = findArrayEnd(priv->fullmodes) - 1; i >=0; i--) {
704 if( (priv->fullmodes[i]->w >= priv->dstwidth) &&
705 (priv->fullmodes[i]->h >= priv->dstheight) ) {
706 imax = i;
707 for (j = findArrayEnd(priv->fullmodes) - 1; j >=0; j--) {
708 if (priv->fullmodes[j]->w > priv->fullmodes[imax]->w
709 && priv->fullmodes[j]->h == priv->fullmodes[imax]->h)
710 imax = j;
712 mode = imax;
713 break;
716 if (verbose) printf("SET SDL Mode: %d: %d x %d\n", mode, priv->fullmodes[mode]->w, priv->fullmodes[mode]->h);
717 priv->fullmode = mode;
718 screen_surface_h = priv->fullmodes[mode]->h;
719 screen_surface_w = priv->fullmodes[mode]->w;
721 else {
722 screen_surface_h = priv->fullmodes[mode]->h;
723 screen_surface_w = priv->fullmodes[mode]->w;
726 aspect_save_screenres(screen_surface_w, screen_surface_h);
728 /* calculate new video size/aspect */
729 if(priv->mode == YUV) {
730 if(priv->fulltype&FS)
731 aspect_save_screenres(priv->XWidth, priv->XHeight);
733 aspect(&priv->dstwidth, &priv->dstheight, A_ZOOM);
736 /* try to change to given fullscreenmode */
737 newsurface = SDL_SetVideoMode(priv->dstwidth, screen_surface_h, priv->bpp,
738 priv->sdlfullflags);
741 * In Mac OS X (and possibly others?) SDL_SetVideoMode() appears to
742 * destroy the datastructure previously retrived, so we need to
743 * re-assign it. The comment in sdl_close() seems to imply that we
744 * should not free() anything.
746 #ifdef SYS_DARWIN
748 const SDL_VideoInfo *vidInfo = NULL;
749 vidInfo = SDL_GetVideoInfo ();
751 /* collect all fullscreen & hardware modes available */
752 if (!(priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags))) {
754 /* non hardware accelerated fullscreen modes */
755 priv->sdlfullflags &= ~SDL_HWSURFACE;
756 priv->fullmodes = SDL_ListModes (vidInfo->vfmt, priv->sdlfullflags);
759 #endif
763 /* if creation of new surface was successfull, save it and hide mouse cursor */
764 if(newsurface) {
765 if (priv->surface)
766 SDL_FreeSurface(priv->surface);
767 priv->surface = newsurface;
768 SDL_ShowCursor(0);
769 SDL_SRF_LOCK(priv->surface, -1)
770 SDL_FillRect(priv->surface, NULL, 0);
771 SDL_SRF_UNLOCK(priv->surface)
772 setup_surfaces();
774 else
775 printf("set_fullmode: SDL_SetVideoMode failed: %s\n", SDL_GetError());
780 * Initialize an SDL surface and an SDL YUV overlay.
782 * params : width == width of video we'll be displaying.
783 * height == height of video we'll be displaying.
784 * fullscreen == want to be fullscreen?
785 * title == Title for window titlebar.
786 * returns : non-zero on success, zero on error.
789 static uint32_t
790 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
791 //static int sdl_setup (int width, int height)
793 struct sdl_priv_s *priv = &sdl_priv;
795 switch(format){
796 case IMGFMT_I420:
797 printf("SDL: Mapping I420 to IYUV\n");
798 format = SDL_IYUV_OVERLAY;
799 case IMGFMT_YV12:
800 case IMGFMT_IYUV:
801 case IMGFMT_YUY2:
802 case IMGFMT_UYVY:
803 case IMGFMT_YVYU:
804 priv->mode = YUV;
805 break;
806 case IMGFMT_BGR15:
807 case IMGFMT_BGR16:
808 case IMGFMT_BGR24:
809 case IMGFMT_BGR32:
810 priv->mode = BGR;
811 break;
812 case IMGFMT_RGB15:
813 case IMGFMT_RGB16:
814 case IMGFMT_RGB24:
815 case IMGFMT_RGB32:
816 priv->mode = RGB;
817 break;
818 default:
819 printf("SDL: Unsupported image format (0x%X)\n",format);
820 return -1;
823 if ( vo_config_count ) sdl_close();
825 if(verbose) printf("SDL: Using 0x%X (%s) image format\n", format,
826 vo_format_name(format));
828 if(priv->mode != YUV) {
829 priv->sdlflags |= SDL_ANYFORMAT;
830 priv->sdlfullflags |= SDL_ANYFORMAT;
833 /* SDL can only scale YUV data */
834 if(priv->mode == RGB || priv->mode == BGR) {
835 d_width = width;
836 d_height = height;
839 aspect_save_orig(width,height);
840 aspect_save_prescale(d_width ? d_width : width, d_height ? d_height : height);
842 /* Save the original Image size */
843 priv->width = width;
844 priv->height = height;
845 priv->dstwidth = d_width ? d_width : width;
846 priv->dstheight = d_height ? d_height : height;
848 priv->format = format;
850 if (sdl_open(NULL, NULL) != 0)
851 return -1;
853 /* Set output window title */
854 SDL_WM_SetCaption (".: MPlayer : F = Fullscreen/Windowed : C = Cycle Fullscreen Resolutions :.", title);
855 //SDL_WM_SetCaption (title, title);
857 if(priv->X) {
858 aspect_save_screenres(priv->XWidth,priv->XHeight);
859 aspect(&priv->dstwidth,&priv->dstheight,A_NOZOOM);
862 priv->windowsize.w = priv->dstwidth;
863 priv->windowsize.h = priv->dstheight;
865 /* bit 0 (0x01) means fullscreen (-fs)
866 * bit 1 (0x02) means mode switching (-vm)
867 * bit 2 (0x04) enables software scaling (-zoom)
868 * bit 3 (0x08) enables flipping (-flip)
870 // printf("SDL: flags are set to: %i\n", flags);
871 // printf("SDL: Width: %i Height: %i D_Width %i D_Height: %i\n", width, height, d_width, d_height);
872 if(flags&FLIP) {
873 if(verbose) printf("SDL: using flipped video (only with RGB/BGR/packed YUV)\n");
874 priv->flip = 1;
876 if(flags&FS) {
877 if(verbose) printf("SDL: setting zoomed fullscreen without modeswitching\n");
878 printf("SDL: Info - please use -vm or -zoom to switch to best resolution.\n");
879 priv->fulltype = FS;
880 set_fullmode(priv->fullmode);
881 /*if((priv->surface = SDL_SetVideoMode (d_width, d_height, priv->bpp, priv->sdlfullflags)))
882 SDL_ShowCursor(0);*/
883 } else
884 if(flags&VM) {
885 if(verbose) printf("SDL: setting zoomed fullscreen with modeswitching\n");
886 priv->fulltype = VM;
887 set_fullmode(priv->fullmode);
888 /*if((priv->surface = SDL_SetVideoMode (d_width ? d_width : width, d_height ? d_height : height, priv->bpp, priv->sdlfullflags)))
889 SDL_ShowCursor(0);*/
890 } else
891 if(flags&ZOOM) {
892 if(verbose) printf("SDL: setting zoomed fullscreen with modeswitching\n");
893 priv->fulltype = ZOOM;
894 set_fullmode(priv->fullmode);
896 else {
897 if((strcmp(priv->driver, "x11") == 0)
898 ||(strcmp(priv->driver, "windib") == 0)
899 ||(strcmp(priv->driver, "directx") == 0)
900 ||(strcmp(priv->driver, "Quartz") == 0)
901 ||(strcmp(priv->driver, "cgx") == 0)
902 ||((strcmp(priv->driver, "aalib") == 0) && priv->X)){
903 if(verbose) printf("SDL: setting windowed mode\n");
904 set_video_mode(priv->dstwidth, priv->dstheight, priv->bpp, priv->sdlflags);
906 else {
907 if(verbose) printf("SDL: setting zoomed fullscreen with modeswitching\n");
908 priv->fulltype = ZOOM;
909 set_fullmode(priv->fullmode);
913 if(!priv->surface) { // cannot SetVideoMode
914 printf("SDL: failed to set video mode: %s\n", SDL_GetError());
915 return -1;
918 return 0;
921 /* Free priv->rgbsurface or priv->overlay if they are != NULL.
922 * Setup priv->rgbsurface or priv->overlay depending on source format.
923 * The size of the created surface or overlay depends on the size of
924 * priv->surface, priv->width, priv->height, priv->dstwidth and priv->dstheight.
926 static int setup_surfaces(void)
928 struct sdl_priv_s *priv = &sdl_priv;
929 float v_scale = ((float) priv->dstheight) / priv->height;
930 int surfwidth, surfheight;
932 surfwidth = priv->width;
933 surfheight = priv->height + (priv->surface->h - priv->dstheight) / v_scale;
934 surfheight&= ~1;
935 /* Place the image in the middle of the screen */
936 priv->y = (surfheight - priv->height) / 2;
937 priv->y_screen_top = priv->y * v_scale;
938 priv->y_screen_bottom = priv->y_screen_top + priv->dstheight;
940 priv->dirty_off_frame[0].x = -1;
941 priv->dirty_off_frame[0].y = -1;
942 priv->dirty_off_frame[1].x = -1;
943 priv->dirty_off_frame[1].y = -1;
945 /* Make sure the entire screen is updated */
946 vo_osd_changed(1);
948 if(priv->rgbsurface)
949 SDL_FreeSurface(priv->rgbsurface);
950 else if(priv->overlay)
951 SDL_FreeYUVOverlay(priv->overlay);
953 priv->rgbsurface = NULL;
954 priv->overlay = NULL;
956 if(priv->mode != YUV && (priv->format&0xFF) == priv->bpp) {
957 if(strcmp(priv->driver, "x11") == 0) {
958 priv->dblit = 1;
959 priv->framePlaneRGB = priv->width * priv->height * priv->surface->format->BytesPerPixel;
960 priv->stridePlaneRGB = priv->width * priv->surface->format->BytesPerPixel;
961 erase_rectangle(0, 0, priv->surface->w, priv->surface->h);
962 return 0;
966 switch(priv->format) {
967 /* Initialize and create the RGB Surface used for video out in BGR/RGB mode */
968 //SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
969 // SDL_SWSURFACE,SDL_HWSURFACE,SDL_SRCCOLORKEY, priv->flags? guess: exchange Rmask and Bmask for BGR<->RGB
970 // 32 bit: a:ff000000 r:ff000 g:ff00 b:ff
971 // 24 bit: r:ff0000 g:ff00 b:ff
972 // 16 bit: r:1111100000000000b g:0000011111100000b b:0000000000011111b
973 // 15 bit: r:111110000000000b g:000001111100000b b:000000000011111b
974 // FIXME: colorkey detect based on bpp, FIXME static bpp value, FIXME alpha value correct?
975 case IMGFMT_RGB15:
976 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 15, 31, 992, 31744, 0);
977 break;
978 case IMGFMT_BGR15:
979 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 15, 31744, 992, 31, 0);
980 break;
981 case IMGFMT_RGB16:
982 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 16, 31, 2016, 63488, 0);
983 break;
984 case IMGFMT_BGR16:
985 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 16, 63488, 2016, 31, 0);
986 break;
987 case IMGFMT_RGB24:
988 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 24, 0x0000FF, 0x00FF00, 0xFF0000, 0);
989 break;
990 case IMGFMT_BGR24:
991 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 24, 0xFF0000, 0x00FF00, 0x0000FF, 0);
992 break;
993 case IMGFMT_RGB32:
994 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0/*0xFF000000*/);
995 break;
996 case IMGFMT_BGR32:
997 priv->rgbsurface = SDL_CreateRGBSurface (SDL_SRCCOLORKEY, surfwidth, surfheight, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0/*0xFF000000*/);
998 break;
999 default:
1000 /* Initialize and create the YUV Overlay used for video out */
1001 if (!(priv->overlay = SDL_CreateYUVOverlay (surfwidth, surfheight, priv->format, priv->surface))) {
1002 printf ("SDL: Couldn't create a YUV overlay: %s\n", SDL_GetError());
1003 return -1;
1005 priv->framePlaneY = priv->width * priv->height;
1006 priv->framePlaneUV = (priv->width * priv->height) >> 2;
1007 priv->framePlaneYUY = priv->width * priv->height * 2;
1008 priv->stridePlaneY = priv->width;
1009 priv->stridePlaneUV = priv->width/2;
1010 priv->stridePlaneYUY = priv->width * 2;
1013 if(priv->mode != YUV) {
1014 if(!priv->rgbsurface) {
1015 printf ("SDL: Couldn't create a RGB surface: %s\n", SDL_GetError());
1016 return -1;
1019 priv->dblit = 0;
1021 if((priv->format&0xFF) != priv->bpp)
1022 printf("SDL: using depth/colorspace conversion, this will slow things"
1023 "down (%ibpp -> %ibpp).\n", priv->format&0xFF, priv->bpp);
1025 priv->framePlaneRGB = priv->width * priv->height * priv->rgbsurface->format->BytesPerPixel;
1026 priv->stridePlaneRGB = priv->width * priv->rgbsurface->format->BytesPerPixel;
1029 erase_rectangle(0, 0, surfwidth, surfheight);
1031 return 0;
1036 * Draw a frame to the SDL YUV overlay.
1038 * params : *src[] == the Y, U, and V planes that make up the frame.
1039 * returns : non-zero on success, zero on error.
1042 //static int sdl_draw_frame (frame_t *frame)
1043 static uint32_t draw_frame(uint8_t *src[])
1045 struct sdl_priv_s *priv = &sdl_priv;
1046 uint8_t *dst;
1047 int i;
1048 uint8_t *mysrc = src[0];
1050 switch(priv->format){
1051 case IMGFMT_YUY2:
1052 case IMGFMT_UYVY:
1053 case IMGFMT_YVYU:
1054 SDL_OVR_LOCK(-1)
1055 dst = (uint8_t *) *(priv->overlay->pixels) + priv->overlay->pitches[0]*priv->y;
1056 if(priv->flip) {
1057 mysrc+=priv->framePlaneYUY;
1058 for(i = 0; i < priv->height; i++) {
1059 mysrc-=priv->stridePlaneYUY;
1060 memcpy (dst, mysrc, priv->stridePlaneYUY);
1061 dst+=priv->overlay->pitches[0];
1064 else memcpy (dst, src[0], priv->framePlaneYUY);
1065 SDL_OVR_UNLOCK
1066 break;
1068 case IMGFMT_RGB15:
1069 case IMGFMT_BGR15:
1070 case IMGFMT_RGB16:
1071 case IMGFMT_BGR16:
1072 case IMGFMT_RGB24:
1073 case IMGFMT_BGR24:
1074 case IMGFMT_RGB32:
1075 case IMGFMT_BGR32:
1076 if(priv->dblit) {
1077 SDL_SRF_LOCK(priv->surface, -1)
1078 dst = (uint8_t *) priv->surface->pixels + priv->y*priv->surface->pitch;
1079 if(priv->flip) {
1080 mysrc+=priv->framePlaneRGB;
1081 for(i = 0; i < priv->height; i++) {
1082 mysrc-=priv->stridePlaneRGB;
1083 memcpy (dst, mysrc, priv->stridePlaneRGB);
1084 dst += priv->surface->pitch;
1087 else memcpy (dst, src[0], priv->framePlaneRGB);
1088 SDL_SRF_UNLOCK(priv->surface)
1089 } else {
1090 SDL_SRF_LOCK(priv->rgbsurface, -1)
1091 dst = (uint8_t *) priv->rgbsurface->pixels + priv->y*priv->rgbsurface->pitch;
1092 if(priv->flip) {
1093 mysrc+=priv->framePlaneRGB;
1094 for(i = 0; i < priv->height; i++) {
1095 mysrc-=priv->stridePlaneRGB;
1096 memcpy (dst, mysrc, priv->stridePlaneRGB);
1097 dst += priv->rgbsurface->pitch;
1100 else memcpy (dst, src[0], priv->framePlaneRGB);
1101 SDL_SRF_UNLOCK(priv->rgbsurface)
1103 break;
1107 return 0;
1112 * Draw a slice (16 rows of image) to the SDL YUV overlay.
1114 * params : *src[] == the Y, U, and V planes that make up the slice.
1115 * returns : non-zero on error, zero on success.
1118 //static uint32_t draw_slice(uint8_t *src[], uint32_t slice_num)
1119 static uint32_t draw_slice(uint8_t *image[], int stride[], int w,int h,int x,int y)
1121 struct sdl_priv_s *priv = &sdl_priv;
1122 uint8_t *dst;
1124 SDL_OVR_LOCK(-1)
1126 y += priv->y;
1128 dst = priv->overlay->pixels[0] + priv->overlay->pitches[0]*y + x;
1129 memcpy_pic(dst, image[0], w, h, priv->overlay->pitches[0], stride[0]);
1130 x/=2;y/=2;w/=2;h/=2;
1132 switch(priv->format) {
1133 case IMGFMT_YV12:
1134 dst = priv->overlay->pixels[2] + priv->overlay->pitches[2]*y + x;
1135 memcpy_pic(dst, image[1], w, h, priv->overlay->pitches[2], stride[1]);
1137 dst = priv->overlay->pixels[1] + priv->overlay->pitches[1]*y + x;
1138 memcpy_pic(dst, image[2], w, h, priv->overlay->pitches[1], stride[2]);
1140 break;
1141 case IMGFMT_I420:
1142 case IMGFMT_IYUV:
1143 dst = priv->overlay->pixels[1] + priv->overlay->pitches[1]*y + x;
1144 memcpy_pic(dst, image[1], w, h, priv->overlay->pitches[1], stride[1]);
1146 dst = priv->overlay->pixels[2] + priv->overlay->pitches[2]*y + x;
1147 memcpy_pic(dst, image[2], w, h, priv->overlay->pitches[2], stride[2]);
1149 break;
1150 default:
1151 printf("SDL: unsupported image format in draw_slice, contact MPlayer developers!\n");
1154 SDL_OVR_UNLOCK
1156 return 0;
1162 * Checks for SDL keypress and window resize events
1164 * params : none
1165 * returns : doesn't return
1168 #include "osdep/keycodes.h"
1169 extern void mplayer_put_key(int code);
1171 #define shift_key (event.key.keysym.mod==(KMOD_LSHIFT||KMOD_RSHIFT))
1172 static void check_events (void)
1174 struct sdl_priv_s *priv = &sdl_priv;
1175 SDL_Event event;
1176 SDLKey keypressed = 0;
1178 /* Poll the waiting SDL Events */
1179 while ( SDL_PollEvent(&event) ) {
1180 switch (event.type) {
1182 /* capture window resize events */
1183 case SDL_VIDEORESIZE:
1184 if(!priv->dblit)
1185 set_video_mode(event.resize.w, event.resize.h, priv->bpp, priv->sdlflags);
1187 /* save video extents, to restore them after going fullscreen */
1188 //if(!(priv->surface->flags & SDL_FULLSCREEN)) {
1189 priv->windowsize.w = priv->surface->w;
1190 priv->windowsize.h = priv->surface->h;
1192 if(verbose > 2) printf("SDL: Window resize\n");
1193 break;
1195 case SDL_MOUSEBUTTONDOWN:
1196 if(vo_nomouse_input)
1197 break;
1198 if(event.button.button == 4 || event.button.button == 5)
1199 mplayer_put_key(MOUSE_BASE+event.button.button-1);
1200 else
1201 mplayer_put_key((MOUSE_BASE+event.button.button-1) | MP_KEY_DOWN);
1202 break;
1204 case SDL_MOUSEBUTTONUP:
1205 if(vo_nomouse_input)
1206 break;
1207 mplayer_put_key(MOUSE_BASE+event.button.button-1);
1208 break;
1210 /* graphics mode selection shortcuts */
1211 #ifdef BUGGY_SDL
1212 case SDL_KEYDOWN:
1213 switch(event.key.keysym.sym) {
1214 case SDLK_UP: mplayer_put_key(KEY_UP); break;
1215 case SDLK_DOWN: mplayer_put_key(KEY_DOWN); break;
1216 case SDLK_LEFT: mplayer_put_key(KEY_LEFT); break;
1217 case SDLK_RIGHT: mplayer_put_key(KEY_RIGHT); break;
1218 case SDLK_LESS: mplayer_put_key(shift_key?'>':'<'); break;
1219 case SDLK_GREATER: mplayer_put_key('>'); break;
1220 case SDLK_ASTERISK:
1221 case SDLK_KP_MULTIPLY:
1222 case SDLK_SLASH:
1223 case SDLK_KP_DIVIDE:
1224 default: break;
1226 break;
1227 case SDL_KEYUP:
1228 #else
1229 case SDL_KEYDOWN:
1230 #endif
1231 keypressed = event.key.keysym.sym;
1232 if(verbose > 1) printf("SDL: Key pressed: '%i'\n", keypressed);
1234 /* c key pressed. c cycles through available fullscreenmodes, if we have some */
1235 if ( ((keypressed == SDLK_c)) && (priv->fullmodes) ) {
1236 /* select next fullscreen mode */
1237 priv->fullmode++;
1238 if (priv->fullmode > (findArrayEnd(priv->fullmodes) - 1)) priv->fullmode = 0;
1239 set_fullmode(priv->fullmode);
1241 if(verbose > 1) printf("SDL: Set next available fullscreen mode.\n");
1244 else if ( keypressed == SDLK_n ) {
1245 #ifdef HAVE_X11
1246 aspect(&priv->dstwidth, &priv->dstheight,A_NOZOOM);
1247 #endif
1248 if (priv->surface->w != priv->dstwidth || priv->surface->h != priv->dstheight) {
1249 set_video_mode(priv->dstwidth, priv->dstheight, priv->bpp, priv->sdlflags);
1250 priv->windowsize.w = priv->surface->w;
1251 priv->windowsize.h = priv->surface->h;
1252 if(verbose > 1) printf("SDL: Normal size\n");
1253 } else
1254 if (priv->surface->w != priv->dstwidth * 2 || priv->surface->h != priv->dstheight * 2) {
1255 set_video_mode(priv->dstwidth * 2, priv->dstheight * 2, priv->bpp, priv->sdlflags);
1256 priv->windowsize.w = priv->surface->w;
1257 priv->windowsize.h = priv->surface->h;
1258 if(verbose > 1) printf("SDL: Double size\n");
1262 else switch(keypressed){
1263 case SDLK_RETURN: mplayer_put_key(KEY_ENTER);break;
1264 case SDLK_ESCAPE: mplayer_put_key(KEY_ESC);break;
1265 case SDLK_q: mplayer_put_key('q');break;
1266 case SDLK_F1: mplayer_put_key(KEY_F+1);break;
1267 case SDLK_F2: mplayer_put_key(KEY_F+2);break;
1268 case SDLK_F3: mplayer_put_key(KEY_F+3);break;
1269 case SDLK_F4: mplayer_put_key(KEY_F+4);break;
1270 case SDLK_F5: mplayer_put_key(KEY_F+5);break;
1271 case SDLK_F6: mplayer_put_key(KEY_F+6);break;
1272 case SDLK_F7: mplayer_put_key(KEY_F+7);break;
1273 case SDLK_F8: mplayer_put_key(KEY_F+8);break;
1274 case SDLK_F9: mplayer_put_key(KEY_F+9);break;
1275 case SDLK_F10: mplayer_put_key(KEY_F+10);break;
1276 case SDLK_F11: mplayer_put_key(KEY_F+11);break;
1277 case SDLK_F12: mplayer_put_key(KEY_F+12);break;
1278 /*case SDLK_o: mplayer_put_key('o');break;
1279 case SDLK_SPACE: mplayer_put_key(' ');break;
1280 case SDLK_p: mplayer_put_key('p');break;*/
1281 case SDLK_7: mplayer_put_key(shift_key?'/':'7');
1282 case SDLK_PLUS: mplayer_put_key(shift_key?'*':'+');
1283 case SDLK_KP_PLUS: mplayer_put_key('+');break;
1284 case SDLK_MINUS:
1285 case SDLK_KP_MINUS: mplayer_put_key('-');break;
1286 case SDLK_TAB: mplayer_put_key('\t');break;
1287 case SDLK_PAGEUP: mplayer_put_key(KEY_PAGE_UP);break;
1288 case SDLK_PAGEDOWN: mplayer_put_key(KEY_PAGE_DOWN);break;
1289 #ifdef BUGGY_SDL
1290 case SDLK_UP:
1291 case SDLK_DOWN:
1292 case SDLK_LEFT:
1293 case SDLK_RIGHT:
1294 case SDLK_ASTERISK:
1295 case SDLK_KP_MULTIPLY:
1296 case SDLK_SLASH:
1297 case SDLK_KP_DIVIDE:
1298 break;
1299 #else
1300 case SDLK_UP: mplayer_put_key(KEY_UP);break;
1301 case SDLK_DOWN: mplayer_put_key(KEY_DOWN);break;
1302 case SDLK_LEFT: mplayer_put_key(KEY_LEFT);break;
1303 case SDLK_RIGHT: mplayer_put_key(KEY_RIGHT);break;
1304 case SDLK_LESS: mplayer_put_key(shift_key?'>':'<'); break;
1305 case SDLK_GREATER: mplayer_put_key('>'); break;
1306 case SDLK_ASTERISK:
1307 case SDLK_KP_MULTIPLY: mplayer_put_key('*'); break;
1308 case SDLK_SLASH:
1309 case SDLK_KP_DIVIDE: mplayer_put_key('/'); break;
1310 #endif
1311 case SDLK_KP0: mplayer_put_key(KEY_KP0); break;
1312 case SDLK_KP1: mplayer_put_key(KEY_KP1); break;
1313 case SDLK_KP2: mplayer_put_key(KEY_KP2); break;
1314 case SDLK_KP3: mplayer_put_key(KEY_KP3); break;
1315 case SDLK_KP4: mplayer_put_key(KEY_KP4); break;
1316 case SDLK_KP5: mplayer_put_key(KEY_KP5); break;
1317 case SDLK_KP6: mplayer_put_key(KEY_KP6); break;
1318 case SDLK_KP7: mplayer_put_key(KEY_KP7); break;
1319 case SDLK_KP8: mplayer_put_key(KEY_KP8); break;
1320 case SDLK_KP9: mplayer_put_key(KEY_KP9); break;
1321 case SDLK_KP_PERIOD: mplayer_put_key(KEY_KPDEC); break;
1322 case SDLK_KP_ENTER: mplayer_put_key(KEY_KPENTER); break;
1323 default:
1324 //printf("got scancode: %d keysym: %d mod: %d %d\n", event.key.keysym.scancode, keypressed, event.key.keysym.mod);
1325 mplayer_put_key(keypressed);
1328 break;
1329 case SDL_QUIT: mplayer_put_key('q');break;
1333 #undef shift_key
1335 /* Erase (paint it black) the rectangle specified by x, y, w and h in the surface
1336 or overlay which is used for OSD
1338 static void erase_rectangle(int x, int y, int w, int h)
1340 struct sdl_priv_s *priv = &sdl_priv;
1342 switch(priv->format) {
1343 case IMGFMT_YV12:
1344 case IMGFMT_I420:
1345 case IMGFMT_IYUV:
1347 SDL_OVR_LOCK((void) 0)
1349 /* Erase Y plane */
1350 erase_area_1(x, w, h,
1351 priv->overlay->pitches[0], 0,
1352 priv->overlay->pixels[0] +
1353 priv->overlay->pitches[0]*y);
1355 /* Erase U and V planes */
1356 w /= 2;
1357 x /= 2;
1358 h /= 2;
1359 y /= 2;
1361 erase_area_1(x, w, h,
1362 priv->overlay->pitches[1], 128,
1363 priv->overlay->pixels[1] +
1364 priv->overlay->pitches[1]*y);
1366 erase_area_1(x, w, h,
1367 priv->overlay->pitches[2], 128,
1368 priv->overlay->pixels[2] +
1369 priv->overlay->pitches[2]*y);
1370 SDL_OVR_UNLOCK
1371 break;
1374 case IMGFMT_YUY2:
1375 case IMGFMT_YVYU:
1377 /* yuy2 and yvyu represent black the same way */
1378 uint8_t yuy2_black[] = {0, 128, 0, 128};
1380 SDL_OVR_LOCK((void) 0)
1381 erase_area_4(x*2, w*2, h,
1382 priv->overlay->pitches[0],
1383 *((uint32_t*) yuy2_black),
1384 priv->overlay->pixels[0] +
1385 priv->overlay->pitches[0]*y);
1386 SDL_OVR_UNLOCK
1387 break;
1390 case IMGFMT_UYVY:
1392 uint8_t uyvy_black[] = {128, 0, 128, 0};
1394 SDL_OVR_LOCK((void) 0)
1395 erase_area_4(x*2, w*2, h,
1396 priv->overlay->pitches[0],
1397 *((uint32_t*) uyvy_black),
1398 priv->overlay->pixels[0] +
1399 priv->overlay->pitches[0]*y);
1400 SDL_OVR_UNLOCK
1401 break;
1404 case IMGFMT_RGB15:
1405 case IMGFMT_BGR15:
1406 case IMGFMT_RGB16:
1407 case IMGFMT_BGR16:
1408 case IMGFMT_RGB24:
1409 case IMGFMT_BGR24:
1410 case IMGFMT_RGB32:
1411 case IMGFMT_BGR32:
1413 SDL_Rect rect;
1414 rect.w = w; rect.h = h;
1415 rect.x = x; rect.y = y;
1417 if(priv->dblit) {
1418 SDL_SRF_LOCK(priv->surface, (void) 0)
1419 SDL_FillRect(priv->surface, &rect, 0);
1420 SDL_SRF_UNLOCK(priv->surface)
1422 else {
1423 SDL_SRF_LOCK(priv->rgbsurface, (void) 0)
1424 SDL_FillRect(priv->rgbsurface, &rect, 0);
1425 SDL_SRF_UNLOCK(priv->rgbsurface)
1427 break;
1432 static void draw_osd(void)
1433 { struct sdl_priv_s *priv = &sdl_priv;
1435 priv->osd_has_changed = vo_osd_changed(0);
1437 if(priv->osd_has_changed)
1439 int i;
1441 for(i = 0; i < 2; i++) {
1442 if(priv->dirty_off_frame[i].x < 0 || priv->dirty_off_frame[i].y < 0)
1443 continue;
1445 erase_rectangle(priv->dirty_off_frame[i].x, priv->dirty_off_frame[i].y,
1446 priv->dirty_off_frame[i].w, priv->dirty_off_frame[i].h);
1448 priv->dirty_off_frame[i].x = -1;
1449 priv->dirty_off_frame[i].y = -1;
1453 /* update osd/subtitles */
1454 if(priv->mode == YUV)
1455 vo_draw_text(priv->overlay->w, priv->overlay->h, draw_alpha);
1456 else {
1457 if(priv->dblit)
1458 vo_draw_text(priv->surface->w, priv->surface->h, draw_alpha);
1459 else
1460 vo_draw_text(priv->rgbsurface->w, priv->rgbsurface->h, draw_alpha);
1464 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1465 * are given in bytes. 4 bytes at a time.
1467 static void erase_area_4(int x_start, int width, int height, int pitch, uint32_t color, uint8_t* pixels)
1469 int x_end = x_start/4 + width/4;
1470 int x, y;
1471 uint32_t* data = (uint32_t*) pixels;
1473 x_start /= 4;
1474 pitch /= 4;
1476 for(y = 0; y < height; y++) {
1477 for(x = x_start; x < x_end; x++)
1478 data[y*pitch + x] = color;
1482 /* Fill area beginning at 'pixels' with 'color'. 'x_start', 'width' and 'pitch'
1483 * are given in bytes. 1 byte at a time.
1485 static void erase_area_1(int x_start, int width, int height, int pitch, uint8_t color, uint8_t* pixels)
1487 int y;
1489 for(y = 0; y < height; y++) {
1490 memset(&pixels[y*pitch + x_start], color, width);
1495 * Display the surface we have written our data to
1497 * params : mode == index of the desired fullscreen mode
1498 * returns : doesn't return
1501 static void flip_page (void)
1503 struct sdl_priv_s *priv = &sdl_priv;
1505 switch(priv->format) {
1506 case IMGFMT_RGB15:
1507 case IMGFMT_BGR15:
1508 case IMGFMT_RGB16:
1509 case IMGFMT_BGR16:
1510 case IMGFMT_RGB24:
1511 case IMGFMT_BGR24:
1512 case IMGFMT_RGB32:
1513 case IMGFMT_BGR32:
1514 if(!priv->dblit) {
1515 /* blit to the RGB surface */
1516 if(SDL_BlitSurface (priv->rgbsurface, NULL, priv->surface, NULL))
1517 printf("SDL: Blit failed: %s\n", SDL_GetError());
1520 /* update screen */
1521 //SDL_UpdateRect(priv->surface, 0, 0, priv->surface->clip_rect.w, priv->surface->clip_rect.h);
1522 if(priv->osd_has_changed) {
1523 priv->osd_has_changed = 0;
1524 SDL_UpdateRects(priv->surface, 1, &priv->surface->clip_rect);
1526 else
1527 SDL_UpdateRect(priv->surface, 0, priv->y_screen_top,
1528 priv->surface->clip_rect.w, priv->y_screen_bottom);
1530 /* check if we have a double buffered surface and flip() if we do. */
1531 if ( priv->surface->flags & SDL_DOUBLEBUF )
1532 SDL_Flip(priv->surface);
1534 break;
1535 default:
1536 /* blit to the YUV overlay */
1537 SDL_DisplayYUVOverlay (priv->overlay, &priv->surface->clip_rect);
1539 /* check if we have a double buffered surface and flip() if we do. */
1540 if ( priv->surface->flags & SDL_DOUBLEBUF )
1541 SDL_Flip(priv->surface);
1543 //SDL_LockYUVOverlay (priv->overlay); // removed because unused!?
1547 static uint32_t
1548 query_format(uint32_t format)
1550 switch(format){
1551 case IMGFMT_YV12:
1552 // it seems buggy (not hw accelerated), so just use YV12 instead!
1553 // case IMGFMT_I420:
1554 // case IMGFMT_IYUV:
1555 case IMGFMT_YUY2:
1556 case IMGFMT_UYVY:
1557 case IMGFMT_YVYU:
1558 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD |
1559 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
1560 case IMGFMT_RGB15:
1561 case IMGFMT_BGR15:
1562 case IMGFMT_RGB16:
1563 case IMGFMT_BGR16:
1564 case IMGFMT_RGB24:
1565 case IMGFMT_BGR24:
1566 case IMGFMT_RGB32:
1567 case IMGFMT_BGR32:
1568 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_FLIP;
1570 return 0;
1574 static void
1575 uninit(void)
1577 #ifdef HAVE_X11
1578 struct sdl_priv_s *priv = &sdl_priv;
1579 if(priv->X) {
1580 if(verbose) printf("SDL: activating XScreensaver/DPMS\n");
1581 vo_x11_uninit();
1583 #endif
1584 sdl_close();
1586 /* Cleanup SDL */
1587 if(SDL_WasInit(SDL_INIT_VIDEO))
1588 SDL_QuitSubSystem(SDL_INIT_VIDEO);
1590 if(verbose > 2) printf("SDL: Closed Plugin\n");
1594 static uint32_t preinit(const char *arg)
1596 struct sdl_priv_s *priv = &sdl_priv;
1598 priv->rgbsurface = NULL;
1599 priv->overlay = NULL;
1600 priv->surface = NULL;
1602 if(verbose > 2) printf("SDL: Opening Plugin\n");
1604 if(vo_subdevice) setenv("SDL_VIDEODRIVER", vo_subdevice, 1);
1606 /* does the user want SDL to try and force Xv */
1607 if(sdl_forcexv) setenv("SDL_VIDEO_X11_NODIRECTCOLOR", "1", 1);
1609 /* does the user want to disable Xv and use software scaling instead */
1610 if(sdl_noxv) setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
1612 /* default to no fullscreen mode, we'll set this as soon we have the avail. modes */
1613 priv->fullmode = -2;
1615 priv->fullmodes = NULL;
1616 priv->bpp = 0;
1618 /* initialize the SDL Video system */
1619 if (!SDL_WasInit(SDL_INIT_VIDEO)) {
1620 if (SDL_Init (SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE)) {
1621 printf("SDL: Initializing of SDL failed: %s.\n", SDL_GetError());
1622 return -1;
1626 SDL_VideoDriverName(priv->driver, 8);
1627 printf("SDL: Using driver: %s\n", priv->driver);
1629 priv->X = 0;
1630 #ifdef HAVE_X11
1631 if(vo_init()) {
1632 if(verbose) printf("SDL: deactivating XScreensaver/DPMS\n");
1633 priv->XWidth = vo_screenwidth;
1634 priv->XHeight = vo_screenheight;
1635 priv->X = 1;
1636 if(verbose) printf("SDL: X11 Resolution %ix%i\n", priv->XWidth, priv->XHeight);
1638 #endif
1640 return 0;
1643 static uint32_t get_image(mp_image_t *mpi)
1645 struct sdl_priv_s *priv = &sdl_priv;
1647 if(priv->format != mpi->imgfmt) return VO_FALSE;
1648 if(mpi->type == MP_IMGTYPE_STATIC || mpi->type == MP_IMGTYPE_TEMP) {
1649 if(mpi->flags&MP_IMGFLAG_PLANAR) {
1650 mpi->planes[0] = priv->overlay->pixels[0] + priv->y*priv->overlay->pitches[0];
1651 mpi->stride[0] = priv->overlay->pitches[0];
1652 if(mpi->flags&MP_IMGFLAG_SWAPPED) {
1653 mpi->planes[1] = priv->overlay->pixels[1] + priv->y*priv->overlay->pitches[1]/2;
1654 mpi->stride[1] = priv->overlay->pitches[1];
1655 mpi->planes[2] = priv->overlay->pixels[2] + priv->y*priv->overlay->pitches[2]/2;
1656 mpi->stride[2] = priv->overlay->pitches[2];
1657 } else {
1658 mpi->planes[2] = priv->overlay->pixels[1] + priv->y*priv->overlay->pitches[1]/2;
1659 mpi->stride[2] = priv->overlay->pitches[1];
1660 mpi->planes[1] = priv->overlay->pixels[2] + priv->y*priv->overlay->pitches[2]/2;
1661 mpi->stride[1] = priv->overlay->pitches[2];
1664 else if(IMGFMT_IS_RGB(priv->format) || IMGFMT_IS_BGR(priv->format)) {
1665 if(priv->dblit) {
1666 if(mpi->type == MP_IMGTYPE_STATIC && (priv->surface->flags & SDL_DOUBLEBUF))
1667 return VO_FALSE;
1669 mpi->planes[0] = priv->surface->pixels + priv->y*priv->surface->pitch;
1670 mpi->stride[0] = priv->surface->pitch;
1672 else {
1673 mpi->planes[0] = priv->rgbsurface->pixels + priv->y*priv->rgbsurface->pitch;
1674 mpi->stride[0] = priv->rgbsurface->pitch;
1677 else {
1678 mpi->planes[0] = priv->overlay->pixels[0] + priv->y*priv->overlay->pitches[0];
1679 mpi->stride[0] = priv->overlay->pitches[0];
1682 mpi->flags|=MP_IMGFLAG_DIRECT;
1683 return VO_TRUE;
1686 return VO_FALSE;
1689 static uint32_t control(uint32_t request, void *data, ...)
1691 struct sdl_priv_s *priv = &sdl_priv;
1692 switch (request) {
1693 case VOCTRL_GET_IMAGE:
1694 return get_image(data);
1695 case VOCTRL_QUERY_FORMAT:
1696 return query_format(*((uint32_t*)data));
1697 case VOCTRL_FULLSCREEN:
1698 if (priv->surface->flags & SDL_FULLSCREEN) {
1699 set_video_mode(priv->windowsize.w, priv->windowsize.h, priv->bpp, priv->sdlflags);
1700 SDL_ShowCursor(1);
1701 if(verbose > 1) printf("SDL: Windowed mode\n");
1702 } else if (priv->fullmodes) {
1703 set_fullmode(priv->fullmode);
1704 if(verbose > 1) printf("SDL: Set fullscreen mode\n");
1706 return VO_TRUE;
1709 return VO_NOTIMPL;