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
6 #if defined _WIN32 && defined HAVE_MSMF
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
14 //require Windows 8 for some of the formats defined otherwise could baseline on lower version
15 #if WINVER < _WIN32_WINNT_WIN8
17 #define WINVER _WIN32_WINNT_WIN8
24 #include <mfobjects.h>
27 #include <Mfreadwrite.h>
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")
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()
58 HMODULE h = LoadLibraryExA("mfplat.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
61 pMFCreateDXGIDeviceManager = (FN_MFCreateDXGIDeviceManager)GetProcAddress(h, "MFCreateDXGIDeviceManager");
63 pMFCreateDXGIDeviceManager_initialized = true;
66 #pragma comment(lib, "Shlwapi.lib")
73 #include <shlwapi.h> // QISearch
77 struct IMFMediaSource;
94 ComPtr(_In_ const ComPtr<T>& lp)
104 CV_Assert(p == NULL);
105 return p.operator&();
107 T* operator->() const
109 CV_Assert(p != NULL);
110 return p.operator->();
114 return p.operator!=(NULL);
128 // query for U interface
130 HRESULT As(_Out_ ComPtr<U>& lp) const
133 return p->QueryInterface(__uuidof(U), reinterpret_cast<void**>((T**)&lp));
136 _COM_SMARTPTR_TYPEDEF(T, __uuidof(T));
140 #define _ComPtr ComPtr
142 template <typename T> inline T absDiff(T a, T b) { return a >= b ? a - b : b - a; }
144 //==================================================================================================
146 // Structure for collecting info about types of video which are supported by current video device
151 INT32 stride; // stride is negative if image is bottom-up
154 UINT32 frameRateDenom;
155 UINT32 aspectRatioNum;
156 UINT32 aspectRatioDenom;
158 UINT32 interlaceMode;
159 GUID majorType; // video or audio
160 GUID subType; // fourCC
161 MediaType(IMFMediaType *pType = 0) :
165 frameRateNum(1), frameRateDenom(1),
166 aspectRatioNum(1), aspectRatioDenom(1),
169 majorType(MFMediaType_Video),
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);
185 static MediaType createDefault()
190 res.setFramerate(30.0);
193 inline bool isEmpty() const
195 return width == 0 && height == 0;
197 _ComPtr<IMFMediaType> createMediaType() const
199 _ComPtr<IMFMediaType> res;
200 MFCreateMediaType(&res);
201 if (width != 0 || height != 0)
202 MFSetAttributeSize(res.Get(), MF_MT_FRAME_SIZE, width, height);
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);
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);
219 void setFramerate(double fps)
221 frameRateNum = (UINT32)cvRound(fps * 1000.0);
222 frameRateDenom = 1000;
224 double getFramerate() const
226 return frameRateDenom != 0 ? ((double)frameRateNum) / ((double)frameRateDenom) : 0;
228 LONGLONG getFrameStep() const
230 const double fps = getFramerate();
231 return (LONGLONG)(fps > 0 ? 1e7 / fps : 0);
233 inline unsigned long resolutionDiff(const MediaType& other) const
235 const unsigned long wdiff = absDiff(width, other.width);
236 const unsigned long hdiff = absDiff(height, other.height);
237 return wdiff + hdiff;
239 // check if 'this' is better than 'other' comparing to reference
240 bool isBetterThan(const MediaType& other, const MediaType& ref) const
242 const unsigned long thisDiff = resolutionDiff(ref);
243 const unsigned long otherDiff = other.resolutionDiff(ref);
244 if (thisDiff < otherDiff)
246 if (thisDiff == otherDiff)
248 if (width > other.width)
250 if (width == other.width && height > other.height)
252 if (width == other.width && height == other.height)
254 const double thisRateDiff = absDiff(getFramerate(), ref.getFramerate());
255 const double otherRateDiff = absDiff(other.getFramerate(), ref.getFramerate());
256 if (thisRateDiff < otherRateDiff)
264 void printFormat(std::ostream& out, const GUID& fmt)
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)
276 char fourcc[5] = { 0 };
277 memcpy(fourcc, &fmt.Data1, 4);
283 std::ostream& operator<<(std::ostream& out, const MediaType& mt)
285 out << "(" << mt.width << "x" << mt.height << " @ " << mt.getFramerate() << ") ";
286 printFormat(out, mt.subType);
290 //==================================================================================================
292 // Class for creating of Media Foundation context
293 class Media_Foundation
296 ~Media_Foundation(void) { /*CV_Assert(SUCCEEDED(MFShutdown()));*/ CoUninitialize(); }
297 static Media_Foundation& getInstance()
299 static Media_Foundation instance;
303 Media_Foundation(void) { CoInitialize(0); CV_Assert(SUCCEEDED(MFStartup(MF_VERSION))); }
306 //==================================================================================================
308 class SourceReaderCB : public IMFSourceReaderCallback
312 m_nRefCount(0), m_hEvent(CreateEvent(NULL, FALSE, FALSE, NULL)), m_bEOS(FALSE), m_hrStatus(S_OK), m_reader(NULL), m_dwStreamIndex(0)
317 STDMETHODIMP QueryInterface(REFIID iid, void** ppv) CV_OVERRIDE
320 #pragma warning(push)
321 #pragma warning(disable:4838)
323 static const QITAB qit[] =
325 QITABENT(SourceReaderCB, IMFSourceReaderCallback),
331 return QISearch(this, qit, iid, ppv);
333 STDMETHODIMP_(ULONG) AddRef() CV_OVERRIDE
335 return InterlockedIncrement(&m_nRefCount);
337 STDMETHODIMP_(ULONG) Release() CV_OVERRIDE
339 ULONG uCount = InterlockedDecrement(&m_nRefCount);
347 STDMETHODIMP OnReadSample(HRESULT hrStatus, DWORD dwStreamIndex, DWORD dwStreamFlags, LONGLONG llTimestamp, IMFSample *pSample) CV_OVERRIDE
349 CV_UNUSED(llTimestamp);
352 cv::AutoLock lock(m_mutex);
354 if (SUCCEEDED(hrStatus))
358 CV_LOG_DEBUG(NULL, "videoio(MSMF): got frame at " << llTimestamp);
359 if (m_lastSample.Get())
361 CV_LOG_DEBUG(NULL, "videoio(MSMF): drop frame (not processed)");
363 m_lastSample = pSample;
368 CV_LOG_WARNING(NULL, "videoio(MSMF): OnReadSample() is called with error status: " << hrStatus);
371 if (MF_SOURCE_READERF_ENDOFSTREAM & dwStreamFlags)
373 // Reached the end of the stream.
376 m_hrStatus = hrStatus;
378 if (FAILED(hr = m_reader->ReadSample(dwStreamIndex, 0, NULL, NULL, NULL, NULL)))
380 CV_LOG_WARNING(NULL, "videoio(MSMF): async ReadSample() call is failed with error status: " << hr);
384 if (pSample || m_bEOS)
391 STDMETHODIMP OnEvent(DWORD, IMFMediaEvent *) CV_OVERRIDE
395 STDMETHODIMP OnFlush(DWORD) CV_OVERRIDE
400 HRESULT Wait(DWORD dwMilliseconds, _ComPtr<IMFSample>& videoSample, BOOL& pbEOS)
404 DWORD dwResult = WaitForSingleObject(m_hEvent, dwMilliseconds);
405 if (dwResult == WAIT_TIMEOUT)
409 else if (dwResult != WAIT_OBJECT_0)
411 return HRESULT_FROM_WIN32(GetLastError());
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.
427 // Destructor is private. Caller should call Release.
428 virtual ~SourceReaderCB()
430 CV_LOG_WARNING(NULL, "terminating async callback");
434 long m_nRefCount; // Reference count.
440 IMFSourceReader *m_reader;
441 DWORD m_dwStreamIndex;
442 _ComPtr<IMFSample> m_lastSample;
445 //==================================================================================================
447 // Enumerate and store supported formats and finds format which is most similar to the one requested
455 MediaID() : stream(0), media(0) {}
465 bool operator<(const MediaID& other) const
467 return (stream < other.stream) || (stream == other.stream && media < other.media);
470 void read(IMFSourceReader* source)
474 while (SUCCEEDED(hr))
476 _ComPtr<IMFMediaType> raw_type;
477 hr = source->GetNativeMediaType(cur.stream, cur.media, &raw_type);
478 if (hr == MF_E_NO_MORE_TYPES)
483 else if (SUCCEEDED(hr))
485 formats[cur] = MediaType(raw_type.Get());
490 std::pair<MediaID, MediaType> findBest(const MediaType& newType)
492 std::pair<MediaID, MediaType> best;
493 std::map<MediaID, MediaType>::const_iterator i = formats.begin();
494 for (; i != formats.end(); ++i)
496 if (newType.isEmpty()) // file input - choose first returned media type
501 if (best.second.isEmpty() || i->second.isBetterThan(best.second, newType))
509 std::map<MediaID, MediaType> formats;
512 //==================================================================================================
514 // Enumerates devices and activates one of them
518 DeviceList() : devices(NULL), count(0) {}
523 for (UINT32 i = 0; i < count; ++i)
525 devices[i]->Release();
526 CoTaskMemFree(devices);
529 UINT32 read(IID sourceType = MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID)
531 _ComPtr<IMFAttributes> attr;
532 if (FAILED(MFCreateAttributes(&attr, 1)) ||
533 FAILED(attr->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, sourceType)))
535 CV_Error(CV_StsError, "Failed to create attributes");
537 if (FAILED(MFEnumDeviceSources(attr.Get(), &devices, &count)))
539 CV_LOG_DEBUG(NULL, "Failed to enumerate MSMF devices");
544 _ComPtr<IMFMediaSource> activateSource(UINT32 index)
546 _ComPtr<IMFMediaSource> result;
547 if (count == 0 || index >= count || FAILED(devices[index]->ActivateObject(__uuidof(IMFMediaSource), (void**)&result)))
549 CV_LOG_DEBUG(NULL, "Failed to activate media source (device " << index << ")");
554 IMFActivate** devices;
560 //==================================================================================================
562 /******* Capturing video from camera or file via Microsoft Media Foundation **********/
563 class CvCapture_MSMF : public cv::IVideoCapture
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; }
582 bool configureOutput(MediaType newType, cv::uint32_t outFormat);
583 bool setTime(double time, bool rough);
584 bool configureHW(bool enable);
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);
593 Media_Foundation& MF;
596 MSMFCapture_Mode captureMode;
597 #ifdef HAVE_MSMF_DXVA
598 _ComPtr<ID3D11Device> D3DDev;
599 _ComPtr<IMFDXGIDeviceManager> D3DMgr;
601 _ComPtr<IMFSourceReader> videoFileSource;
603 MediaType nativeFormat;
604 MediaType captureFormat;
609 _ComPtr<IMFSample> videoSample;
612 _ComPtr<IMFSourceReaderCallback> readCallback; // non-NULL for "live" streams (camera capture)
615 CvCapture_MSMF::CvCapture_MSMF():
616 MF(Media_Foundation::getInstance()),
619 captureMode(MODE_SW),
620 #ifdef HAVE_MSMF_DXVA
624 videoFileSource(NULL),
626 outputFormat(CV_CAP_MODE_BGR),
634 CvCapture_MSMF::~CvCapture_MSMF()
640 void CvCapture_MSMF::close()
645 videoSample.Release();
646 videoFileSource.Release();
650 readCallback.Release();
653 bool CvCapture_MSMF::initStream(DWORD streamID, const MediaType& mt)
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)))
659 CV_LOG_WARNING(NULL, "Failed to reset streams");
662 if (FAILED(videoFileSource->SetStreamSelection(streamID, true)))
664 CV_LOG_WARNING(NULL, "Failed to select stream " << streamID);
667 HRESULT hr = videoFileSource->SetCurrentMediaType(streamID, NULL, mediaTypeOut.Get());
668 if (hr == MF_E_TOPO_CODEC_NOT_FOUND)
670 CV_LOG_WARNING(NULL, "Failed to set mediaType (stream " << streamID << ", " << mt << "(codec not found)");
673 else if (hr == MF_E_INVALIDMEDIATYPE)
675 CV_LOG_WARNING(NULL, "Failed to set mediaType (stream " << streamID << ", " << mt << "(unsupported media type)");
680 CV_LOG_WARNING(NULL, "Failed to set mediaType (stream " << streamID << ", " << mt << "(HRESULT " << hr << ")");
687 _ComPtr<IMFAttributes> CvCapture_MSMF::getDefaultSourceConfig(UINT32 num)
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))
698 CV_Error(CV_StsError, "Failed to create attributes");
700 #ifdef HAVE_MSMF_DXVA
703 if (FAILED(res->SetUnknown(MF_SOURCE_READER_D3D_MANAGER, D3DMgr.Get())))
705 CV_Error(CV_StsError, "Failed to create attributes");
712 bool CvCapture_MSMF::configureHW(bool enable)
714 #ifdef HAVE_MSMF_DXVA
715 if ((enable && D3DMgr && D3DDev) || (!enable && !D3DMgr && !D3DDev))
717 if (!pMFCreateDXGIDeviceManager_initialized)
718 init_MFCreateDXGIDeviceManager();
719 if (enable && !pMFCreateDXGIDeviceManager)
722 bool reopen = isOpen;
724 cv::String prevfile = filename;
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)))
734 // NOTE: Getting ready for multi-threaded operation
735 _ComPtr<ID3D11Multithread> D3DDevMT;
737 if (SUCCEEDED(D3DDev->QueryInterface(IID_PPV_ARGS(&D3DDevMT))))
739 D3DDevMT->SetMultithreadProtected(TRUE);
741 if (SUCCEEDED(pMFCreateDXGIDeviceManager(&mgrRToken, &D3DMgr)))
743 if (SUCCEEDED(D3DMgr->ResetDevice(D3DDev.Get(), mgrRToken)))
745 captureMode = MODE_HW;
746 return reopen ? (prevcam >= 0 ? open(prevcam) : open(prevfile.c_str())) : true;
761 captureMode = MODE_SW;
762 return reopen ? (prevcam >= 0 ? open(prevcam) : open(prevfile.c_str())) : true;
769 bool CvCapture_MSMF::configureOutput(MediaType newType, cv::uint32_t outFormat)
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;
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;
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;
792 case CV_CAP_MODE_YUYV:
793 newFormat.subType = MFVideoFormat_YUY2;
794 newFormat.stride = 2 * newFormat.width;
795 newFormat.sampleSize = newFormat.stride * newFormat.height;
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;
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);
811 bool CvCapture_MSMF::open(int index)
817 UINT32 count = devices.read();
818 if (count == 0 || static_cast<UINT32>(index) > count)
820 CV_LOG_DEBUG(NULL, "Device " << index << " not found (total " << count << " devices)");
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)))
829 CV_LOG_DEBUG(NULL, "Failed to create source reader");
837 if (configureOutput(MediaType::createDefault(), outputFormat))
839 frameStep = captureFormat.getFrameStep();
844 bool CvCapture_MSMF::open(const cv::String& _filename)
847 if (_filename.empty())
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)))
858 if (configureOutput(MediaType(), outputFormat))
860 frameStep = captureFormat.getFrameStep();
861 filename = _filename;
864 if (SUCCEEDED(hr = videoFileSource->GetPresentationAttribute((DWORD)MF_SOURCE_READER_MEDIASOURCE, MF_PD_DURATION, &var)) &&
867 duration = var.uhVal.QuadPart;
868 PropVariantClear(&var);
878 bool CvCapture_MSMF::grabFrame()
881 if (readCallback) // async "live" capture mode
884 SourceReaderCB* reader = ((SourceReaderCB*)readCallback.Get());
885 if (!reader->m_reader)
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)))
892 CV_LOG_ERROR(NULL, "videoio(MSMF): can't grab frame - initial async ReadSample() call failed: " << hr);
893 reader->m_reader = NULL;
898 if (FAILED(hr = reader->Wait(10000, videoSample, bEOS))) // 10 sec
900 CV_LOG_WARNING(NULL, "videoio(MSMF): can't grab frame. Error: " << hr);
905 CV_LOG_WARNING(NULL, "videoio(MSMF): EOS signal. Capture stream is lost");
912 DWORD streamIndex, flags;
913 videoSample.Release();
917 CV_TRACE_REGION("ReadSample");
918 if (!SUCCEEDED(hr = videoFileSource->ReadSample(
919 dwStreamIndex, // Stream index.
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.
927 if (streamIndex != dwStreamIndex)
929 if (flags & (MF_SOURCE_READERF_ERROR | MF_SOURCE_READERF_ALLEFFECTSREMOVED | MF_SOURCE_READERF_ENDOFSTREAM))
933 if (flags & MF_SOURCE_READERF_STREAMTICK)
935 CV_LOG_DEBUG(NULL, "videoio(MSMF): Stream tick detected. Retrying to grab the frame");
941 if (streamIndex != dwStreamIndex)
943 CV_LOG_DEBUG(NULL, "videoio(MSMF): Wrong stream read. Abort capturing");
946 else if (flags & MF_SOURCE_READERF_ERROR)
948 CV_LOG_DEBUG(NULL, "videoio(MSMF): Stream reading error. Abort capturing");
951 else if (flags & MF_SOURCE_READERF_ALLEFFECTSREMOVED)
953 CV_LOG_DEBUG(NULL, "videoio(MSMF): Stream decoding error. Abort capturing");
956 else if (flags & MF_SOURCE_READERF_ENDOFSTREAM)
958 sampleTime += frameStep;
959 CV_LOG_DEBUG(NULL, "videoio(MSMF): End of stream detected");
963 sampleTime += frameStep;
964 if (flags & MF_SOURCE_READERF_NEWSTREAM)
966 CV_LOG_DEBUG(NULL, "videoio(MSMF): New stream detected");
968 if (flags & MF_SOURCE_READERF_NATIVEMEDIATYPECHANGED)
970 CV_LOG_DEBUG(NULL, "videoio(MSMF): Stream native media type changed");
972 if (flags & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED)
974 CV_LOG_DEBUG(NULL, "videoio(MSMF): Stream current media type changed");
983 bool CvCapture_MSMF::retrieveFrame(int, cv::OutputArray frame)
991 _ComPtr<IMFMediaBuffer> buf = NULL;
993 CV_TRACE_REGION("get_contiguous_buffer");
994 if (!SUCCEEDED(videoSample->ConvertToContiguousBuffer(&buf)))
996 CV_TRACE_REGION("get_buffer");
998 if (!SUCCEEDED(videoSample->GetBufferCount(&bcnt)))
1002 if (!SUCCEEDED(videoSample->GetBufferByIndex(0, &buf)))
1006 bool lock2d = false;
1009 DWORD maxsize = 0, cursize = 0;
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;
1016 if (SUCCEEDED(buf.As<IMF2DBuffer>(buffer2d)))
1018 CV_TRACE_REGION_NEXT("lock2d");
1019 if (SUCCEEDED(buffer2d->Lock2D(&ptr, &pitch)))
1027 CV_Assert(lock2d == false);
1028 CV_TRACE_REGION_NEXT("lock");
1029 if (!SUCCEEDED(buf->Lock(&ptr, &maxsize, &cursize)))
1038 if (lock2d || (unsigned int)cursize == captureFormat.sampleSize)
1040 switch (outputFormat)
1042 case CV_CAP_MODE_YUYV:
1043 cv::Mat(captureFormat.height, captureFormat.width, CV_8UC2, ptr, pitch).copyTo(frame);
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);
1049 cv::Mat(captureFormat.height, captureFormat.width, CV_8UC3, ptr, pitch).copyTo(frame);
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);
1055 cv::cvtColor(cv::Mat(captureFormat.height, captureFormat.width, CV_8UC3, ptr, pitch), frame, cv::COLOR_BGR2RGB);
1057 case CV_CAP_MODE_GRAY:
1058 cv::Mat(captureFormat.height, captureFormat.width, CV_8UC1, ptr, pitch).copyTo(frame);
1070 cv::Mat(1, cursize, CV_8UC1, ptr, pitch).copyTo(frame);
1072 CV_TRACE_REGION_NEXT("unlock");
1074 buffer2d->Unlock2D();
1077 return !frame.empty();
1084 bool CvCapture_MSMF::setTime(double time, bool rough)
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)
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);
1095 var.hVal.QuadPart = sampleTime;
1096 bool resOK = SUCCEEDED(videoFileSource->SetCurrentPosition(GUID_NULL, var));
1097 PropVariantClear(&var);
1098 if (resOK && useGrabbing)
1100 LONGLONG timeborder = (LONGLONG)floor(time + 0.5) - frameStep / 2;
1101 do { resOK = grabFrame(); videoSample.Release(); } while (resOK && sampleTime < timeborder);
1108 template <typename CtrlT>
1109 bool CvCapture_MSMF::readComplexPropery(long prop, long & val) const
1111 _ComPtr<CtrlT> ctrl;
1112 if (FAILED(videoFileSource->GetServiceForStream((DWORD)MF_SOURCE_READER_MEDIASOURCE, GUID_NULL, IID_PPV_ARGS(&ctrl))))
1114 CV_LOG_DEBUG(NULL, "Failed to get service for stream");
1117 long paramVal, paramFlag;
1118 if (FAILED(ctrl->Get(prop, ¶mVal, ¶mFlag)))
1120 CV_LOG_DEBUG(NULL, "Failed to get property " << prop);
1123 // fallback - get default value
1124 long minVal, maxVal, stepVal;
1125 if (FAILED(ctrl->GetRange(prop, &minVal, &maxVal, &stepVal, ¶mVal, ¶mFlag)))
1127 CV_LOG_DEBUG(NULL, "Failed to get default value for property " << prop);
1134 double CvCapture_MSMF::getProperty( int property_id ) const
1138 switch (property_id)
1140 case CV_CAP_PROP_FORMAT:
1141 return outputFormat;
1142 case CV_CAP_PROP_MODE:
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:
1160 return floor(((double)duration / 1e7)* captureFormat.getFramerate() + 0.5);
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:
1169 return (double)sampleTime / duration;
1172 case CV_CAP_PROP_BRIGHTNESS:
1173 if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Brightness, cVal))
1176 case CV_CAP_PROP_CONTRAST:
1177 if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Contrast, cVal))
1180 case CV_CAP_PROP_SATURATION:
1181 if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Saturation, cVal))
1184 case CV_CAP_PROP_HUE:
1185 if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Hue, cVal))
1188 case CV_CAP_PROP_GAIN:
1189 if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Gain, cVal))
1192 case CV_CAP_PROP_SHARPNESS:
1193 if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Sharpness, cVal))
1196 case CV_CAP_PROP_GAMMA:
1197 if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_Gamma, cVal))
1200 case CV_CAP_PROP_BACKLIGHT:
1201 if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_BacklightCompensation, cVal))
1204 case CV_CAP_PROP_MONOCHROME:
1205 if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_ColorEnable, cVal))
1206 return cVal == 0 ? 1 : 0;
1208 case CV_CAP_PROP_TEMPERATURE:
1209 if (readComplexPropery<IAMVideoProcAmp>(VideoProcAmp_WhiteBalance, cVal))
1212 case CV_CAP_PROP_PAN:
1213 if (readComplexPropery<IAMCameraControl>(CameraControl_Pan, cVal))
1216 case CV_CAP_PROP_TILT:
1217 if (readComplexPropery<IAMCameraControl>(CameraControl_Tilt, cVal))
1220 case CV_CAP_PROP_ROLL:
1221 if (readComplexPropery<IAMCameraControl>(CameraControl_Roll, cVal))
1224 case CV_CAP_PROP_IRIS:
1225 if (readComplexPropery<IAMCameraControl>(CameraControl_Iris, cVal))
1228 case CV_CAP_PROP_EXPOSURE:
1229 case CV_CAP_PROP_AUTO_EXPOSURE:
1230 if (readComplexPropery<IAMCameraControl>(CameraControl_Exposure, cVal))
1232 if (property_id == CV_CAP_PROP_EXPOSURE)
1235 return cVal == VideoProcAmp_Flags_Auto;
1238 case CV_CAP_PROP_ZOOM:
1239 if (readComplexPropery<IAMCameraControl>(CameraControl_Zoom, cVal))
1242 case CV_CAP_PROP_FOCUS:
1243 case CV_CAP_PROP_AUTOFOCUS:
1244 if (readComplexPropery<IAMCameraControl>(CameraControl_Focus, cVal))
1246 if (property_id == CV_CAP_PROP_FOCUS)
1249 return cVal == VideoProcAmp_Flags_Auto;
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:
1267 template <typename CtrlT>
1268 bool CvCapture_MSMF::writeComplexProperty(long prop, double val, long flags)
1270 _ComPtr<CtrlT> ctrl;
1271 if (FAILED(videoFileSource->GetServiceForStream((DWORD)MF_SOURCE_READER_MEDIASOURCE, GUID_NULL, IID_PPV_ARGS(&ctrl))))
1273 CV_LOG_DEBUG(NULL, "Failed get service for stream");
1276 if (FAILED(ctrl->Set(prop, (long)val, flags)))
1278 CV_LOG_DEBUG(NULL, "Failed to set property " << prop);
1284 bool CvCapture_MSMF::setProperty( int property_id, double value )
1286 MediaType newFormat = captureFormat;
1288 switch (property_id)
1290 case CV_CAP_PROP_MODE:
1291 switch ((MSMFCapture_Mode)((int)value))
1294 return configureHW(false);
1296 return configureHW(true);
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:
1308 newFormat.aspectRatioNum = (UINT32)cvRound(value);
1309 return configureOutput(newFormat, outputFormat);
1312 case CV_CAP_PROP_SAR_DEN:
1315 newFormat.aspectRatioDenom = (UINT32)cvRound(value);
1316 return configureOutput(newFormat, outputFormat);
1319 case CV_CAP_PROP_FRAME_WIDTH:
1322 newFormat.width = (UINT32)cvRound(value);
1323 return configureOutput(newFormat, outputFormat);
1326 case CV_CAP_PROP_FRAME_HEIGHT:
1329 newFormat.height = (UINT32)cvRound(value);
1330 return configureOutput(newFormat, outputFormat);
1333 case CV_CAP_PROP_FPS:
1336 newFormat.setFramerate(value);
1337 return configureOutput(newFormat, outputFormat);
1340 case CV_CAP_PROP_FOURCC:
1342 case CV_CAP_PROP_FRAME_COUNT:
1344 case CV_CAP_PROP_POS_AVI_RATIO:
1346 return setTime(duration * value, true);
1348 case CV_CAP_PROP_POS_FRAMES:
1349 if (std::fabs(captureFormat.getFramerate()) > 0)
1350 return setTime(value * 1e7 / captureFormat.getFramerate(), false);
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:
1407 cv::Ptr<cv::IVideoCapture> cv::cvCreateCapture_MSMF( int index )
1409 cv::Ptr<CvCapture_MSMF> capture = cv::makePtr<CvCapture_MSMF>();
1412 capture->open(index);
1413 if (capture->isOpened())
1416 return cv::Ptr<cv::IVideoCapture>();
1419 cv::Ptr<cv::IVideoCapture> cv::cvCreateCapture_MSMF (const cv::String& filename)
1421 cv::Ptr<CvCapture_MSMF> capture = cv::makePtr<CvCapture_MSMF>();
1424 capture->open(filename);
1425 if (capture->isOpened())
1428 return cv::Ptr<cv::IVideoCapture>();
1433 // Media Foundation-based Video Writer
1437 class CvVideoWriter_MSMF : public cv::IVideoWriter
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);
1447 virtual double getProperty(int) const { return 0; }
1448 virtual bool setProperty(int, double) { return false; }
1449 virtual bool isOpened() const { return initiated; }
1451 int getCaptureDomain() const CV_OVERRIDE { return cv::CAP_MSMF; }
1453 Media_Foundation& MF;
1459 GUID encodingFormat;
1463 _ComPtr<IMFSinkWriter> sinkWriter;
1470 static const GUID FourCC2GUID(int fourcc);
1473 CvVideoWriter_MSMF::CvVideoWriter_MSMF():
1474 MF(Media_Foundation::getInstance()),
1489 CvVideoWriter_MSMF::~CvVideoWriter_MSMF()
1494 const GUID CvVideoWriter_MSMF::FourCC2GUID(int fourcc)
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;
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;
1543 return MFVideoFormat_H264;
1547 bool CvVideoWriter_MSMF::open( const cv::String& filename, int fourcc,
1548 double _fps, cv::Size _frameSize, bool /*isColor*/ )
1552 videoWidth = _frameSize.width;
1553 videoHeight = _frameSize.height;
1555 bitRate = (UINT32)fps*videoWidth*videoHeight; // 1-bit per pixel
1556 encodingFormat = FourCC2GUID(fourcc);
1557 inputFormat = MFVideoFormat_RGB32;
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 * 1000), 1000)) &&
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 * 1000), 1000)) &&
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))
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);
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()))
1599 MFFrameRateToAverageTimePerFrame((UINT32)(fps * 1000), 1000, &rtDuration);
1608 void CvVideoWriter_MSMF::close()
1613 sinkWriter->Finalize();
1614 sinkWriter.Release();
1618 void CvVideoWriter_MSMF::write(cv::InputArray img)
1621 (img.channels() != 1 && img.channels() != 3 && img.channels() != 4) ||
1622 (UINT32)img.cols() != videoWidth || (UINT32)img.rows() != videoHeight)
1625 const LONG cbWidth = 4 * videoWidth;
1626 const DWORD cbBuffer = cbWidth * videoHeight;
1627 _ComPtr<IMFSample> sample;
1628 _ComPtr<IMFMediaBuffer> buffer;
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())) &&
1642 SUCCEEDED(buffer->Lock(&pData, NULL, NULL)))
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);
1647 // Send media sample to the Sink Writer.
1648 if (SUCCEEDED(sinkWriter->WriteSample(streamIndex, sample.Get())))
1650 rtStart += rtDuration;
1655 cv::Ptr<cv::IVideoWriter> cv::cvCreateVideoWriter_MSMF( const cv::String& filename, int fourcc,
1656 double fps, cv::Size frameSize, int isColor )
1658 cv::Ptr<CvVideoWriter_MSMF> writer = cv::makePtr<CvVideoWriter_MSMF>();
1661 writer->open(filename, fourcc, fps, frameSize, isColor != 0);
1662 if (writer->isOpened())
1665 return cv::Ptr<cv::IVideoWriter>();