- Timestamp:
- Feb 23, 2018, 1:59:55 AM (6 years ago)
- Location:
- trunk/Utilities/DirectShowReferenceSource/Sample/MfGenerate
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Utilities/DirectShowReferenceSource/Sample/MfGenerate/Application.cpp
r769 r823 1 1 //////////////////////////////////////////////////////////// 2 // Copyright (C) Roman Ryltsov, 2015-201 72 // Copyright (C) Roman Ryltsov, 2015-2018 3 3 // Created by Roman Ryltsov roman@alax.info 4 4 // … … 38 38 #pragma region Media Source 39 39 CComQIPtr<IMFMediaSource> pMediaSource; 40 40 41 //{ 41 42 // static const LPCTSTR g_pszPath = … … 68 69 69 70 const SIZE g_Extent = { 1920, 1080 }; 70 const LONG g_nFrameRateNumerator = 25* 1000; // frames/second71 const LONG g_nFrameRateNumerator = 50 * 1000; // frames/second 71 72 const LONG g_nFrameRateDenominator = 1000; 72 73 const LONG g_nDuration = 1 * 60; // minutes 73 const LONG g_nBitrate = ( 2 << 10) * 1000; // 2MBps74 const LONG g_nBitrate = (10 * 1000) * 1000; // 10 MBps 74 75 75 76 //const SIZE g_Extent = { 4096, 2304 }; … … 78 79 //const LONG g_nDuration = 5 * 60; // 5 minutes 79 80 //const LONG g_nBitrate = (2 << 10) * 1000; // 2 MBps 81 82 #pragma region DXGI Device Manager 83 static SIZE_T g_nDxgiAdapterIndex = 0; 84 MF::CDxgiDeviceManager pDxgiDeviceManager; 85 { 86 #pragma region DXGI Adapter 87 DXGI::CFactory2 pFactory2; 88 pFactory2.DebugCreate(); 89 CRoArrayT<DXGI::CAdapter1> AdapterArray; 90 _A(pFactory2.EnumerateAdapters(AdapterArray) == 3); 91 DXGI::CAdapter1& pAdapter1 = AdapterArray[g_nDxgiAdapterIndex]; 92 #pragma region D3D11 Device 93 CComPtr<ID3D11Device> pDevice; 94 UINT nFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT; 95 #if defined(_DEBUG) 96 nFlags |= D3D11_CREATE_DEVICE_DEBUG; 97 #endif // defined(_DEBUG) 98 static const D3D_FEATURE_LEVEL g_pFeatureLevels[] = 99 { 100 D3D_FEATURE_LEVEL_12_1, 101 D3D_FEATURE_LEVEL_12_0, 102 D3D_FEATURE_LEVEL_11_1, 103 D3D_FEATURE_LEVEL_11_0, 104 D3D_FEATURE_LEVEL_10_1, 105 D3D_FEATURE_LEVEL_10_0, 106 D3D_FEATURE_LEVEL_9_3, 107 D3D_FEATURE_LEVEL_9_2, 108 D3D_FEATURE_LEVEL_9_1, 109 }; 110 D3D_FEATURE_LEVEL FeatureLevel; 111 CComPtr<ID3D11DeviceContext> pDeviceContext; 112 // SUGG: Reuse CVideoInput's CreateDevice doing D3D11CreateDevice 113 __C(D3D11CreateDevice(pAdapter1, D3D_DRIVER_TYPE_UNKNOWN, NULL, nFlags, g_pFeatureLevels, DIM(g_pFeatureLevels), D3D11_SDK_VERSION, &pDevice, &FeatureLevel, &pDeviceContext)); 114 const CComQIPtr<ID3D11Multithread> pMultithread = pDevice; 115 __D(pMultithread, E_NOINTERFACE); 116 pMultithread->SetMultithreadProtected(TRUE); 117 #pragma endregion 118 #pragma endregion 119 pDxgiDeviceManager.Create(); 120 pDxgiDeviceManager.Reset(pDevice); 121 } 122 #pragma endregion 80 123 81 124 { … … 91 134 #pragma endregion 92 135 CComPtr<IMFSourceReader> pSourceReader; 93 __C(MFCreateSourceReaderFromMediaSource(pMediaSource, NULL, &pSourceReader)); 136 { 137 MF::CAttributes pAttributes; 138 if(pDxgiDeviceManager) 139 { 140 pAttributes.Create(4); 141 pAttributes[MF_READWRITE_D3D_OPTIONAL] = (UINT32) 0; 142 pAttributes[MF_SOURCE_READER_D3D_MANAGER] = (IMFDXGIDeviceManager*) pDxgiDeviceManager; 143 } 144 __C(MFCreateSourceReaderFromMediaSource(pMediaSource, pAttributes, &pSourceReader)); 145 } 94 146 MF::CMediaType pMediaType; 95 147 __C(pSourceReader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, &pMediaType)); … … 99 151 const CString sFileName = AtlFormatString(_T("%04d%02d%02d-%02d%02d%02d.mp4"), Time.wYear, Time.wMonth, Time.wDay, Time.wHour, Time.wMinute, Time.wSecond); 100 152 CComPtr<IMFSinkWriter> pSinkWriter; 101 __C(MFCreateSinkWriterFromURL(Combine(GetPathDirectory(GetModulePath()), sFileName), NULL, NULL, &pSinkWriter)); 153 { 154 MF::CAttributes pAttributes; 155 if(pDxgiDeviceManager) 156 { 157 pAttributes.Create(4); 158 pAttributes[MF_READWRITE_D3D_OPTIONAL] = (UINT32) 0; 159 pAttributes[MF_SINK_WRITER_D3D_MANAGER] = (IMFDXGIDeviceManager*) pDxgiDeviceManager; 160 } 161 __C(MFCreateSinkWriterFromURL(Combine(GetPathDirectory(GetModulePath()), sFileName), NULL, pAttributes, &pSinkWriter)); 162 } 102 163 MF::CMediaType pWriterMediaType; 103 164 pWriterMediaType.Create(); … … 105 166 pWriterMediaType[MF_MT_MAJOR_TYPE] = MFMediaType_Video; 106 167 pWriterMediaType[MF_MT_SUBTYPE] = MFVideoFormat_H264; 168 pWriterMediaType.CopyFrom(pMediaType, MF_MT_FRAME_RATE); 169 pWriterMediaType.CopyFrom(pMediaType, MF_MT_FRAME_SIZE); 170 pWriterMediaType.CopyFrom(pMediaType, MF_MT_INTERLACE_MODE); 171 pWriterMediaType.CopyFrom(pMediaType, MF_MT_PIXEL_ASPECT_RATIO); 107 172 pWriterMediaType[MF_MT_AVG_BITRATE] = (UINT32) g_nBitrate; 108 pWriterMediaType[MF_MT_FRAME_RATE] = pMediaType.GetUINT64(MF_MT_FRAME_RATE); 109 pWriterMediaType[MF_MT_FRAME_SIZE] = pMediaType.GetUINT64(MF_MT_FRAME_SIZE); 110 pWriterMediaType[MF_MT_INTERLACE_MODE] = pMediaType.GetUINT32(MF_MT_INTERLACE_MODE); 111 pWriterMediaType[MF_MT_PIXEL_ASPECT_RATIO] = pMediaType.GetUINT64(MF_MT_PIXEL_ASPECT_RATIO); 112 113 //pWriterMediaType[MF_MT_MPEG2_PROFILE] = (UINT32) eAVEncH264VProfile_High; 114 173 //pWriterMediaType[MF_MT_MPEG2_PROFILE] = (UINT32) eAVEncH264VProfile_High; 115 174 pWriterMediaType.Trace(); 116 175 DWORD nWriterStreamIndex; 117 176 __C(pSinkWriter->AddStream(pWriterMediaType, &nWriterStreamIndex)); 118 177 // NOTE: Sink Writer Attributes https://msdn.microsoft.com/en-us/library/windows/desktop/dd389284 178 179 //class __declspec(uuid("4E498B6F-7DCA-403E-A523-912A73943341")) NvEncoderTransform; 180 //MFT_REGISTER_TYPE_INFO pInputTypeInformation[] = { { MFMediaType_Video, MFVideoFormat_ARGB32 } }; 181 //MFT_REGISTER_TYPE_INFO pOutputTypeInformation[] = { { MFMediaType_Video, MFVideoFormat_H264 } }; 182 //__C(MFTRegisterLocalByCLSID(__uuidof(NvEncoderTransform), MFT_CATEGORY_VIDEO_ENCODER, L"Rainway H.264 Encoder for NVIDIA HW", MFT_ENUM_FLAG_ASYNCMFT, DIM(pInputTypeInformation), pInputTypeInformation, DIM(pOutputTypeInformation), pOutputTypeInformation)); 119 183 { 120 184 MF::CAttributes pAttributes; … … 123 187 __C(pSinkWriter->SetInputMediaType(nWriterStreamIndex, pMediaType, pAttributes)); 124 188 } 125 126 //CRoArrayT<CComPtr<IMFTransform>> TransformArray; 127 //MF::CSinkWriterHelper::GetTransforms(pSinkWriter, nWriterStreamIndex, TransformArray); 128 //_A(!TransformArray.IsEmpty()); 129 //MF::CCodecApi pCodecApi(TransformArray[TransformArray.GetCount() - 1]); 130 //// NOTE: H.264 Video Encoder https://msdn.microsoft.com/en-us/library/windows/desktop/dd797816 131 //pCodecApi[CODECAPI_AVEncMPVGOPSize] = (UINT32) 16; 132 //pCodecApi[CODECAPI_AVEncMPVDefaultBPictureCount] = (UINT32) 3; 133 189 MF::CSinkWriterHelper::TraceTransformsT<MF::CSinkWriterHelper::CVideoTraceTraits>(pSinkWriter); 190 191 CRoArrayT<CComPtr<IMFTransform>> TransformArray; 192 MF::CSinkWriterHelper::GetTransforms(pSinkWriter, nWriterStreamIndex, TransformArray); 193 _A(!TransformArray.IsEmpty()); 194 MF::CCodecApi pCodecApi(TransformArray[TransformArray.GetCount() - 1]); 195 pCodecApi[CODECAPI_AVEncCommonRateControlMode] = (UINT32) eAVEncCommonRateControlMode_CBR; 196 pCodecApi[CODECAPI_AVEncCommonMeanBitRate] = (UINT32) g_nBitrate; 197 pCodecApi[CODECAPI_AVEncMPVGOPSize] = (UINT32) 1; 198 //pCodecApi[CODECAPI_AVEncMPVDefaultBPictureCount] = (UINT32) 0; 199 200 const ULONG nTimeA = GetTickCount(); 134 201 __C(pSinkWriter->BeginWriting()); 135 202 for(; ; ) … … 143 210 { 144 211 pSample.Trace(); 212 #pragma region Re-Configuration 213 #if defined(_DEBUG) 214 static INT g_nState = 0; 215 if(!g_nState && pSample.GetTime() >= 30000 * 10000i64) 216 { 217 g_nState = 1; 218 const CComQIPtr<IMFSinkWriterEncoderConfig> pSinkWriterEncoderConfig = pSinkWriter; 219 __D(pSinkWriterEncoderConfig, E_NOINTERFACE); 220 MF::CAttributes pAttributes; 221 pAttributes.Create(4); 222 //pAttributes[CODECAPI_AVEncCommonRateControlMode] = (UINT32) eAVEncCommonRateControlMode_CBR; 223 pAttributes[CODECAPI_AVEncCommonMeanBitRate] = (UINT32) g_nBitrate / 4; 224 // NOTE: MF_SOURCE_READER_FIRST_VIDEO_STREAM results in MF_E_INVALIDSTREAMNUMBER 225 __C(pSinkWriterEncoderConfig->PlaceEncodingParameters(0, pAttributes)); 226 // SUGG: Check IMFSinkWriterEncoderConfig::SetTargetMediaType as well 227 } 228 #endif // defined(_DEBUG) 229 #pragma endregion 145 230 __C(pSinkWriter->WriteSample(nWriterStreamIndex, pSample)); 146 231 } … … 152 237 } 153 238 __C(pSourceReader->Flush(MF_SOURCE_READER_ALL_STREAMS)); 239 const ULONG nTimeB = GetTickCount(); 240 _tprintf(_T("Elapsed Time: %.2f seconds\n"), (DOUBLE) (nTimeB - nTimeA) / 1E3); 154 241 __C(pSinkWriter->Finalize()); 155 242 } -
trunk/Utilities/DirectShowReferenceSource/Sample/MfGenerate/MfGenerate.vcxproj
r769 r823 1 1 <?xml version="1.0" encoding="utf-8"?> 2 <Project DefaultTargets="Build" ToolsVersion="1 4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">2 <Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 3 3 <ItemGroup Label="ProjectConfigurations"> 4 4 <ProjectConfiguration Include="Debug|Win32"> … … 29 29 <ConfigurationType>Application</ConfigurationType> 30 30 <UseDebugLibraries>true</UseDebugLibraries> 31 <PlatformToolset>v14 0</PlatformToolset>31 <PlatformToolset>v141</PlatformToolset> 32 32 <CharacterSet>Unicode</CharacterSet> 33 33 </PropertyGroup> … … 35 35 <ConfigurationType>Application</ConfigurationType> 36 36 <UseDebugLibraries>true</UseDebugLibraries> 37 <PlatformToolset>v14 0</PlatformToolset>37 <PlatformToolset>v141</PlatformToolset> 38 38 <CharacterSet>Unicode</CharacterSet> 39 39 </PropertyGroup> … … 41 41 <ConfigurationType>Application</ConfigurationType> 42 42 <UseDebugLibraries>false</UseDebugLibraries> 43 <PlatformToolset>v14 0</PlatformToolset>43 <PlatformToolset>v141</PlatformToolset> 44 44 <WholeProgramOptimization>true</WholeProgramOptimization> 45 45 <CharacterSet>Unicode</CharacterSet> … … 48 48 <ConfigurationType>Application</ConfigurationType> 49 49 <UseDebugLibraries>false</UseDebugLibraries> 50 <PlatformToolset>v14 0</PlatformToolset>50 <PlatformToolset>v141</PlatformToolset> 51 51 <WholeProgramOptimization>true</WholeProgramOptimization> 52 52 <CharacterSet>Unicode</CharacterSet>
Note: See TracChangeset
for help on using the changeset viewer.