summaryrefslogtreecommitdiff
path: root/ARMFCaptureD3D/device.cpp
blob: 13ae7ccc3a522598837524b5fa5da47b288077d1 (plain)
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
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
1189
1190
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
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
//////////////////////////////////////////////////////////////////////////
//
// device.cpp: Manages the Direct3D device
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////

#include "ARMFCaptureD3D.h"
#include "BufferLock.h"
#include "debug.h"
#include "ARAnalyse.h"

extern ARAnalyse	*g_pAnalyse;

void TransformImage_RGB24(
    BYTE*       pDest,
    LONG        lDestStride,
    const BYTE* pSrc,
    LONG        lSrcStride,
    DWORD       dwWidthInPixels,
    DWORD       dwHeightInPixels
    );

void TransformImage_RGB32(
    BYTE*       pDest,
    LONG        lDestStride,
    const BYTE* pSrc,
    LONG        lSrcStride,
    DWORD       dwWidthInPixels,
    DWORD       dwHeightInPixels
    );

void TransformImage_YUY2(
    BYTE*       pDest,
    LONG        lDestStride,
    const BYTE* pSrc,
    LONG        lSrcStride,
    DWORD       dwWidthInPixels,
    DWORD       dwHeightInPixels
    );

void TransformImage_NV12(
    BYTE* pDst,
    LONG dstStride,
    const BYTE* pSrc,
    LONG srcStride,
    DWORD dwWidthInPixels,
    DWORD dwHeightInPixels
    );


RECT    LetterBoxRect(const RECT& rcSrc, const RECT& rcDst);
RECT    CorrectAspectRatio(const RECT& src, const MFRatio& srcPAR);
HRESULT GetDefaultStride(IMFMediaType *pType, LONG *plStride);


inline LONG Width(const RECT& r)
{
    return r.right - r.left;
}

inline LONG Height(const RECT& r)
{
    return r.bottom - r.top;
}


// Static table of output formats and conversion functions.
struct ConversionFunction
{
    GUID               subtype;
    IMAGE_TRANSFORM_FN xform;
};


ConversionFunction   g_FormatConversions[] =
{
    { MFVideoFormat_RGB32, TransformImage_RGB32 },
    { MFVideoFormat_RGB24, TransformImage_RGB24 },
    { MFVideoFormat_YUY2,  TransformImage_YUY2  },
    { MFVideoFormat_NV12,  TransformImage_NV12  }
};

const DWORD   g_cFormats = ARRAYSIZE(g_FormatConversions);


//-------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------

DrawDevice::DrawDevice() :
    m_hwnd(NULL),
    m_pDevice(NULL),
    m_pSwapChain(NULL),
	m_pVertexBuffer(NULL),
	m_pIndexBuffer(NULL),
#ifdef DX9
    m_pD3D(NULL),
    m_format(D3DFMT_UNKNOWN),
#else
	m_format(DXGI_FORMAT_UNKNOWN),
#endif
    m_width(0),
    m_height(0),
    m_lDefaultStride(0),
    m_interlace(MFVideoInterlace_Unknown),
    m_convertFn(NULL)
{
    m_PixelAR.Denominator = m_PixelAR.Numerator = 1;

    ZeroMemory(&m_d3dpp, sizeof(m_d3dpp));
}


//-------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------

DrawDevice::~DrawDevice()
{
    DestroyDevice();
}

void DrawDevice::GetSize(UINT* width, UINT *height) const
{
	*width = m_width;
	*height = m_height;
}
//-------------------------------------------------------------------
// GetFormat
//
// Get a supported output format by index.
//-------------------------------------------------------------------

HRESULT DrawDevice::GetFormat(DWORD index, GUID *pSubtype) const
{
    if (index < g_cFormats)
    {
        *pSubtype = g_FormatConversions[index].subtype;
        return S_OK;
    }
    return MF_E_NO_MORE_TYPES;
}


//-------------------------------------------------------------------
//  IsFormatSupported
//
//  Query if a format is supported.
//-------------------------------------------------------------------

BOOL DrawDevice::IsFormatSupported(REFGUID subtype) const
{
    for (DWORD i = 0; i < g_cFormats; i++)
    {
        if (subtype == g_FormatConversions[i].subtype)
        {
            return TRUE;
        }
    }
    return FALSE;
}




//-------------------------------------------------------------------
// CreateDevice
//
// Create the Direct3D device.
//-------------------------------------------------------------------

HRESULT DrawDevice::CreateDevice(HWND hwnd)
{
    HRESULT hr = S_OK;
    if (m_pDevice)
    {
        return S_OK;
    }
#ifdef DX9
    // Create the Direct3D object.
    if (m_pD3D == NULL)
    {
        m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);

        if (m_pD3D == NULL)
        {
            return E_FAIL;
        }
    }


    D3DPRESENT_PARAMETERS pp = { 0 };
    D3DDISPLAYMODE mode = { 0 };

	pp.BackBufferFormat = D3DFMT_A8R8G8B8;
    pp.SwapEffect = D3DSWAPEFFECT_FLIP;
    pp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
    pp.Windowed = TRUE;
    pp.hDeviceWindow = hwnd;

    hr = m_pD3D->GetAdapterDisplayMode(
        D3DADAPTER_DEFAULT,
        &mode
        );

    if (FAILED(hr)) { goto done; }

    hr = m_pD3D->CheckDeviceType(
        D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL,
        mode.Format,
        pp.BackBufferFormat,
        TRUE    // windowed
        );

    if (FAILED(hr)) { goto done; }

    

    hr = m_pD3D->CreateDevice(
        D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL,
        hwnd,
        D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE,
        &pp,
        &m_pDevice
        );
#else

    DXGI_SWAP_CHAIN_DESC pp = { 0 };
	ZeroMemory( &pp, sizeof( pp ) );        


	pp.BufferCount = 1;
	//pp.BufferDesc.Width = width;
	//pp.BufferDesc.Height = height;
	pp.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	pp.BufferDesc.RefreshRate.Numerator = 0;     //users =0, originally 60        
	pp.BufferDesc.RefreshRate.Denominator = 0;   
	pp.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;

    //pp.SwapEffect = D3DSWAPEFFECT_COPY;
    //pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    pp.Windowed = TRUE;
    pp.OutputWindow  = hwnd;

	D3D_FEATURE_LEVEL FeatureLevels = D3D_FEATURE_LEVEL_11_0;


    hr = D3D11CreateDevice( NULL,D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &FeatureLevels, 1, D3D11_SDK_VERSION, &m_pDevice, &FeatureLevels, &m_pDeviceContext );

#endif
	
    if (FAILED(hr)) { goto done; }
	//  Turn on Blending
	m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	m_pDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
	m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

    m_hwnd = hwnd;
    m_d3dpp = pp;

done:
    return hr;
}

//-------------------------------------------------------------------
// SetConversionFunction
//
// Set the conversion function for the specified video format.
//-------------------------------------------------------------------

HRESULT DrawDevice::SetConversionFunction(REFGUID subtype)
{
    m_convertFn = NULL;

    for (DWORD i = 0; i < g_cFormats; i++)
    {
        if (g_FormatConversions[i].subtype == subtype)
        {
            m_convertFn = g_FormatConversions[i].xform;
            return S_OK;
        }
    }

    return MF_E_INVALIDMEDIATYPE;
}


//-------------------------------------------------------------------
// SetVideoType
//
// Set the video format.
//-------------------------------------------------------------------

HRESULT DrawDevice::SetVideoType(IMFMediaType *pType)
{
    HRESULT hr = S_OK;
    GUID subtype = { 0 };
    MFRatio PAR = { 0 };

    // Find the video subtype.
    hr = pType->GetGUID(MF_MT_SUBTYPE, &subtype);

    if (FAILED(hr)) { goto done; }

    // Choose a conversion function.
    // (This also validates the format type.)

    hr = SetConversionFunction(subtype);

    if (FAILED(hr)) { goto done; }

    //
    // Get some video attributes.
    //

    // Get the frame size.
    hr = MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &m_width, &m_height);

    if (FAILED(hr)) { goto done; }


    // Get the interlace mode. Default: assume progressive.
    m_interlace = (MFVideoInterlaceMode)MFGetAttributeUINT32(
        pType,
        MF_MT_INTERLACE_MODE,
        MFVideoInterlace_Progressive
        );

    // Get the image stride.
    hr = GetDefaultStride(pType, &m_lDefaultStride);

    if (FAILED(hr)) { goto done; }

    // Get the pixel aspect ratio. Default: Assume square pixels (1:1)
    hr = MFGetAttributeRatio(
        pType,
        MF_MT_PIXEL_ASPECT_RATIO,
        (UINT32*)&PAR.Numerator,
        (UINT32*)&PAR.Denominator
        );

    if (SUCCEEDED(hr))
    {
        m_PixelAR = PAR;
    }
    else
    {
        m_PixelAR.Numerator = m_PixelAR.Denominator = 1;
    }
#ifdef DX9
    m_format = (D3DFORMAT)subtype.Data1;
#else
	m_format = (DXGI_FORMAT)subtype.Data1;
#endif
    // Create Direct3D swap chains.

    hr = CreateSwapChains();

    if (FAILED(hr)) { goto done; }


    // Update the destination rectangle for the correct
    // aspect ratio.

    UpdateDestinationRect();

done:
    if (FAILED(hr))
    {
#ifdef DX9
        m_format = D3DFMT_UNKNOWN;
#else
		m_format = DXGI_FORMAT_UNKNOWN;
#endif
        m_convertFn = NULL;
    }
    return hr;
}

//-------------------------------------------------------------------
//  UpdateDestinationRect
//
//  Update the destination rectangle for the current window size.
//  The destination rectangle is letterboxed to preserve the
//  aspect ratio of the video image.
//-------------------------------------------------------------------

void DrawDevice::UpdateDestinationRect()
{
    RECT rcClient;
    RECT rcSrc = { 0, 0, m_width, m_height };

    GetClientRect(m_hwnd, &rcClient);

    rcSrc = CorrectAspectRatio(rcSrc, m_PixelAR);

    m_rcDest = LetterBoxRect(rcSrc, rcClient);
}


//-------------------------------------------------------------------
// CreateSwapChains
//
// Create Direct3D swap chains.
//-------------------------------------------------------------------

HRESULT DrawDevice::CreateSwapChains()
{
    HRESULT hr = S_OK;
#ifdef DX9
    D3DPRESENT_PARAMETERS pp = { 0 };

    SafeRelease(&m_pSwapChain);

    pp.BackBufferWidth  = m_width;
    pp.BackBufferHeight = m_height;
    pp.Windowed = TRUE;
    pp.SwapEffect = D3DSWAPEFFECT_FLIP;
    pp.hDeviceWindow = m_hwnd;
    pp.BackBufferFormat = D3DFMT_A8R8G8B8;
    pp.Flags =
        D3DPRESENTFLAG_VIDEO | D3DPRESENTFLAG_DEVICECLIP |
        D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
    pp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
	//pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    pp.BackBufferCount = 2;

    hr = m_pDevice->CreateAdditionalSwapChain(&pp, &m_pSwapChain);
#else
	DXGI_SWAP_CHAIN_DESC pp = { 0 };

    SafeRelease(&m_pSwapChain);
	pp.BufferCount = 1;
	pp.BufferDesc.Width = m_width;
	pp.BufferDesc.Height = m_height;
	pp.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	pp.BufferDesc.RefreshRate.Numerator = 0;     //users =0, originally 60        
	pp.BufferDesc.RefreshRate.Denominator = 0;   
	pp.Windowed = TRUE;
    pp.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; //D3DSWAPEFFECT_FLIP;
    pp.OutputWindow = m_hwnd;
    
    pp.Flags = DXGI_SWAP_CHAIN_FLAG_NONPREROTATED;
       /* D3DPRESENTFLAG_VIDEO | D3DPRESENTFLAG_DEVICECLIP |
        D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;*/
    //pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

	IDXGIDevice* m_pDXGIDevice; 
	IDXGIAdapter* m_pDXGIAdapter;
	IDXGIFactory* m_pDXGIFactory;

	hr = m_pDevice->QueryInterface(__uuidof(IDXGIDevice), (void **)& m_pDXGIDevice);

	 if ( hr != S_OK )
	 {
	 DBGMSG(L"cDX10Mananger::Initialise(): Unable to get DXGIDevice (hr = 0x%0X)\n", hr );
	 return hr;
	 }

	 hr = m_pDXGIDevice->GetParent(__uuidof(IDXGIAdapter), (void **)& m_pDXGIAdapter);

	 if ( hr != S_OK )
	 {
	 DBGMSG(L"cDX10Mananger::Initialise(): Unable to get DXGIAdapter (hr = 0x%0X)\n", hr );
	 return hr;
	 }

	 hr = m_pDXGIAdapter->GetParent(__uuidof(IDXGIFactory), (void **)& m_pDXGIFactory);

	 if ( hr != S_OK )
	 {
	 DBGMSG(L"cDX10Mananger::Initialise(): Unable to get DXGIFactory (hr = 0x%0X)\n", hr );
	 return hr;
	 } 
	 hr =  m_pDXGIFactory->CreateSwapChain(m_pDevice, &pp,  &m_pSwapChain );
#endif

    return hr;
}


//-------------------------------------------------------------------
// DrawFrame
//
// Draw the video frame.
//-------------------------------------------------------------------

HRESULT DrawDevice::DrawFrame(IMFMediaBuffer *pBuffer)
{
    if (m_convertFn == NULL)
    {
        return MF_E_INVALIDREQUEST;
    }

    HRESULT hr = S_OK;
    BYTE *pbScanline0 = NULL;
    LONG lStride = 0;
#ifdef DX9
    D3DLOCKED_RECT lr;

    IDirect3DSurface9 *pSurf = NULL;
    IDirect3DSurface9 *pBB = NULL;
#else
	DXGI_MAPPED_RECT lr;

    IDXGISurface  *pSurf = NULL;
    IDXGISurface  *pBB = NULL;

#endif
    if (m_pDevice == NULL || m_pSwapChain == NULL)
    {
        return S_OK;
    }


    VideoBufferLock buffer(pBuffer);    // Helper object to lock the video buffer.

    hr = TestCooperativeLevel();

    if (FAILED(hr)) { goto done; }

    // Lock the video buffer. This method returns a pointer to the first scan
    // line in the image, and the stride in bytes.

    hr = buffer.LockBuffer(m_lDefaultStride, m_height, &pbScanline0, &lStride);
	
    if (FAILED(hr)) { goto done; }
#ifdef DX9
    // Get the swap-chain surface.
    hr = m_pSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pSurf);

    if (FAILED(hr)) { goto done; }

    // Lock the swap-chain surface.
	hr = pSurf->LockRect(&lr, NULL, D3DLOCK_NOSYSLOCK );
#else
	// Get the swap-chain surface.
    hr = m_pSwapChain->GetBuffer(0, __uuidof(pSurf), reinterpret_cast<void**>(&pSurf));

    if (FAILED(hr)) { goto done; }

    // Lock the swap-chain surface.
	hr = pSurf->Map( &lr, DXGI_MAP_WRITE  );
#endif
    if (FAILED(hr)) { goto done; }


    // Convert the frame. This also copies it to the Direct3D surface.

    m_convertFn(
        (BYTE*)lr.pBits,
        lr.Pitch,
        pbScanline0,
        lStride,
        m_width,
        m_height
        );
#ifdef DX9
    hr = pSurf->UnlockRect();

    if (FAILED(hr)) { goto done; }


    // Color fill the back buffer.
    hr = m_pDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBB);

    if (FAILED(hr)) { goto done; }

    hr = m_pDevice->ColorFill(pBB, NULL, D3DCOLOR_XRGB(0x80, 0x80, 0x80));

    if (FAILED(hr)) { goto done; }


    // Blit the frame.

    hr = m_pDevice->StretchRect(pSurf, NULL, pBB, &m_rcDest, D3DTEXF_LINEAR);

    if (FAILED(hr)) { goto done; }

	RenderObjects();
    // Present the frame.

    hr = m_pDevice->Present(NULL, NULL, NULL, NULL);

	
#else
	hr = pSurf->Unmap();

    if (FAILED(hr)) { goto done; }

	//Create a render target view
    ID3D11Texture2D *pBackBuffer;
	ID3D11RenderTargetView *g_pRenderTargetView;

	hr = m_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID*)&pBackBuffer );
    
	if (FAILED(hr)) { goto done; }

    hr = m_pDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );
    pBackBuffer->Release();
   if (FAILED(hr)) { goto done; }

    m_pDeviceContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL );

    //
    // Clear the backbuffer
    //
    float ClearColor[4] = { 0.5f, 0.5f, 0.5f, 1.0f }; // RGBA
    m_pDeviceContext->ClearRenderTargetView( g_pRenderTargetView, ClearColor );
    

    if (FAILED(hr)) { goto done; }


    // Blit the frame.

    //hr = m_pDeviceContext->StretchRect(pSurf, NULL, pBB, &m_rcDest, D3DTEXF_LINEAR);

    if (FAILED(hr)) { goto done; }


    // Present the frame.

    hr = m_pSwapChain->Present(0,0);
#endif

done:
#ifdef DX9
    SafeRelease(&pBB);
    if ( pSurf) SafeRelease(&pSurf);
#endif
    return hr;
}

//-------------------------------------------------------------------
// TestCooperativeLevel
//
// Test the cooperative-level status of the Direct3D device.
//-------------------------------------------------------------------

HRESULT DrawDevice::TestCooperativeLevel()
{
    if (m_pDevice == NULL)
    {
        return E_FAIL;
    }

    HRESULT hr = S_OK;
#ifdef DX9
    // Check the current status of D3D9 device.
    hr = m_pDevice->TestCooperativeLevel();

    switch (hr)
    {
    case D3D_OK:
        break;

    case D3DERR_DEVICELOST:
        hr = S_OK;

    case D3DERR_DEVICENOTRESET:
        hr = ResetDevice();
        break;

    default:
        // Some other failure.
        break;
    }
#endif
    return hr;
}


//-------------------------------------------------------------------
// ResetDevice
//
// Resets the Direct3D device.
//-------------------------------------------------------------------

HRESULT DrawDevice::ResetDevice()
{

    HRESULT hr = S_OK;
#ifdef DX9
    if (m_pDevice)
    {
        D3DPRESENT_PARAMETERS d3dpp = m_d3dpp;

        hr = m_pDevice->Reset(&d3dpp);

        if (FAILED(hr))
        {
            DestroyDevice();
        }
    }

    if (m_pDevice == NULL)
    {
        hr = CreateDevice(m_hwnd);

        if (FAILED(hr)) { goto done; }
    }

    if ((m_pSwapChain == NULL) && (m_format != D3DFMT_UNKNOWN))
    {
        hr = CreateSwapChains();

        if (FAILED(hr)) { goto done; }

        UpdateDestinationRect();
    }
#else
	if (m_pDevice)
    {
        DestroyDevice();
    }

    if (m_pDevice == NULL)
    {
        hr = CreateDevice(m_hwnd);

        if (FAILED(hr)) { goto done; }
    }

    if ((m_pSwapChain == NULL) && (m_format != DXGI_FORMAT_UNKNOWN))
    {
        hr = CreateSwapChains();

        if (FAILED(hr)) { goto done; }

        UpdateDestinationRect();
    }
#endif
done:

   return hr;
}


//-------------------------------------------------------------------
// DestroyDevice
//
// Release all Direct3D resources.
//-------------------------------------------------------------------

void DrawDevice::DestroyDevice()
{
    SafeRelease(&m_pSwapChain);
    SafeRelease(&m_pDevice);
#ifdef DX9
    SafeRelease(&m_pD3D);
	if ( m_pVertexBuffer ) SafeRelease(&m_pVertexBuffer);
	if ( m_pIndexBuffer ) SafeRelease(&m_pIndexBuffer);
#endif
}


struct CUSTOMVERTEX {FLOAT X, Y, Z,RHW; DWORD COLOR;};

typedef struct CUSTOMVERTEX CustomVertex;

#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

#define TRSF( axe,  data )  (translate##axe +  (scale##axe * data));

HRESULT DrawDevice::RenderObjects()
{   
	HRESULT hr = S_OK;
	//const ARFloat* nMatrix;
	DWORD color = D3DCOLOR_ARGB(127, 0, 127, 0);
	int count = g_pAnalyse->markers.size();
	if ( !count ) return hr;
	//D3DMATRIX world_matrix;
	//D3DMATRIX view_matrix;
	//D3DMATRIX projection_matrix;

	//nMatrix =	g_pAnalyse->getModelViewMatrix();
	//
	////OpenGL
 //           //m0 m4 m8  m12
 //           //m1 m5 m9  m13
 //           //m2 m6 m10 m14
 //           //m3 m7 m11 m15
 //  //WPF
 //           //M11   M12   M13   M14
 //           //M21   M22   M23   M24
 //           //M31   M32   M33   M34
 //           //M41   M42   M43   M44
 //           
	//view_matrix._11 = nMatrix[0];
 //   view_matrix._12 = nMatrix[4];
 //   view_matrix._13 = nMatrix[8];
 //   view_matrix._14 = nMatrix[12];
 //   view_matrix._21 = nMatrix[1];
 //   view_matrix._22 = nMatrix[5];
 //   view_matrix._23 = nMatrix[9];
 //   view_matrix._24 = nMatrix[13];
 //   view_matrix._31 = nMatrix[2];
 //   view_matrix._32 = nMatrix[6];
 //   view_matrix._33 = nMatrix[10];
 //   view_matrix._34 = nMatrix[14];
 //   view_matrix._41 = nMatrix[3];
 //   view_matrix._42 = nMatrix[7];
 //   view_matrix._43 = nMatrix[11];
 //   view_matrix._44 = nMatrix[15];
	//nMatrix =	g_pAnalyse->getProjectionMatrix();
	//projection_matrix._11 = nMatrix[0];
 //   projection_matrix._12 = nMatrix[4];
 //   projection_matrix._13 = nMatrix[8];
 //   projection_matrix._14 = nMatrix[12];
 //   projection_matrix._21 = nMatrix[1];
 //   projection_matrix._22 = nMatrix[5];
 //   projection_matrix._23 = nMatrix[9];
 //   projection_matrix._24 = nMatrix[13];
 //   projection_matrix._31 = nMatrix[2];
 //   projection_matrix._32 = nMatrix[6];
 //   projection_matrix._33 = nMatrix[10];
 //   projection_matrix._34 = nMatrix[14];
 //   projection_matrix._41 = nMatrix[3];
 //   projection_matrix._42 = nMatrix[7];
 //   projection_matrix._43 = nMatrix[11];
 //   projection_matrix._44 = nMatrix[15];

	float scaleX = (float)(m_rcDest.right - m_rcDest.left ) / (float)m_width;
	float scaleY = (float)(m_rcDest.bottom - m_rcDest.top ) / (float)m_height;
	float translateX = m_rcDest.left;
	float translateY = m_rcDest.top;
	CustomVertex *quad =   new CustomVertex[4*count];
	short *indexes = new short[6*count];
	int i =0;
	for( ARAnalyse::MarkersMap::iterator iter = g_pAnalyse->markers.begin(); iter != g_pAnalyse->markers.end(); iter++)
	{
		int index = i*4;
		ARToolKitPlus::ARMarkerInfo info = g_pAnalyse->getDetectedMarker(i++);
		
		quad[index].X = TRSF( X, info.vertex[0][0]) ;
		quad[index].Y = TRSF( Y, (m_height - info.vertex[0][1]));
		quad[index].Z = 0.0;
		quad[index].RHW = 1.0f;
		quad[index++].COLOR = color;                
		quad[index].X = TRSF( X, info.vertex[1][0]);
		quad[index].Y = TRSF( Y, (m_height - info.vertex[1][1]));
		quad[index].Z = 0;
		quad[index].RHW = 1.0f;
		quad[index++].COLOR = color;
		quad[index].X = TRSF( X, info.vertex[2][0]);
		quad[index].Y = TRSF( Y, (m_height - info.vertex[2][1]));
		quad[index].Z = 0;
		quad[index].RHW = 1.0f;
		quad[index++].COLOR = color;
		quad[index].X = TRSF( X, info.vertex[3][0]);
		quad[index].Y = TRSF( Y, (m_height - info.vertex[3][1]));
		quad[index].Z = 0;
		quad[index].RHW = 1.0f;
		quad[index].COLOR = color;
	}
	int j = 0;
	for ( int i = 0; i < count; i++ )
	{
		int index = i*4;
		indexes[j++] = index+0;
		indexes[j++] = index+2;
		indexes[j++] = index+1;
		indexes[j++] = index+3;
		indexes[j++] = index+2;
		indexes[j++] = index+0;
	}
	//if ( !m_pVertexBuffer )
	//{
	//	// dim the vertex buffer to containt maximun markers
	//hr = m_pDevice->CreateVertexBuffer(4*4096 *sizeof(CustomVertex), D3DUSAGE_WRITEONLY, CUSTOMFVF, D3DPOOL_DEFAULT, &m_pVertexBuffer, NULL);
	//if(FAILED(hr)) goto done;               
	//} 
	//if ( !m_pIndexBuffer )
	//{
	//	// dim the index buffer to containt maximun markers
	//hr = m_pDevice->CreateIndexBuffer(6*4096*sizeof(short), D3DUSAGE_WRITEONLY,  D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_pIndexBuffer, NULL);
	//if(FAILED(hr)) goto done;               
	//} 
	//// copy vertex buffer
	//VOID* pVoid;        
	//hr = m_pVertexBuffer->Lock(0, 0, (void**)&pVoid, 0);  
	//if(FAILED(hr)) goto done;                
	//
	//memcpy(pVoid, quad,4*count*sizeof(CustomVertex));        
	//m_pVertexBuffer->Unlock();

	//// copy index buffer
	//hr = m_pIndexBuffer->Lock(0, 0, (void**)&pVoid, 0);  
	//if(FAILED(hr)) goto done;                
	//
	//memcpy(pVoid, indexes, 6*count*sizeof(short));        
	//m_pIndexBuffer->Unlock();
	//memset( (void*) &world_matrix , 0, sizeof( world_matrix ));
	////D3DMatrixIdentity( &world_matrix );
	//world_matrix._11 = scaleX;
	//world_matrix._22 = scaleY;
	//world_matrix._33 = 1.0;  //scale Z
	//world_matrix._41 = translateX;
	//world_matrix._42 = translateY;
	//world_matrix._43 = 0.0; // translate Z
	//world_matrix._44 = 1.0;

	//m_pDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
	//m_pDevice->SetRenderState(D3DRS_LIGHTING, true);
	//m_pDevice->LightEnable( 0, TRUE );

	m_pDevice->BeginScene();
	//m_pDevice->SetTransform(D3DTS_WORLD, &world_matrix);
	//m_pDevice->SetTransform(D3DTS_VIEW, &view_matrix);
	//m_pDevice->SetTransform(D3DTS_PROJECTION, &projection_matrix);
	m_pDevice->SetFVF(CUSTOMFVF);        
	//m_pDevice->SetIndices(m_pIndexBuffer);
	//m_pDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(CustomVertex));        
	//m_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4*count, 0, 2*count);
	//m_pDevice->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 4*count, 0, 2*count);
	m_pDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, 4*count, 2*count, indexes, D3DFMT_INDEX16,quad,sizeof(CustomVertex));
	m_pDevice->EndScene();
	
	//m_pDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);

done:
	delete quad;
	delete indexes;
   return hr;
}

//-------------------------------------------------------------------
//
// Conversion functions
//
//-------------------------------------------------------------------

__forceinline BYTE Clip(int clr)
{
    return (BYTE)(clr < 0 ? 0 : ( clr > 255 ? 255 : clr ));
}

__forceinline RGBQUAD ConvertYCrCbToRGB(
    int y,
    int cr,
    int cb
    )
{
    RGBQUAD rgbq;

    int c = y - 16;
    int d = cb - 128;
    int e = cr - 128;

    rgbq.rgbRed =   Clip(( 298 * c           + 409 * e + 128) >> 8);
    rgbq.rgbGreen = Clip(( 298 * c - 100 * d - 208 * e + 128) >> 8);
    rgbq.rgbBlue =  Clip(( 298 * c + 516 * d           + 128) >> 8);

    return rgbq;
}


//-------------------------------------------------------------------
// TransformImage_RGB24
//
// RGB-24 to RGB-32
//-------------------------------------------------------------------

void TransformImage_RGB24(
    BYTE*       pDest,
    LONG        lDestStride,
    const BYTE* pSrc,
    LONG        lSrcStride,
    DWORD       dwWidthInPixels,
    DWORD       dwHeightInPixels
    )
{
    for (DWORD y = 0; y < dwHeightInPixels; y++)
    {
        RGBTRIPLE *pSrcPel = (RGBTRIPLE*)pSrc;
        DWORD *pDestPel = (DWORD*)pDest;

        for (DWORD x = 0; x < dwWidthInPixels; x++)
        {
#ifdef DX9
            pDestPel[x] = D3DCOLOR_XRGB(
                pSrcPel[x].rgbtRed,
                pSrcPel[x].rgbtGreen,
                pSrcPel[x].rgbtBlue
				);
#else
			   pDestPel[x] = pSrcPel[x].rgbtRed;
#endif
        }

        pSrc += lSrcStride;
        pDest += lDestStride;
    }
}

//-------------------------------------------------------------------
// TransformImage_RGB32
//
// RGB-32 to RGB-32
//
// Note: This function is needed to copy the image from system
// memory to the Direct3D surface.
//-------------------------------------------------------------------

void TransformImage_RGB32(
    BYTE*       pDest,
    LONG        lDestStride,
    const BYTE* pSrc,
    LONG        lSrcStride,
    DWORD       dwWidthInPixels,
    DWORD       dwHeightInPixels
    )
{
    MFCopyImage(pDest, lDestStride, pSrc, lSrcStride, dwWidthInPixels * 4, dwHeightInPixels);
}

//-------------------------------------------------------------------
// TransformImage_YUY2
//
// YUY2 to RGB-32
//-------------------------------------------------------------------

void TransformImage_YUY2(
    BYTE*       pDest,
    LONG        lDestStride,
    const BYTE* pSrc,
    LONG        lSrcStride,
    DWORD       dwWidthInPixels,
    DWORD       dwHeightInPixels
    )
{
    for (DWORD y = 0; y < dwHeightInPixels; y++)
    {
        RGBQUAD *pDestPel = (RGBQUAD*)pDest;
        WORD    *pSrcPel = (WORD*)pSrc;

        for (DWORD x = 0; x < dwWidthInPixels; x += 2)
        {
            // Byte order is U0 Y0 V0 Y1

            int y0 = (int)LOBYTE(pSrcPel[x]);
            int u0 = (int)HIBYTE(pSrcPel[x]);
            int y1 = (int)LOBYTE(pSrcPel[x + 1]);
            int v0 = (int)HIBYTE(pSrcPel[x + 1]);

            pDestPel[x] = ConvertYCrCbToRGB(y0, v0, u0);
            pDestPel[x + 1] = ConvertYCrCbToRGB(y1, v0, u0);
        }

        pSrc += lSrcStride;
        pDest += lDestStride;
    }

}


//-------------------------------------------------------------------
// TransformImage_NV12
//
// NV12 to RGB-32
//-------------------------------------------------------------------

void TransformImage_NV12(
    BYTE* pDst,
    LONG dstStride,
    const BYTE* pSrc,
    LONG srcStride,
    DWORD dwWidthInPixels,
    DWORD dwHeightInPixels
    )
{
    const BYTE* lpBitsY = pSrc;
    const BYTE* lpBitsCb = lpBitsY  + (dwHeightInPixels * srcStride);;
    const BYTE* lpBitsCr = lpBitsCb + 1;

    for (UINT y = 0; y < dwHeightInPixels; y += 2)
    {
        const BYTE* lpLineY1 = lpBitsY;
        const BYTE* lpLineY2 = lpBitsY + srcStride;
        const BYTE* lpLineCr = lpBitsCr;
        const BYTE* lpLineCb = lpBitsCb;

        LPBYTE lpDibLine1 = pDst;
        LPBYTE lpDibLine2 = pDst + dstStride;

        for (UINT x = 0; x < dwWidthInPixels; x += 2)
        {
            int  y0 = (int)lpLineY1[0];
            int  y1 = (int)lpLineY1[1];
            int  y2 = (int)lpLineY2[0];
            int  y3 = (int)lpLineY2[1];
            int  cb = (int)lpLineCb[0];
            int  cr = (int)lpLineCr[0];

            RGBQUAD r = ConvertYCrCbToRGB(y0, cr, cb);
            lpDibLine1[0] = r.rgbBlue;
            lpDibLine1[1] = r.rgbGreen;
            lpDibLine1[2] = r.rgbRed;
            lpDibLine1[3] = 0; // Alpha

            r = ConvertYCrCbToRGB(y1, cr, cb);
            lpDibLine1[4] = r.rgbBlue;
            lpDibLine1[5] = r.rgbGreen;
            lpDibLine1[6] = r.rgbRed;
            lpDibLine1[7] = 0; // Alpha

            r = ConvertYCrCbToRGB(y2, cr, cb);
            lpDibLine2[0] = r.rgbBlue;
            lpDibLine2[1] = r.rgbGreen;
            lpDibLine2[2] = r.rgbRed;
            lpDibLine2[3] = 0; // Alpha

            r = ConvertYCrCbToRGB(y3, cr, cb);
            lpDibLine2[4] = r.rgbBlue;
            lpDibLine2[5] = r.rgbGreen;
            lpDibLine2[6] = r.rgbRed;
            lpDibLine2[7] = 0; // Alpha

            lpLineY1 += 2;
            lpLineY2 += 2;
            lpLineCr += 2;
            lpLineCb += 2;

            lpDibLine1 += 8;
            lpDibLine2 += 8;
        }

        pDst += (2 * dstStride);
        lpBitsY   += (2 * srcStride);
        lpBitsCr  += srcStride;
        lpBitsCb  += srcStride;
    }
}


//-------------------------------------------------------------------
// LetterBoxDstRect
//
// Takes a src rectangle and constructs the largest possible
// destination rectangle within the specifed destination rectangle
// such thatthe video maintains its current shape.
//
// This function assumes that pels are the same shape within both the
// source and destination rectangles.
//
//-------------------------------------------------------------------

RECT    LetterBoxRect(const RECT& rcSrc, const RECT& rcDst)
{
    // figure out src/dest scale ratios
    int iSrcWidth  = Width(rcSrc);
    int iSrcHeight = Height(rcSrc);

    int iDstWidth  = Width(rcDst);
    int iDstHeight = Height(rcDst);

    int iDstLBWidth;
    int iDstLBHeight;

    if (MulDiv(iSrcWidth, iDstHeight, iSrcHeight) <= iDstWidth) {

        // Column letter boxing ("pillar box")

        iDstLBWidth  = MulDiv(iDstHeight, iSrcWidth, iSrcHeight);
        iDstLBHeight = iDstHeight;
    }
    else {

        // Row letter boxing.

        iDstLBWidth  = iDstWidth;
        iDstLBHeight = MulDiv(iDstWidth, iSrcHeight, iSrcWidth);
    }


    // Create a centered rectangle within the current destination rect

    RECT rc;

    LONG left = rcDst.left + ((iDstWidth - iDstLBWidth) / 2);
    LONG top = rcDst.top + ((iDstHeight - iDstLBHeight) / 2);

    SetRect(&rc, left, top, left + iDstLBWidth, top + iDstLBHeight);

    return rc;
}


//-----------------------------------------------------------------------------
// CorrectAspectRatio
//
// Converts a rectangle from the source's pixel aspect ratio (PAR) to 1:1 PAR.
// Returns the corrected rectangle.
//
// For example, a 720 x 486 rect with a PAR of 9:10, when converted to 1x1 PAR,
// is stretched to 720 x 540.
//-----------------------------------------------------------------------------

RECT CorrectAspectRatio(const RECT& src, const MFRatio& srcPAR)
{
    // Start with a rectangle the same size as src, but offset to the origin (0,0).
    RECT rc = {0, 0, src.right - src.left, src.bottom - src.top};

    if ((srcPAR.Numerator != 1) || (srcPAR.Denominator != 1))
    {
        // Correct for the source's PAR.

        if (srcPAR.Numerator > srcPAR.Denominator)
        {
            // The source has "wide" pixels, so stretch the width.
            rc.right = MulDiv(rc.right, srcPAR.Numerator, srcPAR.Denominator);
        }
        else if (srcPAR.Numerator < srcPAR.Denominator)
        {
            // The source has "tall" pixels, so stretch the height.
            rc.bottom = MulDiv(rc.bottom, srcPAR.Denominator, srcPAR.Numerator);
        }
        // else: PAR is 1:1, which is a no-op.
    }
    return rc;
}


//-----------------------------------------------------------------------------
// GetDefaultStride
//
// Gets the default stride for a video frame, assuming no extra padding bytes.
//
//-----------------------------------------------------------------------------

HRESULT GetDefaultStride(IMFMediaType *pType, LONG *plStride)
{
    LONG lStride = 0;

    // Try to get the default stride from the media type.
    HRESULT hr = pType->GetUINT32(MF_MT_DEFAULT_STRIDE, (UINT32*)&lStride);
    if (FAILED(hr))
    {
        // Attribute not set. Try to calculate the default stride.
        GUID subtype = GUID_NULL;

        UINT32 width = 0;
        UINT32 height = 0;

        // Get the subtype and the image size.
        hr = pType->GetGUID(MF_MT_SUBTYPE, &subtype);
        if (SUCCEEDED(hr))
        {
            hr = MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &width, &height);
        }
        if (SUCCEEDED(hr))
        {
            hr = MFGetStrideForBitmapInfoHeader(subtype.Data1, width, &lStride);
        }

        // Set the attribute for later reference.
        if (SUCCEEDED(hr))
        {
            (void)pType->SetUINT32(MF_MT_DEFAULT_STRIDE, UINT32(lStride));
        }
    }

    if (SUCCEEDED(hr))
    {
        *plStride = lStride;
    }
    return hr;
}