File: videoDS.cpp

package info (click to toggle)
gem 1%3A0.93.3-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,548 kB
  • ctags: 29,332
  • sloc: cpp: 134,188; sh: 11,215; makefile: 2,853; ansic: 84
file content (1255 lines) | stat: -rw-r--r-- 42,614 bytes parent folder | download | duplicates (3)
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
////////////////////////////////////////////////////////
//
// videoDS - Graphics Environment for Multimedia
//
// daniel@bogusfront.org
// zmoelnig@iem.at
//
// Implementation file 
//
//    Copyright (c) 2003 Daniel Heckenberg.
//    Copyright (c) 2010-2011 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "videoDS.h"
using namespace gem::plugins;
#include "Gem/RTE.h"
#include "plugins/PluginFactory.h"

#ifdef HAVE_DIRECTSHOW
REGISTER_VIDEOFACTORY("DS", videoDS);

# include <memory>
# include <Dvdmedia.h>
# include <streams.h>
# include <atlbase.h>
# include "DSgrabber.h"

# include <strsafe.h>
# include <comdef.h>

#define COMRELEASE(x) { if (x) x->Release(); x = NULL; }
//#define REGISTER_FILTERGRAPH 1

// Utility functions
void SetupCaptureDevice(ICaptureGraphBuilder2* pCG, IBaseFilter * pCDbase);
void dialogSource      (ICaptureGraphBuilder2* pCG, IBaseFilter * pCDbase);
void dialogFormat      (ICaptureGraphBuilder2* pCG, IBaseFilter * pCDbase);
void dialogDisplay     (ICaptureGraphBuilder2* pCG, IBaseFilter * pCDbase);
void dialogCrossbar    (ICaptureGraphBuilder2* pCG, IBaseFilter * pCDbase);

HRESULT FindCaptureDevice(int device, IBaseFilter ** ppSrcFilter);
HRESULT ConnectFilters(IGraphBuilder *pGraph, IBaseFilter *pFirst, IBaseFilter *pSecond);
void GetBitmapInfoHdr(AM_MEDIA_TYPE* pmt, BITMAPINFOHEADER** ppbmih);
HRESULT GetPin(IBaseFilter *, PIN_DIRECTION, IPin **);
HRESULT AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister);
HRESULT SetAviOptions(IBaseFilter *ppf, InterleavingMode INTERLEAVE_MODE);
void RemoveGraphFromRot(DWORD pdwRegister);

/////////////////////////////////////////////////////////
//
// videoDS
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////
videoDS :: videoDS(void)
  : videoBase("directshow", 0),
m_readIdx (0), m_lastreadIdx (0),
m_writeIdx(0), m_lastwriteIdx(0),
m_format(GL_BGR_EXT),
#ifdef USE_RECORDING
m_recording(false),
#endif

m_pGB(NULL),
m_pMC(NULL),
m_pME(NULL),
m_pMF(NULL),
m_pMS(NULL),
m_pMP(NULL),
SampleFilter(NULL),
NullFilter(NULL),
FileFilter(NULL),
SampleGrabber(NULL),
#ifdef DIRECTSHOW_LOGGING
LogFileHandle(NULL),
#endif
m_pCDbase(NULL),
m_pCG(NULL),
m_GraphRegister(0)
{
    // Initialize COM
    if(FAILED(CoInitialize(NULL))) {
        throw("could not initialise COM.");
    }

    m_width=720;
    m_height=576;

    // Initialize the input buffers
    for (int i = 0; i <= 2; i++) {
        m_pixBlockBuf[i].image.xsize=m_width;
        m_pixBlockBuf[i].image.ysize=m_height;
        m_pixBlockBuf[i].image.setCsizeByFormat(GL_RGBA);
        m_pixBlockBuf[i].image.reallocate();

        m_pixBlockBuf[i].newimage = 0;
        m_nPixDataSize[i] = 
            m_pixBlockBuf[i].image.xsize*
            m_pixBlockBuf[i].image.ysize*
            m_pixBlockBuf[i].image.csize;
    }

    m_image.image.xsize=m_width;
    m_image.image.ysize=m_height;
    m_image.image.setCsizeByFormat(GL_RGBA);
    m_image.image.reallocate();

#ifdef USE_RECORDING
    memset(m_filename, 0, MAXPDSTRING);
#endif

    provide("dv");
    provide("iidc");
    provide("analog");
}

////////////////////////////////////////////////////////
// Destructor
//
/////////////////////////////////////////////////////////
videoDS :: ~videoDS(void)
{
    // Clean up the movie
    close();

    for (int i = 0; i <= 2; i++) {
        m_pixBlockBuf[i].image.allocate(0);
        m_pixBlockBuf[i].newimage = 0;
        m_nPixDataSize[i] = 0;
    }

    // Finished with COM
    CoUninitialize();
}

/////////////////////////////////////////////////////////
// open message
//
/////////////////////////////////////////////////////////
bool videoDS :: openDevice(gem::Properties&props)
{
    HRESULT			hr;
    AM_MEDIA_TYPE	MediaType;
    int device = m_devicenum;

    do  {
        // Get the interface for DirectShow's GraphBuilder
        if (FAILED(hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&m_pGB)))  {
            error("Could not get DShow GraphBuilder, hr 0x%X", hr);
            break;
        }

#ifdef DIRECTSHOW_LOGGING
        OFSTRUCT	OFStruct;

        LogFileHandle	= OpenFile("DirectShow.log", &OFStruct, OF_CREATE);

        if (LogFileHandle != NULL) {
            m_pGB->SetLogFile(LogFileHandle);
        }
#endif

        // Get the interface for DirectShow's CaptureGraphBuilder2 which allows the use of capture devices instead of file sources
        if (	FAILED(hr = (CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void **)&m_pCG)))
            ||	FAILED(hr = m_pCG->SetFiltergraph(m_pGB))){
                error("Could not get DShow GraphBuilder, hr 0x%X", hr);
                break;
            }

            // Create the capture device.
            if (FAILED(hr = FindCaptureDevice(device, &m_pCDbase))) {
                error("Could not open device: %d\n", device);
                break;
            }

            // Create the SampleGrabber filter
            hr	= CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
                IID_IBaseFilter, (void**)&SampleFilter);

            if (hr != S_OK || NULL == SampleFilter) {
                error("Unable to create SampleFilter interface %d", hr);
                return false;
            }

            // Query for the SampleGrabber interface in the SampleGrabber filter
            // Needed to grab the buffer using GetCurrentBuffer()
            hr	= SampleFilter->QueryInterface(IID_ISampleGrabber, (void **)&SampleGrabber);

            if (hr != S_OK || NULL == SampleGrabber) {
                error("Unable to create SampleGrabber interface %d", hr);
                return false;
            }

            // Set the media type that the SampleGrabber wants.
            // MEDIATYPE_Video selects only video and not interleaved audio and video
            // MEDIASUBTYPE_RGB24 is the colorspace and format to deliver frames
            // MediaType.formattype is GUID_NULL since it is handled later to get file info
            memset(&MediaType, 0, sizeof(AM_MEDIA_TYPE));
            MediaType.majortype		= MEDIATYPE_Video;
            MediaType.subtype		= MEDIASUBTYPE_RGB24;
            MediaType.formattype	= GUID_NULL;
            hr				= SampleGrabber->SetMediaType(&MediaType);

            // Set the SampleGrabber to return continuous frames
            hr	= SampleGrabber->SetOneShot(FALSE);

            if (hr != S_OK) {
                error("Unable to setup sample grabber %d", hr);
                return false;
            }

            // Set the SampleGrabber to copy the data to a buffer. This only set to FALSE when a 
            // callback is used.
            hr	= SampleGrabber->SetBufferSamples(TRUE);

            if (hr != S_OK) {
                error("Unable to setup sample grabber %d", hr);
                return false;
            }

            // Create the Null Renderer
            hr	= CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER,
                IID_IBaseFilter, (void**)&NullFilter);

            if (hr != S_OK || NULL == NullFilter) {
                error("Unable to create NullFilter interface %d", hr);
                return false;
            }

            // add the filters to the graph
            if (FAILED(hr = m_pGB->AddFilter(m_pCDbase, L"Capture Device")) || 
                FAILED(hr = m_pGB->AddFilter(SampleFilter, L"Sample Grabber")) ||
                FAILED(hr = m_pGB->AddFilter(NullFilter, L"Null Renderer"))) {
                    error("Could not add the filters to the graph, hr 0x%X", hr);
                    break;
                }

                // Automatically connect the Device filter to the NullFilter through the SampleFilter.
                // Additional filters may be added.
                // Try Interleaved Audio and Video first for DV input
                if (FAILED(hr = m_pCG->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Interleaved, 
                    m_pCDbase, SampleFilter, NullFilter))) {
                        //try Video only for devices with no audio
                        if (FAILED(hr = m_pCG->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, 
                            m_pCDbase, SampleFilter, NullFilter))) {
                                error("Unable to connect to SampleGrabber.");
                                return false;
                            }
                    }

                    // QueryInterface for DirectShow interfaces
                    if (FAILED(hr = (m_pGB->QueryInterface(IID_IMediaFilter, (void **)&m_pMF)))) {
                        error("Could not get media filter interface, hr 0x%X", hr);
                        break;
                    }

                    //MediaControl is used for Run, Stop, Pause and running state queries
                    if (FAILED(hr = (m_pGB->QueryInterface(IID_IMediaControl, (void **)&m_pMC)))) {
                        error("Could not get media control interface, hr 0x%X", hr);
                        break;
                    }

                    //not used right now
                    if (FAILED(hr = (m_pGB->QueryInterface(IID_IMediaEvent, (void **)&m_pME))))	{
                        error("Could not get media event interface, hr 0x%X", hr);
                        break;
                    }

                    //MediaSeeking for the end of a clip.  not really used here
                    if (FAILED(hr = (m_pGB->QueryInterface(IID_IMediaSeeking, (void **)&m_pMS)))){
                        error("Could not get media seeking interface, hr 0x%X", hr);
                        break;
                    }

                    //for the position of a clip.  not really used for device capture
                    if (FAILED(hr = (m_pGB->QueryInterface(IID_IMediaPosition, (void **)&m_pMP)))){
                        error("Could not get media position interface, hr 0x%X", hr);
                        break;
                    }

                    // Expose the filter graph so we can view it using GraphEdit
#ifdef REGISTER_FILTERGRAPH
                    if (FAILED(hr = AddGraphToRot(m_pGB, &m_GraphRegister))){
                        error("failed to register filter graph with ROT!  hr=0x%X", hr);
                        m_GraphRegister = 0;
                    }
#endif
                    // THIS KILLS FILE WRITING!! May improve latency on video preview/playback.
                    // Turn off the reference clock. 
                    //	if (FAILED(hr = m_pMF->SetSyncSource(NULL)))
                    //	{
                    //		error("failed to turn off the reference clock  hr=0x%X", hr);
                    //		break;
                    //	}

                    // Indicate that the video is ready.
#if USE_RECORDING
                    // JMZ ???
                    stopCapture();
#endif
                    return true;
    } while (0);

    closeDevice();
    return false;
}

////////////////////////////////////////////////////////
// close message
//
/////////////////////////////////////////////////////////
void videoDS :: closeDevice(void)
{
#ifdef DIRECTSHOW_LOGGING
    m_pGB->SetLogFile(NULL);
    CloseHandle((HANDLE)LogFileHandle);
#endif
    // Release the DirectShow interfaces
    COMRELEASE(m_pGB);
    COMRELEASE(m_pMC);
    COMRELEASE(m_pME);
    COMRELEASE(m_pMF);
    COMRELEASE(m_pMS);
    COMRELEASE(m_pMP);
    COMRELEASE(SampleFilter);
    COMRELEASE(NullFilter);
    COMRELEASE(FileFilter);
    COMRELEASE(m_pCDbase);
    COMRELEASE(m_pCG);

#ifdef REGISTER_FILTERGRAPH
    if (m_GraphRegister) {	
        HRESULT hr;
        RemoveGraphFromRot(m_GraphRegister);
        m_GraphRegister = 0;
    }
#endif
}
std::vector<std::string>videoDS :: dialogs(void) {
    std::vector<std::string>result;
    result.push_back("source");
    result.push_back("format");
    result.push_back("display");
    result.push_back("crossbar");
    return result;
}

////////////////////////////////////////////////////////
// enumerate message
//
/////////////////////////////////////////////////////////
std::vector<std::string>videoDS :: enumerate(void)
{
    std::vector<std::string>result;

    HRESULT hr;
    IBaseFilter * pSrc = NULL;

    IMoniker* pMoniker =NULL;
    ULONG cFetched;

    ICreateDevEnum* pDevEnum =NULL;
    IEnumMoniker* pClassEnum = NULL;

    do {
        // Create the system device enumerator
        hr = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
            IID_ICreateDevEnum, (void ** ) &pDevEnum);
        if (FAILED(hr)) {
            error("Couldn't create system enumerator!");
            break;
        }

        // Create an enumerator for the video capture devices
        hr = pDevEnum->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &pClassEnum, 0);
        if (FAILED(hr)) {
            error("Couldn't create class enumerator!");
            break;
        }

        // If there are no enumerators for the requested type, then 
        // CreateClassEnumerator will succeed, but pClassEnum will be NULL.
        if (pClassEnum == NULL) {
            error("No video capture devices found!");
            break;
        }

        // Use the first video capture device on the device list.
        // Note that if the Next() call succeeds but there are no monikers,
        // it will return S_FALSE (which is not a failure).  Therefore, we
        // check that the return code is S_OK instead of using SUCCEEDED() macro.
        int devIndex = 0;
        while (S_OK == (pClassEnum->Next (1, &pMoniker, &cFetched))) {
            IPropertyBag *pPropBag;
            if (SUCCEEDED(hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pPropBag))) {
                // To retrieve the friendly name of the filter, do the following:
                VARIANT varName;
                VariantInit(&varName);
                hr = pPropBag->Read(L"FriendlyName", &varName, 0);
                if (SUCCEEDED(hr)) {
                    std::string s=_bstr_t(varName.bstrVal);
                    result.push_back(s);
                    //post("Dev %d: %S", devIndex, varName.bstrVal);
                }
                VariantClear(&varName);

                COMRELEASE(pPropBag);
            }
            COMRELEASE(pMoniker);
            devIndex++;
        }
    } while (0);

    // Copy the found filter pointer to the output parameter.
    // Do NOT Release() the reference, since it will still be used
    // by the calling function.
    COMRELEASE(pDevEnum);
    COMRELEASE(pClassEnum);

    return result;
}

////////////////////////////////////////////////////////
// render
//
/////////////////////////////////////////////////////////
pixBlock* videoDS :: getFrame(void)
{
    // Copy the buffer from the camera buffer to the texture buffer
    copyBuffer();

    m_readIdx = m_lastwriteIdx;
    if (m_nPixDataSize[m_readIdx]){
        m_image.newimage=m_pixBlockBuf[m_readIdx].newimage;
        m_image.image.xsize=m_pixBlockBuf[m_readIdx].image.xsize;
        m_image.image.ysize=m_pixBlockBuf[m_readIdx].image.ysize;
        switch (m_pixBlockBuf[m_readIdx].image.format){
        case GL_BGR_EXT:
        default:
            m_image.image.fromBGR(m_pixBlockBuf[m_readIdx].image.data);
        }
        m_image.newimage=true;

        return &m_image;
    }
    return NULL;
}

/* copies the image from DS into the current m_pixBlockBuf */
void videoDS :: copyBuffer(void)
{
    HRESULT	hr;
    long	SampleSize;	

    // Get the media type
    AM_MEDIA_TYPE	pmt;
    int readIdx = m_readIdx;

    if ((m_writeIdx = (m_lastwriteIdx + 1) % 3) == readIdx)
        m_writeIdx = (readIdx + 1) % 3;

    // Get the current buffer size from the SampleGrabber.
    if (SampleGrabber != NULL) {
        hr	= SampleGrabber->GetCurrentBuffer(&SampleSize, NULL);

        if (hr != S_OK) return;
    }		

    // Check for a format change.
    if (NULL == SampleGrabber || FAILED(hr = SampleGrabber->GetConnectedMediaType(&pmt))) {
        error("could not get sample media type.");
        close();
        return;
    }

    if (S_OK == hr) {
        BITMAPINFOHEADER* pbmih;
        GetBitmapInfoHdr(&pmt, &pbmih);
        m_width = pbmih->biWidth;
        m_height = pbmih->biHeight;
        m_format = GL_BGR_EXT;
        FreeMediaType(pmt);	// is this necessary?!	
    }

    m_pixBlockBuf[m_writeIdx].image.xsize = m_width;
    m_pixBlockBuf[m_writeIdx].image.ysize = m_height;
    m_pixBlockBuf[m_writeIdx].image.setCsizeByFormat(m_format);
    m_pixBlockBuf[m_writeIdx].image.reallocate();
    m_pixBlockBuf[m_writeIdx].image.reallocate(SampleSize);
    m_nPixDataSize[m_writeIdx] = SampleSize;

    // Get the current buffer from the SampleGrabber.
    if (SampleGrabber != NULL) {
        hr = SampleGrabber->GetCurrentBuffer(&SampleSize, 
            (long *)m_pixBlockBuf[m_readIdx].image.data);
        if (hr != S_OK) return;
        m_pixBlockBuf[m_writeIdx].newimage = 1;
    }		

    m_lastwriteIdx = m_writeIdx;
}


////////////////////////////////////////////////////////
// postrender
//
/////////////////////////////////////////////////////////
void videoDS :: releaseFrame(void)
{
    if (!m_haveVideo || !m_capturing)return;

    m_pixBlockBuf[m_readIdx].newimage = 0;
    m_lastreadIdx = m_readIdx;
}

////////////////////////////////////////////////////////
// startTransfer
//
/////////////////////////////////////////////////////////
bool videoDS :: startTransfer(void)
{
    HRESULT hr;

    m_readIdx = 0;
    m_lastreadIdx = 0;
    m_writeIdx = 1;
    m_lastwriteIdx = 0;

    // Get the stream characteristics
    AM_MEDIA_TYPE mt;
    BITMAPINFOHEADER* pbmih;
    if (NULL == SampleGrabber || FAILED(hr = SampleGrabber->GetConnectedMediaType(&mt))) {
        error("Could not get connect media type, hr 0x%X", hr);
        return false;
    }
    GetBitmapInfoHdr(&mt, &pbmih);
    m_width = pbmih->biWidth;
    m_height = pbmih->biHeight;
    m_format = GL_BGR_EXT;

    //starts the graph rendering
    if (FAILED(hr = m_pMC->Run())) {
        error("Could not start graph playback, hr 0x%X", hr);
        return false;
    }

    return true;
}

/////////////////////////////////////////////////////////
// stopTransfer
//
/////////////////////////////////////////////////////////
bool videoDS :: stopTransfer(void)
{
    HRESULT hr;
    if (FAILED(hr = m_pMC->Stop())) {
        error("Could not stop graph playback, hr 0x%X", hr);
        return false;
    }
    return true;
}

////////////////////////////////////////////////////////
// dvMess
//
/////////////////////////////////////////////////////////
bool videoDS :: setDimen(int x, int y, int leftmargin, int rightmargin,
                         int topmargin, int bottommargin)
{
    /*
    norm                       NTSC        PAL
    DVRESOLUTION_FULL	    720 x 480	720 x 576
    DVRESOLUTION_HALF	    360 x 240	360 x 288
    DVRESOLUTION_QUARTER    180 x 120	180 x 144
    DVRESOLUTION_DC	     88 x 60     88 x 72
    */

    // 5*yPAL = 6*yNTSC

    bool pal=true; // how to know this?

    float y_fac = pal?1.:(5./6.);
    int resolution=DVRESOLUTION_FULL;
    if     ((x<= 88) && (y<=( 72*y_fac)))resolution=DVRESOLUTION_DC;
    else if((x<=180) && (y<=(144*y_fac)))resolution=DVRESOLUTION_QUARTER;
    else if((x<=360) && (y<=(288*y_fac)))resolution=DVRESOLUTION_HALF;
    else                                 resolution=DVRESOLUTION_FULL;

    HRESULT hr = S_OK;

    if(stop()) {
        IIPDVDec	*pDV=NULL;
        if (SUCCEEDED(hr = (m_pCG->FindInterface(NULL, NULL, m_pCDbase, IID_IIPDVDec, (void **)&pDV)))) {
            hr = pDV->put_IPDisplay(resolution);

            if (FAILED(hr)) {
                error("Could not set decoder resolution.");
            }
        }
        if(pDV)
            pDV->Release();

        start();      
    }
return true;
}

////////////////////////////////////////////////////////
// colorspace message
//
/////////////////////////////////////////////////////////
bool videoDS :: setColor(int format)
{
    if(format)m_image.image.setCsizeByFormat(format);
    return true;
}

////////////////////////////////////////////////////
// dialogMess
//
/////////////////////////////////////////////////////////
bool videoDS :: dialog(std::vector<std::string>dlg)
{
    HRESULT hr;  
    if (!m_haveVideo) return true;

    bool running=stop();

    if(dlg.empty()) {
        SetupCaptureDevice(m_pCG, m_pCDbase);
    } else {
        int i;
        for(i=0; i<dlg.size(); i++) {
            std::string type=dlg[i];

            if(type=="source")
                dialogSource(m_pCG, m_pCDbase);
            else if(type=="format")
                dialogFormat(m_pCG, m_pCDbase);
            else if(type=="display")
                dialogDisplay(m_pCG, m_pCDbase);
            else if(type=="crossbar")
                dialogCrossbar(m_pCG, m_pCDbase);
            else
                error ("dialog not known");
        }
    }

    if(running)start();
    return true;
}



#ifdef USE_RECORDING
void videoDS :: startCapture(void)
{
    HRESULT	hr;
    WCHAR	WideFileName[MAXPDSTRING];
    if (FALSE == m_recording && m_pCG != NULL && m_haveVideo) {
        // Convert filename to wide chars
        memset(&WideFileName, 0, MAXPDSTRING * 2);
        if (0 == MultiByteToWideChar(CP_ACP, 0, m_filename, strlen(m_filename), WideFileName, 
            MAXPDSTRING)) {
                error("Unable to capture to %s", m_filename);
                return;
            }
            // Set filename of output AVI. Returns pointer to a File Writer filter.
            if (FAILED(hr = m_pCG->SetOutputFileName(&MEDIASUBTYPE_Avi, WideFileName, 
                &FileFilter, NULL))) {
                    error("Unable to set output filename.");
                    return;
                }
                // Set AVI output option for interleaving.
                if (FAILED(hr = SetAviOptions(FileFilter, INTERLEAVE_NONE))) {
                    error("Unable to set avi options.");
                    return;
                }
                // Connect the Capture Device filter to the File Writer filter. Try using 
                //	MEDIATYPE_Interleaved first, else default to MEDIATYPE_Video.
                if (FAILED(hr = m_pCG->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Interleaved,  
                    m_pCDbase, NULL, FileFilter))) {
                        if (FAILED(hr = m_pCG->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, 
                            m_pCDbase, NULL, FileFilter))) {
                                error("Unable to record to avi.");
                                return;
                            }
                    }
                    m_recording	= true;
    }
}
void videoDS :: stopCapture(void)
{
    HRESULT	RetVal;

    if (m_recording && m_haveVideo) {
        IBaseFilter	*Filter;

        // Remove the File Writer filter, if available.
        //This is probably where DS releases the written AVI file
        RetVal	= m_pGB->FindFilterByName(L"File Writer", &Filter);

        if (S_OK == RetVal) {
            m_pGB->RemoveFilter(Filter);
        }

        // Remove the AVI Mux filter, if available.
        RetVal	= m_pGB->FindFilterByName(L"Mux", &Filter);

        if (S_OK == RetVal) {
            m_pGB->RemoveFilter(Filter);
        }

        m_recording	= false;
    }
}
void videoDS :: fileMess(t_symbol *filename)
{
    _snprintf(m_filename, MAXPDSTRING, "%s", findFile(filename->s_name).c_str());
}
void videoDS :: recordMess(int state)
{
    if (NULL==m_filename || 0 == m_filename[0]) {
        error("No filename passed");
        return;
    }

    stopTransfer();
    if (state) 
        startCapture();
    else
        stopCapture();
    startTransfer();
}
#endif /* recording */












#if 0
"device" -> openMess
"open"   -> openMess
"close"  -> closeMess
"dv"     -> dvMess
"record" -> recordMess
"file"   -> fileMess
float    -> captureOnOff
#endif


// From Microsoft sample:
//
// Let's talk about UI for a minute.  There are many programmatic interfaces
// you can use to program a capture filter or related filter to capture the
// way you want it to.... eg:  IAMStreamConfig, IAMVideoCompression,
// IAMCrossbar, IAMTVTuner, IAMTVAudio, IAMAnalogVideoDecoder, IAMCameraControl,
// IAMVideoProcAmp, etc.
//
// But you probably want some UI to let the user play with all these settings.
// For new WDM-style capture devices, we offer some default UI you can use.
// The code below shows how to bring up all of the dialog boxes supported 
// by any capture filters.
//
// The following code shows you how you can bring up all of the
// dialogs supported by a particular object at once on a big page with lots
// of thumb tabs.  You do this by starting with an interface on the object that
// you want, and using ISpecifyPropertyPages to get the whole list, and
// OleCreatePropertyFrame to bring them all up.  This way you will get custom
// property pages a filter has, too, that are not one of the standard pages that
// you know about.  There are at least 9 objects that may have property pages.
// Your app already has 2 of the object pointers, the video capture filter and
// the audio capture filter (let's call them pVCap and pACap)
// 1.  The video capture filter - pVCap
// 2.  The video capture filter's capture pin - get this by calling
//     FindInterface(&PIN_CATEGORY_CAPTURE, pVCap, IID_IPin, &pX);
// 3.  The video capture filter's preview pin - get this by calling
//     FindInterface(&PIN_CATEGORY_PREVIEW, pVCap, IID_IPin, &pX);
// 4.  The audio capture filter - pACap
// 5.  The audio capture filter's capture pin - get this by calling
//     FindInterface(&PIN_CATEGORY_CAPTURE, pACap, IID_IPin, &pX);
// 6.  The crossbar connected to the video capture filter - get this by calling
//     FindInterface(NULL, pVCap, IID_IAMCrossbar, &pX);
// 7.  There is a possible second crossbar to control audio - get this by 
//     looking upstream of the first crossbar like this:
//     FindInterface(&LOOK_UPSTREAM_ONLY, pX, IID_IAMCrossbar, &pX2);
// 8.  The TV Tuner connected to the video capture filter - get this by calling
//     FindInterface(NULL, pVCap, IID_IAMTVTuner, &pX);
// 9.  The TV Audio connected to the audio capture filter - get this by calling
//     FindInterface(NULL, pACap, IID_IAMTVAudio, &pX);
// 10. We have a helper class, CCrossbar, which makes the crossbar issue less
//     confusing.  In fact, although not supported here, there may be more than
//     two crossbars, arranged in many different ways.  An application may not
//     wish to have separate dialogs for each crossbar, but instead hide the
//     complexity and simply offer the user a list of inputs that can be chosen.
//     This list represents all the unique inputs from all the crossbars.
//     The crossbar helper class does this and offers that list as #10.  It is
//     expected that an application will either provide the crossbar dialogs
//     above (#6 and #7) OR provide the input list (this #10), but not both.
//     That would be confusing because if you select an input using dialog 6 or
//     7 the input list here in #10 won't know about your choice.
//
// Your last choice for UI is to make your own pages, and use the results of 
// your custom page to call the interfaces programmatically.


void SetupCaptureDevice(ICaptureGraphBuilder2* pCG, IBaseFilter * pCDbase) 
{
    HRESULT hr;

    // Check for old style VFW dialogs
    IAMVfwCaptureDialogs *pDlg;
    if (SUCCEEDED(hr = (pCG->FindInterface(NULL, NULL, pCDbase, IID_IAMVfwCaptureDialogs, (void **)&pDlg)))) {
        if (S_OK == (pDlg->HasDialog(VfwCaptureDialog_Source)))
            if (FAILED(hr = pDlg->ShowDialog(VfwCaptureDialog_Source, NULL)))
                error("Could not show VFW Capture Source Dialog");
        if (S_OK == (pDlg->HasDialog(VfwCaptureDialog_Format)))
            if (FAILED(hr = pDlg->ShowDialog(VfwCaptureDialog_Format, NULL)))
                error("Could not show VFW Capture Format Dialog");
        if (S_OK == (pDlg->HasDialog(VfwCaptureDialog_Display)))
            if (FAILED(hr = pDlg->ShowDialog(VfwCaptureDialog_Display, NULL)))
                error("Could not show VFW Capture Display Dialog");
        pDlg->Release();
    }

    ISpecifyPropertyPages *pSpec;
    CAUUID cauuid;
    // 1. the video capture filter itself
    hr = pCDbase->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pSpec);
    if(hr == S_OK) {
        hr = pSpec->GetPages(&cauuid);
        if (hr == S_OK && cauuid.cElems > 0) {
            hr = OleCreatePropertyFrame(NULL, 30, 30, NULL, 1,
                (IUnknown **)&pCDbase, cauuid.cElems,
                (GUID *)cauuid.pElems, 0, 0, NULL);
            CoTaskMemFree(cauuid.pElems);
        }
        pSpec->Release();
    }


    // 2.  The video capture capture pin
    IAMStreamConfig *pSC;
    hr = pCG->FindInterface(&PIN_CATEGORY_CAPTURE,
        &MEDIATYPE_Interleaved,
        pCDbase, IID_IAMStreamConfig, (void **)&pSC);
    if(hr != S_OK)
        hr = pCG->FindInterface(&PIN_CATEGORY_CAPTURE,
        &MEDIATYPE_Video, pCDbase,
        IID_IAMStreamConfig, (void **)&pSC);

    if(hr == S_OK) {
        hr = pSC->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pSpec);
        if(hr == S_OK) {
            hr = pSpec->GetPages(&cauuid);
            if(hr == S_OK && cauuid.cElems > 0) {
                hr = OleCreatePropertyFrame(NULL, 30, 30, NULL, 1,
                    (IUnknown **)&pSC, cauuid.cElems,
                    (GUID *)cauuid.pElems, 0, 0, NULL);
                CoTaskMemFree(cauuid.pElems);
            }
            pSpec->Release();
        }
        pSC->Release();
    }

    // 4 & 5.  The video crossbar, and a possible second crossbar
    IAMCrossbar *pX, *pX2;
    IBaseFilter *pXF;
    hr = pCG->FindInterface(&PIN_CATEGORY_CAPTURE,
        &MEDIATYPE_Interleaved, pCDbase,
        IID_IAMCrossbar, (void **)&pX);
    if(hr != S_OK)
        hr = pCG->FindInterface(&PIN_CATEGORY_CAPTURE,
        &MEDIATYPE_Video, pCDbase,
        IID_IAMCrossbar, (void **)&pX);

    if(hr == S_OK) {
        hr = pX->QueryInterface(IID_IBaseFilter, (void **)&pXF);
        if(hr == S_OK) {
            hr = pX->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pSpec);
            if(hr == S_OK) {
                hr = pSpec->GetPages(&cauuid);
                if(hr == S_OK && cauuid.cElems > 0) {
                    hr = OleCreatePropertyFrame(NULL, 30, 30, NULL, 1,
                        (IUnknown **)&pX, cauuid.cElems,
                        (GUID *)cauuid.pElems, 0, 0, NULL);
                    CoTaskMemFree(cauuid.pElems);
                }
                pSpec->Release();
            }
            hr = pCG->FindInterface(&LOOK_UPSTREAM_ONLY, NULL, pXF,
                IID_IAMCrossbar, (void **)&pX2);
            if(hr == S_OK) {
                hr = pX2->QueryInterface(IID_ISpecifyPropertyPages,
                    (void **)&pSpec);
                if(hr == S_OK) {
                    hr = pSpec->GetPages(&cauuid);
                    if(hr == S_OK && cauuid.cElems > 0) {
                        hr = OleCreatePropertyFrame(NULL, 30, 30, NULL, 1,
                            (IUnknown **)&pX2, cauuid.cElems,
                            (GUID *)cauuid.pElems, 0, 0, NULL);
                        CoTaskMemFree(cauuid.pElems);
                    }
                    pSpec->Release();
                }
                pX2->Release();
            }
            pXF->Release();
        }
        pX->Release();
    }
}

void dialogSource(ICaptureGraphBuilder2* pCG, IBaseFilter * pCDbase) {
    HRESULT hr;
    ISpecifyPropertyPages *pSpec;
    CAUUID cauuid;
    IAMVfwCaptureDialogs *pDlg = NULL;
    hr = pCG->FindInterface(NULL, NULL, pCDbase, IID_IAMVfwCaptureDialogs, (void **)&pDlg);

    if (pDlg) {
        if (S_OK == (pDlg->HasDialog(VfwCaptureDialog_Source))) {
            if (FAILED(hr = pDlg->ShowDialog(VfwCaptureDialog_Source, NULL))) {
                error("Could not show VFW Capture Source Dialog");
            }
        }
    } else {
        hr = pCDbase->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pSpec);
        if(hr == S_OK) {
            hr = pSpec->GetPages(&cauuid);
            if (hr == S_OK && cauuid.cElems > 0) {
                hr = OleCreatePropertyFrame(NULL, 30, 30, NULL, 1,
                    (IUnknown **)&pCDbase, cauuid.cElems,
                    (GUID *)cauuid.pElems, 0, 0, NULL);
                CoTaskMemFree(cauuid.pElems);
            }
            pSpec->Release();
        }
    }
}

void dialogFormat(ICaptureGraphBuilder2* pCG, IBaseFilter * pCDbase) {
    HRESULT hr;
    ISpecifyPropertyPages *pSpec;
    CAUUID cauuid;
    IAMVfwCaptureDialogs *pDlg = NULL;
    hr = pCG->FindInterface(NULL, NULL, pCDbase, IID_IAMVfwCaptureDialogs, (void **)&pDlg);

    if (pDlg)	{
        if (S_OK == (pDlg->HasDialog(VfwCaptureDialog_Format)))
            if (FAILED(hr = pDlg->ShowDialog(VfwCaptureDialog_Format, NULL))) {
                error("Could not show VFW Capture Format Dialog");
            }
    } else {
        IAMStreamConfig *pSC;
        hr = pCG->FindInterface(&PIN_CATEGORY_CAPTURE,
            &MEDIATYPE_Interleaved,
            pCDbase, IID_IAMStreamConfig, (void **)&pSC);
        if(hr != S_OK)
            hr = pCG->FindInterface(&PIN_CATEGORY_CAPTURE,
            &MEDIATYPE_Video, pCDbase,
            IID_IAMStreamConfig, (void **)&pSC);
        if(hr == S_OK) {
            hr = pSC->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pSpec);
            if(hr == S_OK) {
                hr = pSpec->GetPages(&cauuid);
                if(hr == S_OK && cauuid.cElems > 0) {
                    hr = OleCreatePropertyFrame(NULL, 30, 30, NULL, 1,
                        (IUnknown **)&pSC, cauuid.cElems,
                        (GUID *)cauuid.pElems, 0, 0, NULL);
                    CoTaskMemFree(cauuid.pElems);
                }
                pSpec->Release();
            }
            pSC->Release();
        }
    }
}


void dialogDisplay(ICaptureGraphBuilder2* pCG, IBaseFilter * pCDbase) {
    HRESULT hr;
    ISpecifyPropertyPages *pSpec;
    CAUUID cauuid;
    IAMVfwCaptureDialogs *pDlg = NULL;
    hr = pCG->FindInterface(NULL, NULL, pCDbase, IID_IAMVfwCaptureDialogs, (void **)&pDlg);

    if (pDlg) {
        if (S_OK == (pDlg->HasDialog(VfwCaptureDialog_Display))) {
            if (FAILED((hr = pDlg->ShowDialog(VfwCaptureDialog_Display, NULL)))) {
                error("Could not show VFW Capture Display Dialog");
            }
        } else
            error("No display dialog for this device");
    }
}

void dialogCrossbar(ICaptureGraphBuilder2* pCG, IBaseFilter * pCDbase) {
    HRESULT hr;
    ISpecifyPropertyPages *pSpec=NULL;
    CAUUID cauuid;
    IAMCrossbar *pX=NULL, *pX2=NULL;
    IBaseFilter *pXF=NULL;

    hr = pCG->FindInterface(&PIN_CATEGORY_CAPTURE,
        &MEDIATYPE_Interleaved, pCDbase,
        IID_IAMCrossbar, (void **)&pX);
    if(hr != S_OK)
        hr = pCG->FindInterface(&PIN_CATEGORY_CAPTURE,
        &MEDIATYPE_Video, pCDbase,
        IID_IAMCrossbar, (void **)&pX);

    if(hr == S_OK) {
        hr = pX->QueryInterface(IID_IBaseFilter, (void **)&pXF);
        if(hr == S_OK) {
            hr = pX->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pSpec);
            if(hr == S_OK) {
                hr = pSpec->GetPages(&cauuid);
                if(hr == S_OK && cauuid.cElems > 0) {
                    hr = OleCreatePropertyFrame(NULL, 30, 30, NULL, 1,
                        (IUnknown **)&pX, cauuid.cElems,
                        (GUID *)cauuid.pElems, 0, 0, NULL);
                    CoTaskMemFree(cauuid.pElems);
                }
                pSpec->Release();
            }
            hr = pCG->FindInterface(&LOOK_UPSTREAM_ONLY, NULL, pXF,
                IID_IAMCrossbar, (void **)&pX2);
            if(hr == S_OK) {
                hr = pX2->QueryInterface(IID_ISpecifyPropertyPages,
                    (void **)&pSpec);
                if(hr == S_OK) {
                    hr = pSpec->GetPages(&cauuid);
                    if(hr == S_OK && cauuid.cElems > 0) {
                        hr = OleCreatePropertyFrame(NULL, 30, 30, NULL, 1,
                            (IUnknown **)&pX2, cauuid.cElems,
                            (GUID *)cauuid.pElems, 0, 0, NULL);
                        CoTaskMemFree(cauuid.pElems);
                    }
                    pSpec->Release();
                }
                pX2->Release();
            }
            pXF->Release();
        }
        pX->Release();
    }
}


HRESULT
FindCaptureDevice(int device, IBaseFilter ** ppSrcFilter)
{
    HRESULT hr;
    IBaseFilter * pSrc = NULL;

    IMoniker* pMoniker =NULL;
    ULONG cFetched;

    ICreateDevEnum* pDevEnum =NULL;
    IEnumMoniker* pClassEnum = NULL;

    do {
        // Create the system device enumerator
        hr = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
            IID_ICreateDevEnum, (void ** ) &pDevEnum);
        if (FAILED(hr)) {
            error("Couldn't create system enumerator!");
            break;
        }

        // Create an enumerator for the video capture devices

        hr = pDevEnum->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &pClassEnum, 0);
        if (FAILED(hr)){
            error("Couldn't create class enumerator!");
            break;
        }

        // If there are no enumerators for the requested type, then
        // CreateClassEnumerator will succeed, but pClassEnum will be NULL.
        if (pClassEnum == NULL) {
            error("No video capture devices found!");
            hr = E_FAIL;
            break;
        }

        // Use the first video capture device on the device list.
        // Note that if the Next() call succeeds but there are no monikers,
        // it will return S_FALSE (which is not a failure).  Therefore, we
        // check that the return code is S_OK instead of using SUCCEEDED() macro.
        int devIndex = 0;
        while(		(S_OK == (pClassEnum->Next (1, &pMoniker, &cFetched)))
            &&	(devIndex <= device)) {
                if (devIndex == device) {
                    // Bind Moniker to a filter object
                    hr = pMoniker->BindToObject(0,0,IID_IBaseFilter, (void**)&pSrc);
                    if (FAILED(hr)) {
                        error("Couldn't bind moniker to filter object!");
                    }
                }
                COMRELEASE(pMoniker);
                devIndex++;
            }
    } while (0);

    // Copy the found filter pointer to the output parameter.
    // Do NOT Release() the reference, since it will still be used
    // by the calling function.
    COMRELEASE(pDevEnum);
    COMRELEASE(pClassEnum);

    *ppSrcFilter = pSrc;

    return hr;
}

HRESULT ConnectFilters(IGraphBuilder *pGraph, IBaseFilter *pFirst, IBaseFilter *pSecond)
{
    IPin *pOut = NULL, *pIn = NULL;
    HRESULT hr = GetPin(pFirst, PINDIR_OUTPUT, &pOut);
    if (FAILED(hr)) return hr;
    hr = GetPin(pSecond, PINDIR_INPUT, &pIn);
    if (FAILED(hr)) {
        pOut->Release();
        return E_FAIL;
    }
    hr = pGraph->Connect(pOut, pIn);
    pIn->Release();
    pOut->Release();
    return hr;
}

void GetBitmapInfoHdr(AM_MEDIA_TYPE* pmt, BITMAPINFOHEADER** ppbmih) {
    *ppbmih = NULL;

    if (IsEqualGUID(pmt->formattype, FORMAT_VideoInfo) ||
        IsEqualGUID(pmt->formattype, FORMAT_MPEGVideo)) {

            VIDEOINFOHEADER * pVideoFormat = (VIDEOINFOHEADER *) pmt->pbFormat;
            *ppbmih = &(((VIDEOINFOHEADER *) pmt->pbFormat)->bmiHeader);
        } else if (	IsEqualGUID(pmt->formattype, FORMAT_MPEG2Video) ||
            IsEqualGUID(pmt->formattype, FORMAT_VideoInfo2)) {
                *ppbmih = &(((VIDEOINFOHEADER2 *) pmt->pbFormat)->bmiHeader);
            } else {
                error("Unknown media format");
                return;
            }
}

HRESULT GetPin(IBaseFilter *pFilter, PIN_DIRECTION PinDir, IPin **ppPin)
{
    IEnumPins  *pEnum;
    IPin       *pPin;
    pFilter->EnumPins(&pEnum);
    while(pEnum->Next(1, &pPin, 0) == S_OK) {
        PIN_DIRECTION PinDirThis;

        pPin->QueryDirection(&PinDirThis);
        if (PinDir == PinDirThis) {
            pEnum->Release();
            *ppPin = pPin;
            return S_OK;
        }
        pPin->Release();
    }
    pEnum->Release();
    return E_FAIL;
}


HRESULT SetAviOptions(IBaseFilter *ppf, InterleavingMode INTERLEAVE_MODE)
{
    HRESULT hr;
    CComPtr<IConfigAviMux>        pMux           = NULL;
    CComPtr<IConfigInterleaving>  pInterleaving  = NULL;

    ASSERT(ppf);
    if (!ppf)
        return E_POINTER;

    // QI for interface AVI Muxer
    if (FAILED(hr = ppf->QueryInterface(IID_IConfigAviMux, reinterpret_cast<PVOID *>(&pMux)))) {
        error("IConfigAviMux failed.");
    }

    if (FAILED(hr = pMux->SetOutputCompatibilityIndex(TRUE))) {
        error("SetOutputCompatibilityIndex failed.");
    }

    // QI for interface Interleaving
    if (FAILED(hr = ppf->QueryInterface(IID_IConfigInterleaving, reinterpret_cast<PVOID *>(&pInterleaving)))) {
        error("IConfigInterleaving failed.");
    }

    // put the interleaving mode (full, none, half)
    if (FAILED(pInterleaving->put_Mode(INTERLEAVE_MODE))) {
        error("put_Mode failed.");
    }

    return hr;
}

HRESULT AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister)
{
    IMoniker * pMoniker;
    IRunningObjectTable *pROT;
    if (FAILED(GetRunningObjectTable(0, &pROT))) {
        return E_FAIL;
    }

    WCHAR wsz[128];
    StringCchPrintfW(wsz, 128, L"FilterGraph %08x pid %08x", (DWORD_PTR)pUnkGraph,
        GetCurrentProcessId());

    HRESULT hr = CreateItemMoniker(L"!", wsz, &pMoniker);
    if (SUCCEEDED(hr)) {
        hr = pROT->Register(0, pUnkGraph, pMoniker, pdwRegister);
        pMoniker->Release();
    }

    pROT->Release();
    return hr;
}

void RemoveGraphFromRot(DWORD pdwRegister)
{
    IRunningObjectTable *pROT;

    if (SUCCEEDED(GetRunningObjectTable(0, &pROT))) {
        pROT->Revoke(pdwRegister);
        pROT->Release();
    }
}
#else /* !HAVE_DIRECTSHOW */
videoDS ::  videoDS() : videoBase("") {}
videoDS :: ~videoDS() {}
#endif