mf_codec.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /* Copyright (C) 2013 Mamadou DIOP
  2. * Copyright (C) 2013 Doubango Telecom <http://www.doubango.org>
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. */
  19. #include "mf_codec.h"
  20. #include "mf_utils.h"
  21. #include "mf_sample_queue.h"
  22. #include "tinymedia/tmedia_common.h"
  23. #include "tsk_debug.h"
  24. #include <KS.h>
  25. #include <Codecapi.h>
  26. #include <assert.h>
  27. #include <initguid.h>
  28. // NV12 is the only format supported by all HW encoders and decoders
  29. #if !defined(kMFCodecUncompressedFormat)
  30. # define kMFCodecUncompressedFormat MFVideoFormat_NV12
  31. #endif
  32. // Max frames allowed in the queue
  33. #if !defined(kMFCodecQueuedFramesMax)
  34. # define kMFCodecQueuedFramesMax (30 << 1)
  35. #endif
  36. // Make sure usable on Win7 SDK targeting Win8 OS
  37. #if !defined(CODECAPI_AVLowLatencyMode)
  38. DEFINE_GUID(CODECAPI_AVLowLatencyMode,
  39. 0x9c27891a, 0xed7a, 0x40e1, 0x88, 0xe8, 0xb2, 0x27, 0x27, 0xa0, 0x24, 0xee);
  40. #endif
  41. #if !defined(CODECAPI_AVDecVideoH264ErrorConcealment)
  42. DEFINE_GUID(CODECAPI_AVDecVideoH264ErrorConcealment,
  43. 0xececace8, 0x3436, 0x462c, 0x92, 0x94, 0xcd, 0x7b, 0xac, 0xd7, 0x58, 0xa9);
  44. #endif
  45. //
  46. // MFCodec
  47. //
  48. MFCodec::MFCodec(MFCodecId_t eId, MFCodecType_t eType, IMFTransform *pMFT /*= NULL*/)
  49. : m_nRefCount(1)
  50. , m_eId(eId)
  51. , m_eType(eType)
  52. , m_pMFT(NULL)
  53. , m_pCodecAPI(NULL)
  54. , m_pOutputType(NULL)
  55. , m_pInputType(NULL)
  56. , m_dwInputID(0)
  57. , m_dwOutputID(0)
  58. , m_rtStart(0)
  59. , m_rtDuration(0)
  60. , m_pSampleIn(NULL)
  61. , m_pSampleOut(NULL)
  62. , m_pEventGenerator(NULL)
  63. , m_bIsAsync(FALSE)
  64. , m_bIsFirstFrame(TRUE)
  65. , m_bIsBundled(FALSE)
  66. , m_nMETransformNeedInputCount(0)
  67. , m_nMETransformHaveOutputCount(0)
  68. , m_pSampleQueueAsyncInput(NULL)
  69. {
  70. MFUtils::Startup();
  71. HRESULT hr = S_OK;
  72. switch(eId) {
  73. case MFCodecId_H264Base:
  74. case MFCodecId_H264Main: {
  75. m_eMediaType = MFCodecMediaType_Video;
  76. m_guidCompressedFormat = MFVideoFormat_H264;
  77. break;
  78. }
  79. case MFCodecId_AAC: {
  80. m_eMediaType = MFCodecMediaType_Audio;
  81. m_guidCompressedFormat = MFAudioFormat_AAC;
  82. break;
  83. }
  84. default: {
  85. assert(false);
  86. break;
  87. }
  88. }
  89. CHECK_HR(hr = MFCreateMediaType(&m_pOutputType));
  90. CHECK_HR(hr = MFCreateMediaType(&m_pInputType));
  91. if(pMFT) { // up to the caller to make sure all parameters are corrrect
  92. m_pMFT = pMFT;
  93. m_pMFT->AddRef();
  94. }
  95. else {
  96. CHECK_HR(hr = MFUtils::GetBestCodec(
  97. (m_eType == MFCodecType_Encoder) ? TRUE : FALSE, // Encoder ?
  98. (m_eMediaType == MFCodecMediaType_Video) ? MFMediaType_Video : MFMediaType_Audio, // Media Type
  99. (m_eType == MFCodecType_Encoder) ? kMFCodecUncompressedFormat : m_guidCompressedFormat/*GUID_NULL*/, // Input
  100. (m_eType == MFCodecType_Encoder) ? m_guidCompressedFormat : kMFCodecUncompressedFormat, // Output
  101. &m_pMFT));
  102. }
  103. hr = m_pMFT->QueryInterface(IID_PPV_ARGS(&m_pCodecAPI));
  104. if(FAILED(hr) && m_eType == MFCodecType_Encoder) { // Required only for Encoders
  105. CHECK_HR(hr);
  106. }
  107. CHECK_HR(hr = MFUtils::IsAsyncMFT(m_pMFT, &m_bIsAsync));
  108. if(m_bIsAsync) {
  109. m_pSampleQueueAsyncInput = new MFSampleQueue();
  110. if(!m_pSampleQueueAsyncInput) {
  111. CHECK_HR(hr = E_OUTOFMEMORY);
  112. }
  113. CHECK_HR(hr = MFUtils::UnlockAsyncMFT(m_pMFT));
  114. CHECK_HR(hr = m_pMFT->QueryInterface(IID_PPV_ARGS(&m_pEventGenerator)));
  115. }
  116. bail:
  117. if(FAILED(hr)) {
  118. SafeRelease(&m_pMFT);
  119. SafeRelease(&m_pCodecAPI);
  120. }
  121. if(!IsValid()) {
  122. TSK_DEBUG_ERROR("Failed to create codec with id = %d", m_eId);
  123. }
  124. }
  125. MFCodec::~MFCodec()
  126. {
  127. assert(m_nRefCount == 0);
  128. if(m_bIsAsync && m_pMFT) {
  129. m_pMFT->ProcessMessage(MFT_MESSAGE_NOTIFY_END_OF_STREAM, NULL);
  130. m_pMFT->ProcessMessage(MFT_MESSAGE_COMMAND_DRAIN, NULL);
  131. }
  132. SafeRelease(&m_pMFT);
  133. SafeRelease(&m_pCodecAPI);
  134. SafeRelease(&m_pOutputType);
  135. SafeRelease(&m_pInputType);
  136. SafeRelease(&m_pSampleIn);
  137. SafeRelease(&m_pSampleOut);
  138. SafeRelease(&m_pEventGenerator);
  139. SafeRelease(&m_pSampleQueueAsyncInput);
  140. }
  141. ULONG MFCodec::AddRef()
  142. {
  143. return InterlockedIncrement(&m_nRefCount);
  144. }
  145. ULONG MFCodec::Release()
  146. {
  147. ULONG uCount = InterlockedDecrement(&m_nRefCount);
  148. if (uCount == 0) {
  149. delete this;
  150. }
  151. // For thread safety, return a temporary variable.
  152. return uCount;
  153. }
  154. HRESULT MFCodec::QueryInterface(REFIID iid, void** ppv)
  155. {
  156. if(!IsValid()) {
  157. return E_FAIL;
  158. }
  159. return m_pMFT->QueryInterface(iid, ppv);
  160. }
  161. // IMFAsyncCallback
  162. STDMETHODIMP MFCodec::GetParameters(DWORD *pdwFlags, DWORD *pdwQueue)
  163. {
  164. return E_NOTIMPL;
  165. }
  166. STDMETHODIMP MFCodec::Invoke(IMFAsyncResult *pAsyncResult)
  167. {
  168. HRESULT hr = S_OK, hrStatus = S_OK;
  169. IMFMediaEvent* pEvent = NULL;
  170. MediaEventType meType = MEUnknown;
  171. CHECK_HR(hr = m_pEventGenerator->EndGetEvent(pAsyncResult, &pEvent));
  172. CHECK_HR(hr = pEvent->GetType(&meType));
  173. CHECK_HR(hr = pEvent->GetStatus(&hrStatus));
  174. if (SUCCEEDED(hrStatus)) {
  175. switch(meType) {
  176. case METransformNeedInput: {
  177. InterlockedIncrement(&m_nMETransformNeedInputCount);
  178. break;
  179. }
  180. case METransformHaveOutput: {
  181. InterlockedIncrement(&m_nMETransformHaveOutputCount);
  182. break;
  183. }
  184. }
  185. }
  186. CHECK_HR(hr = m_pEventGenerator->BeginGetEvent(this, NULL));
  187. bail:
  188. SafeRelease(&pEvent);
  189. return hr;
  190. }
  191. HRESULT MFCodec::ProcessInput(IMFSample* pSample)
  192. {
  193. assert(IsReady());
  194. HRESULT hr = S_OK;
  195. if(m_bIsFirstFrame) {
  196. if(m_bIsAsync && !m_bIsBundled) {
  197. CHECK_HR(hr = m_pMFT->ProcessMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, NULL));
  198. CHECK_HR(hr = m_pEventGenerator->BeginGetEvent(this, NULL));
  199. }
  200. m_bIsFirstFrame = FALSE;
  201. }
  202. if(m_bIsAsync) {
  203. if(m_nMETransformNeedInputCount == 1 && m_pSampleQueueAsyncInput->IsEmpty()) {
  204. InterlockedDecrement(&m_nMETransformNeedInputCount);
  205. return m_pMFT->ProcessInput(m_dwInputID, pSample, 0);
  206. }
  207. if(m_pSampleQueueAsyncInput->Count() > kMFCodecQueuedFramesMax) {
  208. m_pSampleQueueAsyncInput->Clear();
  209. CHECK_HR(hr = E_UNEXPECTED);
  210. }
  211. // Input sample holds shared memory (also used by other samples)
  212. IMFSample *pSampleCopy = NULL;
  213. IMFMediaBuffer *pMediaBuffer = NULL, *pMediaBufferCopy = NULL;
  214. BYTE *pBufferPtr = NULL, *pBufferPtrCopy = NULL;
  215. DWORD dwDataLength = 0;
  216. BOOL bMediaBufferLocked = FALSE, bMediaBufferLockedCopy = FALSE;
  217. CHECK_HR(hr = pSample->GetBufferByIndex(0, &pMediaBuffer));
  218. hr = pMediaBuffer->GetCurrentLength(&dwDataLength);
  219. if(FAILED(hr)) {
  220. goto endofcopy;
  221. }
  222. hr = pMediaBuffer->Lock(&pBufferPtr, NULL, NULL);
  223. if(FAILED(hr)) {
  224. goto endofcopy;
  225. }
  226. bMediaBufferLocked = TRUE;
  227. hr = MFUtils::CreateMediaSample(dwDataLength, &pSampleCopy);
  228. if(FAILED(hr)) {
  229. goto endofcopy;
  230. }
  231. hr = pSampleCopy->GetBufferByIndex(0, &pMediaBufferCopy);
  232. if(FAILED(hr)) {
  233. goto endofcopy;
  234. }
  235. hr = pMediaBufferCopy->Lock(&pBufferPtrCopy, NULL, NULL);
  236. if(FAILED(hr)) {
  237. goto endofcopy;
  238. }
  239. bMediaBufferLockedCopy = TRUE;
  240. memcpy(pBufferPtrCopy, pBufferPtr, dwDataLength);
  241. hr = pMediaBufferCopy->SetCurrentLength(dwDataLength);
  242. if(FAILED(hr)) {
  243. goto endofcopy;
  244. }
  245. LONGLONG hnsSampleTime = 0;
  246. LONGLONG hnsSampleDuration = 0;
  247. hr = pSample->GetSampleTime(&hnsSampleTime);
  248. if(SUCCEEDED(hr)) {
  249. hr = pSampleCopy->SetSampleTime(hnsSampleTime);
  250. }
  251. hr = pSample->GetSampleDuration(&hnsSampleDuration);
  252. if(SUCCEEDED(hr)) {
  253. hr = pSampleCopy->SetSampleDuration(hnsSampleDuration);
  254. }
  255. // EnQueue
  256. hr = m_pSampleQueueAsyncInput->Queue(pSampleCopy);
  257. endofcopy:
  258. if(pMediaBuffer && bMediaBufferLocked) {
  259. pMediaBuffer->Unlock();
  260. }
  261. if(pMediaBufferCopy && bMediaBufferLockedCopy) {
  262. pMediaBufferCopy->Unlock();
  263. }
  264. SafeRelease(&pSampleCopy);
  265. SafeRelease(&pMediaBuffer);
  266. CHECK_HR(hr);
  267. while(m_nMETransformNeedInputCount > 0) {
  268. if(m_pSampleQueueAsyncInput->IsEmpty()) {
  269. break;
  270. }
  271. IMFSample *_pSample = NULL;
  272. hr = m_pSampleQueueAsyncInput->Dequeue(&_pSample);
  273. if(SUCCEEDED(hr)) {
  274. InterlockedDecrement(&m_nMETransformNeedInputCount);
  275. hr = m_pMFT->ProcessInput(m_dwInputID, _pSample, 0);
  276. }
  277. SafeRelease(&_pSample);
  278. CHECK_HR(hr);
  279. }
  280. }
  281. else {
  282. CHECK_HR(hr = m_pMFT->ProcessInput(m_dwInputID, pSample, 0));
  283. }
  284. bail:
  285. return hr;
  286. }
  287. HRESULT MFCodec::ProcessOutput(IMFSample **ppSample)
  288. {
  289. assert(IsReady());
  290. if(m_bIsAsync) {
  291. if(m_nMETransformHaveOutputCount == 0) {
  292. return S_OK;
  293. }
  294. InterlockedDecrement(&m_nMETransformHaveOutputCount);
  295. }
  296. *ppSample = NULL;
  297. IMFMediaBuffer* pBufferOut = NULL;
  298. DWORD dwStatus;
  299. HRESULT hr = S_OK;
  300. MFT_OUTPUT_STREAM_INFO mftStreamInfo = { 0 };
  301. MFT_OUTPUT_DATA_BUFFER mftOutputData = { 0 };
  302. CHECK_HR(hr = m_pMFT->GetOutputStreamInfo(m_dwOutputID, &mftStreamInfo));
  303. BOOL bOutputStreamProvidesSamples = (mftStreamInfo.dwFlags & MFT_OUTPUT_STREAM_PROVIDES_SAMPLES) == MFT_OUTPUT_STREAM_PROVIDES_SAMPLES;
  304. if(!bOutputStreamProvidesSamples) {
  305. if(!m_pSampleOut) {
  306. CHECK_HR(hr = MFUtils::CreateMediaSample(mftStreamInfo.cbSize, &m_pSampleOut));
  307. hr = m_pSampleOut->GetBufferByIndex(0, &pBufferOut);
  308. if(FAILED(hr)) {
  309. SafeRelease(&m_pSampleOut);
  310. CHECK_HR(hr);
  311. }
  312. }
  313. else {
  314. DWORD dwMaxLength = 0;
  315. CHECK_HR(hr = m_pSampleOut->GetBufferByIndex(0, &pBufferOut));
  316. CHECK_HR(hr = pBufferOut->GetMaxLength(&dwMaxLength));
  317. if(dwMaxLength < mftStreamInfo.cbSize) {
  318. CHECK_HR(hr = m_pSampleOut->RemoveAllBuffers());
  319. SafeRelease(&pBufferOut);
  320. CHECK_HR(hr = MFCreateMemoryBuffer(mftStreamInfo.cbSize, &pBufferOut));
  321. CHECK_HR(hr = m_pSampleOut->AddBuffer(pBufferOut));
  322. }
  323. }
  324. }
  325. if(pBufferOut) {
  326. CHECK_HR(hr = pBufferOut->SetCurrentLength(0));
  327. }
  328. //Set the output sample
  329. mftOutputData.pSample = bOutputStreamProvidesSamples ? NULL : m_pSampleOut;
  330. //Set the output id
  331. mftOutputData.dwStreamID = m_dwOutputID;
  332. //Generate the output sample
  333. hr = m_pMFT->ProcessOutput(0, 1, &mftOutputData, &dwStatus);
  334. if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
  335. hr = S_OK;
  336. goto bail;
  337. }
  338. if (FAILED(hr)) {
  339. goto bail;
  340. }
  341. *ppSample = mftOutputData.pSample;
  342. if(*ppSample) {
  343. (*ppSample)->AddRef();
  344. }
  345. bail:
  346. if(bOutputStreamProvidesSamples) {
  347. SafeRelease(&mftOutputData.pSample);
  348. }
  349. SafeRelease(&pBufferOut);
  350. return hr;
  351. }
  352. bool MFCodec::IsValid()
  353. {
  354. return (m_pMFT && (m_eType == MFCodecType_Decoder || m_pCodecAPI));
  355. }
  356. bool MFCodec::IsReady()
  357. {
  358. return (IsValid() && m_pOutputType && m_pInputType);
  359. }
  360. HRESULT MFCodec::Process(const void* pcInputPtr, UINT32 nInputSize, IMFSample **ppSampleOut)
  361. {
  362. if(!pcInputPtr || !nInputSize || !ppSampleOut) {
  363. TSK_DEBUG_ERROR("Invalid parameter");
  364. return E_INVALIDARG;
  365. }
  366. *ppSampleOut = NULL;
  367. HRESULT hr = S_OK;
  368. IMFMediaBuffer* pBufferIn = NULL;
  369. BYTE* pBufferPtr = NULL;
  370. BOOL bMediaChangeHandled = FALSE; // Endless loop guard
  371. if(!m_pSampleIn) {
  372. CHECK_HR(hr = MFUtils::CreateMediaSample(nInputSize, &m_pSampleIn));
  373. hr = m_pSampleIn->GetBufferByIndex(0, &pBufferIn);
  374. if(FAILED(hr)) {
  375. SafeRelease(&m_pSampleIn);
  376. CHECK_HR(hr);
  377. }
  378. }
  379. else {
  380. DWORD dwMaxLength = 0;
  381. CHECK_HR(hr = m_pSampleIn->GetBufferByIndex(0, &pBufferIn));
  382. CHECK_HR(hr = pBufferIn->GetMaxLength(&dwMaxLength));
  383. if(dwMaxLength < nInputSize) {
  384. CHECK_HR(hr = m_pSampleIn->RemoveAllBuffers());
  385. SafeRelease(&pBufferIn);
  386. CHECK_HR(hr = MFCreateMemoryBuffer(nInputSize, &pBufferIn));
  387. CHECK_HR(hr = m_pSampleIn->AddBuffer(pBufferIn));
  388. }
  389. }
  390. CHECK_HR(hr = pBufferIn->Lock(&pBufferPtr, NULL, NULL));
  391. memcpy(pBufferPtr, pcInputPtr, nInputSize);
  392. CHECK_HR(hr = pBufferIn->Unlock());
  393. CHECK_HR(hr = pBufferIn->SetCurrentLength(nInputSize));
  394. if(m_eType == MFCodecType_Encoder) {
  395. CHECK_HR(hr = m_pSampleIn->SetSampleDuration(m_rtDuration));
  396. CHECK_HR(hr = m_pSampleIn->SetSampleTime(m_rtStart)); // FIXME: use clock(), Same for custom source
  397. }
  398. Label_ProcessInput:
  399. hr = ProcessInput(m_pSampleIn);
  400. while(hr == MF_E_NOTACCEPTING) {
  401. TSK_DEBUG_INFO("MF_E_NOTACCEPTING");
  402. IMFSample* pSample = NULL;
  403. hr = ProcessOutput(&pSample);
  404. if(SUCCEEDED(hr) && pSample) {
  405. SafeRelease(ppSampleOut);
  406. *ppSampleOut = pSample, pSample = NULL;
  407. hr = m_pSampleIn->SetUINT32(MFSampleExtension_Discontinuity, TRUE);
  408. hr = ProcessInput(m_pSampleIn);
  409. }
  410. }
  411. if(!*ppSampleOut) {
  412. hr = ProcessOutput(ppSampleOut);
  413. if(hr == MF_E_TRANSFORM_STREAM_CHANGE) { /* Handling Stream Changes: http://msdn.microsoft.com/en-us/library/windows/desktop/ee663587(v=vs.85).aspx */
  414. TSK_DEBUG_INFO("[MF Codec] Stream changed");
  415. if(m_eType == MFCodecType_Decoder) {
  416. IMFMediaType *pTypeOut = NULL;
  417. hr = m_pMFT->GetOutputAvailableType(m_dwOutputID, 0, &pTypeOut);
  418. if(SUCCEEDED(hr)) {
  419. UINT32 uWidth = 0, uHeight = 0;
  420. hr = MFGetAttributeSize(pTypeOut, MF_MT_FRAME_SIZE, &uWidth, &uHeight);
  421. if(SUCCEEDED(hr)) {
  422. TSK_DEBUG_INFO("[MF Decoder] New size: width=%u, height=%u", uWidth, uHeight);
  423. hr = m_pMFT->SetOutputType(m_dwOutputID, pTypeOut, 0);
  424. if(SUCCEEDED(hr)) {
  425. SafeRelease(&m_pOutputType);
  426. pTypeOut->AddRef();
  427. m_pOutputType = pTypeOut;
  428. if(m_eMediaType == MFCodecMediaType_Video) {
  429. dynamic_cast<MFCodecVideo*>(this)->m_nWidth = uWidth;
  430. dynamic_cast<MFCodecVideo*>(this)->m_nHeight = uHeight;
  431. }
  432. }
  433. }
  434. }
  435. SafeRelease(&pTypeOut);
  436. if(SUCCEEDED(hr)) {
  437. if(!bMediaChangeHandled) {
  438. bMediaChangeHandled = TRUE;
  439. goto Label_ProcessInput;
  440. }
  441. }
  442. }
  443. }
  444. }
  445. m_rtStart += m_rtDuration;
  446. bail:
  447. SafeRelease(&pBufferIn);
  448. return hr;
  449. }
  450. enum tmedia_chroma_e MFCodec::GetUncompressedChroma()
  451. {
  452. if(kMFCodecUncompressedFormat == MFVideoFormat_NV12) {
  453. return tmedia_chroma_nv12;
  454. }
  455. assert(false);
  456. return tmedia_chroma_none;
  457. }
  458. //
  459. // MFCodecVideo
  460. //
  461. MFCodecVideo::MFCodecVideo(MFCodecId_t eId, MFCodecType_t eType, IMFTransform *pMFT /*= NULL*/)
  462. : MFCodec(eId, eType, pMFT)
  463. , m_nFrameRate(0)
  464. , m_nWidth(0)
  465. , m_nHeight(0)
  466. {
  467. assert(m_eMediaType == MFCodecMediaType_Video);
  468. }
  469. MFCodecVideo::~MFCodecVideo()
  470. {
  471. }
  472. HRESULT MFCodecVideo::Initialize(
  473. UINT32 nFrameRate,
  474. UINT32 nWidth,
  475. UINT32 nHeight,
  476. UINT32 nOutputBitRateInBps /*= 0*/
  477. )
  478. {
  479. assert(IsValid());
  480. HRESULT hr = S_OK;
  481. VARIANT var = {0};
  482. // make sure identifiers are zero-based (other layouts not supported yet)
  483. hr = m_pMFT->GetStreamIDs(1, &m_dwInputID, 1, &m_dwOutputID);
  484. if (hr == E_NOTIMPL) {
  485. m_dwInputID = 0;
  486. m_dwOutputID = 0;
  487. hr = S_OK;
  488. }
  489. else if (FAILED(hr)) {
  490. TSK_DEBUG_ERROR("The stream identifiers are not zero-based");
  491. return hr;
  492. }
  493. m_rtStart = 0;
  494. CHECK_HR(hr = MFFrameRateToAverageTimePerFrame(nFrameRate, 1, &m_rtDuration));
  495. CHECK_HR(hr = m_pOutputType->SetGUID(MF_MT_MAJOR_TYPE, (m_eMediaType == MFCodecMediaType_Video) ? MFMediaType_Video : MFMediaType_Audio));
  496. CHECK_HR(hr = m_pInputType->SetGUID(MF_MT_MAJOR_TYPE, (m_eMediaType == MFCodecMediaType_Video) ? MFMediaType_Video : MFMediaType_Audio));
  497. CHECK_HR(hr = m_pOutputType->SetGUID(MF_MT_SUBTYPE, (m_eType == MFCodecType_Encoder) ? m_guidCompressedFormat : kMFCodecUncompressedFormat));
  498. CHECK_HR(hr = m_pInputType->SetGUID(MF_MT_SUBTYPE, (m_eType == MFCodecType_Encoder) ? kMFCodecUncompressedFormat : m_guidCompressedFormat));
  499. CHECK_HR(hr = m_pOutputType->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, (m_eType == MFCodecType_Encoder) ? FALSE : TRUE));
  500. CHECK_HR(hr = m_pInputType->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, (m_eType == MFCodecType_Encoder) ? TRUE : FALSE));
  501. CHECK_HR(hr = m_pOutputType->SetUINT32(MF_MT_FIXED_SIZE_SAMPLES, (m_eType == MFCodecType_Encoder) ? FALSE : TRUE));
  502. CHECK_HR(hr = m_pInputType->SetUINT32(MF_MT_FIXED_SIZE_SAMPLES, (m_eType == MFCodecType_Encoder) ? TRUE : FALSE));
  503. // Set bitrate
  504. // Set (MF_MT_AVG_BITRATE) for MediaType
  505. // Set (CODECAPI_AVEncCommonMeanBitRate) for H.264
  506. hr = SetBitRate(nOutputBitRateInBps);
  507. CHECK_HR(hr = m_pOutputType->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive));
  508. CHECK_HR(hr = m_pInputType->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive));
  509. CHECK_HR(hr = MFSetAttributeSize(m_pOutputType, MF_MT_FRAME_SIZE, nWidth, nHeight));
  510. CHECK_HR(hr = MFSetAttributeSize(m_pInputType, MF_MT_FRAME_SIZE, nWidth, nHeight));
  511. CHECK_HR(hr = MFSetAttributeRatio(m_pOutputType, MF_MT_FRAME_RATE, nFrameRate, 1));
  512. CHECK_HR(hr = MFSetAttributeRatio(m_pInputType, MF_MT_FRAME_RATE, nFrameRate, 1));
  513. CHECK_HR(hr = MFSetAttributeRatio(m_pOutputType, MF_MT_PIXEL_ASPECT_RATIO, 1, 1));
  514. CHECK_HR(hr = MFSetAttributeRatio(m_pInputType, MF_MT_PIXEL_ASPECT_RATIO, 1, 1));
  515. // Encoder: Output format must be set before input
  516. // Decoder: Input format must be set before output
  517. if(m_eType == MFCodecType_Encoder) {
  518. CHECK_HR(hr = m_pMFT->SetOutputType(m_dwOutputID, m_pOutputType, 0));
  519. CHECK_HR(hr = m_pMFT->SetInputType(m_dwInputID, m_pInputType, 0));
  520. }
  521. else {
  522. CHECK_HR(hr = m_pMFT->SetInputType(m_dwInputID, m_pInputType, 0));
  523. CHECK_HR(hr = m_pMFT->SetOutputType(m_dwOutputID, m_pOutputType, 0));
  524. }
  525. if(m_eId == MFCodecId_H264Base || m_eId == MFCodecId_H264Main) {
  526. if(m_eType == MFCodecType_Decoder) {
  527. // Only decoder support GetAttributes()
  528. IMFAttributes* pAttributes = NULL;
  529. hr = m_pMFT->GetAttributes(&pAttributes);
  530. if(SUCCEEDED(hr)) {
  531. // FIXME: Very strange that "CODECAPI_AVLowLatencyMode" only works with "IMFAttributes->" and not "ICodecAPI->SetValue()"
  532. hr = pAttributes->SetUINT32(CODECAPI_AVLowLatencyMode, TRUE);
  533. }
  534. SafeRelease(&pAttributes);
  535. }
  536. else {
  537. var.vt = VT_BOOL;
  538. var.boolVal = VARIANT_TRUE;
  539. hr = m_pCodecAPI->SetValue(&CODECAPI_AVLowLatencyMode, &var);
  540. var.vt = VT_BOOL;
  541. var.boolVal = VARIANT_TRUE;
  542. hr = m_pCodecAPI->SetValue(&CODECAPI_AVEncCommonLowLatency, &var); // Correct for the decoder
  543. // Disable B-Frames
  544. var.vt = VT_UI4;
  545. var.ulVal = 0;
  546. hr = m_pCodecAPI->SetValue(&CODECAPI_AVEncMPVDefaultBPictureCount, &var);
  547. // Constant bitrate (updated using RTCP)
  548. var.vt = VT_UI4;
  549. var.ulVal = eAVEncCommonRateControlMode_CBR;
  550. hr = m_pCodecAPI->SetValue(&CODECAPI_AVEncCommonRateControlMode, &var);
  551. }
  552. hr = S_OK; // Not mandatory features
  553. }
  554. bail:
  555. if(SUCCEEDED(hr)) {
  556. m_nFrameRate = nFrameRate;
  557. m_nWidth = nWidth;
  558. m_nHeight = nHeight;
  559. }
  560. return hr;
  561. }
  562. HRESULT MFCodecVideo::SetGOPSize(UINT32 nFramesCount)
  563. {
  564. assert(IsValid());
  565. HRESULT hr = S_OK;
  566. if(m_eType == MFCodecType_Encoder && (m_eId == MFCodecId_H264Base || m_eId == MFCodecId_H264Main)) {
  567. VARIANT var = {0};
  568. var.vt = VT_UI4;
  569. var.ullVal = nFramesCount;
  570. CHECK_HR(hr = m_pCodecAPI->SetValue(&CODECAPI_AVEncMPVGOPSize, &var));
  571. }
  572. bail:
  573. return hr;
  574. }
  575. HRESULT MFCodecVideo::SetBitRate(UINT32 nBitRateInBps)
  576. {
  577. assert(IsValid());
  578. HRESULT hr = S_OK;
  579. if(nBitRateInBps > 0 && m_eType == MFCodecType_Encoder) {
  580. CHECK_HR(hr = m_pOutputType->SetUINT32(MF_MT_AVG_BITRATE, nBitRateInBps));
  581. if((m_eId == MFCodecId_H264Base || m_eId == MFCodecId_H264Main)) {
  582. VARIANT var = {0};
  583. // Set BitRate
  584. var.vt = VT_UI4;
  585. var.ullVal = nBitRateInBps;
  586. CHECK_HR(hr = m_pCodecAPI->SetValue(&CODECAPI_AVEncCommonMeanBitRate, &var));
  587. }
  588. }
  589. bail:
  590. return hr;
  591. }
  592. HRESULT MFCodecVideo::IsSetSliceMaxSizeInBytesSupported(BOOL &supported)
  593. {
  594. HRESULT hr = S_OK;
  595. supported = FALSE;
  596. if ((m_eId == MFCodecId_H264Base || m_eId == MFCodecId_H264Main)) {
  597. #if defined(CODECAPI_AVEncSliceControlMode) && defined(CODECAPI_AVEncSliceControlSize)
  598. if (m_pCodecAPI->IsSupported(&CODECAPI_AVEncSliceControlMode) == S_OK && m_pCodecAPI->IsSupported(&CODECAPI_AVEncSliceControlSize) == S_OK) {
  599. supported = TRUE;
  600. }
  601. #endif
  602. }
  603. return hr;
  604. }
  605. HRESULT MFCodecVideo::SetSliceMaxSizeInBytes(UINT32 nSliceMaxSizeInBytes)
  606. {
  607. assert(IsValid() && nSliceMaxSizeInBytes > 0);
  608. HRESULT hr = S_OK;
  609. if ((m_eId == MFCodecId_H264Base || m_eId == MFCodecId_H264Main)) {
  610. #if defined(CODECAPI_AVEncSliceControlMode) && defined(CODECAPI_AVEncSliceControlSize)
  611. if (m_pCodecAPI->IsSupported(&CODECAPI_AVEncSliceControlMode) == S_OK && m_pCodecAPI->IsSupported(&CODECAPI_AVEncSliceControlSize) == S_OK) {
  612. VARIANT var = { 0 };
  613. var.vt = VT_UI4;
  614. var.ulVal = 1; // Bits
  615. CHECK_HR(hr = m_pCodecAPI->SetValue(&CODECAPI_AVEncSliceControlMode, &var));
  616. var.ulVal = (nSliceMaxSizeInBytes << 3); // From Bytes to Bits
  617. CHECK_HR(hr = m_pCodecAPI->SetValue(&CODECAPI_AVEncSliceControlSize, &var));
  618. }
  619. #else
  620. CHECK_HR(hr = S_OK);
  621. #endif
  622. }
  623. bail:
  624. return hr;
  625. }
  626. HRESULT MFCodecVideo::RequestKeyFrame()
  627. {
  628. assert(IsValid());
  629. HRESULT hr = S_OK;
  630. if ((m_eId == MFCodecId_H264Base || m_eId == MFCodecId_H264Main)) {
  631. #if defined(CODECAPI_AVEncVideoForceKeyFrame)
  632. if (m_pCodecAPI->IsSupported(&CODECAPI_AVEncVideoForceKeyFrame) == S_OK) {
  633. VARIANT var = { 0 };
  634. var.vt = VT_UI4;
  635. var.ulVal = 1;
  636. CHECK_HR(hr = m_pCodecAPI->SetValue(&CODECAPI_AVEncVideoForceKeyFrame, &var));
  637. }
  638. #else
  639. CHECK_HR(hr = S_OK);
  640. #endif
  641. }
  642. bail:
  643. return hr;
  644. }
  645. //
  646. // MFCodecVideo
  647. //
  648. MFCodecVideoH264::MFCodecVideoH264(MFCodecId_t eId, MFCodecType_t eType, IMFTransform *pMFT /*= NULL*/)
  649. : MFCodecVideo(eId, eType, pMFT)
  650. {
  651. assert(eId == MFCodecId_H264Base || eId == MFCodecId_H264Main);
  652. HRESULT hr = S_OK;
  653. if(m_pOutputType) {
  654. CHECK_HR(hr = m_pOutputType->SetUINT32(MF_MT_MPEG2_PROFILE, (m_eId == MFCodecId_H264Base) ? eAVEncH264VProfile_Base : eAVEncH264VProfile_Main));
  655. }
  656. bail:
  657. assert(SUCCEEDED(hr));
  658. }
  659. MFCodecVideoH264::~MFCodecVideoH264()
  660. {
  661. }
  662. MFCodecVideoH264* MFCodecVideoH264::CreateCodecH264Base(MFCodecType_t eType, IMFTransform *pMFT /*= NULL*/)
  663. {
  664. MFCodecVideoH264* pCodec = new MFCodecVideoH264(MFCodecId_H264Base, eType, pMFT);
  665. if(pCodec && !pCodec->IsValid()) {
  666. SafeRelease(&pCodec);
  667. }
  668. return pCodec;
  669. }
  670. MFCodecVideoH264* MFCodecVideoH264::CreateCodecH264Main(MFCodecType_t eType, IMFTransform *pMFT /*= NULL*/)
  671. {
  672. MFCodecVideoH264* pCodec = new MFCodecVideoH264(MFCodecId_H264Main, eType, pMFT);
  673. if(pCodec && !pCodec->IsValid()) {
  674. SafeRelease(&pCodec);
  675. }
  676. return pCodec;
  677. }