aboutsummaryrefslogtreecommitdiff
path: root/Src/Winamp/vid_d3d.cpp
blob: 1048ed09894bd50629d7cf2a27ae0ad8024f6532 (plain) (blame)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#include "vid_d3d.h"
#include "vid_subs.h"
#include "../nu/AutoWide.h"
#include "videooutput.h"
#include "../nsutil/image.h"
#include <d3d9.h>
#include "WinampAttributes.h"
#include "IVideoD3DOSD.h"
#include <stdint.h>

struct YV12_PLANES;

typedef HRESULT (WINAPI *DIRECT3DCREATE9EX)(UINT SDKVersion, IDirect3D9Ex **);
typedef IDirect3D9 *(WINAPI *DIRECT3DCREATE9)(UINT SDKVersion);
static DIRECT3DCREATE9EX creatorex=0;
static DIRECT3DCREATE9 creator=0;
static HMODULE d3d_lib = 0;
static bool tried=false;
static DWORD winver;

extern IVideoOSD *posd;

static bool CreateDirect3D(IDirect3D9 **d3d, IDirect3D9Ex **d3dEx)
{
	if (!d3d_lib && !creator)
	{
		if (tried)
			return false;

		d3d_lib = LoadLibraryW(L"d3d9.dll");
		if (!d3d_lib)
		{
			tried=true;
			return false;
		}

		creatorex = (DIRECT3DCREATE9EX)GetProcAddress(d3d_lib, "Direct3DCreate9Ex");
		creator = (DIRECT3DCREATE9)GetProcAddress(d3d_lib, "Direct3DCreate9");
		if (!creatorex && !creator)
		{
			FreeLibrary(d3d_lib);
			tried=true;
			return false;
		}
	}
	if (creatorex)
	{
		if (SUCCEEDED(creatorex(D3D_SDK_VERSION, d3dEx)) && *d3dEx)
		{
			(*d3dEx)->QueryInterface(__uuidof(IDirect3D9), (void **)d3d);
			return true;
		}
	}

	if (creator)
	{
		*d3d = creator(D3D_SDK_VERSION);
		*d3dEx=0;
		return !!d3d;
	}
	return false;
}

static void BuildPresentationParameters(D3DPRESENT_PARAMETERS &presentation_parameters,  HWND hwnd, D3DSWAPEFFECT swap_effect)
{
	memset(&presentation_parameters, 0, sizeof(presentation_parameters));
	presentation_parameters.BackBufferWidth = 0;
	presentation_parameters.BackBufferHeight = 0;
	presentation_parameters.BackBufferFormat = D3DFMT_UNKNOWN;
	presentation_parameters.BackBufferCount = 1;
	presentation_parameters.MultiSampleType = D3DMULTISAMPLE_NONE;
	presentation_parameters.MultiSampleQuality = 0;
	presentation_parameters.SwapEffect = swap_effect;
	
	presentation_parameters.hDeviceWindow = hwnd;
	presentation_parameters.Windowed = TRUE;
	presentation_parameters.EnableAutoDepthStencil = FALSE;
	presentation_parameters.Flags = /*D3DPRESENTFLAG_LOCKABLE_BACKBUFFER |*/ D3DPRESENTFLAG_VIDEO;
	presentation_parameters.FullScreen_RefreshRateInHz = 0;
	presentation_parameters.PresentationInterval = (!config_video_vsync2 ? D3DPRESENT_INTERVAL_IMMEDIATE : D3DPRESENT_INTERVAL_ONE);
}

static UINT FindMyMonitor(IDirect3D9 *d3d, HWND hwnd)
{
	HMONITOR monitor=MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
	if (monitor)
	{
		UINT num_adapters = d3d->GetAdapterCount();
		for (UINT i=0;i!=num_adapters;i++)
		{
			if (d3d->GetAdapterMonitor(i) == monitor)
				return i;
		}
	}
	return D3DADAPTER_DEFAULT;
}

D3DDEVTYPE Direct3DVideoOutput::GetDeviceType(IDirect3D9 *d3d, UINT display_adapter)
{
	D3DCAPS9 caps;
	D3DDEVTYPE device_type = D3DDEVTYPE_HAL;
	for(;;)
	{
		HRESULT hr = d3d->GetDeviceCaps(display_adapter, device_type, &caps);
		if (hr == S_OK)
		{
			if ((D3DPTFILTERCAPS_MAGFLINEAR & caps.StretchRectFilterCaps) && (D3DPTFILTERCAPS_MINFLINEAR & caps.StretchRectFilterCaps))
				stretch_filter = D3DTEXF_LINEAR;
			else if ((D3DPTFILTERCAPS_MAGFPOINT & caps.StretchRectFilterCaps) && (D3DPTFILTERCAPS_MINFPOINT & caps.StretchRectFilterCaps))
				stretch_filter = D3DTEXF_POINT;
			else
				stretch_filter = D3DTEXF_NONE;
			return device_type;
		}
		if (device_type == D3DDEVTYPE_HAL)
			device_type = D3DDEVTYPE_REF;
		else
			break;
	}

	return (D3DDEVTYPE)0;
}

static D3DSWAPEFFECT GetSwapEffect(IDirect3D9 *d3d, UINT adapter, D3DDEVTYPE device_type, bool ex)
{
	if (ex)
	{
		D3DCAPS9 caps;
		d3d->GetDeviceCaps(adapter, device_type, &caps);
		if (caps.Caps & D3DCAPS_OVERLAY)
		{
			return D3DSWAPEFFECT_OVERLAY;
		}
		else 
		{
			return D3DSWAPEFFECT_FLIPEX;// (D3DSWAPEFFECT)5;
		}
	}
	else
	{
		return D3DSWAPEFFECT_FLIP;
	}
}

Direct3DVideoOutput::Direct3DVideoOutput(HWND parent, VideoAspectAdjuster *_adjuster)
{
	CreateDirect3D(&d3d, &d3dEx);
	device = 0;
	deviceEx = 0;
	surface = 0;
	width=0;
	height=0;
	GetWindowRect(parent, &last_rect);
	surface_type = D3DFMT_UNKNOWN;
	display_adapter = 0;
	subtitle_font = 0;
	current_subtitle = 0;
	need_change = 0;
	adjuster = _adjuster;
	hwnd = parent;
	opened=false;
	valid_surface=false;
	logo_surface = 0;
	input_type = 0;
	flip = 0;
	m_palette = 0;

	if (d3dEx)
	{
		display_adapter = FindMyMonitor(d3d, parent);
		D3DDEVTYPE device_type = GetDeviceType(d3d, display_adapter);
		swap_effect = GetSwapEffect(d3d, display_adapter, device_type, true);
		D3DPRESENT_PARAMETERS presentation_parameters;
		BuildPresentationParameters(presentation_parameters, parent, swap_effect);
		HRESULT hr = d3dEx->CreateDeviceEx(display_adapter, D3DDEVTYPE_HAL, parent,
			D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED|D3DCREATE_NOWINDOWCHANGES,
			&presentation_parameters, 0, 
			&deviceEx);

		if (FAILED(hr))
		{ // try again with mixed processing
			hr = d3dEx->CreateDeviceEx(display_adapter, D3DDEVTYPE_HAL, parent,
				D3DCREATE_MIXED_VERTEXPROCESSING|D3DCREATE_MULTITHREADED|D3DCREATE_NOWINDOWCHANGES,
				&presentation_parameters, 0, 
				&deviceEx);

			if (FAILED(hr))
			{ // and finally software
				hr = d3dEx->CreateDeviceEx(display_adapter, D3DDEVTYPE_HAL, parent,
					D3DCREATE_SOFTWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED|D3DCREATE_NOWINDOWCHANGES,
					&presentation_parameters, 0, 
					&deviceEx);
			}
		}

		if (SUCCEEDED(hr))
			deviceEx->QueryInterface(__uuidof(IDirect3DDevice9), (void **)&device);
	}

	if (!deviceEx)
	{
		display_adapter = FindMyMonitor(d3d, parent);
		D3DDEVTYPE device_type = GetDeviceType(d3d, display_adapter);
		swap_effect = GetSwapEffect(d3d, display_adapter, device_type, false);
		if (device_type)
		{
			D3DPRESENT_PARAMETERS presentation_parameters;
			BuildPresentationParameters(presentation_parameters, parent, swap_effect);
			HRESULT hr = d3d->CreateDevice(display_adapter, device_type, parent,
				D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED|D3DCREATE_NOWINDOWCHANGES,
				&presentation_parameters,
				&device);

			if (FAILED(hr))
			{ // try again with mixed processing
				hr = d3d->CreateDevice(display_adapter, device_type, parent,
					D3DCREATE_MIXED_VERTEXPROCESSING|D3DCREATE_MULTITHREADED|D3DCREATE_NOWINDOWCHANGES,
					&presentation_parameters,
					&device);
				if (FAILED(hr))
				{ // and finally software
					/*hr = */d3d->CreateDevice(display_adapter, device_type, parent,
						D3DCREATE_SOFTWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED|D3DCREATE_NOWINDOWCHANGES,
						&presentation_parameters,
						&device);
				}
			}
		}
	}

	if (device)
	{
		// TODO: retrieve dimensions from VideoOutput
		device->CreateOffscreenPlainSurface(128, 128, D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &logo_surface, 0);
		((IVideoD3DOSD *)posd)->CreateOSD(device);
	}	
}

static D3DFORMAT GetColorFormat(unsigned int type)
{
	switch(type)
	{
	case MAKEFOURCC('Y', 'V', '1', '2'):
		return (D3DFORMAT)type;
	case MAKEFOURCC('Y', 'U', 'Y', '2'):
		return D3DFMT_YUY2;
	case MAKEFOURCC('U', 'Y', 'V', 'Y'):
		return D3DFMT_UYVY;
	case MAKEFOURCC('R', 'G', 'B', '8'):
		return D3DFMT_P8;
	case MAKEFOURCC('R', 'G', '3', '2'):
		return D3DFMT_X8R8G8B8;
	case MAKEFOURCC('R', 'G', '2', '4'):
		return D3DFMT_R8G8B8;
	case MAKEFOURCC('R', '5', '5', '5'):
		return D3DFMT_X1R5G5B5;
	case MAKEFOURCC('R', '5', '6', '5'):
		return D3DFMT_R5G6B5;
	default:
		return (D3DFORMAT)D3DFMT_UNKNOWN;
	}
}

static bool SubstituteSurfaceType(D3DFORMAT surface_format, D3DFORMAT &substitute_format, int n)
{
	if (surface_format == D3DFMT_R8G8B8)
	{
		switch(n)
		{
		case 0:
			substitute_format = D3DFMT_X8R8G8B8;
			return true;
		}
	}
	else if (surface_format == 	(D3DFORMAT)MAKEFOURCC('Y', 'V', '1', '2'))
	{
		switch(n)
		{
		case 0:
			substitute_format = D3DFMT_YUY2;
			return true;
		case 1:
			substitute_format = D3DFMT_UYVY;
			return true;
		case 2:
			substitute_format =  D3DFMT_X8R8G8B8;
			return true;
		}
	}
	return false;
}

static void PreferredSurfaceType(D3DFORMAT &surface_format)
{
	if (surface_format == D3DFMT_P8)
	{
		surface_format = D3DFMT_X8R8G8B8;
	}
}

void Direct3DVideoOutput::setVFlip(int on)
{
	flip = on;
	if (config_video_fliprgb)
		flip = !flip;
}

int Direct3DVideoOutput::OpenVideo(int w, int h, unsigned int type, int flipit, double aspectratio)
{
	if (!device)
		return 0;
	D3DFORMAT new_surface_type = GetColorFormat(type);
	if (new_surface_type == D3DFMT_UNKNOWN) // not supported
		return 0;

	PreferredSurfaceType(new_surface_type);

	input_type = type;

	// see what was set for the flip flag
	// as nsv streams are generally flipped
	// so we'll need to invert the handling
	flip = flipit;
	if (config_video_fliprgb)
		flip = !flip;

	// see if we can re-use our old surface
	if (!surface || surface_type != new_surface_type || w != width || h != height)
	{ 
		if (surface)
		{
			surface->Release();
			surface=0;
		}

		HRESULT hr;

		D3DFORMAT try_surface_type = new_surface_type;
		int n=0;
		do 
		{
			hr = device->CreateOffscreenPlainSurface(w, h, try_surface_type, D3DPOOL_DEFAULT, &surface, 0);

		} while (hr != S_OK && SubstituteSurfaceType(new_surface_type, try_surface_type, n++));

		surface_type = try_surface_type;

		if (hr != S_OK)
			return 0;

		width = w;
		height =h;
	}

	valid_surface=false;
	opened=true;

	return 1;
}

void Direct3DVideoOutput::OnWindowSize()
{
	if (device)
	{
		RECT wnd_rect;
		GetWindowRect(hwnd, &wnd_rect);
		if (need_change || !EqualRect(&wnd_rect, &last_rect))
		{
			if ( need_change || (last_rect.right - last_rect.left) != (wnd_rect.right - wnd_rect.left)
				|| (last_rect.bottom - last_rect.top) != (wnd_rect.bottom - wnd_rect.top))
			{
				// TODO: check adapter
				D3DPRESENT_PARAMETERS presentation_parameters;
				BuildPresentationParameters(presentation_parameters, hwnd, swap_effect);
				//if (surface)
				//				surface->Release();
				//surface=0;
				if (subtitle_font)
					subtitle_font->Release();  // I hate to do this but we might need a new font size, and it works around a bug
				subtitle_font = 0;

				if (((IVideoD3DOSD *)posd)->isOSDInited())
				{ 
					((IVideoD3DOSD *)posd)->LostOSD();
				}
				HRESULT hr = device->Reset(&presentation_parameters);
				if (FAILED(hr))
				{
					if (surface)
						surface->Release();
					surface=0;
					/*hr = */device->Reset(&presentation_parameters);
					/*hr = */device->CreateOffscreenPlainSurface(width, height, surface_type, D3DPOOL_DEFAULT, &surface, 0);
				}
				if (((IVideoD3DOSD *)posd)->isOSDInited())
				{ 
					((IVideoD3DOSD *)posd)->ResetOSD(device);
				}
				drawSubtitle(current_subtitle);
				last_rect = wnd_rect;
				hr = device->BeginScene();
				if (SUCCEEDED(hr))
					DoRender();
			}
			else
			{
				// TODO: check adapter
			}

			last_rect = wnd_rect;
		}
	}
}

HRESULT Direct3DVideoOutput::DoRender()
{
	RECT r;
	r = last_rect;
	r.right -= r.left;
	r.left = 0;
	r.bottom -= r.top;
	r.top = 0;

	//GetClientRect(hwnd, &r);
	HRESULT hr ;

	IDirect3DSurface9 *back_buffer = 0;
	hr = device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back_buffer);
	if (FAILED(hr))
	{
		need_change=1;
		device->EndScene();
		return hr;
	}
	RECT full_rect = r;
	adjuster->adjustAspect(r);

	if (opened  && valid_surface)
	{
		RECT sides[4]={
			{0, 0, r.left, full_rect.bottom}, // left side
			{r.right, 0, full_rect.right, full_rect.bottom}, // right side
			{0, 0, full_rect.right, r.top}, // top
			{0, r.bottom, full_rect.right, full_rect.bottom} // bottom
		};
		for (int i=0;i<4;i++)
			hr=device->ColorFill(back_buffer, &sides[i], D3DCOLOR_XRGB(0, 0, 0));

		hr = device->StretchRect(surface, NULL, back_buffer, &r, stretch_filter);
	}
	else
	{
		hr=device->ColorFill(back_buffer, 0, D3DCOLOR_XRGB(0, 0, 0));
		HDC logo_dc=0;
		HRESULT hr = logo_surface->GetDC(&logo_dc);
		if (hr == S_OK)
		{ // draw logo
			RECT r={0,0,128,128};
			adjuster->DrawLogo(logo_dc, &r);
			logo_surface->ReleaseDC(logo_dc);
			POINT logo_pt = {full_rect.right/2 - 64,full_rect.bottom/2 - 64};
			device->UpdateSurface(logo_surface, 0, back_buffer, &logo_pt);
		}
	}
	back_buffer->Release();

	if (current_subtitle && subtitle_font)
	{
		DWORD oldValue;
		device->GetRenderState(D3DRS_ALPHABLENDENABLE, &oldValue);
		device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
		RECT subRect, origRect;
		GetClientRect(hwnd, &subRect);
		origRect = subRect;
		D3DCOLOR text_color = D3DCOLOR_XRGB(current_subtitle->colorRed, current_subtitle->colorGreen, current_subtitle->colorBlue);
		AutoWide wide_text(current_subtitle->text);

		// calculate where to draw
		// TODO: move to drawSubtitle
		subtitle_font->DrawTextW(NULL, wide_text, -1, &subRect, DT_TOP | DT_WORDBREAK | DT_NOCLIP | DT_CENTER | DT_CALCRECT, text_color);
		int height_delta = (origRect.bottom - origRect.top) - (subRect.bottom - subRect.top);
		subRect.top += height_delta;
		subRect.bottom += height_delta;
		// draw 
		hr = subtitle_font->DrawTextW(NULL, wide_text, -1, &subRect, DT_TOP | DT_WORDBREAK | DT_NOCLIP | DT_CENTER, text_color);
		device->SetRenderState(D3DRS_ALPHABLENDENABLE, oldValue);
	}

	if (posd && ((IVideoD3DOSD *)posd)->Showing() && ((IVideoD3DOSD *)posd)->isOSDReadyToDraw())
	{
		((IVideoD3DOSD *)posd)->DrawOSD(device);
	}

	hr = device->EndScene();

	if (FAILED(hr))
	{
		need_change = 1;
		return hr;
	}

	if (deviceEx)
		hr = deviceEx->PresentEx(NULL, NULL, NULL, NULL, D3DPRESENT_DONOTWAIT);
	else
		hr = device->Present(NULL, NULL, NULL, NULL);

	if (hr == D3DERR_DEVICELOST)
		need_change = 1;

	return hr;
}

void YV12_to_UYVY(unsigned char *output, const YV12_PLANES *planes, int pitch, int width, int height, int flip);
void YV12_to_YV12(unsigned char *output, const YV12_PLANES *planes, int pitch, int width, int height, int flip);
void YV12_to_YUY2(unsigned char *output, const YV12_PLANES *planes, int pitch, int width, int height, int flip);
void YUY2_to_YUY2(unsigned char *output, const char *buf, int pitch, int width, int height, int flip);

void Direct3DVideoOutput::displayFrame(const char *buf, int size, int time)
{
	if (need_change || !surface)
		return;

	RECT r;
	GetClientRect(hwnd, &r);

	HRESULT hr = device->BeginScene();
	if (FAILED(hr))
	{
		need_change=1;
		return;
	}

	D3DLOCKED_RECT locked_surface;
	hr = surface->LockRect(&locked_surface, NULL, D3DLOCK_DISCARD);
	if (SUCCEEDED(hr))
	{
		if (input_type == MAKEFOURCC('Y','V','1','2'))
		{
			const YV12_PLANES *planes = (const YV12_PLANES *)buf;
			switch((DWORD)surface_type)
			{
			case MAKEFOURCC('Y','V','1','2'):
				YV12_to_YV12((unsigned char *)locked_surface.pBits, planes, locked_surface.Pitch, width, height, flip);
				break;
			case MAKEFOURCC('Y','U','Y','2'):
				YV12_to_YUY2((unsigned char *)locked_surface.pBits, planes, locked_surface.Pitch, width, height, flip);
				break;
			case MAKEFOURCC('U','Y','V','Y'):
				YV12_to_UYVY((unsigned char *)locked_surface.pBits, planes, locked_surface.Pitch, width, height, flip);
				break;
			case D3DFMT_X8R8G8B8:
				{
					const uint8_t *plane_bufs[3] = { planes->y.baseAddr, planes->v.baseAddr, planes->u.baseAddr};
					const size_t plane_strides[3] = { (size_t)planes->y.rowBytes, (size_t)planes->v.rowBytes, (size_t)planes->u.rowBytes };
					if (flip)
						nsutil_image_Convert_YUV420_RGB32((RGB32 *)((int8_t *)locked_surface.pBits + locked_surface.Pitch*(height-1)), -locked_surface.Pitch, width, height, plane_bufs, plane_strides);
					else
						nsutil_image_Convert_YUV420_RGB32((RGB32 *)locked_surface.pBits, locked_surface.Pitch, width, height, plane_bufs, plane_strides);
				}
				break;
			}
		}
		else if (surface_type == D3DFMT_YUY2) // YUY2
		{
			YUY2_to_YUY2((unsigned char *)locked_surface.pBits, buf, locked_surface.Pitch, width, height, flip);
		}
		else if (surface_type == D3DFMT_UYVY) // YUY2
		{
			YUY2_to_YUY2((unsigned char *)locked_surface.pBits, buf, locked_surface.Pitch, width, height, flip);
		}
		else if (surface_type == D3DFMT_P8) // 8bit with palette
		{
			if (flip)
				nsutil_image_CopyFlipped_U8((uint8_t *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width, width, height);
			else
				nsutil_image_Copy_U8((uint8_t *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width, width, height);
		}
		else if (surface_type == D3DFMT_X8R8G8B8) // RGB32
		{
			if (input_type == MAKEFOURCC('R','G','3','2'))
			{
				if (flip)
					nsutil_image_CopyFlipped_U8((uint8_t *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width*4, width*4, height);
				else
					nsutil_image_Copy_U8((uint8_t *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width*4, width*4, height);
			}
			else if (input_type == MAKEFOURCC('R','G','2','4'))
			{
				if (flip)
					nsutil_image_ConvertFlipped_RGB24_RGB32((RGB32 *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width*3, width, height);
				else
					nsutil_image_Convert_RGB24_RGB32((RGB32 *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width*3, width, height);
			}
			else if (input_type == MAKEFOURCC('R','G','B','8') && m_palette)
			{
				if (flip)
					nsutil_image_PaletteFlipped_RGB32((RGB32 *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width, width, height, (RGB32 *)m_palette);
				else
					nsutil_image_Palette_RGB32((RGB32 *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width, width, height, (RGB32 *)m_palette);
			}
		}
		else if (surface_type == D3DFMT_R8G8B8) // RGB24
		{
			if (flip || locked_surface.Pitch != width*3)
			{
				char *start = (char *)locked_surface.pBits;
				if (flip)
					start += locked_surface.Pitch * (height-1);
				ptrdiff_t pitch = flip?-locked_surface.Pitch:locked_surface.Pitch;

				for (int i=0;i<height;i++)
				{
					char *line = start + pitch * i;
					memcpy(line, buf + width*i*3, width*3);
				}
			}
			else
			{
				memcpy(locked_surface.pBits, buf, width*height*3);
			}
		}
		else if (surface_type == D3DFMT_X1R5G5B5)
		{
			if (flip)
				nsutil_image_CopyFlipped_U8((uint8_t *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width*2, width*2, height);
			else
				nsutil_image_Copy_U8((uint8_t *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width*2, width*2, height);
		}
		else if (surface_type == D3DFMT_R5G6B5)
		{
			if (flip)
				nsutil_image_CopyFlipped_U8((uint8_t *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width*2, width*2, height);
			else
				nsutil_image_Copy_U8((uint8_t *)locked_surface.pBits, locked_surface.Pitch, (const uint8_t *)buf, width*2, width*2, height);
		}

		valid_surface = true;
		/*hr = */surface->UnlockRect();
	}
	DoRender();
}

void Direct3DVideoOutput::close()
{
	opened=false;
}

int Direct3DVideoOutput::onPaint(HWND hwnd)
{
	PAINTSTRUCT p;
	BeginPaint(hwnd, &p);
	if (swap_effect == D3DSWAPEFFECT_OVERLAY)
				{
					deviceEx->PresentEx(0, 0, 0, 0, D3DPRESENT_UPDATEOVERLAYONLY);
				}
				else
				{
	RECT r;
	GetClientRect(hwnd, &r);

	if (surface && !need_change)
	{
		HRESULT hr = device->BeginScene();
		if (SUCCEEDED(hr))
			DoRender();
		else
			need_change=1;
	}
	}
	EndPaint(hwnd, &p);
	return 1;
}

void Direct3DVideoOutput::Refresh()
{
	/* TODO: sanity check but do this
	HRESULT hr = device->BeginScene();
	IDirect3DSurface9 *back_buffer = 0;
	hr = device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back_buffer);
	adjuster->adjustAspect(r);
	hr = device->StretchRect(surface, NULL, back_buffer, &r, stretch_filter);
	back_buffer->Release();
	hr = device->EndScene();
	hr = device->Present(NULL, NULL, NULL, NULL);
	*/
	InvalidateRect(hwnd, NULL, TRUE);
}

void Direct3DVideoOutput::timerCallback()
{
}

void Direct3DVideoOutput::drawSubtitle(SubsItem *item)
{
	current_subtitle = item; 
	if (current_subtitle) 
	{
		if (!subtitle_font)
		{
			int font_size = 14 + item->fontSize + MulDiv(18, (last_rect.bottom - last_rect.top), 768);
			pCreateFontW(device, font_size, 0,		400,

				1,                       
				0, 
				DEFAULT_CHARSET, 
				OUT_DEFAULT_PRECIS, 
				ANTIALIASED_QUALITY,//DEFAULT_QUALITY, 
				DEFAULT_PITCH,
				L"Arial", 
				&subtitle_font);
		}

		if (subtitle_font)
		{
			// TODO: make an AutoWideDup and use during rendering also, saves some mallocs
			AutoWide wide_text(item->text, CP_UTF8);
			if (wide_text)
				subtitle_font->PreloadTextW(wide_text, lstrlenW(wide_text));
			// TODO: DT_CALCRECT
		}
	}
}

void Direct3DVideoOutput::resetSubtitle()
{
	current_subtitle = 0;
}

void Direct3DVideoOutput::setPalette(RGBQUAD *pal)
{
	/* benski> can't get D3DFMT_P8 surfaces to use this during StretchRect, so I'll just forget about it
	for (int i=0;i<256;i++)
	{
	pal[i].rgbReserved = 0xFF;
	}
	HRESULT hr = device->SetPaletteEntries(0, (CONST PALETTEENTRY *)pal);
	hr = device->SetCurrentTexturePalette(0);
	hr = device->SetPaletteEntries(0, (CONST PALETTEENTRY *)pal);*/
	m_palette = pal;
}