-
Notifications
You must be signed in to change notification settings - Fork 14
/
OsdCompositor.cpp
759 lines (727 loc) · 28.3 KB
/
OsdCompositor.cpp
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#include "stdafx.h"
#include <DbgHelp.h>
#include "OsdCompositor.h"
#include <vmr9.h>
#include <evr9.h>
#pragma comment(lib, "dbghelp.lib")
#pragma comment(lib, "strmiids.lib")
#pragma comment(lib, "mfuuid.lib")
#if 0
static int GetRefCount(IUnknown *pUnk)
{
if (!pUnk) return 0;
pUnk->AddRef();
return pUnk->Release();
}
#endif
#define WM_SET_CONTAINER_WINDOW (WM_APP + 0)
#define WM_ADD_TEXTURE (WM_APP + 1)
#define WM_DELETE_TEXTURE (WM_APP + 2)
#define WM_GET_TEXTURE_INFO (WM_APP + 3)
#define WM_SET_TEXTURE_INFO (WM_APP + 4)
#define WM_GET_SURFACE_RECT (WM_APP + 5)
#define WM_UPDATE_SURFACE (WM_APP + 6)
#if OSD_COMPOSITOR_VERSION >= 1
#define WM_SET_UPDATE_CALLBACK (WM_APP + 7)
#define WM_GET_VERSION (WM_APP + 8)
#endif
static const LPCTSTR OSD_COMPOSITOR_WINDOW_CLASS = TEXT("TVTest Plugin OsdCompositor");
static const CLSID CLSID_madVR = {0xe1a8b82a, 0x32ce, 0x4b0d, {0xbe, 0x0d, 0xaa, 0x68, 0xc7, 0x72, 0xe4, 0x23}};
COsdCompositor::COsdCompositor()
: m_hwnd(NULL)
, m_hwndContainer(NULL)
, m_hOle32(NULL)
, m_pfnCoCreateInstance(NULL)
, m_pRenderer(NULL)
, m_RendererType(RT_VMR9)
, m_hD3D9(NULL)
, m_pD3D9(NULL)
, m_pD3DD9(NULL)
, m_pD3DS9(NULL)
, m_TxListLen(0)
, m_TxCount(0)
, m_GroupListLen(0)
#if OSD_COMPOSITOR_VERSION >= 1
, m_CallbackListLen(0)
, m_LocCallback(NULL)
#endif
{
}
COsdCompositor::~COsdCompositor()
{
Uninitialize();
}
HWND COsdCompositor::FindHandle()
{
HWND hwnd = NULL;
while ((hwnd = ::FindWindowEx(HWND_MESSAGE, hwnd, OSD_COMPOSITOR_WINDOW_CLASS, NULL)) != NULL) {
DWORD pid;
::GetWindowThreadProcessId(hwnd, &pid);
if (pid == ::GetCurrentProcessId()) break;
}
return hwnd;
}
LRESULT COsdCompositor::SendMessageToHandle(UINT Msg, WPARAM wParam, LPARAM lParam) const
{
// 自分でウィンドウを作成した場合は探さなくていい
HWND hwnd = m_hwnd ? m_hwnd : FindHandle();
if (hwnd) {
return ::SendMessage(hwnd, Msg, wParam, lParam);
}
return FALSE;
}
// TVTestの"Video Container"ウィンドウのハンドルを設定する
// このウィンドウは起動中に変化しないようなので、オブジェクトを利用する初回に1度だけ呼べばいい
bool COsdCompositor::SetContainerWindow(HWND hwndContainer)
{
return SendMessageToHandle(WM_SET_CONTAINER_WINDOW, 0, reinterpret_cast<LPARAM>(hwndContainer)) != FALSE;
}
// OSDのテクスチャを追加する
// Groupに0以外の値を入れておくとDeleteTexture()でまとめて削除できる
// テクスチャのID、または0(失敗)を返す
int COsdCompositor::AddTexture(HBITMAP hbm, int Left, int Top, bool fShow, int Group)
{
if (m_GroupListLen < _countof(m_GroupList)) {
TEXTURE_PARAM txp = {0};
txp.nSize = sizeof(TEXTURE_PARAM);
txp.Left = !fShow ? Left - 200000 : Left;
txp.Top = Top;
txp.hbm = hbm;
int ID = static_cast<int>(SendMessageToHandle(WM_ADD_TEXTURE, 0, reinterpret_cast<LPARAM>(&txp)));
if (ID != 0) {
m_GroupList[m_GroupListLen].ID = ID;
m_GroupList[m_GroupListLen++].Group = Group;
}
return ID;
}
return 0;
}
// OSDのテクスチャを削除する
// (ID,Group) = (0,0):すべて削除, (0,!0):グループのテクスチャを削除, (!0,any):IDのテクスチャを削除
// 1つ以上の削除が行われればtrueを返す
bool COsdCompositor::DeleteTexture(int ID, int Group)
{
bool fDeleted = false;
for (int i = 0; i < m_GroupListLen; ++i) {
if (!ID && !Group || !ID && m_GroupList[i].Group == Group || m_GroupList[i].ID == ID) {
if (SendMessageToHandle(WM_DELETE_TEXTURE, 0, m_GroupList[i].ID) != FALSE) {
fDeleted = true;
}
::memmove(&m_GroupList[i], &m_GroupList[i + 1], (m_GroupListLen - (i + 1)) * sizeof(m_GroupList[0]));
--m_GroupListLen;
--i;
}
}
return fDeleted;
}
// テクスチャの表示/非表示を設定する
bool COsdCompositor::ShowTexture(bool fShow, int ID, int Group)
{
bool fModified = false;
for (int i = 0; i < m_GroupListLen; ++i) {
if (!ID && !Group || !ID && m_GroupList[i].Group == Group || m_GroupList[i].ID == ID) {
TEXTURE_PARAM txp = {0};
txp.nSize = sizeof(TEXTURE_PARAM);
txp.ID = m_GroupList[i].ID;
if (SendMessageToHandle(WM_GET_TEXTURE_INFO, 0, reinterpret_cast<LPARAM>(&txp)) != FALSE) {
TEXTURE_PARAM txq = {0};
txq.nSize = sizeof(TEXTURE_PARAM);
txq.ID = txp.ID;
txq.Left = !fShow && txp.Left >= -100000 ? txp.Left - 200000 :
fShow && txp.Left < -100000 ? txp.Left + 200000 : txp.Left;
txq.Top = txp.Top;
if (txq.Left != txp.Left) {
if (SendMessageToHandle(WM_SET_TEXTURE_INFO, 0, reinterpret_cast<LPARAM>(&txq)) != FALSE) {
fModified = true;
}
}
}
}
}
return fModified;
}
// 描画領域の矩形を得る
bool COsdCompositor::GetSurfaceRect(RECT *pRect)
{
return SendMessageToHandle(WM_GET_SURFACE_RECT, 0, reinterpret_cast<LPARAM>(pRect)) != FALSE;
}
// テクスチャを合成して描画領域を更新する
// AddTexture()/DeleteTexture()が成功したら呼ぶ
bool COsdCompositor::UpdateSurface()
{
return SendMessageToHandle(WM_UPDATE_SURFACE, 0, 0) != FALSE;
}
#if OSD_COMPOSITOR_VERSION >= 1
// サーフェイス合成時(UpdateSurface()を呼んだとき)のコールバックを設定する
// AddTexture()より低コストだけど自力で再描画が必要
bool COsdCompositor::SetUpdateCallback(UpdateCallbackFunc Callback, void *pClientData, bool fTop)
{
// 自オブジェクトが設定したコールバックがあれば解除
if (m_LocCallback) {
SET_UPDATE_CALLBACK_PARAM ucp = {0};
ucp.nSize = sizeof(ucp);
ucp.Flags = 0;
ucp.Callback = m_LocCallback;
SendMessageToHandle(WM_SET_UPDATE_CALLBACK, 0, reinterpret_cast<LPARAM>(&ucp));
m_LocCallback = NULL;
}
if (Callback) {
SET_UPDATE_CALLBACK_PARAM ucp = {0};
ucp.nSize = sizeof(ucp);
ucp.Flags = fTop ? 3 : 1;
ucp.Callback = Callback;
ucp.pClientData = pClientData;
if (!SendMessageToHandle(WM_SET_UPDATE_CALLBACK, 0, reinterpret_cast<LPARAM>(&ucp))) {
return false;
}
m_LocCallback = Callback;
}
return true;
}
// 生成されたOsdCompositorウィンドウのバージョンを取得する
// 失敗または無印版の場合は0を返す
int COsdCompositor::GetVersion()
{
return static_cast<int>(SendMessageToHandle(WM_GET_VERSION, 0, 0));
}
#endif
// 初期化する
// 先行プラグインが既にウィンドウを作成していた場合にもfalseを返すので、
// 実際にオブジェクトを利用できるかどうかはFindHandle()がNULLを返すかどうかで判断する
bool COsdCompositor::Initialize()
{
if (m_hwnd) return true;
// ウィンドウクラス登録(プロセス内でただ1つに限定)
HMODULE hBase = ::GetModuleHandle(NULL);
WNDCLASS wc = {0};
wc.lpfnWndProc = WndProc;
wc.hInstance = hBase;
wc.lpszClassName = OSD_COMPOSITOR_WINDOW_CLASS;
if (::RegisterClass(&wc)) {
// ウィンドウ作成
m_hwnd = ::CreateWindow(OSD_COMPOSITOR_WINDOW_CLASS, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hBase, this);
if (m_hwnd) {
// CoCreateInstanceをフック
m_hOle32 = ::LoadLibrary(TEXT("ole32.dll"));
if (m_hOle32) {
m_pfnCoCreateInstance = (CoCreateInstanceFunc*)::GetProcAddress(m_hOle32, "CoCreateInstance");
if (m_pfnCoCreateInstance) {
if (Hook(CoCreateInstanceHook, m_pfnCoCreateInstance)) {
return true;
}
m_pfnCoCreateInstance = NULL;
}
::FreeLibrary(m_hOle32);
m_hOle32 = NULL;
}
::DestroyWindow(m_hwnd);
m_hwnd = NULL;
}
::UnregisterClass(OSD_COMPOSITOR_WINDOW_CLASS, hBase);
}
return false;
}
// 破棄する
// Initialize()の戻り値に関わらず呼ぶ
void COsdCompositor::Uninitialize()
{
#if OSD_COMPOSITOR_VERSION >= 1
SetUpdateCallback(NULL);
#endif
if (m_pfnCoCreateInstance) {
Hook(m_pfnCoCreateInstance, CoCreateInstanceHook);
m_pfnCoCreateInstance = NULL;
}
if (m_pRenderer) {
m_pRenderer->Release();
m_pRenderer = NULL;
}
if (m_hOle32) {
::FreeLibrary(m_hOle32);
m_hOle32 = NULL;
}
if (m_hwnd) {
::DestroyWindow(m_hwnd);
::UnregisterClass(OSD_COMPOSITOR_WINDOW_CLASS, ::GetModuleHandle(NULL));
m_hwnd = NULL;
}
}
bool COsdCompositor::Hook(CoCreateInstanceFunc *pfnNew, CoCreateInstanceFunc *pfnOld)
{
HMODULE hBase = ::GetModuleHandle(NULL);
ULONG ulSize;
PIMAGE_IMPORT_DESCRIPTOR pIID =
(PIMAGE_IMPORT_DESCRIPTOR)::ImageDirectoryEntryToData(hBase, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &ulSize);
if (pIID) {
for (; pIID->Name && ::lstrcmpiA("ole32.dll", (char*)hBase + pIID->Name); ++pIID);
if (pIID->Name) {
PIMAGE_THUNK_DATA pITD = (PIMAGE_THUNK_DATA)((BYTE*)hBase + pIID->FirstThunk);
for (; pITD->u1.Function && (DWORD_PTR)pITD->u1.Function != (DWORD_PTR)pfnOld; ++pITD);
if (pITD->u1.Function) {
DWORD dwOldProtect;
if (::VirtualProtect(&pITD->u1.Function, sizeof(DWORD_PTR), PAGE_EXECUTE_READWRITE, &dwOldProtect)) {
DWORD_PTR dwpBuf = (DWORD_PTR)pfnNew;
::WriteProcessMemory(::GetCurrentProcess(), &pITD->u1.Function, &dwpBuf, sizeof(DWORD_PTR), NULL);
::VirtualProtect(&pITD->u1.Function, sizeof(DWORD_PTR), dwOldProtect, &dwOldProtect);
return true;
}
}
}
}
return false;
}
LRESULT CALLBACK COsdCompositor::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
COsdCompositor *pThis = reinterpret_cast<COsdCompositor*>(::GetWindowLongPtr(hwnd, GWLP_USERDATA));
switch (uMsg) {
case WM_CREATE:
{
LPCREATESTRUCT pcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
pThis = reinterpret_cast<COsdCompositor*>(pcs->lpCreateParams);
::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
}
return 0;
case WM_DESTROY:
pThis->ReleaseDevice();
while (pThis->m_TxListLen > 0) {
_aligned_free(pThis->m_TxList[--pThis->m_TxListLen].pBits);
}
#if OSD_COMPOSITOR_VERSION >= 1
pThis->m_CallbackListLen = 0;
#endif
return 0;
case WM_SET_CONTAINER_WINDOW:
{
HWND hwnd = reinterpret_cast<HWND>(lParam);
if (pThis->m_hwndContainer != hwnd) {
pThis->m_hwndContainer = hwnd;
pThis->ReleaseDevice();
}
}
return TRUE;
case WM_ADD_TEXTURE:
if (pThis->m_TxListLen < _countof(pThis->m_TxList)) {
TEXTURE_PARAM *pTxp = reinterpret_cast<TEXTURE_PARAM*>(lParam);
BITMAP bm;
if (::GetObject(pTxp->hbm, sizeof(BITMAP), &bm) &&
bm.bmType == 0 &&
bm.bmPlanes == 1 &&
bm.bmBitsPixel == 32 &&
bm.bmWidth > 0 &&
bm.bmHeight > 0)
{
// 32bit(アルファ値つき)ビットマップのみ受け入れる
TEXTURE tx;
tx.pBits = _aligned_malloc(bm.bmWidth * bm.bmHeight * 4, 16);
if (tx.pBits) {
// 登録
tx.ID = ++pThis->m_TxCount;
tx.Left = pTxp->Left;
tx.Top = pTxp->Top;
::memcpy(tx.pBits, bm.bmBits, bm.bmWidth * bm.bmHeight * 4);
tx.Width = bm.bmWidth;
tx.Height = bm.bmHeight;
pThis->m_TxList[pThis->m_TxListLen++] = tx;
return tx.ID;
}
}
}
return 0;
case WM_DELETE_TEXTURE:
{
int ID = static_cast<int>(lParam);
for (int i = 0; i < pThis->m_TxListLen; ++i) {
if (pThis->m_TxList[i].ID == ID) {
// 登録抹消
_aligned_free(pThis->m_TxList[i].pBits);
::memmove(&pThis->m_TxList[i], &pThis->m_TxList[i + 1], (pThis->m_TxListLen - (i + 1)) * sizeof(TEXTURE));
--pThis->m_TxListLen;
return TRUE;
}
}
}
return FALSE;
case WM_GET_TEXTURE_INFO:
case WM_SET_TEXTURE_INFO:
{
TEXTURE_PARAM *pTxp = reinterpret_cast<TEXTURE_PARAM*>(lParam);
for (int i = 0; i < pThis->m_TxListLen; ++i) {
TEXTURE *p = &pThis->m_TxList[i];
if (p->ID == pTxp->ID) {
if (uMsg == WM_GET_TEXTURE_INFO) {
pTxp->nSize = sizeof(TEXTURE_PARAM);
pTxp->Left = p->Left;
pTxp->Top = p->Top;
}
else {
if (pTxp->Left != INT_MAX) p->Left = pTxp->Left;
if (pTxp->Top != INT_MAX) p->Top = pTxp->Top;
}
return TRUE;
}
}
}
return FALSE;
case WM_GET_SURFACE_RECT:
if (pThis->m_pRenderer) {
if (GetVideoPosition(reinterpret_cast<RECT*>(lParam), pThis->m_pRenderer, pThis->m_RendererType)) {
return TRUE;
}
}
return FALSE;
case WM_UPDATE_SURFACE:
if (pThis->m_pRenderer) {
RECT rc;
if (pThis->CreateDevice() && GetVideoPosition(&rc, pThis->m_pRenderer, pThis->m_RendererType)) {
int vw = rc.right - rc.left;
int vh = rc.bottom - rc.top;
RECT rcSurface;
if (vw > 0 && vh > 0 && pThis->SetupSurface(vw, vh, &rcSurface)) {
NORMALIZEDRECT nrc;
nrc.left = (float)rcSurface.left / vw;
nrc.top = (float)rcSurface.top / vh;
nrc.right = (float)rcSurface.right / vw;
nrc.bottom = (float)rcSurface.bottom / vh;
SetAlphaBitmap(pThis->m_pRenderer, pThis->m_RendererType, pThis->m_pD3DS9, &nrc);
}
else {
SetAlphaBitmap(pThis->m_pRenderer, pThis->m_RendererType, NULL, NULL);
}
}
}
return TRUE;
#if OSD_COMPOSITOR_VERSION >= 1
case WM_SET_UPDATE_CALLBACK:
{
SET_UPDATE_CALLBACK_PARAM *pUcp = reinterpret_cast<SET_UPDATE_CALLBACK_PARAM*>(lParam);
if (pUcp->Flags & 1) {
if (pThis->m_CallbackListLen < _countof(pThis->m_CallbackList)) {
// 登録
pThis->m_CallbackList[pThis->m_CallbackListLen].pClientData = pUcp->pClientData;
pThis->m_CallbackList[pThis->m_CallbackListLen].fTop = (pUcp->Flags & 2) != 0;
pThis->m_CallbackList[pThis->m_CallbackListLen++].Callback = pUcp->Callback;
return TRUE;
}
}
else {
for (int i = 0; i < pThis->m_CallbackListLen; ++i) {
if (pThis->m_CallbackList[i].Callback == pUcp->Callback) {
// 登録抹消
::memmove(&pThis->m_CallbackList[i], &pThis->m_CallbackList[i + 1],
(pThis->m_CallbackListLen - (i + 1)) * sizeof(pThis->m_CallbackList[0]));
--pThis->m_CallbackListLen;
return TRUE;
}
}
}
}
return FALSE;
case WM_GET_VERSION:
return OSD_COMPOSITOR_VERSION;
#endif
}
return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
}
// ビットイメージをアルファ合成する
// 同時にbottom-up→top-down変換する
void COsdCompositor::ComposeAlpha(DWORD* __restrict pBitsDest, int PitchDest, int WidthDest, int HeightDest,
const DWORD* __restrict pBits, int Pitch, int Width, int Height, int Left, int Top)
{
int ySrc = min(Height - 1 + Top, Height - 1);
int yDest = max(Top, 0);
int yRange = max(Top + Height - HeightDest, 0);
for (; ySrc >= yRange; --ySrc, ++yDest) {
int i = ySrc * Pitch + max(-Left, 0);
int j = yDest * PitchDest + max(Left, 0);
int r = ySrc * Pitch + min(WidthDest - Left, Width);
for (;; ++i, ++j) {
// ほとんど重ならないことを想定
for (; i < r && !(pBitsDest[j] & 0xFF000000); ++i, ++j) {
pBitsDest[j] = pBits[i];
}
if (i >= r) break;
// 重なりがある場合
DWORD v = pBits[i];
DWORD w = pBitsDest[j];
BYTE va = v>>24;
BYTE wa = w>>24;
BYTE outa = va + ((wa*(255-va)+255)>>8);
if (outa) {
BYTE outr = ((v>>16&0xFF)*va + (((w>>16&0xFF)*wa*(255-va)+255)>>8)) / outa;
BYTE outg = ((v>>8&0xFF)*va + (((w>>8&0xFF)*wa*(255-va)+255)>>8)) / outa;
BYTE outb = ((v&0xFF)*va + (((w&0xFF)*wa*(255-va)+255)>>8)) / outa;
pBitsDest[j] = outa<<24 | outr<<16 | outg<<8 | outb;
}
else {
pBitsDest[j] = 0;
}
}
}
}
bool COsdCompositor::GetVideoPosition(RECT *pRect, IBaseFilter *pRenderer, RENDERER_TYPE RendererType)
{
bool fRet = false;
if (RendererType == RT_VMR9) {
IVMRFilterConfig9 *pVMRFC9;
HRESULT hr = pRenderer->QueryInterface(IID_IVMRFilterConfig9, (void**)&pVMRFC9);
if (SUCCEEDED(hr)) {
DWORD dwMode;
hr = pVMRFC9->GetRenderingMode(&dwMode);
if (SUCCEEDED(hr) && dwMode == VMR9Mode_Windowless) {
IVMRWindowlessControl9 *pVMRWC9;
hr = pRenderer->QueryInterface(IID_IVMRWindowlessControl9, (void**)&pVMRWC9);
if (SUCCEEDED(hr)) {
RECT rcSrc;
hr = pVMRWC9->GetVideoPosition(&rcSrc, pRect);
if (SUCCEEDED(hr)) {
fRet = true;
}
pVMRWC9->Release();
}
}
pVMRFC9->Release();
}
}
else if (RendererType == RT_EVR) {
IMFGetService *pMFGS;
HRESULT hr = pRenderer->QueryInterface(IID_IMFGetService, (void**)&pMFGS);
if (SUCCEEDED(hr)) {
IMFVideoDisplayControl *pIMFVDC;
hr = pMFGS->GetService(MR_VIDEO_RENDER_SERVICE, IID_IMFVideoDisplayControl, (void**)&pIMFVDC);
if (SUCCEEDED(hr)) {
MFVideoNormalizedRect nrcSrc;
hr = pIMFVDC->GetVideoPosition(&nrcSrc, pRect);
if (SUCCEEDED(hr)) {
fRet = true;
}
pIMFVDC->Release();
}
pMFGS->Release();
}
}
return fRet;
}
bool COsdCompositor::SetAlphaBitmap(IBaseFilter *pRenderer, RENDERER_TYPE RendererType, IDirect3DSurface9 *pD3DS9, const NORMALIZEDRECT *pNrc)
{
bool fRet = false;
if (RendererType == RT_VMR9) {
IVMRMixerBitmap9 *pVMRMB9;
HRESULT hr = pRenderer->QueryInterface(IID_IVMRMixerBitmap9, (void**)&pVMRMB9);
if (SUCCEEDED(hr)) {
VMR9AlphaBitmap ab;
ab.dwFlags = pD3DS9 ? VMR9AlphaBitmap_EntireDDS | VMR9AlphaBitmap_FilterMode : VMR9AlphaBitmap_Disable;
ab.hdc = NULL;
ab.pDDS = pD3DS9;
ab.rDest.left = pNrc ? pNrc->left : 0.0f;
ab.rDest.top = pNrc ? pNrc->top : 0.0f;
ab.rDest.right = pNrc ? pNrc->right : 1.0f;
ab.rDest.bottom = pNrc ? pNrc->bottom : 1.0f;
ab.fAlpha = 1.0f;
ab.dwFilterMode = MixerPref_PointFiltering;
hr = pVMRMB9->SetAlphaBitmap(&ab);
if (SUCCEEDED(hr)) {
fRet = true;
}
pVMRMB9->Release();
}
}
else if (RendererType == RT_EVR) {
IMFGetService *pMFGS;
HRESULT hr = pRenderer->QueryInterface(IID_IMFGetService, (void**)&pMFGS);
if (SUCCEEDED(hr)) {
IMFVideoMixerBitmap *pMFVMB;
hr = pMFGS->GetService(MR_VIDEO_MIXER_SERVICE, IID_IMFVideoMixerBitmap, (void**)&pMFVMB);
if (SUCCEEDED(hr)) {
if (pD3DS9) {
MFVideoAlphaBitmap ab;
ab.GetBitmapFromDC = FALSE;
ab.bitmap.pDDS = pD3DS9;
ab.params.dwFlags = MFVideoAlphaBitmap_EntireDDS | MFVideoAlphaBitmap_FilterMode;
if (pNrc) {
ab.params.dwFlags |= MFVideoAlphaBitmap_DestRect;
ab.params.nrcDest.left = pNrc->left;
ab.params.nrcDest.top = pNrc->top;
ab.params.nrcDest.right = pNrc->right;
ab.params.nrcDest.bottom = pNrc->bottom;
}
// MSDNによるとMFVideoAlphaBitmap_FilterModeのデフォルトのはずだが実際には違うみたい
ab.params.dwFilterMode = D3DTEXF_POINT;
hr = pMFVMB->SetAlphaBitmap(&ab);
if (SUCCEEDED(hr)) {
fRet = true;
}
}
else {
hr = pMFVMB->ClearAlphaBitmap();
if (SUCCEEDED(hr)) {
fRet = true;
}
}
pMFVMB->Release();
}
pMFGS->Release();
}
}
return fRet;
}
bool COsdCompositor::CreateDevice()
{
if (m_hD3D9) return true;
if (!m_hwndContainer) return false;
m_hD3D9 = ::LoadLibrary(TEXT("d3d9.dll"));
if (m_hD3D9) {
Direct3DCreate9Func *pfnDirect3DCreate9 = (Direct3DCreate9Func*)::GetProcAddress(m_hD3D9, "Direct3DCreate9");
if (pfnDirect3DCreate9) {
m_pD3D9 = pfnDirect3DCreate9(D3D_SDK_VERSION);
if (m_pD3D9) {
D3DDISPLAYMODE dm;
HRESULT hr = m_pD3D9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dm);
if (SUCCEEDED(hr)) {
D3DPRESENT_PARAMETERS pp = {0};
pp.BackBufferFormat = dm.Format;
pp.MultiSampleType = D3DMULTISAMPLE_NONE;
pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
pp.Windowed = TRUE;
pp.hDeviceWindow = m_hwndContainer;
hr = m_pD3D9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pp, &m_pD3DD9);
if (SUCCEEDED(hr)) {
return true;
}
m_pD3DD9 = NULL;
}
m_pD3D9->Release();
m_pD3D9 = NULL;
}
}
::FreeLibrary(m_hD3D9);
m_hD3D9 = NULL;
}
return false;
}
void COsdCompositor::ReleaseDevice()
{
if (m_pD3DS9) {
m_pD3DS9->Release();
m_pD3DS9 = NULL;
}
if (m_pD3DD9) {
m_pD3DD9->Release();
m_pD3DD9 = NULL;
}
if (m_pD3D9) {
m_pD3D9->Release();
m_pD3D9 = NULL;
}
if (m_hD3D9) {
::FreeLibrary(m_hD3D9);
m_hD3D9 = NULL;
}
}
bool COsdCompositor::SetupSurface(int VideoWidth, int VideoHeight, RECT *pSurfaceRect)
{
// テクスチャを映像領域に配置するために必要な最小矩形をもとめる
RECT rcVideo = { 0, 0, VideoWidth, VideoHeight };
RECT rcSurface = {0};
for (int i = 0; i < m_TxListLen; ++i) {
RECT rc = { m_TxList[i].Left, m_TxList[i].Top,
m_TxList[i].Left + m_TxList[i].Width,
m_TxList[i].Top + m_TxList[i].Height };
RECT rcInter;
if (::IntersectRect(&rcInter, &rcVideo, &rc)) {
// テクスチャは映像領域内にある
RECT rcUnion;
::UnionRect(&rcUnion, &rcSurface, &rcInter);
rcSurface = rcUnion;
}
}
// サーフェイスをつくる
HRESULT hr;
if (m_pD3DS9) {
D3DSURFACE_DESC desc;
hr = m_pD3DS9->GetDesc(&desc);
if (FAILED(hr) || ::IsRectEmpty(&rcSurface) ||
(int)desc.Width != rcSurface.right - rcSurface.left ||
(int)desc.Height != rcSurface.bottom - rcSurface.top)
{
m_pD3DS9->Release();
m_pD3DS9 = NULL;
}
}
if (!m_pD3DS9 && m_pD3DD9 && !::IsRectEmpty(&rcSurface)) {
hr = m_pD3DD9->CreateOffscreenPlainSurface(rcSurface.right - rcSurface.left, rcSurface.bottom - rcSurface.top,
D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &m_pD3DS9, NULL);
if (FAILED(hr)) {
m_pD3DS9 = NULL;
}
}
// テクスチャをサーフェイスに合成
if (m_pD3DS9) {
D3DLOCKED_RECT lr;
HRESULT hr = m_pD3DS9->LockRect(&lr, NULL, 0);
if (SUCCEEDED(hr)) {
//::memset(lr.pBits, 0x60, lr.Pitch * (rcSurface.bottom - rcSurface.top)); // DEBUG
::memset(lr.pBits, 0, lr.Pitch * (rcSurface.bottom - rcSurface.top));
#if OSD_COMPOSITOR_VERSION >= 1
for (int i = 0; i < m_CallbackListLen; ++i) {
if (!m_CallbackList[i].fTop) {
m_CallbackList[i].Callback(lr.pBits, &rcSurface, lr.Pitch, m_CallbackList[i].pClientData);
}
}
#endif
for (int i = 0; i < m_TxListLen; ++i) {
ComposeAlpha((DWORD*)lr.pBits, lr.Pitch / 4,
rcSurface.right - rcSurface.left, rcSurface.bottom - rcSurface.top,
(DWORD*)m_TxList[i].pBits, m_TxList[i].Width, m_TxList[i].Width, m_TxList[i].Height,
m_TxList[i].Left - rcSurface.left, m_TxList[i].Top - rcSurface.top);
}
#if OSD_COMPOSITOR_VERSION >= 1
for (int i = 0; i < m_CallbackListLen; ++i) {
if (m_CallbackList[i].fTop) {
m_CallbackList[i].Callback(lr.pBits, &rcSurface, lr.Pitch, m_CallbackList[i].pClientData);
}
}
#endif
m_pD3DS9->UnlockRect();
}
}
*pSurfaceRect = rcSurface;
return m_pD3DS9 != NULL;
}
HRESULT WINAPI COsdCompositor::CoCreateInstanceHook(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID FAR* ppv)
{
HRESULT hr = E_FAIL;
HWND hwnd = FindHandle();
if (hwnd) {
COsdCompositor *pThis = reinterpret_cast<COsdCompositor*>(::GetWindowLongPtr(hwnd, GWLP_USERDATA));
if (pThis) {
// 他のレンダラが生成されるとき解放
if (pThis->m_pRenderer &&
(IsEqualCLSID(CLSID_VideoMixingRenderer, rclsid) ||
IsEqualCLSID(CLSID_VideoMixingRenderer9, rclsid) ||
IsEqualCLSID(CLSID_EnhancedVideoRenderer, rclsid) ||
IsEqualCLSID(CLSID_OverlayMixer, rclsid) ||
IsEqualCLSID(CLSID_madVR, rclsid)))
{
pThis->m_pRenderer->Release();
pThis->m_pRenderer = NULL;
}
hr = pThis->m_pfnCoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, ppv);
// レンダラの生成を監視(TVTestのVideoRenderer.cpp参照)
// 必ずメインスレッドで呼ばれることを仮定している
if (SUCCEEDED(hr)) {
if (IsEqualCLSID(CLSID_VideoMixingRenderer9, rclsid)) {
pThis->m_pRenderer = static_cast<IBaseFilter*>(*ppv);
pThis->m_pRenderer->AddRef();
pThis->m_RendererType = RT_VMR9;
}
else if (IsEqualCLSID(CLSID_EnhancedVideoRenderer, rclsid)) {
pThis->m_pRenderer = static_cast<IBaseFilter*>(*ppv);
pThis->m_pRenderer->AddRef();
pThis->m_RendererType = RT_EVR;
}
}
}
}
return hr;
}