videoout.c 45.7 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#include "mediastreamer-config.h"
#endif

#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/msvideo.h"
smorlat's avatar
smorlat committed
26 27 28

/*required for dllexport of win_display_desc */
#define INVIDEOUT_C 1
aymeric's avatar
aymeric committed
29 30 31 32
#include "mediastreamer2/msvideoout.h"

#include "ffmpeg-priv.h"

33 34
#define SCALE_FACTOR 4.0f
#define SELVIEW_POS_INACTIVE -100.0
35

smorlat's avatar
smorlat committed
36
static int video_out_set_vsize(MSFilter *f,void *arg);
aymeric's avatar
aymeric committed
37

38
int ms_display_poll_event(MSDisplay *d, MSDisplayEvent *ev){
aymeric's avatar
aymeric committed
39 40
	if (d->desc->pollevent)
		return d->desc->pollevent(d,ev);
41
	else return -1;
aymeric's avatar
aymeric committed
42 43
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
static int gcd(int m, int n)
{
   if(n == 0)
     return m;
   else
     return gcd(n, m % n);
}
   
static void reduce(int *num, int *denom)
{
   int divisor = gcd(*num, *denom);
   *num /= divisor;
   *denom /= divisor;
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
#undef HAVE_X11_EXTENSIONS_XV_H
#if defined(HAVE_X11_EXTENSIONS_XV_H) && defined(HAVE_X11_XLIB_H)

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>

//#include <SDL/SDL.h>
//#include <SDL/SDL_video.h>

typedef struct XvDisplay {
	MSFilter *filter;
	ms_mutex_t xv_mutex;
	//SDL_Surface *sdl_screen;
	Display *display;
	Window window;  
	GC gc;
	XImage* ximage;  
	XvImage *xvImage;
	int XvPort;
	int XvFormat;
	float sv_scalefactor;
	MSVideoSize screen_size;

	MSPicture fb;
	mblk_t *fb_block;
	MSPicture fb_selfview;
	mblk_t *fb_selfview_block;
} XvDisplay;

#include <SDL/SDL_syswm.h>

static long xv_get_native_window_id(XvDisplay *wd){
#if 0
	SDL_SysWMinfo info;
	SDL_VERSION(&info.version);
	if ( SDL_GetWMInfo(&info) ) {
		if ( info.subsystem == SDL_SYSWM_X11 ) {
			return (long) info.info.x11.wmwindow;
		}
	}
#else
	return (long) wd->window;
#endif
	return 0;
}

static void xv_show_window(XvDisplay *wd, bool_t show){
#if 0
	SDL_SysWMinfo info;
	SDL_VERSION(&info.version);
	if ( SDL_GetWMInfo(&info) ) {
		if ( info.subsystem == SDL_SYSWM_X11 ) {
		
			info.info.x11.lock_func();
			wd->display = info.info.x11.display;
			wd->window = info.info.x11.wmwindow;
			if (show)
				XMapWindow(wd->display,wd->window);
			else
				XUnmapWindow(wd->display,wd->window);
			info.info.x11.unlock_func();
		}
	}
#else
	if (show)
		XMapWindow(wd->display,wd->window);
	else
		XUnmapWindow(wd->display,wd->window);
#endif
}


static void xv_display_uninit(MSDisplay *obj);

static int xv_create_window(XvDisplay *wd, int w, int h){
	//static bool_t once=TRUE;
	XvAdaptorInfo *xv_adaptor_info=NULL;
	XvAdaptorInfo *ai;
	unsigned int xv_adaptors;
	unsigned int i;
	XGCValues xv_xgc;

	int screen;
	XVisualInfo vinfo;
	XWindowAttributes wattr;
	unsigned long xswamask;
	XSetWindowAttributes xswa;
	XSizeHints hint;
	
#if 0
	uint32_t flags = SDL_ANYFORMAT | SDL_DOUBLEBUF | SDL_RESIZABLE;
	const SDL_VideoInfo *info;
	info =SDL_GetVideoInfo();
	if (info->wm_available) {
		ms_message("Using window manager");
	}
	if (info->hw_available) {
		ms_message("hw surface available (%dk memory)", info->video_mem);
		flags |= SDL_HWSURFACE;
	}
	else
		flags |= SDL_SWSURFACE;
	
	if (info->blit_hw) {
		ms_message("hw surface available (%dk memory)", info->video_mem);
		flags |= SDL_ASYNCBLIT;
	}
	if (info->blit_hw_CC)
		ms_message("Colorkey blits between hw surfaces: accelerated");
	if (info->blit_hw_A)
		ms_message("Alpha blits between hw surfaces:  accelerated");
	if (info->blit_sw)
		ms_message("Copy blits from sw to hw surfaces:  accelerated");
	if (info->blit_hw_CC)
		ms_message("Colorkey blits between sw to hw surfaces: accelerated");
	if (info->blit_hw_A)
		ms_message("Alpha blits between sw to hw surfaces: accelerated");

	wd->sdl_screen = SDL_SetVideoMode(wd->screen_size.width,wd->screen_size.height, 0,flags);
	if (wd->sdl_screen == NULL ) {
		ms_warning("no hardware for video mode: %s\n",
				   SDL_GetError());
	}
	if (wd->sdl_screen->flags & SDL_HWSURFACE) ms_message("SDL surface created in hardware");
	if (once) {
		SDL_WM_SetCaption("Video window", NULL);
		once=FALSE;
	}
#endif

	hint.x=0;
	hint.y=0;
	hint.width=w;
	hint.height=h;
	hint.flags=PPosition | PSize;
	
	wd->display = XOpenDisplay(":0.0");
	if (wd->display==NULL){
		ms_error("videout.c:xv XOpenDisplay failed");
		return -1;
	}
	ms_message("videout.c:xv XOpenDisplay done");
		
	XGetWindowAttributes(wd->display, DefaultRootWindow(wd->display), &wattr);
	screen = DefaultScreen(wd->display);
	XMatchVisualInfo(wd->display, screen, wattr.depth, TrueColor, &vinfo);

	ms_message("videout.c:xv depth=%i", wattr.depth);
	xswa.background_pixel = 0;
	xswa.border_pixel = 1;
	xswamask = CWBackPixel | CWBorderPixel;
	wd->window = XCreateWindow(wd->display,
							   RootWindow(wd->display, screen),
							   hint.x, hint.y, hint.width, hint.height, 4,
							   wattr.depth,
							   CopyFromParent,
							   vinfo.visual,
							   xswamask,
							   &xswa);
	if (wd->window==NULL){
		ms_error("videout.c:xv XCreateWindow failed");
		return -1;
	}
	
	xv_xgc.graphics_exposures = 0;
	wd->gc = XCreateGC(wd->display, wd->window, 0L, &xv_xgc);
	if (wd->gc==NULL){
		ms_error("videout.c:xv XCreateGC failed");
		return -1;
	}

	if (Success != XvQueryAdaptors(wd->display,
                                   wd->window,
                                   &xv_adaptors, 
                                   (XvAdaptorInfo **)&xv_adaptor_info)) {
    }
    
    ai = (XvAdaptorInfo *)xv_adaptor_info;

	wd->XvPort=0;
    for (i = 0; i < xv_adaptors; i++) {
		ms_message("adaptors: name: %s baseport=%i", ai[i].name, ai[i].base_id);
		if ((ai[i].type &XvInputMask) && (ai[i].type &XvImageMask))
			{
				XvPortID xv_p;
				for (xv_p = ai[i].base_id;
                     xv_p < ai[i].base_id + ai[i].num_ports; ++xv_p){
					if (XvGrabPort(wd->display, xv_p, CurrentTime)==0)
						{
							wd->XvPort = xv_p;
							break;
						}
				}
				if (wd->XvPort!=0)
					break;
			}
	}
	XvFreeAdaptorInfo(ai);
	ms_message("using Xvideo port %d for hw scaling", wd->XvPort);
	if (wd->XvPort>0)
		{
			uint32_t fmt = 0x32315659; //YV12
			int formats;
			XvImageFormatValues *fo;
			fo = XvListImageFormats(wd->display, wd->XvPort, (int *)&formats);
			for (i=0;i<formats;i++)
				{
					ms_message("format=[%4.4s] %s", (char *) &fo[i].id, (fo[i].format == XvPacked) ? "packed" : "planar");
					if (fo[i].id==fmt){
						wd->XvFormat =fo[i].id;
						ms_message("found YV12 format");
					}
				}
			XFree(fo);
		}

	wd->xvImage = (XvImage *)XvCreateImage( wd->display , wd->XvPort, wd->XvFormat, NULL, w, h);
	wd->fb_block = yuv_buf_alloc(&wd->fb, w, h);
	wd->xvImage->data = wd->fb_block->b_rptr;
	//SDL_ShowCursor(0);//Hide the mouse cursor if was displayed
	return 0;
}

static bool_t xv_display_init(MSDisplay *obj, MSFilter *f, MSPicture *fbuf, MSPicture *fbuf_selfview){
	XvDisplay *wd = (XvDisplay*)obj->data;
	int i;

	if (wd==NULL){
		//char driver[128];
		/* Initialize the SDL library */
		wd=(XvDisplay*)ms_new0(XvDisplay,1);
		wd->filter = f;
		obj->data=wd;

		#if 0
		if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
			ms_error("Couldn't initialize SDL: %s", SDL_GetError());
			return FALSE;
		}
		if (SDL_VideoDriverName(driver, sizeof(driver))){
			ms_message("Video driver: %s", driver);
		}
#endif
		ms_mutex_init(&wd->xv_mutex,NULL);
		ms_mutex_lock(&wd->xv_mutex);
		wd->screen_size.width = fbuf->w;
		wd->screen_size.height = fbuf->h;
	}else {
		ms_mutex_lock(&wd->xv_mutex);

		if (wd->xvImage!=NULL)
			XFree(wd->xvImage);
		if (wd->fb_block!=NULL)
			freemsg(wd->fb_block);
		XvUngrabPort(wd->display, wd->XvPort, CurrentTime);
		
		//if (wd->sdl_screen!=NULL)
		//	SDL_FreeSurface(wd->sdl_screen);
		//wd->sdl_screen=NULL;
	}
	wd->filter = f;

	wd->XvFormat=0x32315659; //IMGFMT_YV12;
	//xv_show_window(wd, TRUE);
	i=xv_create_window(wd, fbuf->w, fbuf->h);
	if (i==0){
		fbuf->planes[0]=wd->fb.planes[0];
		fbuf->planes[1]=wd->fb.planes[2];
		fbuf->planes[2]=wd->fb.planes[1];
		fbuf->planes[3]=NULL;
		fbuf->strides[0]=wd->fb.strides[0];
		fbuf->strides[1]=wd->fb.strides[2];
		fbuf->strides[2]=wd->fb.strides[1];
		fbuf->strides[3]=0;
		fbuf->w=wd->fb.w;
		fbuf->h=wd->fb.h;
		xv_show_window(wd, TRUE);
		obj->window_id=xv_get_native_window_id(wd);
		ms_mutex_unlock(&wd->xv_mutex);
		return TRUE;
	}
	ms_mutex_unlock(&wd->xv_mutex);
	return FALSE;
}

static void xv_display_lock(MSDisplay *obj){
	XvDisplay *wd = (XvDisplay*)obj->data;
	ms_mutex_lock(&wd->xv_mutex);
	XLockDisplay(wd->display);
	ms_mutex_unlock(&wd->xv_mutex);
}

static void xv_display_unlock(MSDisplay *obj){
	XvDisplay *wd = (XvDisplay*)obj->data;
	ms_mutex_lock(&wd->xv_mutex);
	XUnlockDisplay(wd->display);
	ms_mutex_unlock(&wd->xv_mutex);
}

static void xv_display_update(MSDisplay *obj, int new_image, int new_selfview){
	XvDisplay *wd = (XvDisplay*)obj->data;
	//SDL_Rect rect;
	int ratiow;
	int ratioh;
	int w;
	int h;
	
	//rect.x=0;
	//rect.y=0;
	ms_mutex_lock(&wd->xv_mutex);

	ratiow=wd->fb.w;
	ratioh=wd->fb.h;
	reduce(&ratiow, &ratioh);
	w = wd->screen_size.width/ratiow*ratiow;
	h = wd->screen_size.height/ratioh*ratioh;

	if (h*ratiow>w*ratioh)
	{
		w = w;
		h = w*ratioh/ratiow;
	}
	else
	{
		h = h;
		w = h*ratiow/ratioh;
	}

	if (h*wd->fb.w!=w*wd->fb.h)
		ms_error("wrong ratio");

	//x = (wd->screen_size.width-w)/2;
	//y = (wd->screen_size.height-h)/2;
	//rect.w = w;
	//rect.h = h;
	XLockDisplay(wd->display);
	XvPutImage( wd->display, wd->XvPort, wd->window, wd->gc,
				wd->xvImage, 0, 0, wd->xvImage->width, wd->xvImage->height, 0, 0, w*2, h*2);
	XFlush(wd->display);
	XUnlockDisplay(wd->display);
	ms_mutex_unlock(&wd->xv_mutex);
}

404
static int xv_poll_event(MSDisplay *obj, MSDisplayEvent *ev){
405 406 407 408 409 410 411 412 413 414 415 416 417 418
	XvDisplay *wd = (XvDisplay*)obj->data;
#if 0
	SDL_Event event;
	if (wd->sdl_screen==NULL) return FALSE;
	ms_mutex_lock(&wd->xv_mutex);
	if (SDL_PollEvent(&event)){
		ms_mutex_unlock(&wd->xv_mutex);
		switch(event.type){
			case SDL_VIDEORESIZE:
				ev->evtype=MS_DISPLAY_RESIZE_EVENT;
				ev->w=event.resize.w;
				ev->h=event.resize.h;
				wd->screen_size.width = event.resize.w;
				wd->screen_size.height = event.resize.h;
419
				return 1;
420
			break;
421 422
		default:
			return 0;
423 424 425 426
			break;
		}
	}else ms_mutex_unlock(&wd->xv_mutex);
#endif
427
	return -1;
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
}

static void xv_display_uninit(MSDisplay *obj){
	XvDisplay *wd = (XvDisplay*)obj->data;
#if 0
	SDL_Event event;
	int i;
#endif
	if (wd==NULL)
		return;
	xv_show_window(wd, FALSE);
	if (wd->gc!=NULL)
		XFreeGC(wd->display, wd->gc);
	//if (wd->sdl_screen!=NULL){
	//	SDL_FreeSurface(wd->sdl_screen);
	//	wd->sdl_screen=NULL;
	//}
	if (wd->xvImage!=NULL)
		XFree(wd->xvImage);
	if (wd->fb_block!=NULL)
		freemsg(wd->fb_block);
	XvUngrabPort(wd->display, wd->XvPort, CurrentTime);
	ms_free(wd);
#if 0
#ifdef __linux
	/*purge the event queue before leaving*/
	for(i=0;SDL_PollEvent(&event) && i<100;++i){
	}
#endif
	SDL_Quit();
#endif
}

MSDisplayDesc ms_xv_display_desc={
	.init=xv_display_init,
	.lock=xv_display_lock,
	.unlock=xv_display_unlock,
	.update=xv_display_update,
	.uninit=xv_display_uninit,
	.pollevent=xv_poll_event,
};

#elif HAVE_SDL
aymeric's avatar
aymeric committed
471 472 473 474

#include <SDL/SDL.h>
#include <SDL/SDL_video.h>

475 476 477 478 479 480
typedef struct _SdlDisplay{
	MSFilter *filter;
	bool_t sdl_initialized;
	ms_mutex_t sdl_mutex;
	SDL_Surface *sdl_screen;
	SDL_Overlay *lay;
481

482
	float sv_scalefactor;
483
	MSVideoSize screen_size;
484
} SdlDisplay;
aymeric's avatar
aymeric committed
485 486 487 488 489

#ifdef HAVE_X11_XLIB_H

#include <SDL/SDL_syswm.h>

490 491 492 493 494 495 496 497 498 499 500
static long sdl_get_native_window_id(){
	SDL_SysWMinfo info;
	SDL_VERSION(&info.version);
	if ( SDL_GetWMInfo(&info) ) {
		if ( info.subsystem == SDL_SYSWM_X11 ) {
			return (long) info.info.x11.wmwindow;
		}
	}
	return 0;
}

aymeric's avatar
aymeric committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
static void sdl_show_window(bool_t show){
	SDL_SysWMinfo info;
	SDL_VERSION(&info.version);
	if ( SDL_GetWMInfo(&info) ) {
		if ( info.subsystem == SDL_SYSWM_X11 ) {
			Display *display;
			Window window;
		
			info.info.x11.lock_func();
			display = info.info.x11.display;
			window = info.info.x11.wmwindow;
			if (show)
				XMapWindow(display,window);
			else
				XUnmapWindow(display,window);
			info.info.x11.unlock_func();
		}
	}
}

#else

static void sdl_show_window(bool_t show){
	ms_warning("SDL window show/hide not implemented");
}

527 528 529 530 531
static long sdl_get_native_window_id(){
	ms_warning("sdl_get_native_window_id not implemented");
	return 0;
}

aymeric's avatar
aymeric committed
532 533
#endif

smorlat's avatar
smorlat committed
534 535
static void sdl_display_uninit(MSDisplay *obj);

536
static int sdl_create_window(SdlDisplay *wd, int w, int h){
smorlat's avatar
smorlat committed
537
	static bool_t once=TRUE;
538 539 540 541 542 543 544 545 546 547 548 549
	uint32_t flags = SDL_ANYFORMAT | SDL_DOUBLEBUF | SDL_RESIZABLE;
	const SDL_VideoInfo *info;
	info =SDL_GetVideoInfo();
	if (info->wm_available) {
		ms_message("Using window manager");
	}
	if (info->hw_available) {
		ms_message("hw surface available (%dk memory)", info->video_mem);
		flags |= SDL_HWSURFACE;
	}
	else
		flags |= SDL_SWSURFACE;
550
	
551 552 553
	if (info->blit_hw) {
		ms_message("hw surface available (%dk memory)", info->video_mem);
		flags |= SDL_ASYNCBLIT;
Aymeric Moizard's avatar
Aymeric Moizard committed
554
	}
555 556 557 558 559 560 561 562 563 564 565 566
	if (info->blit_hw_CC)
		ms_message("Colorkey blits between hw surfaces: accelerated");
	if (info->blit_hw_A)
		ms_message("Alpha blits between hw surfaces:  accelerated");
	if (info->blit_sw)
		ms_message("Copy blits from sw to hw surfaces:  accelerated");
	if (info->blit_hw_CC)
		ms_message("Colorkey blits between sw to hw surfaces: accelerated");
	if (info->blit_hw_A)
		ms_message("Alpha blits between sw to hw surfaces: accelerated");

	wd->sdl_screen = SDL_SetVideoMode(wd->screen_size.width,wd->screen_size.height, 0,flags);
567
	if (wd->sdl_screen == NULL ) {
568 569
		ms_warning("no hardware for video mode: %s\n",
				   SDL_GetError());
aymeric's avatar
aymeric committed
570
	}
571
	if (wd->sdl_screen->flags & SDL_HWSURFACE) ms_message("SDL surface created in hardware");
smorlat's avatar
smorlat committed
572 573 574 575
	if (once) {
		SDL_WM_SetCaption("Video window", NULL);
		once=FALSE;
	}
576
	wd->lay=SDL_CreateYUVOverlay(w , h ,SDL_YV12_OVERLAY,wd->sdl_screen);
577
	if (wd->lay==NULL){
aymeric's avatar
aymeric committed
578 579
		ms_warning("Couldn't create yuv overlay: %s\n",
						SDL_GetError());
580
		return -1;
aymeric's avatar
aymeric committed
581
	}else{
582 583 584 585 586
		ms_message("%i x %i YUV overlay created: hw_accel=%i, pitches=%i,%i,%i",wd->lay->w,wd->lay->h,wd->lay->hw_overlay,
			wd->lay->pitches[0],wd->lay->pitches[1],wd->lay->pitches[2]);
		ms_message("planes= %p %p %p  %i %i",wd->lay->pixels[0],wd->lay->pixels[1],wd->lay->pixels[2],
			wd->lay->pixels[1]-wd->lay->pixels[0],wd->lay->pixels[2]-wd->lay->pixels[1]);
	}
Simon Morlat's avatar
Simon Morlat committed
587
	SDL_ShowCursor(0);//Hide the mouse cursor if was displayed
588
	return 0;
aymeric's avatar
aymeric committed
589 590
}

591
static bool_t sdl_display_init(MSDisplay *obj, MSFilter *f, MSPicture *fbuf, MSPicture *fbuf_selfview){
592
	SdlDisplay *wd = (SdlDisplay*)obj->data;
593
	int i;
594
	if (wd==NULL){
595
		char driver[128];
aymeric's avatar
aymeric committed
596
		/* Initialize the SDL library */
597 598 599 600
		wd=(SdlDisplay*)ms_new0(SdlDisplay,1);
		wd->filter = f;
		obj->data=wd;
		
aymeric's avatar
aymeric committed
601 602 603 604
		if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
			ms_error("Couldn't initialize SDL: %s", SDL_GetError());
			return FALSE;
		}
605
		wd->sdl_initialized=TRUE;
606 607 608
		if (SDL_VideoDriverName(driver, sizeof(driver))){
			ms_message("Video driver: %s", driver);
		}
609 610
		ms_mutex_init(&wd->sdl_mutex,NULL);
		ms_mutex_lock(&wd->sdl_mutex);
611 612 613
		wd->screen_size.width = fbuf->w;
		wd->screen_size.height = fbuf->h;
		
614 615
	}else {
		ms_mutex_lock(&wd->sdl_mutex);
616

617 618 619 620 621 622
		if (wd->lay!=NULL)
			SDL_FreeYUVOverlay(wd->lay);
		if (wd->sdl_screen!=NULL)
			SDL_FreeSurface(wd->sdl_screen);
		wd->lay=NULL;
		wd->sdl_screen=NULL;
smorlat's avatar
smorlat committed
623
	}
624
	wd->filter = f;
625
		
626 627
	i=sdl_create_window(wd, fbuf->w, fbuf->h);
	if (i==0){
628 629 630
		fbuf->planes[0]=wd->lay->pixels[0];
		fbuf->planes[1]=wd->lay->pixels[2];
		fbuf->planes[2]=wd->lay->pixels[1];
631
		fbuf->planes[3]=NULL;
632 633 634
		fbuf->strides[0]=wd->lay->pitches[0];
		fbuf->strides[1]=wd->lay->pitches[2];
		fbuf->strides[2]=wd->lay->pitches[1];
635
		fbuf->strides[3]=0;
636 637
		fbuf->w=wd->lay->w;
		fbuf->h=wd->lay->h;
aymeric's avatar
aymeric committed
638
		sdl_show_window(TRUE);
639
		obj->window_id=sdl_get_native_window_id();
640
		ms_mutex_unlock(&wd->sdl_mutex);
aymeric's avatar
aymeric committed
641 642
		return TRUE;
	}
643
	ms_mutex_unlock(&wd->sdl_mutex);
aymeric's avatar
aymeric committed
644 645 646 647
	return FALSE;
}

static void sdl_display_lock(MSDisplay *obj){
648 649 650 651
	SdlDisplay *wd = (SdlDisplay*)obj->data;
	ms_mutex_lock(&wd->sdl_mutex);
	SDL_LockYUVOverlay(wd->lay);
	ms_mutex_unlock(&wd->sdl_mutex);
aymeric's avatar
aymeric committed
652 653 654
}

static void sdl_display_unlock(MSDisplay *obj){
655 656 657 658
	SdlDisplay *wd = (SdlDisplay*)obj->data;
	ms_mutex_lock(&wd->sdl_mutex);
	SDL_UnlockYUVOverlay(wd->lay);
	ms_mutex_unlock(&wd->sdl_mutex);
aymeric's avatar
aymeric committed
659 660
}

661
static void sdl_display_update(MSDisplay *obj, int new_image, int new_selfview){
662
	SdlDisplay *wd = (SdlDisplay*)obj->data;
aymeric's avatar
aymeric committed
663
	SDL_Rect rect;
664 665 666 667 668
	int ratiow;
	int ratioh;
	int w;
	int h;
	
aymeric's avatar
aymeric committed
669 670
	rect.x=0;
	rect.y=0;
671
	ms_mutex_lock(&wd->sdl_mutex);
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

	ratiow=wd->lay->w;
	ratioh=wd->lay->h;
	reduce(&ratiow, &ratioh);
	w = wd->screen_size.width/ratiow*ratiow;
	h = wd->screen_size.height/ratioh*ratioh;

	if (h*ratiow>w*ratioh)
	{
		w = w;
		h = w*ratioh/ratiow;
	}
	else
	{
		h = h;
		w = h*ratiow/ratioh;
	}

	if (h*wd->lay->w!=w*wd->lay->h)
		ms_error("wrong ratio");

	rect.x = (wd->screen_size.width-w)/2;
	rect.y = (wd->screen_size.height-h)/2;
	rect.w = w;
	rect.h = h;
697 698
	SDL_DisplayYUVOverlay(wd->lay,&rect);
	ms_mutex_unlock(&wd->sdl_mutex);
aymeric's avatar
aymeric committed
699 700
}

701
static int sdl_poll_event(MSDisplay *obj, MSDisplayEvent *ev){
702
	SdlDisplay *wd = (SdlDisplay*)obj->data;
aymeric's avatar
aymeric committed
703
	SDL_Event event;
704
	if (wd->sdl_screen==NULL) return -1;
705
	ms_mutex_lock(&wd->sdl_mutex);
aymeric's avatar
aymeric committed
706
	if (SDL_PollEvent(&event)){
707
		ms_mutex_unlock(&wd->sdl_mutex);
aymeric's avatar
aymeric committed
708 709
		switch(event.type){
			case SDL_VIDEORESIZE:
smorlat's avatar
smorlat committed
710 711 712
				ev->evtype=MS_DISPLAY_RESIZE_EVENT;
				ev->w=event.resize.w;
				ev->h=event.resize.h;
713 714
				wd->screen_size.width = event.resize.w;
				wd->screen_size.height = event.resize.h;
715
				return 1;
aymeric's avatar
aymeric committed
716 717
			break;
			default:
718
				return 0;
aymeric's avatar
aymeric committed
719 720
			break;
		}
721
	}else ms_mutex_unlock(&wd->sdl_mutex);
722
	return -1;
aymeric's avatar
aymeric committed
723 724 725
}

static void sdl_display_uninit(MSDisplay *obj){
726
	SdlDisplay *wd = (SdlDisplay*)obj->data;
smorlat's avatar
fixes  
smorlat committed
727 728
	SDL_Event event;
	int i;
729
	if (wd==NULL)
aymeric's avatar
aymeric committed
730
		return;
731 732 733 734 735
	if (wd->lay!=NULL)
		SDL_FreeYUVOverlay(wd->lay);
	if (wd->sdl_screen!=NULL){
		SDL_FreeSurface(wd->sdl_screen);
		wd->sdl_screen=NULL;
aymeric's avatar
aymeric committed
736
	}
737 738
	wd->lay=NULL;
	wd->sdl_screen=NULL;
Simon Morlat's avatar
Simon Morlat committed
739
	ms_free(wd);
aymeric's avatar
aymeric committed
740
#ifdef __linux
smorlat's avatar
fixes  
smorlat committed
741 742 743
	/*purge the event queue before leaving*/
	for(i=0;SDL_PollEvent(&event) && i<100;++i){
	}
aymeric's avatar
aymeric committed
744
#endif
aymeric's avatar
aymeric committed
745
	sdl_show_window(FALSE);
Simon Morlat's avatar
Simon Morlat committed
746
	SDL_Quit();
aymeric's avatar
aymeric committed
747 748 749 750 751 752 753 754
}

MSDisplayDesc ms_sdl_display_desc={
	.init=sdl_display_init,
	.lock=sdl_display_lock,
	.unlock=sdl_display_unlock,
	.update=sdl_display_update,
	.uninit=sdl_display_uninit,
755
	.pollevent=sdl_poll_event,
aymeric's avatar
aymeric committed
756 757
};

smorlat's avatar
smorlat committed
758
#elif defined(WIN32)
aymeric's avatar
aymeric committed
759 760 761 762

#include <Vfw.h>

typedef struct _WinDisplay{
763
	MSFilter *filter;
aymeric's avatar
aymeric committed
764 765 766
	HWND window;
	HDRAWDIB ddh;
	MSPicture fb;
767 768 769
	MSPicture fb_selfview;
	uint8_t *rgb_selfview;
	int rgb_len_selfview;
770
	struct ms_SwsContext *sws_selfview;
aymeric's avatar
aymeric committed
771 772
	MSDisplayEvent last_rsz;
	uint8_t *rgb;
773 774
	int last_rect_w;
	int last_rect_h;
aymeric's avatar
aymeric committed
775
	int rgb_len;
776
	struct ms_SwsContext *sws;
aymeric's avatar
aymeric committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
	bool_t new_ev;
}WinDisplay;

static LRESULT CALLBACK window_proc(
    HWND hwnd,        // handle to window
    UINT uMsg,        // message identifier
    WPARAM wParam,    // first message parameter
    LPARAM lParam)    // second message parameter
{
	switch(uMsg){
		case WM_DESTROY:
		break;
		case WM_SIZE:
			if (wParam==SIZE_RESTORED){
				int h=(lParam>>16) & 0xffff;
				int w=lParam & 0xffff;
				MSDisplay *obj;
				WinDisplay *wd;
				ms_message("Resized to %i,%i",w,h);
				obj=(MSDisplay*)GetWindowLongPtr(hwnd,GWLP_USERDATA);
				if (obj!=NULL){
					wd=(WinDisplay*)obj->data;
					wd->last_rsz.evtype=MS_DISPLAY_RESIZE_EVENT;
					wd->last_rsz.w=w;
					wd->last_rsz.h=h;
					wd->new_ev=TRUE;
				}else{
					ms_error("Could not retrieve MSDisplay from window !");
				}
			}
		break;
		default:
			return DefWindowProc(hwnd, uMsg, wParam, lParam);
	}
	return 0;
}

static HWND create_window(int w, int h)
{
	WNDCLASS wc;
	HINSTANCE hInstance = GetModuleHandle(NULL);
	HWND hwnd;
	RECT rect;
	wc.style = 0 ;
	wc.lpfnWndProc = window_proc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = NULL;
	wc.hIcon = NULL;
	wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
	wc.hbrBackground = NULL;
	wc.lpszMenuName =  NULL;
	wc.lpszClassName = "Video Window";
	
	if(!RegisterClass(&wc))
	{
		/* already registred! */
	}
	rect.left=100;
	rect.top=100;
	rect.right=rect.left+w;
	rect.bottom=rect.top+h;
	if (!AdjustWindowRect(&rect,WS_OVERLAPPEDWINDOW|WS_VISIBLE /*WS_CAPTION WS_TILED|WS_BORDER*/,FALSE)){
		ms_error("AdjustWindowRect failed.");
	}
	ms_message("AdjustWindowRect: %li,%li %li,%li",rect.left,rect.top,rect.right,rect.bottom);
smorlat's avatar
smorlat committed
843 844 845
	hwnd=CreateWindow("Video Window", "Video window", 
		WS_OVERLAPPEDWINDOW /*WS_THICKFRAME*/ | WS_VISIBLE ,
		CW_USEDEFAULT, CW_USEDEFAULT, rect.right-rect.left,rect.bottom-rect.top,
aymeric's avatar
aymeric committed
846 847 848 849 850 851 852
													NULL, NULL, hInstance, NULL);
	if (hwnd==NULL){
		ms_error("Fail to create video window");
	}
	return hwnd;
}

853
static bool_t win_display_init(MSDisplay *obj, MSFilter *f, MSPicture *fbuf, MSPicture *fbuf_selfview){
aymeric's avatar
aymeric committed
854 855 856 857 858
	WinDisplay *wd=(WinDisplay*)obj->data;
	int ysize,usize;

	if (wd!=NULL)
	{
859
		wd->filter = NULL;
aymeric's avatar
aymeric committed
860 861 862 863 864 865
		if (wd->ddh) DrawDibClose(wd->ddh);
		wd->ddh=NULL;
		if (wd->fb.planes[0]) ms_free(wd->fb.planes[0]);
		wd->fb.planes[0]=NULL;
		wd->fb.planes[1]=NULL;
		wd->fb.planes[2]=NULL;
aymeric's avatar
aymeric committed
866
		wd->fb.planes[3]=NULL;
aymeric's avatar
aymeric committed
867 868 869
		if (wd->rgb) ms_free(wd->rgb);
		wd->rgb=NULL;
		wd->rgb_len=0;
870
		ms_sws_freeContext(wd->sws);
871
		wd->sws=NULL;
872 873 874 875 876 877 878 879
		if (wd->fb_selfview.planes[0]) ms_free(wd->fb_selfview.planes[0]);
		wd->fb_selfview.planes[0]=NULL;
		wd->fb_selfview.planes[1]=NULL;
		wd->fb_selfview.planes[2]=NULL;
		wd->fb_selfview.planes[3]=NULL;
		if (wd->rgb_selfview) ms_free(wd->rgb_selfview);
		wd->rgb_selfview=NULL;
		wd->rgb_len_selfview=0;
880
		ms_sws_freeContext(wd->sws_selfview);
881
		wd->sws_selfview=NULL;
882 883
		wd->last_rect_w=0;
		wd->last_rect_h=0;
aymeric's avatar
aymeric committed
884 885 886 887
	}
	else
		wd=(WinDisplay*)ms_new0(WinDisplay,1);
	
888
	wd->filter = f;
aymeric's avatar
aymeric committed
889 890 891 892
	obj->data=wd;
	
	wd->fb.w=fbuf->w;
	wd->fb.h=fbuf->h;
893 894 895
	wd->fb_selfview.w=fbuf_selfview->w;
	wd->fb_selfview.h=fbuf_selfview->h;

aymeric's avatar
aymeric committed
896
	if (wd->window==NULL){
897
		if (obj->use_external_window && obj->window_id!=0){
aymeric's avatar
aymeric committed
898 899 900 901 902 903 904 905 906
			void *p;
			wd->window=(HWND)obj->window_id;
			p=(void*)GetWindowLongPtr(wd->window,GWLP_USERDATA);
			if (p!=NULL){
				ms_error("Gulp: this externally supplied windows seems to "
					"already have a userdata ! resizing will crash !");
			}else SetWindowLongPtr(wd->window,GWLP_USERDATA,(LONG_PTR)obj);
		}else{
			wd->window=create_window(wd->fb.w,wd->fb.h);
smorlat's avatar
smorlat committed
907
			obj->window_id=(long)wd->window;
aymeric's avatar
aymeric committed
908 909 910
			if (wd->window!=NULL) SetWindowLongPtr(wd->window,GWLP_USERDATA,(LONG_PTR)obj);
			else return FALSE;
		}
911
	}else if (!obj->use_external_window){
smorlat's avatar
smorlat committed
912 913 914
		/* the window might need to be resized*/
		RECT cur;
		GetWindowRect(wd->window,&cur);
915
		MoveWindow(wd->window,cur.left, cur.top, wd->fb.w, wd->fb.h,TRUE);
aymeric's avatar
aymeric committed
916 917 918 919 920 921 922
	}
	
	if (wd->ddh==NULL) wd->ddh=DrawDibOpen();
	if (wd->ddh==NULL){
		ms_error("DrawDibOpen() failed.");
		return FALSE;
	}
923

aymeric's avatar
aymeric committed
924
	/*allocate yuv and rgb buffers*/
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	if (wd->fb_selfview.planes[0]) ms_free(wd->fb_selfview.planes[0]);
	if (wd->rgb_selfview) ms_free(wd->rgb_selfview);
	ysize=wd->fb_selfview.w*wd->fb_selfview.h;
	usize=ysize/4;
	fbuf_selfview->planes[0]=wd->fb_selfview.planes[0]=(uint8_t*)ms_malloc0(ysize+2*usize);
	fbuf_selfview->planes[1]=wd->fb_selfview.planes[1]=wd->fb_selfview.planes[0]+ysize;
	fbuf_selfview->planes[2]=wd->fb_selfview.planes[2]=wd->fb_selfview.planes[1]+usize;
	fbuf_selfview->planes[3]=NULL;
	fbuf_selfview->strides[0]=wd->fb_selfview.strides[0]=wd->fb_selfview.w;
	fbuf_selfview->strides[1]=wd->fb_selfview.strides[1]=wd->fb_selfview.w/2;
	fbuf_selfview->strides[2]=wd->fb_selfview.strides[2]=wd->fb_selfview.w/2;
	fbuf_selfview->strides[3]=0;

	wd->rgb_len_selfview=ysize*3;
	wd->rgb_selfview=(uint8_t*)ms_malloc0(wd->rgb_len_selfview);

aymeric's avatar
aymeric committed
941 942 943 944 945 946 947
	if (wd->fb.planes[0]) ms_free(wd->fb.planes[0]);
	if (wd->rgb) ms_free(wd->rgb);
	ysize=wd->fb.w*wd->fb.h;
	usize=ysize/4;
	fbuf->planes[0]=wd->fb.planes[0]=(uint8_t*)ms_malloc0(ysize+2*usize);
	fbuf->planes[1]=wd->fb.planes[1]=wd->fb.planes[0]+ysize;
	fbuf->planes[2]=wd->fb.planes[2]=wd->fb.planes[1]+usize;
948
	fbuf->planes[3]=NULL;
aymeric's avatar
aymeric committed
949 950 951
	fbuf->strides[0]=wd->fb.strides[0]=wd->fb.w;
	fbuf->strides[1]=wd->fb.strides[1]=wd->fb.w/2;
	fbuf->strides[2]=wd->fb.strides[2]=wd->fb.w/2;
952
	fbuf->strides[3]=0;
aymeric's avatar
aymeric committed
953 954 955

	wd->rgb_len=ysize*3;
	wd->rgb=(uint8_t*)ms_malloc0(wd->rgb_len);
956 957
	wd->last_rect_w=0;
	wd->last_rect_h=0;
aymeric's avatar
aymeric committed
958 959 960 961 962 963 964 965 966 967 968 969 970
	return TRUE;
}

typedef struct rgb{
	uint8_t r,g,b;
} rgb_t;

typedef struct yuv{
	uint8_t y,u,v;
} yuv_t;



smorlat's avatar
smorlat committed
971
static void yuv420p_to_rgb(WinDisplay *wd, MSPicture *src, uint8_t *rgb){
aymeric's avatar
aymeric committed
972 973 974 975
	int rgb_stride=-src->w*3;
	uint8_t *p;

	p=rgb+(src->w*3*(src->h-1));
976
	if (wd->sws==NULL){
977
		wd->sws=ms_sws_getContext(src->w,src->h,PIX_FMT_YUV420P,
aymeric's avatar
aymeric committed
978 979
			src->w,src->h, PIX_FMT_BGR24,
			SWS_FAST_BILINEAR, NULL, NULL, NULL);
aymeric's avatar
aymeric committed
980
	}
981
	if (ms_sws_scale(wd->sws,src->planes,src->strides, 0,
982
           			src->h, &p, &rgb_stride)<0){
983
		ms_error("Error in 420->rgb ms_sws_scale().");
984
	}
aymeric's avatar
aymeric committed
985 986
}

987 988 989 990 991 992
static void yuv420p_to_rgb_selfview(WinDisplay *wd, MSPicture *src, uint8_t *rgb){
	int rgb_stride=-src->w*3;
	uint8_t *p;

	p=rgb+(src->w*3*(src->h-1));
	if (wd->sws_selfview==NULL){
993
		wd->sws_selfview=ms_sws_getContext(src->w,src->h,PIX_FMT_YUV420P,
994 995 996
			src->w,src->h, PIX_FMT_BGR24,
			SWS_FAST_BILINEAR, NULL, NULL, NULL);
	}
997
	if (ms_sws_scale(wd->sws_selfview,src->planes,src->strides, 0,
998
           			src->h, &p, &rgb_stride)<0){
999
		ms_error("Error in 420->rgb ms_sws_scale().");
1000 1001 1002
	}
}

1003
static void win_display_update(MSDisplay *obj, int new_image, int new_selfview){
aymeric's avatar
aymeric committed
1004 1005 1006 1007 1008
	WinDisplay *wd=(WinDisplay*)obj->data;
	HDC hdc;
	BITMAPINFOHEADER bi;
	RECT rect;
	bool_t ret;
1009 1010 1011 1012
	int ratiow;
	int ratioh;
	int w;
	int h;
1013
	int corner;
1014 1015
	float sv_scalefactor;
	float sv_pos[3];
1016
	int color[3];
1017 1018 1019

	HDC dd_hdc;
	HBITMAP dd_bmp;
1020
	HBRUSH brush;
1021
	BOOL dont_draw;
1022

aymeric's avatar
aymeric committed
1023 1024 1025 1026 1027 1028
	if (wd->window==NULL) return;
	hdc=GetDC(wd->window);
	if (hdc==NULL) {
		ms_error("Could not get window dc");
		return;
	}
1029 1030
	if (new_image>0)
		yuv420p_to_rgb(wd, &wd->fb, wd->rgb);
aymeric's avatar
aymeric committed
1031 1032 1033
	memset(&bi,0,sizeof(bi));
	bi.biSize=sizeof(bi);
	GetClientRect(wd->window,&rect);
1034

aymeric's avatar
aymeric committed
1035 1036 1037 1038 1039 1040 1041
	bi.biWidth=wd->fb.w;
	bi.biHeight=wd->fb.h;
	bi.biPlanes=1;
	bi.biBitCount=24;
	bi.biCompression=BI_RGB;
	bi.biSizeImage=wd->rgb_len;

1042 1043
	ratiow=wd->fb.w;
	ratioh=wd->fb.h;
1044
	reduce(&ratiow, &ratioh);
1045 1046
	w = rect.right/ratiow*ratiow;
	h = rect.bottom/ratioh*ratioh;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

	if (h*ratiow>w*ratioh)
	{
		w = w;
		h = w*ratioh/ratiow;
	}
	else
	{
		h = h;
		w = h*ratiow/ratioh;
	}

	if (h*wd->fb.w!=w*wd->fb.h)
		ms_error("wrong ratio");

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	dd_hdc = CreateCompatibleDC(hdc);
	if (dd_hdc==NULL) {
		ms_error("Could not get CreateCompatibleDC");
		return;
	}
	dd_bmp = CreateCompatibleBitmap(hdc, rect.right, rect.bottom);
	if (dd_bmp==NULL) {
		ms_error("Could not get CreateCompatibleBitmap");
		return;
	}
1072

1073 1074 1075
	HGDIOBJ old_object = SelectObject(dd_hdc, dd_bmp);

	dont_draw = DrawDibBegin(wd->ddh,dd_hdc, 0, 0, &bi, 0, 0, DDF_BUFFER);
1076 1077 1078 1079 1080 1081 1082
	
	/* full screen in background color */
	color[0]=color[1]=color[2]=0;
	if (wd->filter)
		ms_filter_call_method(wd->filter, MS_VIDEO_OUT_GET_BACKGROUND_COLOR, &color);
	
	brush = CreateSolidBrush(RGB(color[0],color[1],color[2]));
1083
	FillRect(dd_hdc, &rect, brush); 
1084
	DeleteObject(brush);
1085 1086 1087

	corner = 0;
	sv_scalefactor = SCALE_FACTOR;
1088 1089 1090
	sv_pos[0] = SELVIEW_POS_INACTIVE;
	sv_pos[1] = SELVIEW_POS_INACTIVE;
	sv_pos[2] = 0;
1091 1092 1093 1094
	if (wd->filter)
		ms_filter_call_method(wd->filter, MS_VIDEO_OUT_GET_CORNER, &corner);
	if (wd->filter)
		ms_filter_call_method(wd->filter, MS_VIDEO_OUT_GET_SCALE_FACTOR, &sv_scalefactor);
1095 1096
	if (wd->filter)
		ms_filter_call_method(wd->filter, MS_VIDEO_OUT_GET_SELFVIEW_POS, sv_pos);
1097 1098 1099 1100 1101 1102 1103 1104 1105

	if (wd->rgb_selfview==NULL || corner==-1) {
		ret=DrawDibDraw(wd->ddh,dd_hdc,
			(rect.right-w)/2,
			(rect.bottom-h)/2,
			w,
			h,
			&bi,wd->rgb,
			0,0,bi.biWidth,bi.biHeight,dont_draw?DDF_DONTDRAW:0);
aymeric's avatar
aymeric committed
1106
	
1107 1108 1109 1110 1111
	} else {

		int w_selfview = rect.right - w;
		int h_selfview = rect.bottom - h;

1112
		if ((h_selfview < h/sv_scalefactor && w_selfview < w/sv_scalefactor) || corner<=3 || sv_pos[0]!=SELVIEW_POS_INACTIVE)
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
		{
			ret=DrawDibDraw(wd->ddh,dd_hdc,
				(rect.right-w)/2,
				(rect.bottom-h)/2,
				w,
				h,
				&bi,wd->rgb,
				0,0,bi.biWidth,bi.biHeight,dont_draw?DDF_DONTDRAW:0);

			//preserve ratio
			ratiow=wd->fb_selfview.w;
			ratioh=wd->fb_selfview.h;
			reduce(&ratiow, &ratioh);

1127
			w_selfview = (int)(w/sv_scalefactor);
1128 1129 1130 1131 1132
			w_selfview = w_selfview/ratiow*ratiow;
			h_selfview = w_selfview*ratioh/ratiow;

			if (rect.right>100 && rect.bottom>100)
			{
1133 1134
				int x_sv;
				int y_sv;
1135 1136
				if (new_selfview>0)
					yuv420p_to_rgb_selfview(wd, &wd->fb_selfview, wd->rgb_selfview);
1137 1138 1139 1140

				//HPEN hpenDot;
				//hpenDot = CreatePen(PS_SOLID, 1, RGB(10, 10, 10));
				//SelectObject(dd_hdc, hpenDot);
1141 1142
				if (sv_pos[0]!=SELVIEW_POS_INACTIVE)
				{
1143
					x_sv = (int)((rect.right*sv_pos[0])/100.0-w_selfview/2);
1144 1145 1146
					y_sv = (int)((rect.bottom*sv_pos[1])/100.0-h_selfview/2);
				}
				else if (corner==1 || corner==4+1)
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
				{
					/* top left corner */
					x_sv = 20;
					y_sv = 20;
				}
				else if (corner==2 || corner==4+2)
				{
					/* top right corner */
					x_sv = (rect.right-w_selfview-20);
					y_sv = 20;
				}
				else if (corner==3 || corner==4+3)
				{
					/* bottom left corner */
					x_sv = 20;
					y_sv = (rect.bottom-h_selfview-20);
				}
				else /* corner = 0: default */
				{
					/* bottom right corner */
					x_sv = (rect.right-w_selfview-20);
					y_sv = (rect.bottom-h_selfview-20);
				}


				Rectangle(dd_hdc, x_sv-2, y_sv-2, x_sv+w_selfview+2, y_sv+h_selfview+2); 
				ret=DrawDibDraw(wd->ddh,dd_hdc,
					x_sv,
					y_sv,
					w_selfview,
					h_selfview,
					&bi,wd->rgb_selfview,
					0,0,bi.biWidth,bi.biHeight,dont_draw?DDF_DONTDRAW:0);
			}
		}
		else
		{
			//preserve ratio
			ratiow=wd->fb_selfview.w;
			ratioh=wd->fb_selfview.h;
			reduce(&ratiow, &ratioh);

1189 1190
			if (new_selfview>0)
				yuv420p_to_rgb_selfview(wd, &wd->fb_selfview, wd->rgb_selfview);
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
			if (w_selfview >= w/sv_scalefactor)
			{
				w_selfview = w_selfview/ratiow*ratiow;
				h_selfview = w_selfview*ratioh/ratiow;

				ret=DrawDibDraw(wd->ddh,dd_hdc,
					0,
					(rect.bottom-h)/2,
					w,
					h,
					&bi,wd->rgb,
					0,0,bi.biWidth,bi.biHeight,dont_draw?DDF_DONTDRAW:0);

				Rectangle(dd_hdc,
					(rect.right-w_selfview)-4-2,
					(rect.bottom-h_selfview)/2-2,
					(rect.right)-4+2,
					(rect.bottom+h_selfview)/2+2);

				ret=DrawDibDraw(wd->ddh,dd_hdc,
					(rect.right-w_selfview)-4,
					(rect.bottom-h_selfview)/2,
					w_selfview,
					h_selfview,
					&bi,wd->rgb_selfview,
					0,0,bi.biWidth,bi.biHeight,dont_draw?DDF_DONTDRAW:0);
			}
			else
			{
				h_selfview = h_selfview/ratioh*ratioh;
				w_selfview = h_selfview*ratiow/ratioh;

				ret=DrawDibDraw(wd->ddh,dd_hdc,
					(rect.right-w)/2,
					0,
					w,
					h,
					&bi,wd->rgb,
					0,0,bi.biWidth,bi.biHeight,dont_draw?DDF_DONTDRAW:0);

				Rectangle(dd_hdc,
					(rect.right-w_selfview)/2-2,
					(rect.bottom-h_selfview)-4-2,
					(rect.right+w_selfview)/2+2,
					(rect.bottom)-4+2);

				ret=DrawDibDraw(wd->ddh,dd_hdc,
					(rect.right-w_selfview)/2,
					(rect.bottom-h_selfview)-4,
					w_selfview,
					h_selfview,
					&bi,wd->rgb_selfview,
					0,0,bi.biWidth,bi.biHeight,dont_draw?DDF_DONTDRAW:0);
			}
		}
	}

	DrawDibEnd(wd->ddh);
	BitBlt(hdc, 0, 0, rect.right, rect.bottom, dd_hdc, 0, 0, SRCCOPY);
	SelectObject(dd_hdc, old_object);

1252 1253 1254
	DeleteObject(dd_bmp);
	DeleteDC(dd_hdc);

1255 1256 1257
	wd->last_rect_w=rect.right;
	wd->last_rect_h=rect.bottom;

aymeric's avatar
aymeric committed
1258 1259 1260 1261 1262 1263 1264 1265
  	if (!ret) ms_error("DrawDibDraw failed.");
	ReleaseDC(NULL,hdc);
}

static void win_display_uninit(MSDisplay *obj){
	WinDisplay *wd=(WinDisplay*)obj->data;
	if (wd==NULL)
		return;
smorlat's avatar
smorlat committed
1266
	if (wd->window && !obj->use_external_window) DestroyWindow(wd->window);
aymeric's avatar
aymeric committed
1267
	if (wd->ddh) DrawDibClose(wd->ddh);
1268 1269
	if (wd->fb_selfview.planes[0]) ms_free(wd->fb_selfview.planes[0]);
	if (wd->rgb_selfview) ms_free(wd->rgb_selfview);
1270
	if (wd->sws_selfview) ms_sws_freeContext(wd->sws_selfview);
aymeric's avatar
aymeric committed
1271 1272
	if (wd->fb.planes[0]) ms_free(wd->fb.planes[0]);
	if (wd->rgb) ms_free(wd->rgb);
1273
	if (wd->sws) ms_sws_freeContext(wd->sws);
aymeric's avatar
aymeric committed
1274 1275 1276
	ms_free(wd);
}

1277 1278
int win_display_pollevent(MSDisplay *d, MSDisplayEvent *ev){
	return -1;
aymeric's avatar
aymeric committed
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
}

#ifdef _MSC_VER

extern MSDisplayDesc ms_win_display_desc={
	win_display_init,
	NULL,
	NULL,
	win_display_update,
	win_display_uninit,
	win_display_pollevent
};

#else

MSDisplayDesc ms_win_display_desc={
	.init=win_display_init,
	.update=win_display_update,
	.uninit=win_display_uninit,
	.pollevent=win_display_pollevent
};

#endif

#endif

MSDisplay *ms_display_new(MSDisplayDesc *desc){
	MSDisplay *obj=(MSDisplay *)ms_new0(MSDisplay,1);
	obj->desc=desc;
	obj->data=NULL;
	return obj;
}