Merge pull request #16948 from alalek:warning_16739
[platform/upstream/opencv.git] / modules / videoio / src / cap_msmf.cpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html
4
5 #include "precomp.hpp"
6 #if defined _WIN32 && defined HAVE_MSMF
7 /*
8    Media Foundation-based Video Capturing module is based on
9    videoInput library by Evgeny Pereguda:
10    http://www.codeproject.com/Articles/559437/Capturing-of-video-from-web-camera-on-Windows-7-an
11    Originally licensed under The Code Project Open License (CPOL) 1.02:
12    http://www.codeproject.com/info/cpol10.aspx
13 */
14 //require Windows 8 for some of the formats defined otherwise could baseline on lower version
15 #if WINVER < _WIN32_WINNT_WIN8
16 #undef WINVER
17 #define WINVER _WIN32_WINNT_WIN8
18 #endif
19 #include <windows.h>
20 #include <guiddef.h>
21 #include <mfidl.h>
22 #include <mfapi.h>
23 #include <mfplay.h>
24 #include <mfobjects.h>
25 #include <tchar.h>
26 #include <strsafe.h>
27 #include <Mfreadwrite.h>
28 #ifdef HAVE_MSMF_DXVA
29 #include <d3d11.h>
30 #include <d3d11_4.h>
31 #endif
32 #include <new>
33 #include <map>
34 #include <vector>
35 #include <string>
36 #include <algorithm>
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include <string.h>
40
41 #ifdef _MSC_VER
42 #pragma warning(disable:4503)
43 #pragma comment(lib, "mfplat")
44 #pragma comment(lib, "mf")
45 #pragma comment(lib, "mfuuid")
46 #pragma comment(lib, "Strmiids")
47 #pragma comment(lib, "Mfreadwrite")
48 #ifdef HAVE_MSMF_DXVA
49 #pragma comment(lib, "d3d11")
50 // MFCreateDXGIDeviceManager() is available since Win8 only.
51 // To avoid OpenCV loading failure on Win7 use dynamic detection of this symbol.
52 // Details: https://github.com/opencv/opencv/issues/11858
53 typedef HRESULT (WINAPI *FN_MFCreateDXGIDeviceManager)(UINT *resetToken, IMFDXGIDeviceManager **ppDeviceManager);
54 static bool pMFCreateDXGIDeviceManager_initialized = false;
55 static FN_MFCreateDXGIDeviceManager pMFCreateDXGIDeviceManager = NULL;
56 static void init_MFCreateDXGIDeviceManager()
57 {
58     HMODULE h = LoadLibraryExA("mfplat.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
59     if (h)
60     {
61         pMFCreateDXGIDeviceManager = (FN_MFCreateDXGIDeviceManager)GetProcAddress(h, "MFCreateDXGIDeviceManager");
62     }
63     pMFCreateDXGIDeviceManager_initialized = true;
64 }
65 #endif
66 #pragma comment(lib, "Shlwapi.lib")
67 #endif
68
69 #include <mferror.h>
70
71 #include <comdef.h>
72
73 #include <shlwapi.h>  // QISearch
74
75 struct IMFMediaType;
76 struct IMFActivate;
77 struct IMFMediaSource;
78 struct IMFAttributes;
79
80 namespace
81 {
82
83 template <class T>
84 class ComPtr
85 {
86 public:
87     ComPtr()
88     {
89     }
90     ComPtr(T* lp)
91     {
92         p = lp;
93     }
94     ComPtr(_In_ const ComPtr<T>& lp)
95     {
96         p = lp.p;
97     }
98     virtual ~ComPtr()
99     {
100     }
101
102     T** operator&()
103     {
104         CV_Assert(p == NULL);
105         return p.operator&();
106     }
107     T* operator->() const
108     {
109         CV_Assert(p != NULL);
110         return p.operator->();
111     }
112     operator bool()
113     {
114         return p.operator!=(NULL);
115     }
116
117     T* Get() const
118     {
119         return p;
120     }
121
122     void Release()
123     {
124         if (p)
125             p.Release();
126     }
127
128     // query for U interface
129     template<typename U>
130     HRESULT As(_Out_ ComPtr<U>& lp) const
131     {
132         lp.Release();
133         return p->QueryInterface(__uuidof(U), reinterpret_cast<void**>((T**)&lp));
134     }
135 private:
136     _COM_SMARTPTR_TYPEDEF(T, __uuidof(T));
137     TPtr p;
138 };
139
140 #define _ComPtr ComPtr
141
142 template <typename T> inline T absDiff(T a, T b) { return a >= b ? a - b : b - a; }
143
144 //==================================================================================================
145
146 // Structure for collecting info about types of video which are supported by current video device
147 struct MediaType
148 {
149     UINT32 width;
150     UINT32 height;
151     INT32 stride; // stride is negative if image is bottom-up
152     UINT32 isFixedSize;
153     UINT32 frameRateNum;
154     UINT32 frameRateDenom;
155     UINT32 aspectRatioNum;
156     UINT32 aspectRatioDenom;
157     UINT32 sampleSize;
158     UINT32 interlaceMode;
159     GUID majorType; // video or audio
160     GUID subType; // fourCC
161     MediaType(IMFMediaType *pType = 0) :
162         width(0), height(0),
163         stride(0),
164         isFixedSize(true),
165         frameRateNum(1), frameRateDenom(1),
166         aspectRatioNum(1), aspectRatioDenom(1),
167         sampleSize(0),
168         interlaceMode(0),
169         majorType(MFMediaType_Video),
170         subType({ 0 })
171     {
172         if (pType)
173         {
174             MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &width, &height);
175             pType->GetUINT32(MF_MT_DEFAULT_STRIDE, (UINT32*)&stride); // value is stored as UINT32 but should be casted to INT3)
176             pType->GetUINT32(MF_MT_FIXED_SIZE_SAMPLES, &isFixedSize);
177             MFGetAttributeRatio(pType, MF_MT_FRAME_RATE, &frameRateNum, &frameRateDenom);
178             MFGetAttributeRatio(pType, MF_MT_PIXEL_ASPECT_RATIO, &aspectRatioNum, &aspectRatioDenom);
179             pType->GetUINT32(MF_MT_SAMPLE_SIZE, &sampleSize);
180             pType->GetUINT32(MF_MT_INTERLACE_MODE, &interlaceMode);
181             pType->GetGUID(MF_MT_MAJOR_TYPE, &majorType);
182             pType->GetGUID(MF_MT_SUBTYPE, &subType);
183         }
184     }
185     static MediaType createDefault()
186     {
187         MediaType res;
188         res.width = 640;
189         res.height = 480;
190         res.setFramerate(30.0);
191         return res;
192     }
193     inline bool isEmpty() const
194     {
195         return width == 0 && height == 0;
196     }
197     _ComPtr<IMFMediaType> createMediaType() const
198     {
199         _ComPtr<IMFMediaType> res;
200         MFCreateMediaType(&res);
201         if (width != 0 || height != 0)
202             MFSetAttributeSize(res.Get(), MF_MT_FRAME_SIZE, width, height);
203         if (stride != 0)
204             res->SetUINT32(MF_MT_DEFAULT_STRIDE, stride);
205         res->SetUINT32(MF_MT_FIXED_SIZE_SAMPLES, isFixedSize);
206         if (frameRateNum != 0 || frameRateDenom != 0)
207             MFSetAttributeRatio(res.Get(), MF_MT_FRAME_RATE, frameRateNum, frameRateDenom);
208         if (aspectRatioNum != 0 || aspectRatioDenom != 0)
209             MFSetAttributeRatio(res.Get(), MF_MT_PIXEL_ASPECT_RATIO, aspectRatioNum, aspectRatioDenom);
210         if (sampleSize > 0)
211             res->SetUINT32(MF_MT_SAMPLE_SIZE, sampleSize);
212         res->SetUINT32(MF_MT_INTERLACE_MODE, interlaceMode);
213         if (majorType != GUID())
214             res->SetGUID(MF_MT_MAJOR_TYPE, majorType);
215         if (subType != GUID())
216             res->SetGUID(MF_MT_SUBTYPE, subType);
217         return res;
218     }
219     void setFramerate(double fps)
220     {
221         frameRateNum = (UINT32)cvRound(fps * 1000.0);
222         frameRateDenom = 1000;
223     }
224     double getFramerate() const
225     {
226         return frameRateDenom != 0 ? ((double)frameRateNum) / ((double)frameRateDenom) : 0;
227     }
228     LONGLONG getFrameStep() const
229     {
230         const double fps = getFramerate();
231         return (LONGLONG)(fps > 0 ? 1e7 / fps : 0);
232     }
233     inline unsigned long resolutionDiff(const MediaType& other) const
234     {
235         const unsigned long wdiff = absDiff(width, other.width);
236         const unsigned long hdiff = absDiff(height, other.height);
237         return wdiff + hdiff;
238     }
239     // check if 'this' is better than 'other' comparing to reference
240     bool isBetterThan(const MediaType& other, const MediaType& ref) const
241     {
242         const unsigned long thisDiff = resolutionDiff(ref);
243         const unsigned long otherDiff = other.resolutionDiff(ref);
244         if (thisDiff < otherDiff)
245             return true;
246         if (thisDiff == otherDiff)
247         {
248             if (width > other.width)
249                 return true;
250             if (width == other.width && height > other.height)
251                 return true;
252             if (width == other.width && height == other.height)
253             {
254                 const double thisRateDiff = absDiff(getFramerate(), ref.getFramerate());
255                 const double otherRateDiff = absDiff(other.getFramerate(), ref.getFramerate());
256                 if (thisRateDiff < otherRateDiff)
257                     return true;
258             }
259         }
260         return false;
261     }
262 };
263
264 void printFormat(std::ostream& out, const GUID& fmt)
265 {
266 #define PRNT(FMT) else if (fmt == FMT) out << #FMT;
267     if (fmt == MFVideoFormat_Base) out << "Base";
268     PRNT(MFVideoFormat_RGB32)
269     PRNT(MFVideoFormat_ARGB32)
270     PRNT(MFVideoFormat_RGB24)
271     PRNT(MFVideoFormat_RGB555)
272     PRNT(MFVideoFormat_RGB565)
273     PRNT(MFVideoFormat_RGB8)
274     else
275     {
276         char fourcc[5] = { 0 };
277         memcpy(fourcc, &fmt.Data1, 4);
278         out << fourcc;
279     }
280 #undef PRNT
281 }
282
283 std::ostream& operator<<(std::ostream& out, const MediaType& mt)
284 {
285     out << "(" << mt.width << "x" << mt.height << " @ " << mt.getFramerate() << ") ";
286     printFormat(out, mt.subType);
287     return out;
288 }
289
290 //==================================================================================================
291
292 // Class for creating of Media Foundation context
293 class Media_Foundation
294 {
295 public:
296     ~Media_Foundation(void) { /*CV_Assert(SUCCEEDED(MFShutdown()));*/ CoUninitialize(); }
297     static Media_Foundation& getInstance()
298     {
299         static Media_Foundation instance;
300         return instance;
301     }
302 private:
303     Media_Foundation(void) { CoInitialize(0); CV_Assert(SUCCEEDED(MFStartup(MF_VERSION))); }
304 };
305
306 //==================================================================================================
307
308 class SourceReaderCB : public IMFSourceReaderCallback
309 {
310 public:
311     SourceReaderCB() :
312         m_nRefCount(0), m_hEvent(CreateEvent(NULL, FALSE, FALSE, NULL)), m_bEOS(FALSE), m_hrStatus(S_OK), m_reader(NULL), m_dwStreamIndex(0)
313     {
314     }
315
316     // IUnknown methods
317     STDMETHODIMP QueryInterface(REFIID iid, void** ppv) CV_OVERRIDE
318     {
319 #ifdef _MSC_VER
320 #pragma warning(push)
321 #pragma warning(disable:4838)
322 #endif
323         static const QITAB qit[] =
324         {
325             QITABENT(SourceReaderCB, IMFSourceReaderCallback),
326             { 0 },
327         };
328 #ifdef _MSC_VER
329 #pragma warning(pop)
330 #endif
331         return QISearch(this, qit, iid, ppv);
332     }
333     STDMETHODIMP_(ULONG) AddRef() CV_OVERRIDE
334     {
335         return InterlockedIncrement(&m_nRefCount);
336     }
337     STDMETHODIMP_(ULONG) Release() CV_OVERRIDE
338     {
339         ULONG uCount = InterlockedDecrement(&m_nRefCount);
340         if (uCount == 0)
341         {
342             delete this;
343         }
344         return uCount;
345     }
346
347     STDMETHODIMP OnReadSample(HRESULT hrStatus, DWORD dwStreamIndex, DWORD dwStreamFlags, LONGLONG llTimestamp, IMFSample *pSample) CV_OVERRIDE
348     {
349         CV_UNUSED(llTimestamp);
350
351         HRESULT hr = 0;
352         cv::AutoLock lock(m_mutex);
353
354         if (SUCCEEDED(hrStatus))
355         {
356             if (pSample)
357             {
358                 CV_LOG_DEBUG(NULL, "videoio(MSMF): got frame at " << llTimestamp);
359                 if (m_lastSample.Get())
360                 {
361                     CV_LOG_DEBUG(NULL, "videoio(MSMF): drop frame (not processed)");
362                 }
363                 m_lastSample = pSample;
364             }
365         }
366         else
367         {
368             CV_LOG_WARNING(NULL, "videoio(MSMF): OnReadSample() is called with error status: " << hrStatus);
369         }
370
371         if (MF_SOURCE_READERF_ENDOFSTREAM & dwStreamFlags)
372         {
373             // Reached the end of the stream.
374             m_bEOS = true;
375         }
376         m_hrStatus = hrStatus;
377
378         if (FAILED(hr = m_reader->ReadSample(dwStreamIndex, 0, NULL, NULL, NULL, NULL)))
379         {
380             CV_LOG_WARNING(NULL, "videoio(MSMF): async ReadSample() call is failed with error status: " << hr);
381             m_bEOS = true;
382         }
383
384         if (pSample || m_bEOS)
385         {
386             SetEvent(m_hEvent);
387         }
388         return S_OK;
389     }
390
391     STDMETHODIMP OnEvent(DWORD, IMFMediaEvent *) CV_OVERRIDE
392     {
393         return S_OK;
394     }
395     STDMETHODIMP OnFlush(DWORD) CV_OVERRIDE
396     {
397         return S_OK;
398     }
399
400     HRESULT Wait(DWORD dwMilliseconds, _ComPtr<IMFSample>& videoSample, BOOL& pbEOS)
401     {
402         pbEOS = FALSE;
403
404         DWORD dwResult = WaitForSingleObject(m_hEvent, dwMilliseconds);
405         if (dwResult == WAIT_TIMEOUT)
406         {
407             return E_PENDING;
408         }
409         else if (dwResult != WAIT_OBJECT_0)
410         {
411             return HRESULT_FROM_WIN32(GetLastError());
412         }
413
414         pbEOS = m_bEOS;
415         if (!pbEOS)
416         {
417             cv::AutoLock lock(m_mutex);
418             videoSample = m_lastSample;
419             CV_Assert(videoSample);
420             m_lastSample.Release();
421             ResetEvent(m_hEvent);  // event is auto-reset, but we need this forced reset due time gap between wait() and mutex hold.
422         }
423
424         return m_hrStatus;
425     }
426 private:
427     // Destructor is private. Caller should call Release.
428     virtual ~SourceReaderCB()
429     {
430         CV_LOG_WARNING(NULL, "terminating async callback");
431     }
432
433 public:
434     long                m_nRefCount;        // Reference count.
435     cv::Mutex           m_mutex;
436     HANDLE              m_hEvent;
437     BOOL                m_bEOS;
438     HRESULT             m_hrStatus;
439
440     IMFSourceReader *m_reader;
441     DWORD m_dwStreamIndex;
442     _ComPtr<IMFSample>  m_lastSample;
443 };
444
445 //==================================================================================================
446
447 // Enumerate and store supported formats and finds format which is most similar to the one requested
448 class FormatStorage
449 {
450 public:
451     struct MediaID
452     {
453         DWORD stream;
454         DWORD media;
455         MediaID() : stream(0), media(0) {}
456         void nextStream()
457         {
458             stream++;
459             media = 0;
460         }
461         void nextMedia()
462         {
463             media++;
464         }
465         bool operator<(const MediaID& other) const
466         {
467             return (stream < other.stream) || (stream == other.stream && media < other.media);
468         }
469     };
470     void read(IMFSourceReader* source)
471     {
472         HRESULT hr = S_OK;
473         MediaID cur;
474         while (SUCCEEDED(hr))
475         {
476             _ComPtr<IMFMediaType> raw_type;
477             hr = source->GetNativeMediaType(cur.stream, cur.media, &raw_type);
478             if (hr == MF_E_NO_MORE_TYPES)
479             {
480                 hr = S_OK;
481                 cur.nextStream();
482             }
483             else if (SUCCEEDED(hr))
484             {
485                 formats[cur] = MediaType(raw_type.Get());
486                 cur.nextMedia();
487             }
488         }
489     }
490     std::pair<MediaID, MediaType> findBest(const MediaType& newType)
491     {
492         std::pair<MediaID, MediaType> best;
493         std::map<MediaID, MediaType>::const_iterator i = formats.begin();
494         for (; i != formats.end(); ++i)
495         {
496             if (newType.isEmpty()) // file input - choose first returned media type
497             {
498                 best = *i;
499                 break;
500             }
501             if (best.second.isEmpty() || i->second.isBetterThan(best.second, newType))
502             {
503                 best = *i;
504             }
505         }
506         return best;
507     }
508 private:
509     std::map<MediaID, MediaType> formats;
510 };
511
512 //==================================================================================================
513
514 // Enumerates devices and activates one of them
515 class DeviceList
516 {
517 public:
518     DeviceList() : devices(NULL), count(0) {}
519     ~DeviceList()
520     {
521         if (devices)
522         {
523             for (UINT32 i = 0; i < count; ++i)
524                 if (devices[i])
525                     devices[i]->Release();
526             CoTaskMemFree(devices);
527         }
528     }
529     UINT32 read(IID sourceType = MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID)
530     {
531         _ComPtr<IMFAttributes> attr;
532         if (FAILED(MFCreateAttributes(&attr, 1)) ||
533             FAILED(attr->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, sourceType)))
534         {
535             CV_Error(CV_StsError, "Failed to create attributes");
536         }
537         if (FAILED(MFEnumDeviceSources(attr.Get(), &devices, &count)))
538         {
539             CV_LOG_DEBUG(NULL, "Failed to enumerate MSMF devices");
540             return 0;
541         }
542         return count;
543     }
544     _ComPtr<IMFMediaSource> activateSource(UINT32 index)
545     {
546         _ComPtr<IMFMediaSource> result;
547         if (count == 0 || index >= count || FAILED(devices[index]->ActivateObject(__uuidof(IMFMediaSource), (void**)&result)))
548         {
549             CV_LOG_DEBUG(NULL, "Failed to activate media source (device " << index << ")");
550         }
551         return result;
552     }
553 private:
554     IMFActivate** devices;
555     UINT32 count;
556 };
557
558 } // namespace::
559
560 //==================================================================================================
561
562 /******* Capturing video from camera or file via Microsoft Media Foundation **********/
563 class CvCapture_MSMF : public cv::IVideoCapture
564 {
565 public:
566     typedef enum {
567         MODE_SW = 0,
568         MODE_HW = 1
569     } MSMFCapture_Mode;
570     CvCapture_MSMF();
571     virtual ~CvCapture_MSMF();
572     virtual bool open(int);
573     virtual bool open(const cv::String&);
574     virtual void close();
575     virtual double getProperty(int) const CV_OVERRIDE;
576     virtual bool setProperty(int, double) CV_OVERRIDE;
577     virtual bool grabFrame() CV_OVERRIDE;
578     virtual bool retrieveFrame(int, cv::OutputArray) CV_OVERRIDE;
579     virtual bool isOpened() const CV_OVERRIDE { return isOpen; }
580     virtual int getCaptureDomain() CV_OVERRIDE { return CV_CAP_MSMF; }
581 protected:
582     bool configureOutput(MediaType newType, cv::uint32_t outFormat);
583     bool setTime(double time, bool rough);
584     bool configureHW(bool enable);
585
586     template <typename CtrlT>
587     bool readComplexPropery(long prop, long& val) const;
588     template <typename CtrlT>
589     bool writeComplexProperty(long prop, double val, long flags);
590     _ComPtr<IMFAttributes> getDefaultSourceConfig(UINT32 num = 10);
591     bool initStream(DWORD streamID, const MediaType& mt);
592
593     Media_Foundation& MF;
594     cv::String filename;
595     int camid;
596     MSMFCapture_Mode captureMode;
597 #ifdef HAVE_MSMF_DXVA
598     _ComPtr<ID3D11Device> D3DDev;
599     _ComPtr<IMFDXGIDeviceManager> D3DMgr;
600 #endif
601     _ComPtr<IMFSourceReader> videoFileSource;
602     DWORD dwStreamIndex;
603     MediaType nativeFormat;
604     MediaType captureFormat;
605     int outputFormat;
606     bool convertFormat;
607     MFTIME duration;
608     LONGLONG frameStep;
609     _ComPtr<IMFSample> videoSample;
610     LONGLONG sampleTime;
611     bool isOpen;
612     _ComPtr<IMFSourceReaderCallback> readCallback;  // non-NULL for "live" streams (camera capture)
613 };
614
615 CvCapture_MSMF::CvCapture_MSMF():
616     MF(Media_Foundation::getInstance()),
617     filename(""),
618     camid(-1),
619     captureMode(MODE_SW),
620 #ifdef HAVE_MSMF_DXVA
621     D3DDev(NULL),
622     D3DMgr(NULL),
623 #endif
624     videoFileSource(NULL),
625     videoSample(NULL),
626     outputFormat(CV_CAP_MODE_BGR),
627     convertFormat(true),
628     sampleTime(0),
629     isOpen(false)
630 {
631     configureHW(true);
632 }
633
634 CvCapture_MSMF::~CvCapture_MSMF()
635 {
636     close();
637     configureHW(false);
638 }
639
640 void CvCapture_MSMF::close()
641 {
642     if (isOpen)
643     {
644         isOpen = false;
645         videoSample.Release();
646         videoFileSource.Release();
647         camid = -1;
648         filename.clear();
649     }
650     readCallback.Release();
651 }
652
653 bool CvCapture_MSMF::initStream(DWORD streamID, const MediaType& mt)
654 {
655     CV_LOG_DEBUG(NULL, "Init stream " << streamID << " with MediaType " << mt);
656     _ComPtr<IMFMediaType> mediaTypeOut = mt.createMediaType();
657     if (FAILED(videoFileSource->SetStreamSelection((DWORD)MF_SOURCE_READER_ALL_STREAMS, false)))
658     {
659         CV_LOG_WARNING(NULL, "Failed to reset streams");
660         return false;
661     }
662     if (FAILED(videoFileSource->SetStreamSelection(streamID, true)))
663     {
664         CV_LOG_WARNING(NULL, "Failed to select stream " << streamID);
665         return false;
666     }
667     HRESULT hr = videoFileSource->SetCurrentMediaType(streamID, NULL, mediaTypeOut.Get());
668     if (hr == MF_E_TOPO_CODEC_NOT_FOUND)
669     {
670         CV_LOG_WARNING(NULL, "Failed to set mediaType (stream " << streamID << ", " << mt << "(codec not found)");
671         return false;
672     }
673     else if (hr == MF_E_INVALIDMEDIATYPE)
674     {
675         CV_LOG_WARNING(NULL, "Failed to set mediaType (stream " << streamID << ", " << mt << "(unsupported media type)");
676         return false;
677     }
678     else if (FAILED(hr))
679     {
680         CV_LOG_WARNING(NULL, "Failed to set mediaType (stream " << streamID << ", " << mt << "(HRESULT " << hr << ")");
681         return false;
682     }
683     captureFormat = mt;
684     return true;
685 }
686
687 _ComPtr<IMFAttributes> CvCapture_MSMF::getDefaultSourceConfig(UINT32 num)
688 {
689     CV_Assert(num > 0);
690     _ComPtr<IMFAttributes> res;
691     if (FAILED(MFCreateAttributes(&res, num)) ||
692         FAILED(res->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, true)) ||
693         FAILED(res->SetUINT32(MF_SOURCE_READER_DISABLE_DXVA, false)) ||
694         FAILED(res->SetUINT32(MF_SOURCE_READER_ENABLE_VIDEO_PROCESSING, false)) ||
695         FAILED(res->SetUINT32(MF_SOURCE_READER_ENABLE_ADVANCED_VIDEO_PROCESSING, true))
696         )
697     {
698         CV_Error(CV_StsError, "Failed to create attributes");
699     }
700 #ifdef HAVE_MSMF_DXVA
701     if (D3DMgr)
702     {
703         if (FAILED(res->SetUnknown(MF_SOURCE_READER_D3D_MANAGER, D3DMgr.Get())))
704         {
705             CV_Error(CV_StsError, "Failed to create attributes");
706         }
707     }
708 #endif
709     return res;
710 }
711
712 bool CvCapture_MSMF::configureHW(bool enable)
713 {
714 #ifdef HAVE_MSMF_DXVA
715     if ((enable && D3DMgr && D3DDev) || (!enable && !D3DMgr && !D3DDev))
716         return true;
717     if (!pMFCreateDXGIDeviceManager_initialized)
718         init_MFCreateDXGIDeviceManager();
719     if (enable && !pMFCreateDXGIDeviceManager)
720         return false;
721
722     bool reopen = isOpen;
723     int prevcam = camid;
724     cv::String prevfile = filename;
725     close();
726     if (enable)
727     {
728         D3D_FEATURE_LEVEL levels[] = { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0,
729             D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0,
730             D3D_FEATURE_LEVEL_9_3,  D3D_FEATURE_LEVEL_9_2, D3D_FEATURE_LEVEL_9_1 };
731         if (SUCCEEDED(D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_VIDEO_SUPPORT,
732             levels, sizeof(levels) / sizeof(*levels), D3D11_SDK_VERSION, &D3DDev, NULL, NULL)))
733         {
734             // NOTE: Getting ready for multi-threaded operation
735             _ComPtr<ID3D11Multithread> D3DDevMT;
736             UINT mgrRToken;
737             if (SUCCEEDED(D3DDev->QueryInterface(IID_PPV_ARGS(&D3DDevMT))))
738             {
739                 D3DDevMT->SetMultithreadProtected(TRUE);
740                 D3DDevMT.Release();
741                 if (SUCCEEDED(pMFCreateDXGIDeviceManager(&mgrRToken, &D3DMgr)))
742                 {
743                     if (SUCCEEDED(D3DMgr->ResetDevice(D3DDev.Get(), mgrRToken)))
744                     {
745                         captureMode = MODE_HW;
746                         return reopen ? (prevcam >= 0 ? open(prevcam) : open(prevfile.c_str())) : true;
747                     }
748                     D3DMgr.Release();
749                 }
750             }
751             D3DDev.Release();
752         }
753         return false;
754     }
755     else
756     {
757         if (D3DMgr)
758             D3DMgr.Release();
759         if (D3DDev)
760             D3DDev.Release();
761         captureMode = MODE_SW;
762         return reopen ? (prevcam >= 0 ? open(prevcam) : open(prevfile.c_str())) : true;
763     }
764 #else
765     return !enable;
766 #endif
767 }
768
769 bool CvCapture_MSMF::configureOutput(MediaType newType, cv::uint32_t outFormat)
770 {
771     FormatStorage formats;
772     formats.read(videoFileSource.Get());
773     std::pair<FormatStorage::MediaID, MediaType> bestMatch = formats.findBest(newType);
774     dwStreamIndex = bestMatch.first.stream;
775     nativeFormat = bestMatch.second;
776     MediaType newFormat = nativeFormat;
777     if (convertFormat)
778     {
779         switch (outFormat)
780         {
781         case CV_CAP_MODE_BGR:
782         case CV_CAP_MODE_RGB:
783             newFormat.subType = captureMode == MODE_HW ? MFVideoFormat_RGB32 : MFVideoFormat_RGB24;
784             newFormat.stride = (captureMode == MODE_HW ? 4 : 3) * newFormat.width;
785             newFormat.sampleSize = newFormat.stride * newFormat.height;
786             break;
787         case CV_CAP_MODE_GRAY:
788             newFormat.subType = MFVideoFormat_YUY2;
789             newFormat.stride = newFormat.width;
790             newFormat.sampleSize = newFormat.stride * newFormat.height * 3 / 2;
791             break;
792         case CV_CAP_MODE_YUYV:
793             newFormat.subType = MFVideoFormat_YUY2;
794             newFormat.stride = 2 * newFormat.width;
795             newFormat.sampleSize = newFormat.stride * newFormat.height;
796             break;
797         default:
798             return false;
799         }
800         newFormat.interlaceMode = MFVideoInterlace_Progressive;
801         newFormat.isFixedSize = true;
802         if (nativeFormat.subType == MFVideoFormat_MP43) //Unable to estimate FPS for MP43
803             newFormat.frameRateNum = 0;
804     }
805     // we select native format first and then our requested format (related issue #12822)
806     if (!newType.isEmpty()) // camera input
807         initStream(dwStreamIndex, nativeFormat);
808     return initStream(dwStreamIndex, newFormat);
809 }
810
811 bool CvCapture_MSMF::open(int index)
812 {
813     close();
814     if (index < 0)
815         return false;
816     DeviceList devices;
817     UINT32 count = devices.read();
818     if (count == 0 || static_cast<UINT32>(index) > count)
819     {
820         CV_LOG_DEBUG(NULL, "Device " << index << " not found (total " << count << " devices)");
821         return false;
822     }
823     _ComPtr<IMFAttributes> attr = getDefaultSourceConfig();
824     _ComPtr<IMFSourceReaderCallback> cb = new SourceReaderCB();
825     attr->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, cb.Get());
826     _ComPtr<IMFMediaSource> src = devices.activateSource(index);
827     if (!src.Get() || FAILED(MFCreateSourceReaderFromMediaSource(src.Get(), attr.Get(), &videoFileSource)))
828     {
829         CV_LOG_DEBUG(NULL, "Failed to create source reader");
830         return false;
831     }
832
833     isOpen = true;
834     camid = index;
835     readCallback = cb;
836     duration = 0;
837     if (configureOutput(MediaType::createDefault(), outputFormat))
838     {
839         frameStep = captureFormat.getFrameStep();
840     }
841     return isOpen;
842 }
843
844 bool CvCapture_MSMF::open(const cv::String& _filename)
845 {
846     close();
847     if (_filename.empty())
848         return false;
849
850     // Set source reader parameters
851     _ComPtr<IMFAttributes> attr = getDefaultSourceConfig();
852     cv::AutoBuffer<wchar_t> unicodeFileName(_filename.length() + 1);
853     MultiByteToWideChar(CP_ACP, 0, _filename.c_str(), -1, unicodeFileName.data(), (int)_filename.length() + 1);
854     if (SUCCEEDED(MFCreateSourceReaderFromURL(unicodeFileName.data(), attr.Get(), &videoFileSource)))
855     {
856         isOpen = true;
857         sampleTime = 0;
858         if (configureOutput(MediaType(), outputFormat))
859         {
860             frameStep = captureFormat.getFrameStep();
861             filename = _filename;
862             PROPVARIANT var;
863             HRESULT hr;
864             if (SUCCEEDED(hr = videoFileSource->GetPresentationAttribute((DWORD)MF_SOURCE_READER_MEDIASOURCE, MF_PD_DURATION, &var)) &&
865                 var.vt == VT_UI8)
866             {
867                 duration = var.uhVal.QuadPart;
868                 PropVariantClear(&var);
869             }
870             else
871                 duration = 0;
872         }
873     }
874
875     return isOpen;
876 }
877
878 bool CvCapture_MSMF::grabFrame()
879 {
880     CV_TRACE_FUNCTION();
881     if (readCallback)  // async "live" capture mode
882     {
883         HRESULT hr = 0;
884         SourceReaderCB* reader = ((SourceReaderCB*)readCallback.Get());
885         if (!reader->m_reader)
886         {
887             // Initiate capturing with async callback
888             reader->m_reader = videoFileSource.Get();
889             reader->m_dwStreamIndex = dwStreamIndex;
890             if (FAILED(hr = videoFileSource->ReadSample(dwStreamIndex, 0, NULL, NULL, NULL, NULL)))
891             {
892                 CV_LOG_ERROR(NULL, "videoio(MSMF): can't grab frame - initial async ReadSample() call failed: " << hr);
893                 reader->m_reader = NULL;
894                 return false;
895             }
896         }
897         BOOL bEOS = false;
898         if (FAILED(hr = reader->Wait(10000, videoSample, bEOS)))  // 10 sec
899         {
900             CV_LOG_WARNING(NULL, "videoio(MSMF): can't grab frame. Error: " << hr);
901             return false;
902         }
903         if (bEOS)
904         {
905             CV_LOG_WARNING(NULL, "videoio(MSMF): EOS signal. Capture stream is lost");
906             return false;
907         }
908         return true;
909     }
910     else if (isOpen)
911     {
912         DWORD streamIndex, flags;
913         videoSample.Release();
914         HRESULT hr;
915         for(;;)
916         {
917             CV_TRACE_REGION("ReadSample");
918             if (!SUCCEEDED(hr = videoFileSource->ReadSample(
919                 dwStreamIndex, // Stream index.
920                 0,             // Flags.
921                 &streamIndex,  // Receives the actual stream index.
922                 &flags,        // Receives status flags.
923                 &sampleTime,   // Receives the time stamp.
924                 &videoSample   // Receives the sample or NULL.
925             )))
926                 break;
927             if (streamIndex != dwStreamIndex)
928                 break;
929             if (flags & (MF_SOURCE_READERF_ERROR | MF_SOURCE_READERF_ALLEFFECTSREMOVED | MF_SOURCE_READERF_ENDOFSTREAM))
930                 break;
931             if (videoSample)
932                 break;
933             if (flags & MF_SOURCE_READERF_STREAMTICK)
934             {
935                 CV_LOG_DEBUG(NULL, "videoio(MSMF): Stream tick detected. Retrying to grab the frame");
936             }
937         }
938
939         if (SUCCEEDED(hr))
940         {
941             if (streamIndex != dwStreamIndex)
942             {
943                 CV_LOG_DEBUG(NULL, "videoio(MSMF): Wrong stream read. Abort capturing");
944                 close();
945             }
946             else if (flags & MF_SOURCE_READERF_ERROR)
947             {
948                 CV_LOG_DEBUG(NULL, "videoio(MSMF): Stream reading error. Abort capturing");
949                 close();
950             }
951             else if (flags & MF_SOURCE_READERF_ALLEFFECTSREMOVED)
952             {
953                 CV_LOG_DEBUG(NULL, "videoio(MSMF): Stream decoding error. Abort capturing");
954                 close();
955             }
956             else if (flags & MF_SOURCE_READERF_ENDOFSTREAM)
957             {
958                 sampleTime += frameStep;
959                 CV_LOG_DEBUG(NULL, "videoio(MSMF): End of stream detected");
960             }
961             else
962             {
963                 sampleTime += frameStep;
964                 if (flags & MF_SOURCE_READERF_NEWSTREAM)
965                 {
966                     CV_LOG_DEBUG(NULL, "videoio(MSMF): New stream detected");
967                 }
968                 if (flags & MF_SOURCE_READERF_NATIVEMEDIATYPECHANGED)
969                 {
970                     CV_LOG_DEBUG(NULL, "videoio(MSMF): Stream native media type changed");
971                 }
972                 if (flags & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED)
973                 {
974                     CV_LOG_DEBUG(NULL, "videoio(MSMF): Stream current media type changed");
975                 }
976                 return true;
977             }
978         }
979     }
980     return false;
981 }
982
983 bool CvCapture_MSMF::retrieveFrame(int, cv::OutputArray frame)
984 {
985     CV_TRACE_FUNCTION();
986     do
987     {
988         if (!videoSample)
989             break;
990
991         _ComPtr<IMFMediaBuffer> buf = NULL;
992
993         CV_TRACE_REGION("get_contiguous_buffer");
994         if (!SUCCEEDED(videoSample->ConvertToContiguousBuffer(&buf)))
995         {
996             CV_TRACE_REGION("get_buffer");
997             DWORD bcnt = 0;
998             if (!SUCCEEDED(videoSample->GetBufferCount(&bcnt)))
999                 break;
1000             if (bcnt == 0)
1001                 break;
1002             if (!SUCCEEDED(videoSample->GetBufferByIndex(0, &buf)))
1003                 break;
1004         }
1005
1006         bool lock2d = false;
1007         BYTE* ptr = NULL;
1008         LONG pitch = 0;
1009         DWORD maxsize = 0, cursize = 0;
1010
1011         // "For 2-D buffers, the Lock2D method is more efficient than the Lock method"
1012         // see IMFMediaBuffer::Lock method documentation: https://msdn.microsoft.com/en-us/library/windows/desktop/bb970366(v=vs.85).aspx
1013         _ComPtr<IMF2DBuffer> buffer2d;
1014         if (convertFormat)
1015         {
1016             if (SUCCEEDED(buf.As<IMF2DBuffer>(buffer2d)))
1017             {
1018                 CV_TRACE_REGION_NEXT("lock2d");
1019                 if (SUCCEEDED(buffer2d->Lock2D(&ptr, &pitch)))
1020                 {
1021                     lock2d = true;
1022                 }
1023             }
1024         }
1025         if (ptr == NULL)
1026         {
1027             CV_Assert(lock2d == false);
1028             CV_TRACE_REGION_NEXT("lock");
1029             if (!SUCCEEDED(buf->Lock(&ptr, &maxsize, &cursize)))
1030             {
1031                 break;
1032             }
1033         }
1034         if (!ptr)
1035             break;
1036         if (convertFormat)
1037         {
1038             if (lock2d || (unsigned int)cursize == captureFormat.sampleSize)
1039             {
1040                 switch (outputFormat)
1041                 {
1042                 case CV_CAP_MODE_YUYV:
1043                     cv::Mat(captureFormat.height, captureFormat.width, CV_8UC2, ptr, pitch).copyTo(frame);
1044                     break;
1045                 case CV_CAP_MODE_BGR:
1046                     if (captureMode == MODE_HW)
1047                         cv::cvtColor(cv::Mat(captureFormat.height, captureFormat.width, CV_8UC4, ptr, pitch), frame, cv::COLOR_BGRA2BGR);
1048                     else
1049                         cv::Mat(captureFormat.height, captureFormat.width, CV_8UC3, ptr, pitch).copyTo(frame);
1050                     break;
1051                 case CV_CAP_MODE_RGB:
1052                     if (captureMode == MODE_HW)
1053                         cv::cvtColor(cv::Mat(captureFormat.height, captureFormat.width, CV_8UC4, ptr, pitch), frame, cv::COLOR_BGRA2BGR);
1054                     else
1055                         cv::cvtColor(cv::Mat(captureFormat.height, captureFormat.width, CV_8UC3, ptr, pitch), frame, cv::COLOR_BGR2RGB);
1056                     break;
1057                 case CV_CAP_MODE_GRAY:
1058                     cv::Mat(captureFormat.height, captureFormat.width, CV_8UC1, ptr, pitch).copyTo(frame);
1059                     break;
1060                 default:
1061                     frame.release();
1062                     break;
1063                 }
1064             }
1065             else
1066                 frame.release();
1067         }
1068         else
1069         {
1070             cv::Mat(1, cursize, CV_8UC1, ptr, pitch).copyTo(frame);
1071         }
1072         CV_TRACE_REGION_NEXT("unlock");
1073         if (lock2d)
1074             buffer2d->Unlock2D();
1075         else
1076             buf->Unlock();
1077         return !frame.empty();
1078     } while (0);
1079
1080     frame.release();
1081     return false;
1082 }
1083
1084 bool CvCapture_MSMF::setTime(double time, bool rough)
1085 {
1086     PROPVARIANT var;
1087     if (SUCCEEDED(videoFileSource->GetPresentationAttribute((DWORD)MF_SOURCE_READER_MEDIASOURCE, MF_SOURCE_READER_MEDIASOURCE_CHARACTERISTICS, &var)) &&
1088         var.vt == VT_UI4 && var.ulVal & MFMEDIASOURCE_CAN_SEEK)
1089     {
1090         videoSample.Release();
1091         bool useGrabbing = time > 0 && !rough && !(var.ulVal & MFMEDIASOURCE_HAS_SLOW_SEEK);
1092         PropVariantClear(&var);
1093         sampleTime = (useGrabbing && time >= frameStep) ? (LONGLONG)floor(time + 0.5) - frameStep : (LONGLONG)floor(time + 0.5);
1094         var.vt = VT_I8;
1095         var.hVal.QuadPart = sampleTime;
1096         bool resOK = SUCCEEDED(videoFileSource->SetCurrentPosition(GUID_NULL, var));
1097         PropVariantClear(&var);
1098         if (resOK && useGrabbing)
1099         {
1100             LONGLONG timeborder = (LONGLONG)floor(time + 0.5) - frameStep / 2;
1101             do { resOK = grabFrame(); videoSample.Release(); } while (resOK && sampleTime < timeborder);
1102         }
1103         return resOK;
1104     }
1105     return false;
1106 }
1107
1108 template <typename CtrlT>
1109 bool CvCapture_MSMF::readComplexPropery(long prop, long & val) const
1110 {
1111     _ComPtr<CtrlT> ctrl;
1112     if (FAILED(videoFileSource->GetServiceForStream((DWORD)MF_SOURCE_READER_MEDIASOURCE, GUID_NULL, IID_PPV_ARGS(&ctrl))))
1113     {
1114         CV_LOG_DEBUG(NULL, "Failed to get service for stream");
1115         return false;
1116     }
1117     long paramVal, paramFlag;
1118     if (FAILED(ctrl->Get(prop, &paramVal, &paramFlag)))
1119     {
1120         CV_LOG_DEBUG(NULL, "Failed to get property " << prop);
1121         // we continue
1122     }
1123     // fallback - get default value
1124     long minVal, maxVal, stepVal;
1125     if (FAILED(ctrl->GetRange(prop, &minVal, &maxVal, &stepVal, &paramVal, &paramFlag)))
1126     {
1127         CV_LOG_DEBUG(NULL, "Failed to get default value for property " << prop);
1128         return false;
1129     }
1130     val = paramVal;
1131     return true;
1132 }
1133
1134 double CvCapture_MSMF::getProperty( int property_id ) const
1135 {
1136     long cVal = 0;
1137     if (isOpen)
1138         switch (property_id)
1139         {
1140         case CV_CAP_PROP_FORMAT:
1141                 return outputFormat;
1142         case CV_CAP_PROP_MODE:
1143                 return captureMode;
1144         case CV_CAP_PROP_CONVERT_RGB:
1145                 return convertFormat ? 1 : 0;
1146         case CV_CAP_PROP_SAR_NUM:
1147                 return captureFormat.aspectRatioNum;
1148         case CV_CAP_PROP_SAR_DEN:
1149                 return captureFormat.aspectRatioDenom;
1150         case CV_CAP_PROP_FRAME_WIDTH:
1151             return captureFormat.width;
1152         case CV_CAP_PROP_FRAME_HEIGHT:
1153             return captureFormat.height;
1154         case CV_CAP_PROP_FOURCC:
1155             return captureFormat.subType.Data1;
1156         case CV_CAP_PROP_FPS:
1157             return captureFormat.getFramerate();
1158         case CV_CAP_PROP_FRAME_COUNT:
1159             if (duration != 0)
1160                 return floor(((double)duration / 1e7)* captureFormat.getFramerate() + 0.5);
1161             else
1162                 break;
1163         case CV_CAP_PROP_POS_FRAMES:
1164             return floor(((double)sampleTime / 1e7)* captureFormat.getFramerate() + 0.5);
1165         case CV_CAP_PROP_POS_MSEC:
1166             return (double)sampleTime / 1e4;
1167         case CV_CAP_PROP_POS_AVI_RATIO:
1168             if (duration != 0)
1169                 return (double)sampleTime / duration;
1170             else
1171                 break;
1172         case CV_CAP_PROP_BRIGHTNESS:
1173             if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Brightness, cVal))
1174                 return cVal;
1175             break;
1176         case CV_CAP_PROP_CONTRAST:
1177             if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Contrast, cVal))
1178                 return cVal;
1179             break;
1180         case CV_CAP_PROP_SATURATION:
1181             if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Saturation, cVal))
1182                 return cVal;
1183             break;
1184         case CV_CAP_PROP_HUE:
1185             if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Hue, cVal))
1186                 return cVal;
1187             break;
1188         case CV_CAP_PROP_GAIN:
1189             if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Gain, cVal))
1190                 return cVal;
1191             break;
1192         case CV_CAP_PROP_SHARPNESS:
1193             if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Sharpness, cVal))
1194                 return cVal;
1195             break;
1196         case CV_CAP_PROP_GAMMA:
1197             if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Gamma, cVal))
1198                 return cVal;
1199             break;
1200         case CV_CAP_PROP_BACKLIGHT:
1201             if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_BacklightCompensation, cVal))
1202                 return cVal;
1203             break;
1204         case CV_CAP_PROP_MONOCHROME:
1205             if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_ColorEnable, cVal))
1206                 return cVal == 0 ? 1 : 0;
1207             break;
1208         case CV_CAP_PROP_TEMPERATURE:
1209             if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_WhiteBalance, cVal))
1210                 return cVal;
1211             break;
1212         case CV_CAP_PROP_PAN:
1213             if (readComplexPropery<IAMCameraControl>(CameraControl_Pan, cVal))
1214                 return cVal;
1215             break;
1216         case CV_CAP_PROP_TILT:
1217             if (readComplexPropery<IAMCameraControl>(CameraControl_Tilt, cVal))
1218                 return cVal;
1219             break;
1220         case CV_CAP_PROP_ROLL:
1221             if (readComplexPropery<IAMCameraControl>(CameraControl_Roll, cVal))
1222                 return cVal;
1223             break;
1224         case CV_CAP_PROP_IRIS:
1225             if (readComplexPropery<IAMCameraControl>(CameraControl_Iris, cVal))
1226                 return cVal;
1227             break;
1228         case CV_CAP_PROP_EXPOSURE:
1229         case CV_CAP_PROP_AUTO_EXPOSURE:
1230             if (readComplexPropery<IAMCameraControl>(CameraControl_Exposure, cVal))
1231             {
1232                 if (property_id == CV_CAP_PROP_EXPOSURE)
1233                     return cVal;
1234                 else
1235                     return cVal == VideoProcAmp_Flags_Auto;
1236             }
1237             break;
1238         case CV_CAP_PROP_ZOOM:
1239             if (readComplexPropery<IAMCameraControl>(CameraControl_Zoom, cVal))
1240                 return cVal;
1241             break;
1242         case CV_CAP_PROP_FOCUS:
1243         case CV_CAP_PROP_AUTOFOCUS:
1244             if (readComplexPropery<IAMCameraControl>(CameraControl_Focus, cVal))
1245             {
1246                 if (property_id == CV_CAP_PROP_FOCUS)
1247                     return cVal;
1248                 else
1249                     return cVal == VideoProcAmp_Flags_Auto;
1250             }
1251             break;
1252         case CV_CAP_PROP_WHITE_BALANCE_BLUE_U:
1253         case CV_CAP_PROP_WHITE_BALANCE_RED_V:
1254         case CV_CAP_PROP_RECTIFICATION:
1255         case CV_CAP_PROP_TRIGGER:
1256         case CV_CAP_PROP_TRIGGER_DELAY:
1257         case CV_CAP_PROP_GUID:
1258         case CV_CAP_PROP_ISO_SPEED:
1259         case CV_CAP_PROP_SETTINGS:
1260         case CV_CAP_PROP_BUFFERSIZE:
1261         default:
1262             break;
1263         }
1264     return -1;
1265 }
1266
1267 template <typename CtrlT>
1268 bool CvCapture_MSMF::writeComplexProperty(long prop, double val, long flags)
1269 {
1270     _ComPtr<CtrlT> ctrl;
1271     if (FAILED(videoFileSource->GetServiceForStream((DWORD)MF_SOURCE_READER_MEDIASOURCE, GUID_NULL, IID_PPV_ARGS(&ctrl))))
1272     {
1273         CV_LOG_DEBUG(NULL, "Failed get service for stream");
1274         return false;
1275     }
1276     if (FAILED(ctrl->Set(prop, (long)val, flags)))
1277     {
1278         CV_LOG_DEBUG(NULL, "Failed to set property " << prop);
1279         return false;
1280     }
1281     return true;
1282 }
1283
1284 bool CvCapture_MSMF::setProperty( int property_id, double value )
1285 {
1286     MediaType newFormat = captureFormat;
1287     if (isOpen)
1288         switch (property_id)
1289         {
1290         case CV_CAP_PROP_MODE:
1291             switch ((MSMFCapture_Mode)((int)value))
1292             {
1293             case MODE_SW:
1294                 return configureHW(false);
1295             case MODE_HW:
1296                 return configureHW(true);
1297             default:
1298                 return false;
1299             }
1300         case CV_CAP_PROP_FORMAT:
1301             return configureOutput(newFormat, (int)cvRound(value));
1302         case CV_CAP_PROP_CONVERT_RGB:
1303             convertFormat = (value != 0);
1304             return configureOutput(newFormat, outputFormat);
1305         case CV_CAP_PROP_SAR_NUM:
1306             if (value > 0)
1307             {
1308                 newFormat.aspectRatioNum = (UINT32)cvRound(value);
1309                 return configureOutput(newFormat, outputFormat);
1310             }
1311             break;
1312         case CV_CAP_PROP_SAR_DEN:
1313             if (value > 0)
1314             {
1315                 newFormat.aspectRatioDenom = (UINT32)cvRound(value);
1316                 return configureOutput(newFormat, outputFormat);
1317             }
1318             break;
1319         case CV_CAP_PROP_FRAME_WIDTH:
1320             if (value >= 0)
1321             {
1322                 newFormat.width = (UINT32)cvRound(value);
1323                 return configureOutput(newFormat, outputFormat);
1324             }
1325             break;
1326         case CV_CAP_PROP_FRAME_HEIGHT:
1327             if (value >= 0)
1328             {
1329                 newFormat.height = (UINT32)cvRound(value);
1330                 return configureOutput(newFormat, outputFormat);
1331             }
1332             break;
1333         case CV_CAP_PROP_FPS:
1334             if (value >= 0)
1335             {
1336                 newFormat.setFramerate(value);
1337                 return configureOutput(newFormat, outputFormat);
1338             }
1339             break;
1340             case CV_CAP_PROP_FOURCC:
1341                 break;
1342         case CV_CAP_PROP_FRAME_COUNT:
1343             break;
1344         case CV_CAP_PROP_POS_AVI_RATIO:
1345             if (duration != 0)
1346                 return setTime(duration * value, true);
1347             break;
1348         case CV_CAP_PROP_POS_FRAMES:
1349             if (std::fabs(captureFormat.getFramerate()) > 0)
1350                 return setTime(value  * 1e7 / captureFormat.getFramerate(), false);
1351             break;
1352         case CV_CAP_PROP_POS_MSEC:
1353                 return setTime(value  * 1e4, false);
1354         case CV_CAP_PROP_BRIGHTNESS:
1355             return writeComplexProperty<IAMVideoProcAmp>(VideoProcAmp_Brightness, value, VideoProcAmp_Flags_Manual);
1356         case CV_CAP_PROP_CONTRAST:
1357             return writeComplexProperty<IAMVideoProcAmp>(VideoProcAmp_Contrast, value, VideoProcAmp_Flags_Manual);
1358         case CV_CAP_PROP_SATURATION:
1359             return writeComplexProperty<IAMVideoProcAmp>(VideoProcAmp_Saturation, value, VideoProcAmp_Flags_Manual);
1360         case CV_CAP_PROP_HUE:
1361             return writeComplexProperty<IAMVideoProcAmp>(VideoProcAmp_Hue, value, VideoProcAmp_Flags_Manual);
1362         case CV_CAP_PROP_GAIN:
1363             return writeComplexProperty<IAMVideoProcAmp>(VideoProcAmp_Gain, value, VideoProcAmp_Flags_Manual);
1364         case CV_CAP_PROP_SHARPNESS:
1365             return writeComplexProperty<IAMVideoProcAmp>(VideoProcAmp_Sharpness, value, VideoProcAmp_Flags_Manual);
1366         case CV_CAP_PROP_GAMMA:
1367             return writeComplexProperty<IAMVideoProcAmp>(VideoProcAmp_Gamma, value, VideoProcAmp_Flags_Manual);
1368         case CV_CAP_PROP_BACKLIGHT:
1369             return writeComplexProperty<IAMVideoProcAmp>(VideoProcAmp_BacklightCompensation, value, VideoProcAmp_Flags_Manual);
1370         case CV_CAP_PROP_MONOCHROME:
1371             return writeComplexProperty<IAMVideoProcAmp>(VideoProcAmp_ColorEnable, value, VideoProcAmp_Flags_Manual);
1372         case CV_CAP_PROP_TEMPERATURE:
1373             return writeComplexProperty<IAMVideoProcAmp>(VideoProcAmp_WhiteBalance, value, VideoProcAmp_Flags_Manual);
1374         case CV_CAP_PROP_PAN:
1375             return writeComplexProperty<IAMCameraControl>(CameraControl_Pan, value, CameraControl_Flags_Manual);
1376         case CV_CAP_PROP_TILT:
1377             return writeComplexProperty<IAMCameraControl>(CameraControl_Tilt, value, CameraControl_Flags_Manual);
1378         case CV_CAP_PROP_ROLL:
1379             return writeComplexProperty<IAMCameraControl>(CameraControl_Roll, value, CameraControl_Flags_Manual);
1380         case CV_CAP_PROP_IRIS:
1381             return writeComplexProperty<IAMCameraControl>(CameraControl_Iris, value, CameraControl_Flags_Manual);
1382         case CV_CAP_PROP_EXPOSURE:
1383             return writeComplexProperty<IAMCameraControl>(CameraControl_Exposure, value, CameraControl_Flags_Manual);
1384         case CV_CAP_PROP_AUTO_EXPOSURE:
1385             return writeComplexProperty<IAMCameraControl>(CameraControl_Exposure, value, value != 0 ? VideoProcAmp_Flags_Auto : VideoProcAmp_Flags_Manual);
1386         case CV_CAP_PROP_ZOOM:
1387             return writeComplexProperty<IAMCameraControl>(CameraControl_Zoom, value, CameraControl_Flags_Manual);
1388         case CV_CAP_PROP_FOCUS:
1389             return writeComplexProperty<IAMCameraControl>(CameraControl_Focus, value, CameraControl_Flags_Manual);
1390         case CV_CAP_PROP_AUTOFOCUS:
1391             return writeComplexProperty<IAMCameraControl>(CameraControl_Focus, value, value != 0 ? CameraControl_Flags_Auto : CameraControl_Flags_Manual);
1392         case CV_CAP_PROP_WHITE_BALANCE_BLUE_U:
1393         case CV_CAP_PROP_WHITE_BALANCE_RED_V:
1394         case CV_CAP_PROP_RECTIFICATION:
1395         case CV_CAP_PROP_TRIGGER:
1396         case CV_CAP_PROP_TRIGGER_DELAY:
1397         case CV_CAP_PROP_GUID:
1398         case CV_CAP_PROP_ISO_SPEED:
1399         case CV_CAP_PROP_SETTINGS:
1400         case CV_CAP_PROP_BUFFERSIZE:
1401         default:
1402             break;
1403         }
1404     return false;
1405 }
1406
1407 cv::Ptr<cv::IVideoCapture> cv::cvCreateCapture_MSMF( int index )
1408 {
1409     cv::Ptr<CvCapture_MSMF> capture = cv::makePtr<CvCapture_MSMF>();
1410     if (capture)
1411     {
1412         capture->open(index);
1413         if (capture->isOpened())
1414             return capture;
1415     }
1416     return cv::Ptr<cv::IVideoCapture>();
1417 }
1418
1419 cv::Ptr<cv::IVideoCapture> cv::cvCreateCapture_MSMF (const cv::String& filename)
1420 {
1421     cv::Ptr<CvCapture_MSMF> capture = cv::makePtr<CvCapture_MSMF>();
1422     if (capture)
1423     {
1424         capture->open(filename);
1425         if (capture->isOpened())
1426             return capture;
1427     }
1428     return cv::Ptr<cv::IVideoCapture>();
1429 }
1430
1431 //
1432 //
1433 // Media Foundation-based Video Writer
1434 //
1435 //
1436
1437 class CvVideoWriter_MSMF : public cv::IVideoWriter
1438 {
1439 public:
1440     CvVideoWriter_MSMF();
1441     virtual ~CvVideoWriter_MSMF();
1442     virtual bool open(const cv::String& filename, int fourcc,
1443                       double fps, cv::Size frameSize, bool isColor);
1444     virtual void close();
1445     virtual void write(cv::InputArray);
1446
1447     virtual double getProperty(int) const { return 0; }
1448     virtual bool setProperty(int, double) { return false; }
1449     virtual bool isOpened() const { return initiated; }
1450
1451     int getCaptureDomain() const CV_OVERRIDE { return cv::CAP_MSMF; }
1452 private:
1453     Media_Foundation& MF;
1454     UINT32 videoWidth;
1455     UINT32 videoHeight;
1456     double fps;
1457     UINT32 bitRate;
1458     UINT32 frameSize;
1459     GUID   encodingFormat;
1460     GUID   inputFormat;
1461
1462     DWORD  streamIndex;
1463     _ComPtr<IMFSinkWriter> sinkWriter;
1464
1465     bool   initiated;
1466
1467     LONGLONG rtStart;
1468     UINT64 rtDuration;
1469
1470     static const GUID FourCC2GUID(int fourcc);
1471 };
1472
1473 CvVideoWriter_MSMF::CvVideoWriter_MSMF():
1474     MF(Media_Foundation::getInstance()),
1475     videoWidth(0),
1476     videoHeight(0),
1477     fps(0),
1478     bitRate(0),
1479     frameSize(0),
1480     encodingFormat(),
1481     inputFormat(),
1482     streamIndex(0),
1483     initiated(false),
1484     rtStart(0),
1485     rtDuration(0)
1486 {
1487 }
1488
1489 CvVideoWriter_MSMF::~CvVideoWriter_MSMF()
1490 {
1491     close();
1492 }
1493
1494 const GUID CvVideoWriter_MSMF::FourCC2GUID(int fourcc)
1495 {
1496     switch(fourcc)
1497     {
1498         case CV_FOURCC_MACRO('d', 'v', '2', '5'):
1499             return MFVideoFormat_DV25; break;
1500         case CV_FOURCC_MACRO('d', 'v', '5', '0'):
1501             return MFVideoFormat_DV50; break;
1502         case CV_FOURCC_MACRO('d', 'v', 'c', ' '):
1503             return MFVideoFormat_DVC; break;
1504         case CV_FOURCC_MACRO('d', 'v', 'h', '1'):
1505             return MFVideoFormat_DVH1; break;
1506         case CV_FOURCC_MACRO('d', 'v', 'h', 'd'):
1507             return MFVideoFormat_DVHD; break;
1508         case CV_FOURCC_MACRO('d', 'v', 's', 'd'):
1509             return MFVideoFormat_DVSD; break;
1510         case CV_FOURCC_MACRO('d', 'v', 's', 'l'):
1511                 return MFVideoFormat_DVSL; break;
1512 #if (WINVER >= 0x0602)
1513         case CV_FOURCC_MACRO('H', '2', '6', '3'):   // Available only for Win 8 target.
1514                 return MFVideoFormat_H263; break;
1515 #endif
1516         case CV_FOURCC_MACRO('H', '2', '6', '4'):
1517                 return MFVideoFormat_H264; break;
1518         case CV_FOURCC_MACRO('M', '4', 'S', '2'):
1519                 return MFVideoFormat_M4S2; break;
1520         case CV_FOURCC_MACRO('M', 'J', 'P', 'G'):
1521                 return MFVideoFormat_MJPG; break;
1522         case CV_FOURCC_MACRO('M', 'P', '4', '3'):
1523                 return MFVideoFormat_MP43; break;
1524         case CV_FOURCC_MACRO('M', 'P', '4', 'S'):
1525                 return MFVideoFormat_MP4S; break;
1526         case CV_FOURCC_MACRO('M', 'P', '4', 'V'):
1527                 return MFVideoFormat_MP4V; break;
1528         case CV_FOURCC_MACRO('M', 'P', 'G', '1'):
1529                 return MFVideoFormat_MPG1; break;
1530         case CV_FOURCC_MACRO('M', 'S', 'S', '1'):
1531                 return MFVideoFormat_MSS1; break;
1532         case CV_FOURCC_MACRO('M', 'S', 'S', '2'):
1533                 return MFVideoFormat_MSS2; break;
1534         case CV_FOURCC_MACRO('W', 'M', 'V', '1'):
1535                 return MFVideoFormat_WMV1; break;
1536         case CV_FOURCC_MACRO('W', 'M', 'V', '2'):
1537                 return MFVideoFormat_WMV2; break;
1538         case CV_FOURCC_MACRO('W', 'M', 'V', '3'):
1539                 return MFVideoFormat_WMV3; break;
1540         case CV_FOURCC_MACRO('W', 'V', 'C', '1'):
1541                 return MFVideoFormat_WVC1; break;
1542         default:
1543             return MFVideoFormat_H264;
1544     }
1545 }
1546
1547 bool CvVideoWriter_MSMF::open( const cv::String& filename, int fourcc,
1548                                double _fps, cv::Size _frameSize, bool /*isColor*/ )
1549 {
1550     if (initiated)
1551         close();
1552     videoWidth = _frameSize.width;
1553     videoHeight = _frameSize.height;
1554     fps = _fps;
1555     bitRate = (UINT32)fps*videoWidth*videoHeight; // 1-bit per pixel
1556     encodingFormat = FourCC2GUID(fourcc);
1557     inputFormat = MFVideoFormat_RGB32;
1558
1559     _ComPtr<IMFMediaType>  mediaTypeOut;
1560     _ComPtr<IMFMediaType>  mediaTypeIn;
1561     _ComPtr<IMFAttributes> spAttr;
1562     if (// Set the output media type.
1563         SUCCEEDED(MFCreateMediaType(&mediaTypeOut)) &&
1564         SUCCEEDED(mediaTypeOut->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video)) &&
1565         SUCCEEDED(mediaTypeOut->SetGUID(MF_MT_SUBTYPE, encodingFormat)) &&
1566         SUCCEEDED(mediaTypeOut->SetUINT32(MF_MT_AVG_BITRATE, bitRate)) &&
1567         SUCCEEDED(mediaTypeOut->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive)) &&
1568         SUCCEEDED(MFSetAttributeSize(mediaTypeOut.Get(), MF_MT_FRAME_SIZE, videoWidth, videoHeight)) &&
1569         SUCCEEDED(MFSetAttributeRatio(mediaTypeOut.Get(), MF_MT_FRAME_RATE, (UINT32)fps, 1)) &&
1570         SUCCEEDED(MFSetAttributeRatio(mediaTypeOut.Get(), MF_MT_PIXEL_ASPECT_RATIO, 1, 1)) &&
1571         // Set the input media type.
1572         SUCCEEDED(MFCreateMediaType(&mediaTypeIn)) &&
1573         SUCCEEDED(mediaTypeIn->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video)) &&
1574         SUCCEEDED(mediaTypeIn->SetGUID(MF_MT_SUBTYPE, inputFormat)) &&
1575         SUCCEEDED(mediaTypeIn->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive)) &&
1576         SUCCEEDED(mediaTypeIn->SetUINT32(MF_MT_DEFAULT_STRIDE, 4 * videoWidth)) && //Assume BGR32 input
1577         SUCCEEDED(MFSetAttributeSize(mediaTypeIn.Get(), MF_MT_FRAME_SIZE, videoWidth, videoHeight)) &&
1578         SUCCEEDED(MFSetAttributeRatio(mediaTypeIn.Get(), MF_MT_FRAME_RATE, (UINT32)fps, 1)) &&
1579         SUCCEEDED(MFSetAttributeRatio(mediaTypeIn.Get(), MF_MT_PIXEL_ASPECT_RATIO, 1, 1)) &&
1580         // Set sink writer parameters
1581         SUCCEEDED(MFCreateAttributes(&spAttr, 10)) &&
1582         SUCCEEDED(spAttr->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, true)) &&
1583         SUCCEEDED(spAttr->SetUINT32(MF_SINK_WRITER_DISABLE_THROTTLING, true))
1584         )
1585     {
1586         // Create the sink writer
1587         cv::AutoBuffer<wchar_t> unicodeFileName(filename.length() + 1);
1588         MultiByteToWideChar(CP_ACP, 0, filename.c_str(), -1, unicodeFileName.data(), (int)filename.length() + 1);
1589         HRESULT hr = MFCreateSinkWriterFromURL(unicodeFileName.data(), NULL, spAttr.Get(), &sinkWriter);
1590         if (SUCCEEDED(hr))
1591         {
1592             // Configure the sink writer and tell it start to start accepting data
1593             if (SUCCEEDED(sinkWriter->AddStream(mediaTypeOut.Get(), &streamIndex)) &&
1594                 SUCCEEDED(sinkWriter->SetInputMediaType(streamIndex, mediaTypeIn.Get(), NULL)) &&
1595                 SUCCEEDED(sinkWriter->BeginWriting()))
1596             {
1597                 initiated = true;
1598                 rtStart = 0;
1599                 MFFrameRateToAverageTimePerFrame((UINT32)fps, 1, &rtDuration);
1600                 return true;
1601             }
1602         }
1603     }
1604
1605     return false;
1606 }
1607
1608 void CvVideoWriter_MSMF::close()
1609 {
1610     if (initiated)
1611     {
1612         initiated = false;
1613         sinkWriter->Finalize();
1614         sinkWriter.Release();
1615     }
1616 }
1617
1618 void CvVideoWriter_MSMF::write(cv::InputArray img)
1619 {
1620     if (img.empty() ||
1621         (img.channels() != 1 && img.channels() != 3 && img.channels() != 4) ||
1622         (UINT32)img.cols() != videoWidth || (UINT32)img.rows() != videoHeight)
1623         return;
1624
1625     const LONG cbWidth = 4 * videoWidth;
1626     const DWORD cbBuffer = cbWidth * videoHeight;
1627     _ComPtr<IMFSample> sample;
1628     _ComPtr<IMFMediaBuffer> buffer;
1629     BYTE *pData = NULL;
1630     // Prepare a media sample.
1631     if (SUCCEEDED(MFCreateSample(&sample)) &&
1632         // Set sample time stamp and duration.
1633         SUCCEEDED(sample->SetSampleTime(rtStart)) &&
1634         SUCCEEDED(sample->SetSampleDuration(rtDuration)) &&
1635         // Create a memory buffer.
1636         SUCCEEDED(MFCreateMemoryBuffer(cbBuffer, &buffer)) &&
1637         // Set the data length of the buffer.
1638         SUCCEEDED(buffer->SetCurrentLength(cbBuffer)) &&
1639         // Add the buffer to the sample.
1640         SUCCEEDED(sample->AddBuffer(buffer.Get())) &&
1641         // Lock the buffer.
1642         SUCCEEDED(buffer->Lock(&pData, NULL, NULL)))
1643     {
1644         // Copy the video frame to the buffer.
1645         cv::cvtColor(img.getMat(), cv::Mat(videoHeight, videoWidth, CV_8UC4, pData, cbWidth), img.channels() > 1 ? cv::COLOR_BGR2BGRA : cv::COLOR_GRAY2BGRA);
1646         buffer->Unlock();
1647         // Send media sample to the Sink Writer.
1648         if (SUCCEEDED(sinkWriter->WriteSample(streamIndex, sample.Get())))
1649         {
1650             rtStart += rtDuration;
1651         }
1652     }
1653 }
1654
1655 cv::Ptr<cv::IVideoWriter> cv::cvCreateVideoWriter_MSMF( const cv::String& filename, int fourcc,
1656                                                         double fps, cv::Size frameSize, int isColor )
1657 {
1658     cv::Ptr<CvVideoWriter_MSMF> writer = cv::makePtr<CvVideoWriter_MSMF>();
1659     if (writer)
1660     {
1661         writer->open(filename, fourcc, fps, frameSize, isColor != 0);
1662         if (writer->isOpened())
1663             return writer;
1664     }
1665     return cv::Ptr<cv::IVideoWriter>();
1666 }
1667
1668 #endif