- Timestamp:
- Jun 23, 2017, 1:09:48 AM (7 years ago)
- Location:
- trunk/Utilities/MediaFoundation/HardwareEncoderMediaType01
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Utilities/MediaFoundation/HardwareEncoderMediaType01
-
Property
svn:ignore
set to
.vs
*.db
*.opendb
-
Property
svn:ignore
set to
-
trunk/Utilities/MediaFoundation/HardwareEncoderMediaType01/Module.h
r759 r760 82 82 { 83 83 return static_cast<CModule*>(_pAtlModule)->InternalHandlerRoutine(nType); 84 } 85 BOOL ProcessNalUnits(const BYTE* pnData, SIZE_T nDataSize, LPCTSTR pszPrefix) 86 { 87 _A(pnData && nDataSize); 88 CRoArrayT<SIZE_T> StartCodePositionArray; 89 SIZE_T nPositionA = 0; 90 for(; ; ) 91 { 92 SIZE_T nPositionB = nPositionA; 93 BOOL bFound = FALSE; 94 for(; nPositionB + 3 + 1 <= nDataSize; nPositionB++) 95 { 96 if(pnData[nPositionB + 0] == 0x00 && pnData[nPositionB + 1] == 0x00 && pnData[nPositionB + 2] == 0x01) 97 { 98 bFound = TRUE; 99 break; 100 } 101 } 102 if(!bFound) 103 break; 104 nPositionA = nPositionB + 4; 105 if(nPositionB > 0 && pnData[nPositionB - 1] == 0x00) 106 nPositionB--; 107 StartCodePositionArray.Add(nPositionB); 108 } 109 if(StartCodePositionArray.IsEmpty()) 110 return FALSE; 111 for(SIZE_T nIndex = 0; nIndex < StartCodePositionArray.GetCount(); nIndex++) 112 { 113 const SIZE_T nPosition = StartCodePositionArray[nIndex]; 114 const UINT nType = ((pnData[nPosition + 2] == 0x01) ? pnData[nPosition + 3] : pnData[nPosition + 4]) & 0x1F; 115 const SIZE_T nBoundaryPosition = (nIndex + 1 == StartCodePositionArray.GetCount()) ? nDataSize : StartCodePositionArray[nIndex + 1]; 116 static const SIZE_T g_nMaximalDataSize = 32; 117 _tprintf(_T("%s NAL Unit Type %d, Size %d, Data %s%s\n"), 118 pszPrefix, 119 nType, 120 (INT) (nBoundaryPosition - nPosition), 121 (LPCTSTR) AtlFormatData(pnData + nPosition, min(g_nMaximalDataSize, nBoundaryPosition - nPosition)), 122 ((nBoundaryPosition - nPosition) > g_nMaximalDataSize) ? _T("...") : _T("")); 123 } 124 return TRUE; 84 125 } 85 126 VOID ProcessTransform(ID3D11Device* pDevice, const CComPtr<IMFTransform>& pTransform) … … 108 149 pOutputMediaType[MF_MT_FRAME_RATE].SetRatio(60000, 1001); 109 150 pOutputMediaType[MF_MT_AVG_BITRATE] = (UINT32) ((5 << 10) * 1000); 151 pOutputMediaType.Trace(); 110 152 __C(pTransform->SetOutputType(0, pOutputMediaType, 0)); 111 153 #pragma endregion … … 129 171 pInputMediaType[MF_MT_INTERLACE_MODE] = (UINT32) MFVideoInterlace_Progressive; 130 172 pInputMediaType[MF_MT_FRAME_RATE] = pOutputMediaType.GetUINT64(MF_MT_FRAME_RATE); 173 pInputMediaType.Trace(); 131 174 __C(pTransform->SetInputType(0, pInputMediaType, 0)); 132 175 #pragma endregion … … 134 177 MFT_INPUT_STREAM_INFO InputStreamInformation; 135 178 __C(pTransform->GetInputStreamInfo(0, &InputStreamInformation)); 179 _Z3(atlTraceGeneral, 3, _T("Input Stream Information, hnsMaxLatency %s, dwFlags %s, cbSize %d, cbMaxLookahead %d, cbAlignment %d\n"), 180 MF::FormatNanoTime(InputStreamInformation.hnsMaxLatency), 181 MF::FormatTransformInputStreamFlags(InputStreamInformation.dwFlags), 182 InputStreamInformation.cbSize, 183 InputStreamInformation.cbMaxLookahead, 184 InputStreamInformation.cbAlignment, 185 0); 136 186 #pragma endregion 137 187 #pragma region Actual Output Media Type … … 140 190 __C(pTransform->GetOutputCurrentType(0, &pOutputMediaType)); 141 191 pOutputMediaType.Trace(); 142 _tprintf(_T("MF_MT_MPEG_SEQUENCE_HEADER: %d\n"), pOutputMediaType.GetType(MF_MT_MPEG_SEQUENCE_HEADER)); 192 const MF_ATTRIBUTE_TYPE Type = pOutputMediaType.GetType(MF_MT_MPEG_SEQUENCE_HEADER); 193 _tprintf(_T("MF_MT_MPEG_SEQUENCE_HEADER: %s\n"), (LPCTSTR) MF::FormatAttributeType(Type)); 194 if(Type == MF_ATTRIBUTE_BLOB) 195 { 196 CComHeapPtr<UINT8> pnData; 197 UINT32 nDataSize; 198 pOutputMediaType.GetBlob(MF_MT_MPEG_SEQUENCE_HEADER, pnData, nDataSize); 199 ProcessNalUnits(pnData, nDataSize, _T("Media Type")); 200 } 143 201 } 144 202 MFT_OUTPUT_STREAM_INFO OutputStreamInformation; 145 203 __C(pTransform->GetOutputStreamInfo(0, &OutputStreamInformation)); 146 204 _A(OutputStreamInformation.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES | MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES)); 205 _Z3(atlTraceGeneral, 3, _T("Output Stream Information, dwFlags %s, cbSize %d, cbAlignment %d\n"), 206 MF::FormatTransformOutputStreamFlags(OutputStreamInformation.dwFlags), 207 OutputStreamInformation.cbSize, 208 OutputStreamInformation.cbAlignment, 209 0); 147 210 #pragma endregion 148 211 __C(pTransform->ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0)); 149 212 __C(pTransform->ProcessMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0)); 213 #if TRUE 214 #pragma region Asynchronous Processing 215 const CComQIPtr<IMFMediaEventGenerator> pMediaEventGenerator = pTransform; 216 _A(pMediaEventGenerator); 217 UINT nFrameIndex = 0; 218 BOOL bComplete = FALSE; 219 for(UINT nIteration = 0; !bComplete; nIteration++) 220 { 221 __D(nIteration < 256, E_UNNAMED); 222 MF::CMediaEvent pMediaEvent; 223 __C(pMediaEventGenerator->GetEvent(0, &pMediaEvent)); 224 pMediaEvent.Trace(); 225 switch(pMediaEvent.GetType()) 226 { 227 #pragma region METransformNeedInput 228 case METransformNeedInput: 229 { 230 MF::CSample pSample; 231 pSample.Create(); 232 pSample[MFSampleExtension_Discontinuity] = (UINT32) (nFrameIndex == 0 ? 1 : 0); 233 pSample[MFSampleExtension_CleanPoint] = (UINT32) 1; 234 pSample->SetSampleTime(nFrameIndex * 20 * 10000i64); 235 pSample->SetSampleDuration(20 * 10000i64); 236 CComPtr<IMFMediaBuffer> pMediaBuffer; 237 __C(MFCreateMemoryBuffer(1280 * 720 * 12 / 8, &pMediaBuffer)); 238 __C(pMediaBuffer->SetCurrentLength(1280 * 720 * 12 / 8)); 239 __C(pSample->AddBuffer(pMediaBuffer)); 240 pSample.Trace(); 241 const HRESULT nProcessInputResult = pTransform->ProcessInput(0, pSample, 0); 242 _Z45_MFHRESULT(nProcessInputResult); 243 __C(nProcessInputResult); 244 } 245 break; 246 #pragma endregion 247 #pragma region METransformHaveOutput 248 case METransformHaveOutput: 249 { 250 _A(OutputStreamInformation.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES | MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES)); 251 MFT_OUTPUT_DATA_BUFFER Buffer = { 0 }; 252 DWORD nStatus; 253 const HRESULT nProcessOutputResult = pTransform->ProcessOutput(0, 1, &Buffer, &nStatus); 254 _Z45_MFHRESULT(nProcessOutputResult); 255 _Z3(atlTraceGeneral, 3, _T("Output Data Buffer, dwStatus %s, pSample 0x%p, pEvents 0x%p\n"), MF::FormatTransformOutputDataBufferFlags(Buffer.dwStatus), Buffer.pSample, Buffer.pEvents); 256 // NOTE: IMFTransform::ProcessOutput method, Stream Changes https://msdn.microsoft.com/en-us/library/windows/desktop/ms704014#Stream_Changes 257 if(nProcessOutputResult == MF_E_TRANSFORM_STREAM_CHANGE) 258 { 259 __D(Buffer.dwStatus & MFT_OUTPUT_DATA_BUFFER_FORMAT_CHANGE, nProcessOutputResult); 260 _A(!Buffer.pSample && !Buffer.pEvents); 261 { 262 MF::CMediaType pMediaType; 263 __C(pTransform->GetOutputCurrentType(0, &pMediaType)); 264 pMediaType.Trace(); 265 } 266 MF::CMediaType pOutputMediaType; 267 #pragma region Available Output Media Type 268 for(DWORD nTypeIndex = 0; ; nTypeIndex++) 269 { 270 MF::CMediaType pMediaType; 271 if(FAILED(pTransform->GetOutputAvailableType(0, nTypeIndex, &pMediaType))) 272 break; 273 pMediaType.Trace(); 274 if(!nTypeIndex) 275 pOutputMediaType = pMediaType; 276 } 277 #pragma endregion 278 __D(pOutputMediaType, nProcessOutputResult); 279 __C(pTransform->SetOutputType(0, pOutputMediaType, 0)); 280 break; 281 } 282 __C(nProcessOutputResult); 283 _A(Buffer.dwStatus == 0); 284 _A(!Buffer.pEvents); 285 MF::CSample pSample; 286 pSample.m_p.Attach(Buffer.pSample); 287 pSample.Trace(); 288 CComPtr<IMFMediaBuffer> pMediaBuffer; 289 __C(pSample->ConvertToContiguousBuffer(&pMediaBuffer)); 290 MF::CSample::CMediaBufferLock Lock(pMediaBuffer); 291 _Z4(atlTraceGeneral, 4, _T("Lock.m_pnData %s, Lock.m_nDataSize %d\n"), AtlFormatData(Lock.m_pnData, min(32, Lock.m_nDataSize)), Lock.m_nDataSize); 292 ProcessNalUnits(Lock.m_pnData, Lock.m_nDataSize, _T("Output")); 293 bComplete = TRUE; 294 } 295 break; 296 #pragma endregion 297 default: 298 __C(E_UNNAMED); 299 } 300 } 301 #pragma endregion 302 #else 303 #pragma region Synchronous Processing 304 UINT nFrameIndex = 0; 305 for(; ; ) 306 { 307 DWORD nInputStatusFlags; 308 __C(pTransform->GetInputStatus(0, &nInputStatusFlags)); 309 _Z4(atlTraceGeneral, 4, _T("nInputStatusFlags %s\n"), MF::FormatTransformProcessInputStatusFlags(nInputStatusFlags)); 310 if(nInputStatusFlags & MFT_INPUT_STATUS_ACCEPT_DATA) 311 { 312 MF::CSample pSample; 313 pSample.Create(); 314 pSample[MFSampleExtension_Discontinuity] = (UINT32) (nFrameIndex == 0 ? 1 : 0); 315 pSample[MFSampleExtension_CleanPoint] = (UINT32) 1; 316 pSample->SetSampleTime(nFrameIndex * 20 * 10000i64); 317 pSample->SetSampleDuration(20 * 10000i64); 318 CComPtr<IMFMediaBuffer> pMediaBuffer; 319 __C(MFCreateMemoryBuffer(1280 * 720 * 12 / 8, &pMediaBuffer)); 320 __C(pMediaBuffer->SetCurrentLength(1280 * 720 * 12 / 8)); 321 __C(pSample->AddBuffer(pMediaBuffer)); 322 pSample.Trace(); 323 const HRESULT nProcessInputResult = pTransform->ProcessInput(0, pSample, 0); 324 if(nProcessInputResult != MF_E_NOTACCEPTING) 325 { 326 __C(nProcessInputResult); 327 nFrameIndex++; 328 continue; 329 } 330 } 331 _A(OutputStreamInformation.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES | MFT_OUTPUT_STREAM_CAN_PROVIDE_SAMPLES)); 332 MFT_OUTPUT_DATA_BUFFER Buffer = { 0 }; 333 DWORD nStatus; 334 __C(pTransform->ProcessOutput(0, 1, &Buffer, &nStatus)); 335 MF::CSample pSample; 336 pSample.m_p.Attach(Buffer.pSample); 337 pSample.Trace(); 338 CComPtr<IMFMediaBuffer> pMediaBuffer; 339 __C(pSample->ConvertToContiguousBuffer(&pMediaBuffer)); 340 MF::CSample::CMediaBufferLock Lock(pMediaBuffer); 341 _Z4(atlTraceGeneral, 4, _T("Lock.m_pnData %s, Lock.m_nDataSize %d\n"), AtlFormatData(Lock.m_pnData, min(24, Lock.m_nDataSize)), Lock.m_nDataSize); 342 // TODO: ... 343 } 344 #pragma endregion 345 #endif 150 346 } 151 347 VOID RunMessageLoop()
Note: See TracChangeset
for help on using the changeset viewer.