filmDS.cpp 32.2 KB
Newer Older
1
2
3
4
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
5
// zmoelnig@iem.at
6
//
7
// Implementation file
8
//
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
9
10
//    Copyright © 2014-2019 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
//    based on ofDirectShowPlayer, Copyright © 2014 Theodore Watson
11
12
13
14
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
15
16
17
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
18

19
#include "filmDS.h"
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
20
#include "plugins/PluginFactory.h"
21
#include "Gem/Properties.h"
22
#include "Gem/RTE.h"
23
#include "Utils/wstring.h"
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
24
25
26
/* C++11 */
#include <memory>
#include <functional>
27

28
#if 1
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
29
30
31
32
# define MARK_HR(hr) if(hr)printf("%s:%d (%s)\t: 0x%X\n", __FILE__, __LINE__, __FUNCTION__, hr);else printf("%s:%d (%s)\n", __FILE__, __LINE__, __FUNCTION__)
#else
# define MARK_HR(hr) do{;} while(0)
#endif
33
#define MARK() MARK_HR(0)
34

35
36
37

#define USE_CALLBACKS 1

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
38
39
40
41
42
43
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// DirectShow includes and helper methods
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <dshow.h>
#pragma include_alias( "dxtrans.h", "qedit.h" )
#define __IDxtCompositor_INTERFACE_DEFINED__
#define __IDxtAlphaSetter_INTERFACE_DEFINED__
#define __IDxtJpeg_INTERFACE_DEFINED__
#define __IDxtKey_INTERFACE_DEFINED__
#include <aviriff.h>
#include <windows.h>

//for threading
#include <process.h>

// Due to a missing qedit.h in recent Platform SDKs, we've replicated the relevant contents here
// #include <qedit.h>
MIDL_INTERFACE("0579154A-2B53-4994-B0D0-E773148EFF85")
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
59
60
61
ISampleGrabberCB :
public IUnknown {
public:
62
  virtual HRESULT STDMETHODCALLTYPE SampleCB(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
63
64
    double SampleTime,
    IMediaSample *pSample) = 0;
65
66

  virtual HRESULT STDMETHODCALLTYPE BufferCB(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
67
68
69
    double SampleTime,
    BYTE *pBuffer,
    long BufferLen) = 0;
70
71
72
73
74

};

MIDL_INTERFACE("6B652FFF-11FE-4fce-92AD-0266B5D7C78F")
ISampleGrabber :
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
75
76
public IUnknown {
public:
77
  virtual HRESULT STDMETHODCALLTYPE SetOneShot(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
78
    BOOL OneShot) = 0;
79
80

  virtual HRESULT STDMETHODCALLTYPE SetMediaType(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
81
    const AM_MEDIA_TYPE *pType) = 0;
82
83

  virtual HRESULT STDMETHODCALLTYPE GetConnectedMediaType(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
84
    AM_MEDIA_TYPE *pType) = 0;
85
86

  virtual HRESULT STDMETHODCALLTYPE SetBufferSamples(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
87
    BOOL BufferThem) = 0;
88
89

  virtual HRESULT STDMETHODCALLTYPE GetCurrentBuffer(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
90
91
    /* [out][in] */ long *pBufferSize,
    /* [out] */ long *pBuffer) = 0;
92
93

  virtual HRESULT STDMETHODCALLTYPE GetCurrentSample(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
94
    /* [retval][out] */ IMediaSample **ppSample) = 0;
95
96

  virtual HRESULT STDMETHODCALLTYPE SetCallback(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
97
98
    ISampleGrabberCB *pCallback,
    long WhichMethodToCallback) = 0;
99
100
101
102
103
104
105
106
107

};
EXTERN_C const CLSID CLSID_SampleGrabber;
EXTERN_C const IID IID_ISampleGrabber;
EXTERN_C const CLSID CLSID_NullRenderer;

// GetUnconnectedPin
//    Finds an unconnected pin on a filter in the desired direction
HRESULT GetUnconnectedPin(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
108
109
110
  IBaseFilter *pFilter,   // Pointer to the filter.
  PIN_DIRECTION PinDir,   // Direction of the pin to find.
  IPin **ppPin)           // Receives a pointer to the pin.
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
{
  *ppPin = 0;
  IEnumPins *pEnum = 0;
  IPin *pPin = 0;
  HRESULT hr = pFilter->EnumPins(&pEnum);
  if (FAILED(hr)) {
    return hr;
  }
  while (pEnum->Next(1, &pPin, NULL) == S_OK) {
    PIN_DIRECTION ThisPinDir;
    pPin->QueryDirection(&ThisPinDir);
    if (ThisPinDir == PinDir) {
      IPin *pTmp = 0;
      hr = pPin->ConnectedTo(&pTmp);
      if (SUCCEEDED(hr)) { // Already connected, not the pin we want.
        pTmp->Release();
      } else { // Unconnected, this is the pin we want.
        pEnum->Release();
        *ppPin = pPin;
        return S_OK;
      }
    }
    pPin->Release();
  }
  pEnum->Release();
  // Did not find a matching pin.
  return E_FAIL;
}
139

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Disconnect any connections to the filter.
HRESULT DisconnectPins(IBaseFilter *pFilter)
{
  IEnumPins *pEnum = 0;
  IPin *pPin = 0;
  HRESULT hr = pFilter->EnumPins(&pEnum);
  if (FAILED(hr)) {
    return hr;
  }

  while (pEnum->Next(1, &pPin, NULL) == S_OK) {
    pPin->Disconnect();
    pPin->Release();
  }
  pEnum->Release();

  // Did not find a matching pin.
  return S_OK;
}

// ConnectFilters
//    Connects a pin of an upstream filter to the pDest downstream filter
HRESULT ConnectFilters(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
163
164
165
  IGraphBuilder *pGraph, // Filter Graph Manager.
  IPin *pOut,            // Output pin on the upstream filter.
  IBaseFilter *pDest)    // Downstream filter.
166
167
168
169
170
171
172
173
{
  if ((pGraph == NULL) || (pOut == NULL) || (pDest == NULL)) {
    return E_POINTER;
  }
#ifdef debug
  PIN_DIRECTION PinDir;
  pOut->QueryDirection(&PinDir);
  _ASSERTE(PinDir == PINDIR_OUTPUT);
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
174
175
#endif

176
177
178
179
180
181
182
183
184
185
186
  // Find an input pin on the downstream filter.
  IPin *pIn = 0;
  HRESULT hr = GetUnconnectedPin(pDest, PINDIR_INPUT, &pIn);
  if (FAILED(hr)) {
    return hr;
  }
  // Try to connect them.
  hr = pGraph->Connect(pOut, pIn);
  pIn->Release();
  return hr;
}
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
187

188

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
189

190
191
192
// ConnectFilters
//    Connects two filters
HRESULT ConnectFilters(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
193
194
195
  IGraphBuilder *pGraph,
  IBaseFilter *pSrc,
  IBaseFilter *pDest)
196
197
198
199
{
  if ((pGraph == NULL) || (pSrc == NULL) || (pDest == NULL)) {
    return E_POINTER;
  }
zmoelnig's avatar
zmoelnig committed
200

201
202
203
204
205
206
207
208
209
210
  // Find an output pin on the first filter.
  IPin *pOut = 0;
  HRESULT hr = GetUnconnectedPin(pSrc, PINDIR_OUTPUT, &pOut);
  if (FAILED(hr)) {
    return hr;
  }
  hr = ConnectFilters(pGraph, pOut, pDest);
  pOut->Release();
  return hr;
}
211

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// LocalFreeMediaType
//    Free the format buffer in the media type
void LocalFreeMediaType(AM_MEDIA_TYPE& mt)
{
  if (mt.cbFormat != 0) {
    CoTaskMemFree((PVOID)mt.pbFormat);
    mt.cbFormat = 0;
    mt.pbFormat = NULL;
  }
  if (mt.pUnk != NULL) {
    // Unecessary because pUnk should not be used, but safest.
    mt.pUnk->Release();
    mt.pUnk = NULL;
  }
}
227

228
229
230
231
232
233
234
235
236
// LocalDeleteMediaType
//    Free the format buffer in the media type,
//    then delete the MediaType ptr itself
void LocalDeleteMediaType(AM_MEDIA_TYPE *pmt)
{
  if (pmt != NULL) {
    LocalFreeMediaType(*pmt); // See FreeMediaType for the implementation.
    CoTaskMemFree(pmt);
  }
237
}
238

239

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
240
HRESULT SaveGraphFile(IGraphBuilder *pGraph, WCHAR *wszPath)
241
242
{
  const WCHAR wszStreamName[] = L"ActiveMovieGraph";
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
243
244
  HRESULT hr;

245
246
  IStorage *pStorage = NULL;
  hr = StgCreateDocfile(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
247
248
249
         wszPath,
         STGM_CREATE | STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
         0, &pStorage);
250
251
252
253
254
255
  if(FAILED(hr)) {
    return hr;
  }

  IStream *pStream;
  hr = pStorage->CreateStream(
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
256
257
258
         wszStreamName,
         STGM_WRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
         0, 0, &pStream);
259
260
261
262
  if (FAILED(hr)) {
    pStorage->Release();
    return hr;
  }
263

264
265
266
267
268
269
270
271
272
273
274
  IPersistStream *pPersist = NULL;
  pGraph->QueryInterface(IID_IPersistStream, (void**)&pPersist);
  hr = pPersist->Save(pStream, TRUE);
  pStream->Release();
  pPersist->Release();
  if (SUCCEEDED(hr)) {
    hr = pStorage->Commit(STGC_DEFAULT);
  }
  pStorage->Release();
  return hr;
}
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
275

276
277
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
278
// DirectShowVideo - a simple directshow video player implementation
279
280
281
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
282
283
284
namespace
{
int comRefCount = 0;
285

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
286
void retainCom()
287
{
288
  if( comRefCount == 0 ) {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
289
    //printf("com is initialized!\n");
290
291
292
293
    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  }
  comRefCount++;
}
294

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
295
void releaseCom()
296
297
298
{
  comRefCount--;
  if( comRefCount == 0 ) {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
299
    //printf("com is uninitialized!\n");
300
301
302
    CoUninitialize();
  }
}
303

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
304
void releaseSample(IMediaSample * sample)
305
{
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
306
307
308
  sample->Release();
}
}
309

310

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
311
312
313
class gem::plugins::filmDS::DirectShowVideo : public ISampleGrabberCB
{
public:
314

315
316
317
318
319
320
321
322
323
  DirectShowVideo()
  {
    retainCom();
    clearValues();
    InitializeCriticalSection(&critSection);
  }

  ~DirectShowVideo()
  {
324
    stop();
325
    tearDown();
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
326
327
    middleSample.reset();
    backSample.reset();
328
329
330
331
332
333
334
    releaseCom();
    DeleteCriticalSection(&critSection);
  }

  void tearDown()
  {
    //printf("tearDown\n");
335

336
337
338
    if(m_pControl) {
      m_pControl->Release();
    }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
339
340
341
    if(m_pEvent) {
      m_pEvent->Release();
    }
342
343
344
    if(m_pSeek) {
      m_pSeek->Release();
    }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
345
346
347
    if(m_pAudio) {
      m_pAudio->Release();
    }
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    if(m_pBasicVideo) {
      m_pBasicVideo->Release();
    }
    if(m_pGrabber) {
      m_pGrabber->Release();
    }
    if(m_pGrabberF) {
      m_pGrabberF->Release();
    }
    if(m_pGraph) {
      m_pGraph->Release();
    }
    if(m_pNullRenderer) {
      m_pNullRenderer->Release();
    }
    if( m_pSourceFile ) {
      m_pSourceFile->Release();
    }
    if( m_pPosition ) {
      m_pPosition->Release();
368
    }
369
370
    clearValues();
  }
371

372
373
  void clearValues()
  {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
374
375
    hr = 0;

376
377
    m_pGraph = NULL;
    m_pControl = NULL;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
378
    m_pEvent = NULL;
379
    m_pSeek = NULL;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
380
    m_pAudio = NULL;
381
382
383
384
385
386
387
    m_pGrabber = NULL;
    m_pGrabberF = NULL;
    m_pBasicVideo = NULL;
    m_pNullRenderer = NULL;
    m_pSourceFile = NULL;
    m_pPosition = NULL;

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
388
389
390
391
    timeNow = 0;
    lPositionInSecs = 0;
    lDurationInNanoSecs = 0;
    lTotalDuration = 0;
392
    rtNew = 0;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
393
394
395
    lPosition = 0;
    lvolume = -1000;
    evCode = 0;
396
397
    width = height = 0;
    bVideoOpened = false;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
398
399
400
401
402
403
404
405
406
407
    bLoop = true;
    bPaused = false;
    bPlaying = false;
    bEndReached = false;
    bNewPixels = false;
    bFrameNew = false;
    curMovieFrame = -1;
    frameCount = -1;

    movieRate = 1.0;
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
    averageTimePerFrame = 1.0/30.0;
  }

  //------------------------------------------------
  STDMETHODIMP_(ULONG) AddRef()
  {
    return 1;
  }
  STDMETHODIMP_(ULONG) Release()
  {
    return 2;
  }


  //------------------------------------------------
  STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject)
  {
    *ppvObject = static_cast<ISampleGrabberCB*>(this);
    return S_OK;
  }


  //------------------------------------------------
  STDMETHODIMP SampleCB(double Time, IMediaSample *pSample)
  {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
433
MARK();
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
434
435
436
437
438
439
440
441
442
443
    BYTE * ptrBuffer = NULL;
    HRESULT hr = pSample->GetPointer(&ptrBuffer);

    if(hr == S_OK) {
      long latestBufferLength = pSample->GetActualDataLength();
      int currentBufferLength = pixels.xsize * pixels.ysize * pixels.csize;
      if(latestBufferLength == currentBufferLength ) {
        EnterCriticalSection(&critSection);
        pSample->AddRef();
        backSample =
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
444
          std::unique_ptr<IMediaSample, std::function<void(IMediaSample*)>>(pSample, releaseSample);
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
445
446
447
448
449
450
451
452
453
454
455
456
457
        bNewPixels = true;

        //this is just so we know if there is a new frame
        frameCount++;

        LeaveCriticalSection(&critSection);
      } else {
        error("[GEM:videoDS] SampleCB() - buffer sizes do not match %d != %d",
              latestBufferLength, currentBufferLength);
      }
    }

    return S_OK;
458
  }
459

460
461
462
463
464
465
  //This method is meant to have more overhead
  STDMETHODIMP BufferCB(double Time, BYTE *pBuffer, long BufferLen)
  {
    return E_NOTIMPL;
  }

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
466
  bool loadMovie(std::string path, int format)
467
468
  {
    tearDown();
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
469
    pixelFormat = format;
470
471
472

    // Create the Filter Graph Manager and query for interfaces.

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
473
474
475
476
    //printf("step 1\n");
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
                          IID_IGraphBuilder, (void **)&m_pGraph);
    if (FAILED(hr)) {
477
478
479
480
      tearDown();
      return false;
    }

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
481
    //printf("step 2\n");
482
    hr = m_pGraph->QueryInterface(IID_IMediaSeeking, (void**)&m_pSeek);
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
483
    if (FAILED(hr)) {
484
      tearDown();
485
486
487
      return false;
    }

488
489
490
    hr = m_pGraph->QueryInterface(IID_IMediaPosition, (LPVOID *)&m_pPosition);
    if (FAILED(hr)) {
      tearDown();
491
492
      return false;
    }
493

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
494
    hr = m_pGraph->QueryInterface(IID_IBasicAudio,(void**)&m_pAudio);
495
496
    if (FAILED(hr)) {
      tearDown();
497
498
499
      return false;
    }

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
500
501
502
    // Use IGraphBuilder::QueryInterface (inherited from IUnknown) to get the IMediaControl interface.
    //printf("step 4\n");
    hr = m_pGraph->QueryInterface(IID_IMediaControl, (void **)&m_pControl);
503
504
    if (FAILED(hr)) {
      tearDown();
505
506
507
      return false;
    }

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
508
509
510
    // And get the Media Event interface, too.
    //printf("step 5\n");
    hr = m_pGraph->QueryInterface(IID_IMediaEvent, (void **)&m_pEvent);
511
512
    if (FAILED(hr)) {
      tearDown();
513
514
515
      return false;
    }

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
516
517
518
519
    //SAMPLE GRABBER (ALLOWS US TO GRAB THE BUFFER)//
    // Create the Sample Grabber.
    hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
                          IID_IBaseFilter, (void**)&m_pGrabberF);
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
520
    if (FAILED(hr)) {
521
522
      tearDown();
      return false;
523
    }
524

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
525
    hr = m_pGraph->AddFilter(m_pGrabberF, L"Sample Grabber");
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
526
    if (FAILED(hr)) {
527
528
529
      tearDown();
      return false;
    }
530

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
531
    hr = m_pGrabberF->QueryInterface(IID_ISampleGrabber, (void**)&m_pGrabber);
532
533
534
535
    if (FAILED(hr)) {
      tearDown();
      return false;
    }
536

537
#if USE_CALLBACKS
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
538
    hr = m_pGrabber->SetCallback(this, 0);
539
540
541
542
    if (FAILED(hr)) {
      tearDown();
      return false;
    }
543
#endif
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
544
545
546
547
    //MEDIA CONVERSION
    //Get video properties from the stream's mediatype and apply to the grabber (otherwise we don't get an RGB image)
    AM_MEDIA_TYPE mt;
    ZeroMemory(&mt,sizeof(AM_MEDIA_TYPE));
548

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
549
550
551
552
553
554
555
556
557
558
559
560
    mt.majortype    = MEDIATYPE_Video;
    switch (format) {
    case GEM_RGB:
      mt.subtype = MEDIASUBTYPE_RGB24;
      break;
    case GEM_RGBA:
      mt.subtype = MEDIASUBTYPE_RGB32;
      break;
    default:
      verbose(1,
              "[GEM:videoDS] Trying to set unsupported format this is an internal bug, using default RGBA");
      mt.subtype = MEDIASUBTYPE_RGB32;
561
    }
562

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
563
564
565
    mt.formattype   = FORMAT_VideoInfo;
    //printf("step 5.5\n");
    hr = m_pGrabber->SetMediaType(&mt);
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
566
    if (FAILED(hr)) {
567
568
569
      tearDown();
      return false;
    }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
570

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
571
572
    //printf("step 6\n");
    std::wstring filePathW = std::wstring(path.begin(), path.end());
573

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
574
575
    //this is the easier way to connect the graph, but we have to remove the video window manually
    hr = m_pGraph->RenderFile(filePathW.c_str(), NULL);
576

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
577
578
    //printf("step 7\n");
    if (SUCCEEDED(hr)) {
579

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
580
581
      //Set Params - One Shot should be false unless you want to capture just one buffer
      hr = m_pGrabber->SetOneShot(FALSE);
582
      if (FAILED(hr)) {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
583
        verbose(1, "[GEM:videoDS] unable to set one shot");
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
584
585
        tearDown();
        return false;
586
      }
587

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
588
589
590
591
592
593
594
      //apparently setting to TRUE causes a small memory leak
      hr = m_pGrabber->SetBufferSamples(FALSE);
      if (FAILED(hr)) {
        verbose(1, "[GEM:videoDS] unable to set buffer samples");
        tearDown();
        return false;
      }
595

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
596
597
598
599
600
601
602
603
604
      //NULL RENDERER//
      //used to give the video stream somewhere to go to.
      hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER,
                            IID_IBaseFilter, (void**)(&m_pNullRenderer));
      if (FAILED(hr)) {
        verbose(1, "[GEM:videoDS] null renderer error");
        tearDown();
        return false;
      }
605

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
606
607
608
609
610
611
      hr = m_pGraph->AddFilter(m_pNullRenderer, L"Render");
      if (FAILED(hr)) {
        verbose(1, "[GEM:videoDS] unable to add null renderer");
        tearDown();
        return false;
      }
612

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
613
614
      AM_MEDIA_TYPE mt;
      ZeroMemory(&mt,sizeof(AM_MEDIA_TYPE));
615

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
616
617
618
619
620
621
      hr = m_pGrabber->GetConnectedMediaType(&mt);
      if (FAILED(hr)) {
        verbose(1, "[GEM:videoDS] unable to call GetConnectedMediaType");
        tearDown();
        return false;
      }
622

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
      VIDEOINFOHEADER * infoheader = (VIDEOINFOHEADER*)mt.pbFormat;
      width = infoheader->bmiHeader.biWidth;
      height = infoheader->bmiHeader.biHeight;
      averageTimePerFrame = infoheader->AvgTimePerFrame / 10000000.0;
      pixels.xsize = width;
      pixels.ysize = height;
      pixels.setCsizeByFormat(pixelFormat);
      pixels.reallocate();

      //we need to manually change the output from the renderer window to the null renderer
      IBaseFilter * m_pVideoRenderer;
      IPin* pinIn = 0;
      IPin* pinOut = 0;

      hr = m_pGraph->FindFilterByName(L"Video Renderer", &m_pVideoRenderer);
      if (FAILED(hr)) {
        verbose(1, "[GEM:videoDS] failed to find the video renderer");
        tearDown();
        return false;
      }
643

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
644
645
646
647
648
649
650
      //we disconnect the video renderer window by finding the output pin of the sample grabber
      hr = m_pGrabberF->FindPin(L"Out", &pinOut);
      if (FAILED(hr)) {
        verbose(1, "[GEM:videoDS] failed to find the sample grabber output pin");
        tearDown();
        return false;
      }
651

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
652
653
654
655
656
657
      hr = pinOut->Disconnect();
      if (FAILED(hr)) {
        verbose(1, "[GEM:videoDS] failed to disconnect grabber output pin");
        tearDown();
        return false;
      }
658

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
659
660
661
662
663
664
665
666
667
      //we have to remove it as well otherwise the graph builder will reconnect it
      hr = m_pGraph->RemoveFilter(m_pVideoRenderer);
      if (FAILED(hr)) {
        verbose(1, "[GEM:videoDS] failed to remove the default renderer");
        tearDown();
        return false;
      } else {
        m_pVideoRenderer->Release();
      }
668

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
669
670
671
672
673
674
675
676
      //now connect the null renderer to the grabber output, if we don't do this not frames will be captured
      hr = m_pNullRenderer->FindPin(L"In", &pinIn);
      if (FAILED(hr)) {
        verbose(1,
                "[GEM:videoDS] failed to find the input pin of the null renderer");
        tearDown();
        return false;
      }
677

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
678
679
680
      hr = pinOut->Connect(pinIn, NULL);
      if (FAILED(hr)) {
        verbose(1, "[GEM:videoDS] failed to connect the null renderer");
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
681
682
        tearDown();
        return false;
683
684
      }

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
      //printf("step 8\n");


      // Run the graph.

      //SaveGraphFile(m_pGraph, L"test2.grf");
      hr = m_pControl->Run();
      //SaveGraphFile(m_pGraph, L"test3.grf");

      // Now pause the graph.
      hr = m_pControl->Stop();
      updatePlayState();

      if( FAILED(hr) || width == 0 || height == 0 ) {
        tearDown();
        verbose(1,
                "[GEM:videoDS] Error occured while playing or pausing or opening the file");
        return false;
703
      }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
704
705
706
707
708
    } else {
      tearDown();
      verbose(1,
              "[GEM:videoDS] Error occured while playing or pausing or opening the file");
      return false;
709
    }
710

711
712
    bVideoOpened = true;
    return true;
713
714
  }

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
715
  void update()
716
  {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
717
718
719
720
721
722
723
724
725
    if( bVideoOpened ) {
      long eventCode = 0;
#ifdef _WIN64
      long long ptrParam1 = 0;
      long long ptrParam2 = 0;
#else
      long ptrParam1 = 0;
      long ptrParam2 = 0;
#endif
726

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
727
728
      if( curMovieFrame != frameCount ) {
        bFrameNew = true;
729
      } else {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
730
        bFrameNew = false;
731
      }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
732
      curMovieFrame = frameCount;
733

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
734
735
736
737
738
739
740
741
742
743
      while (S_OK == m_pEvent->GetEvent(&eventCode, &ptrParam1, &ptrParam2, 0)) {
        if (eventCode == EC_COMPLETE ) {
          if(bLoop) {
            //printf("Restarting!\n");
            setPosition(0.0);
          } else {
            bEndReached = true;
            //printf("movie end reached!\n");
            stop();
            updatePlayState();
744
          }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
        }
        //printf("Event code: %#04x\n Params: %d, %d\n", eventCode, ptrParam1, ptrParam2);
        m_pEvent->FreeEventParams(eventCode, ptrParam1, ptrParam2);
      }
    }
  }

  bool isLoaded()
  {
    return bVideoOpened;
  }
#if 0
  //volume has to be log corrected/converted
  void setVolume(float volPct)
  {
    if( isLoaded() ) {
      if( volPct < 0 ) {
        volPct = 0.0;
      }
      if( volPct > 1 ) {
        volPct = 1.0;
      }
767

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
768
769
770
771
772
773
774
      long vol = log10(volPct) * 4000.0;
      if(vol < -8000) {
        vol = -10000;
      }
      m_pAudio->put_Volume(vol);
    }
  }
775

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
  float getVolume()
  {
    float volPct = 0.0;
    if( isLoaded() ) {
      long vol = 0;
      m_pAudio->get_Volume(&vol);
      volPct = powf(10, (float)vol/4000.0);
    }
    return volPct;
  }
#endif
  double getDurationInSeconds()
  {
    if( isLoaded() ) {
      long long lDurationInNanoSecs = 0;
      m_pSeek->GetDuration(&lDurationInNanoSecs);
      double timeInSeconds = (double)lDurationInNanoSecs/10000000.0;
793

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
794
795
796
797
      return timeInSeconds;
    }
    return 0.0;
  }
798

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
799
800
801
802
803
804
  double getCurrentTimeInSeconds()
  {
    if( isLoaded() ) {
      long long lCurrentTimeInNanoSecs = 0;
      m_pSeek->GetCurrentPosition(&lCurrentTimeInNanoSecs);
      double timeInSeconds = (double)lCurrentTimeInNanoSecs/10000000.0;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
805

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
806
807
808
809
      return timeInSeconds;
    }
    return 0.0;
  }
810

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
811
812
813
814
815
816
817
818
819
  void setPosition(float pct)
  {
    if( bVideoOpened ) {
      if( pct < 0.0 ) {
        pct = 0.0;
      }
      if( pct > 1.0 ) {
        pct = 1.0;
      }
820

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
821
822
      long long lDurationInNanoSecs = 0;
      m_pSeek->GetDuration(&lDurationInNanoSecs);
823

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
824
825
826
827
828
      rtNew = ((float)lDurationInNanoSecs * pct);
      hr = m_pSeek->SetPositions(&rtNew, AM_SEEKING_AbsolutePositioning,NULL,
                                 AM_SEEKING_NoPositioning);
    }
  }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
829

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
830
831
832
833
834
835
  float getPosition()
  {
    if( bVideoOpened ) {
      float timeDur = getDurationInSeconds();
      if( timeDur > 0.0 ) {
        return getCurrentTimeInSeconds() / timeDur;
836
      }
837
    }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
838
    return 0.0;
839
  }
840

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
841
  void setSpeed(float speed)
842
  {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
843
844
845
846
    if( bVideoOpened ) {
      m_pPosition->put_Rate(speed);
      m_pPosition->get_Rate(&movieRate);
    }
847
  }
848

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
849
  double getSpeed()
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
850
  {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
851
852
    return movieRate;
  }
853

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
854
855
856
857
858
859
860
861
  void play()
  {
    if( bVideoOpened ) {
      m_pControl->Run();
      bEndReached = false;
      updatePlayState();
    }
  }
862

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
863
864
865
866
867
868
869
870
  void stop()
  {
    if( bVideoOpened ) {
      if( isPlaying() ) {
        setPosition(0.0);
      }
      m_pControl->Stop();
      updatePlayState();
871
    }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
872
  }
873

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
874
875
876
877
878
879
880
881
882
  void setPaused(bool bPaused)
  {
    if( bVideoOpened ) {
      if( bPaused ) {
        m_pControl->Pause();
      } else {
        m_pControl->Run();
      }
      updatePlayState();
883
    }
884

885
  }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
886
887

  void updatePlayState()
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
888
  {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
    if( bVideoOpened ) {
      FILTER_STATE fs;
      hr = m_pControl->GetState(4000, (OAFilterState*)&fs);
      if(hr==S_OK) {
        if( fs == State_Running ) {
          bPlaying = true;
          bPaused = false;
        } else if( fs == State_Paused ) {
          bPlaying = false;
          bPaused = true;
        } else if( fs == State_Stopped ) {
          bPlaying = false;
          bPaused = false;
        }
      }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
904
    }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
905
  }
906

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
907
908
909
910
  bool isPlaying()
  {
    return bPlaying;
  }
911

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
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
  bool isPaused()
  {
    return bPaused;
  }

  bool isLooping()
  {
    return bLoop;
  }

  void setLoop(bool loop)
  {
    bLoop = loop;
  }

  bool isMovieDone()
  {
    return bEndReached;
  }

  int getWidth()
  {
    return width;
  }

  int getHeight()
  {
    return height;
  }

  bool isFrameNew()
  {
    return bFrameNew;
  }

  void nextFrame()
  {
    //we have to do it like this as the frame based approach is not very accurate
    if( bVideoOpened && ( isPlaying() || isPaused() ) ) {
      int curFrame = getCurrentFrameNo();
      float curFrameF = curFrame;
      for(int i = 1; i < 20; i++) {
954
        setApproximateFrameF( curFrameF + 0.3 * (float)i );
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
955
956
        if( getCurrentFrameNo() >= curFrame + 1 ) {
          break;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
957
        }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
958
959
960
      }
    }
  }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
961

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
962
963
964
965
966
967
968
  void preFrame()
  {
    //we have to do it like this as the frame based approach is not very accurate
    if( bVideoOpened && ( isPlaying() || isPaused() ) ) {
      int curFrame = getCurrentFrameNo();
      float curFrameF = curFrame;
      for(int i = 1; i < 20; i++) {
969
        setApproximateFrameF( curFrameF - 0.3 * (float)i );
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
970
971
        if( getCurrentFrameNo() <= curFrame + 1 ) {
          break;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
972
        }
973
      }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
974
975
976
    }
  }

977
  void setApproximateFrameF(float frameF)
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
978
979
  {
    if( bVideoOpened ) {
980
      float pct = frameF / (float)getApproximateNoFrames();
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
981
982
      if( pct > 1.0 ) {
        pct = 1.0;
983
      }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
984
985
      if( pct < 0.0 ) {
        pct = 0.0;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
986
      }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
987
988
989
      setPosition(pct);
    }
  }
990

991
  void setApproximateFrame(int frame)
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
992
993
  {
    if( bVideoOpened ) {
994
      float pct = (float)frame / (float)getApproximateNoFrames();
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
995
996
      if( pct > 1.0 ) {
        pct = 1.0;
997
      }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
998
999
      if( pct < 0.0 ) {
        pct = 0.0;
1000
      }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1001
1002
1003
      setPosition(pct);
    }
  }
1004

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1005
1006
1007
  int getCurrentFrameNo()
  {
    if( bVideoOpened ) {
1008
      return getPosition() * (float) getApproximateNoFrames();
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1009
1010
1011
    }
    return 0;
  }
1012

1013
  int getApproximateNoFrames()
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1014
1015
1016
  {
    if( bVideoOpened && averageTimePerFrame > 0.0 ) {
      return getDurationInSeconds() / averageTimePerFrame;
1017
    }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1018
    return 0;
1019
  }
1020

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1021
  double getAverageTimePerFrame()
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1022
  {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1023
    return averageTimePerFrame;
1024
  }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1025
1026
1027
1028


#if 0
  bool needsRBSwap(of_PixelFormat srcFormat, of_PixelFormat dstFormat)
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1029
  {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
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
    return false;
    (srcFormat == OF_PIXELS_BGR
     || srcFormat == OF_PIXELS_BGRA) && (dstFormat == OF_PIXELS_RGB
                                         || dstFormat == OF_PIXELS_RGBA) ||
    (srcFormat == OF_PIXELS_RGB
     || srcFormat == OF_PIXELS_RGBA) && (dstFormat == OF_PIXELS_BGR
                                         || dstFormat == OF_PIXELS_BGRA);
  }
  void processPixels(of_Pixels & src, of_Pixels & dst)
  {
    auto format = src.getPixelFormat();

    if(needsRBSwap(src.getPixelFormat(), dst.getPixelFormat())) {
      if (src.getPixelFormat() == OF_PIXELS_BGR) {
        dst.allocate(src.getWidth(), src.getHeight(), OF_PIXELS_RGB);
        auto dstLine = dst.getLines().begin();
        auto srcLine = --src.getLines().end();
        auto endLine = dst.getLines().end();
        for (; dstLine != endLine; dstLine++, srcLine--) {
          auto dstPixel = dstLine.getPixels().begin();
          auto srcPixel = srcLine.getPixels().begin();
          auto endPixel = dstLine.getPixels().end();
          for (; dstPixel != endPixel; dstPixel++, srcPixel++) {
            dstPixel[0] = srcPixel[2];
            dstPixel[1] = srcPixel[1];
            dstPixel[2] = srcPixel[0];
          }
        }
      } else if (src.getPixelFormat() == OF_PIXELS_BGRA) {
        dst.allocate(src.getWidth(), src.getHeight(), OF_PIXELS_RGBA);
        auto dstLine = dst.getLines().begin();
        auto srcLine = --src.getLines().end();
        auto endLine = dst.getLines().end();
        for (; dstLine != endLine; dstLine++, srcLine--) {
          auto dstPixel = dstLine.getPixels().begin();
          auto srcPixel = srcLine.getPixels().begin();
          auto endPixel = dstLine.getPixels().end();
          for (; dstPixel != endPixel; dstPixel++, srcPixel++) {
            dstPixel[0] = srcPixel[2];
            dstPixel[1] = srcPixel[1];
            dstPixel[2] = srcPixel[0];
          }
        }
      }
    } else {
      src.mirrorTo(dst, true, false);
1076
    }
IOhannes m zmölnig's avatar
get fps    
IOhannes m zmölnig committed
1077
  }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1078
1079
#endif

1080
#ifdef USE_CALLBACKS
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1081
  bool getPixels(imageStruct&img)
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1082
  {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1083
1084
1085
1086
1087
1088
1089
1090
1091
    if(bVideoOpened && bNewPixels) {
      EnterCriticalSection(&critSection);
      std::swap(backSample, middleSample);
      bNewPixels = false;
      LeaveCriticalSection(&critSection);
      BYTE * ptrBuffer = NULL;
      HRESULT hr = middleSample->GetPointer(&ptrBuffer);
      switch (pixelFormat) {
      case GEM_RGB:
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1092
        pixels.fromBGR(ptrBuffer);
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1093
1094
        break;
      case GEM_RGBA:
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1095
        pixels.fromBGRA(ptrBuffer);
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1096
1097
1098
1099
1100
1101
        break;
      }
      img.convertFrom(&pixels);
      //processPixels(srcBuffer, pixels);
    }
    return true;
1102
  }
1103

1104
#else
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1105
  //this is the non-callback approach
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
  bool getPixels(imageStruct&img){
    if(bVideoOpened && isFrameNew()){
      long bufferSize = img.xsize * img.ysize * img.csize;
      post("fetching %d bytes into %p", bufferSize, img.data);
      HRESULT hr = m_pGrabber->GetCurrentBuffer(&bufferSize, (long *)img.data);
#if 1
      return (S_OK==hr);
#else
      if(hr==S_OK){
        if (videoSize == bufferSize){
          //processPixels(rawBuffer, dstBuffer, width, height, true, true);
        }else{
          printf("ERROR: GetPixels() - bufferSizes do not match!\n");
        }
      }else{
        printf("ERROR: GetPixels() - Unable to get pixels for device  bufferSize = %i \n", bufferSize);
      }
#endif
    }
    return false;
  }
#endif
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
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

protected:

  HRESULT hr;                         // COM return value
  IGraphBuilder *m_pGraph;        // Graph Builder interface
  IMediaControl *m_pControl;  // Media Control interface
  IMediaEvent   *m_pEvent;        // Media Event interface
  IMediaSeeking *m_pSeek;     // Media Seeking interface
  IMediaPosition * m_pPosition;
  IBasicAudio   *m_pAudio;        // Audio Settings interface
  ISampleGrabber * m_pGrabber;
  IBaseFilter * m_pSourceFile;
  IBaseFilter * m_pGrabberF;
  IBasicVideo * m_pBasicVideo;
  IBaseFilter * m_pNullRenderer;

  REFERENCE_TIME
  timeNow;             // Used for FF & REW of movie, current time
  LONGLONG lPositionInSecs;       // Time in  seconds
  LONGLONG lDurationInNanoSecs;       // Duration in nanoseconds
  LONGLONG lTotalDuration;        // Total duration
  REFERENCE_TIME rtNew;               // Reference time of movie
  long lPosition;                 // Desired position of movie used in FF & REW
  long lvolume;                   // The volume level in 1/100ths dB Valid values range from -10,000 (silence) to 0 (full volume), 0 = 0 dB -10000 = -100 dB
  long evCode;                    // event variable, used to in file to complete wait.

  long width, height;
1155

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
  double averageTimePerFrame;

  bool bFrameNew;
  bool bNewPixels;
  bool bVideoOpened;
  bool bPlaying;
  bool bPaused;
  bool bLoop;
  bool bEndReached;
  double movieRate;
  int curMovieFrame;
  int frameCount;

  CRITICAL_SECTION critSection;
  std::unique_ptr<IMediaSample, std::function<void(IMediaSample*)>>
      backSample;
  std::unique_ptr<IMediaSample, std::function<void(IMediaSample*)>>
      middleSample;
  imageStruct pixels;
  int pixelFormat;
};
1177
1178
1179
1180
1181
1182
1183
1184
1185


//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// GEM SPECIFIC IMPLEMENTATION BELOW
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------

using namespace gem::plugins;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1186
REGISTER_FILMFACTORY("DirectShow", filmDS);
1187
1188
1189
filmDS::filmDS()
{
  player = NULL;
1190
1191
}

1192
1193
1194
1195
filmDS::~filmDS()
{
  close();
}
1196

1197
bool filmDS::open(const std::string&path, const gem::Properties&props)
1198
1199
1200
{
  close();
  player = new DirectShowVideo();
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1201
  bool res=player->loadMovie(path, GEM_RGBA);
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1202
  if(res) {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1203
    player->setPosition(0);
1204
    double d=0.;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1205
    if(props.get("auto", d)) {
1206
1207
1208
1209
1210
1211
      if(d>-1e8 && d<1e8)
        player->setPaused(true);
      else {
        player->setSpeed(d);
        player->play();
      }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1212
    }
1213
1214
  } else {
    close();
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1215
  }
1216
1217

  return res;
1218
}
1219

1220
1221
1222
1223
1224
1225
void filmDS::close()
{
  if( player ) {
    delete player;
    player = NULL;
  }
1226
1227
}

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1228
pixBlock*filmDS::getFrame(void)
1229
{
1230
  if(!(player && player->isLoaded())) {
1231
    return 0;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1232
  }
1233
#if 0
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1234
  if(player->isMovieDone()) {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1235
    return NULL;
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1236
  }
1237
#endif
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1238

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1239
1240
1241
  m_image.newfilm=false;
  m_image.newimage=false;

IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1242
1243
  int w=player->getWidth();
  int h=player->getHeight();
1244
1245
1246
  if(w!=m_image.image.xsize || h!=m_image.image.ysize) {
    m_image.image.xsize=w;
    m_image.image.ysize=h;
1247
    m_image.image.setCsizeByFormat(GEM_RGBA);
1248
1249
1250
1251
1252
    m_image.image.reallocate();

    m_image.newfilm=true;
    //printf("getting new film\n");
  }
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1253
  bool res=player->getPixels(m_image.image);
1254
  if(res) {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
1255
    m_image.newimage=true;