1 | //////////////////////////////////////////////////////////// |
---|
2 | // Copyright (C) Roman Ryltsov, 2015-2017 |
---|
3 | // Created by Roman Ryltsov roman@alax.info |
---|
4 | // |
---|
5 | // A permission to use the source code is granted as long as reference to |
---|
6 | // source website http://alax.info is retained. |
---|
7 | |
---|
8 | #include "stdafx.h" |
---|
9 | #include "romf.h" |
---|
10 | #import "libid:9E3ABA93-C8D8-41D3-B39E-29508FDE5757" raw_interfaces_only // AlaxInfoDirectShowReferenceSource |
---|
11 | |
---|
12 | //////////////////////////////////////////////////////////// |
---|
13 | // CModule |
---|
14 | |
---|
15 | class CModule : |
---|
16 | public CAtlExeModuleT<CModule> |
---|
17 | { |
---|
18 | public: |
---|
19 | // CModule |
---|
20 | bool ParseCommandLine(LPCTSTR pszCommandLine, HRESULT* pnResult) |
---|
21 | { |
---|
22 | _A(pnResult); |
---|
23 | pszCommandLine; |
---|
24 | *pnResult = S_OK; |
---|
25 | return true; |
---|
26 | } |
---|
27 | HRESULT PreMessageLoop(int nShowCommand) |
---|
28 | { |
---|
29 | // NOTE: Suppress S_FALSE |
---|
30 | _C(__super::PreMessageLoop(nShowCommand)); |
---|
31 | return S_OK; |
---|
32 | } |
---|
33 | VOID RunMessageLoop() |
---|
34 | { |
---|
35 | MF::CStartup Statup; |
---|
36 | CComPtr<IMFSourceResolver> pSourceResolver; |
---|
37 | __C(MFCreateSourceResolver(&pSourceResolver)); |
---|
38 | #pragma region Media Source |
---|
39 | CComQIPtr<IMFMediaSource> pMediaSource; |
---|
40 | //{ |
---|
41 | // static const LPCTSTR g_pszPath = |
---|
42 | // _T("D:\\Projects\\Alax.Info\\Repository-Private\\Utilities\\DirectShow\\ReferenceSource\\_Media\\720p50-Small-V.mp4"); |
---|
43 | // MF_OBJECT_TYPE ObjectType; |
---|
44 | // CComPtr<IUnknown> pMediaSourceUnknown; |
---|
45 | // __C(pSourceResolver->CreateObjectFromURL(CStringW(g_pszPath), MF_RESOLUTION_MEDIASOURCE, NULL, &ObjectType, &pMediaSourceUnknown)); |
---|
46 | // _A(ObjectType == MF_OBJECT_MEDIASOURCE); |
---|
47 | // pMediaSource = pMediaSourceUnknown; |
---|
48 | // __D(pMediaSource, E_NOINTERFACE); |
---|
49 | //} |
---|
50 | |
---|
51 | //const SIZE g_Extent = { 640, 360 }; |
---|
52 | //const LONG g_nFrameRateNumerator = 30000; |
---|
53 | //const LONG g_nFrameRateDenominator = 1001; |
---|
54 | //const LONG g_nDuration = 1 * 60; // 1 minutes |
---|
55 | //const LONG g_nBitrate = (2 << 10) * 1000; // 2 MBps |
---|
56 | |
---|
57 | //const SIZE g_Extent = { 720, 480 }; |
---|
58 | //const LONG g_nFrameRateNumerator = 90; //30 * 1000; |
---|
59 | //const LONG g_nFrameRateDenominator = 1; //1001; |
---|
60 | //const LONG g_nDuration = 60; //5 * 60; // 5 minutes |
---|
61 | //const LONG g_nBitrate = (2 << 10) * 1000; // 2 MBps |
---|
62 | |
---|
63 | //const SIZE g_Extent = { 1280, 720 }; |
---|
64 | //const LONG g_nFrameRateNumerator = 60 * 1000; |
---|
65 | //const LONG g_nFrameRateDenominator = 1000; |
---|
66 | //const LONG g_nDuration = 5 * 60; // 5 minutes |
---|
67 | //const LONG g_nBitrate = (5 << 10) * 1000; // 5 MBps |
---|
68 | |
---|
69 | const SIZE g_Extent = { 1920, 1080 }; |
---|
70 | const LONG g_nFrameRateNumerator = 25 * 1000; // frames/second |
---|
71 | const LONG g_nFrameRateDenominator = 1000; |
---|
72 | const LONG g_nDuration = 1 * 60; // minutes |
---|
73 | const LONG g_nBitrate = (2 << 10) * 1000; // 2 MBps |
---|
74 | |
---|
75 | //const SIZE g_Extent = { 4096, 2304 }; |
---|
76 | //const LONG g_nFrameRateNumerator = 50; |
---|
77 | //const LONG g_nFrameRateDenominator = 1; |
---|
78 | //const LONG g_nDuration = 5 * 60; // 5 minutes |
---|
79 | //const LONG g_nBitrate = (2 << 10) * 1000; // 2 MBps |
---|
80 | |
---|
81 | { |
---|
82 | using namespace AlaxInfoDirectShowReferenceSource; |
---|
83 | CComPtr<IVideoMediaSource> pVideoMediaSource; |
---|
84 | __C(pVideoMediaSource.CoCreateInstance(__uuidof(VideoMediaSource))); |
---|
85 | __C(pVideoMediaSource->SetMediaType(g_Extent.cx, g_Extent.cy, CComVariant(_PersistHelper::StringFromIdentifier(MEDIASUBTYPE_RGB32)))); |
---|
86 | //__C(pVideoMediaSource->SetMediaTypeAspectRatio(...)); |
---|
87 | __C(pVideoMediaSource->SetMediaTypeRate(g_nFrameRateNumerator, g_nFrameRateDenominator)); |
---|
88 | __C(pVideoMediaSource->put_Duration((DOUBLE) g_nDuration)); |
---|
89 | pMediaSource = pVideoMediaSource; |
---|
90 | } |
---|
91 | #pragma endregion |
---|
92 | CComPtr<IMFSourceReader> pSourceReader; |
---|
93 | __C(MFCreateSourceReaderFromMediaSource(pMediaSource, NULL, &pSourceReader)); |
---|
94 | MF::CMediaType pMediaType; |
---|
95 | __C(pSourceReader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, &pMediaType)); |
---|
96 | pMediaType.Trace(); |
---|
97 | SYSTEMTIME Time; |
---|
98 | GetLocalTime(&Time); |
---|
99 | 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 | CComPtr<IMFSinkWriter> pSinkWriter; |
---|
101 | __C(MFCreateSinkWriterFromURL(Combine(GetPathDirectory(GetModulePath()), sFileName), NULL, NULL, &pSinkWriter)); |
---|
102 | MF::CMediaType pWriterMediaType; |
---|
103 | pWriterMediaType.Create(); |
---|
104 | // NOTE: H.264 Video Encoder https://msdn.microsoft.com/en-us/library/windows/desktop/dd797816 |
---|
105 | pWriterMediaType[MF_MT_MAJOR_TYPE] = MFMediaType_Video; |
---|
106 | pWriterMediaType[MF_MT_SUBTYPE] = MFVideoFormat_H264; |
---|
107 | 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 | |
---|
115 | pWriterMediaType.Trace(); |
---|
116 | DWORD nWriterStreamIndex; |
---|
117 | __C(pSinkWriter->AddStream(pWriterMediaType, &nWriterStreamIndex)); |
---|
118 | // NOTE: Sink Writer Attributes https://msdn.microsoft.com/en-us/library/windows/desktop/dd389284 |
---|
119 | { |
---|
120 | MF::CAttributes pAttributes; |
---|
121 | pAttributes.Create(10); |
---|
122 | pAttributes[MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS] = (UINT32) 1; |
---|
123 | __C(pSinkWriter->SetInputMediaType(nWriterStreamIndex, pMediaType, pAttributes)); |
---|
124 | } |
---|
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 | |
---|
134 | __C(pSinkWriter->BeginWriting()); |
---|
135 | for(; ; ) |
---|
136 | { |
---|
137 | DWORD nStreamIndex, nStreamFlags; |
---|
138 | LONGLONG nTime; |
---|
139 | MF::CSample pSample; |
---|
140 | __C(pSourceReader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, &nStreamIndex, &nStreamFlags, &nTime, &pSample)); |
---|
141 | _Z4(atlTraceGeneral, 4, _T("nStreamIndex %d, nStreamFlags %s, nTime %s, pSample 0x%p\n"), nStreamIndex, MF::FormatSourceReaderStreamFlags(nStreamFlags), MF::FormatNanoTime(nTime), pSample); |
---|
142 | if(pSample) |
---|
143 | { |
---|
144 | pSample.Trace(); |
---|
145 | __C(pSinkWriter->WriteSample(nWriterStreamIndex, pSample)); |
---|
146 | } |
---|
147 | if(nStreamFlags & MF_SOURCE_READERF_ENDOFSTREAM) |
---|
148 | { |
---|
149 | _A(!pSample); |
---|
150 | break; |
---|
151 | } |
---|
152 | } |
---|
153 | __C(pSourceReader->Flush(MF_SOURCE_READER_ALL_STREAMS)); |
---|
154 | __C(pSinkWriter->Finalize()); |
---|
155 | } |
---|
156 | }; |
---|
157 | |
---|
158 | int _tmain(int argc, _TCHAR* argv[]) |
---|
159 | { |
---|
160 | _ATLTRY |
---|
161 | { |
---|
162 | CModule Module; |
---|
163 | Module.WinMain(SW_SHOWNORMAL); |
---|
164 | } |
---|
165 | _ATLCATCH(Exception) |
---|
166 | { |
---|
167 | _tprintf(_T("Fatal Error 0x%08X\n"), (HRESULT) Exception); |
---|
168 | } |
---|
169 | return 0; |
---|
170 | } |
---|
171 | |
---|