1
0

MFTDecoder.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. #include "MFTDecoder.h"
  2. #include <Mfapi.h>
  3. #include <wmcodecdsp.h>
  4. #include <Mferror.h>
  5. //-----------------------------------------------------------------------------
  6. // GetDefaultStride
  7. //
  8. // Gets the default stride for a video frame, assuming no extra padding bytes.
  9. //
  10. //-----------------------------------------------------------------------------
  11. HRESULT GetDefaultStride(IMFMediaType *pType, LONG *plStride)
  12. {
  13. LONG lStride = 0;
  14. // Try to get the default stride from the media type.
  15. HRESULT hr = pType->GetUINT32(MF_MT_DEFAULT_STRIDE, (UINT32*)&lStride);
  16. if (FAILED(hr))
  17. {
  18. // Attribute not set. Try to calculate the default stride.
  19. GUID subtype = GUID_NULL;
  20. UINT32 width = 0;
  21. UINT32 height = 0;
  22. // Get the subtype and the image size.
  23. hr = pType->GetGUID(MF_MT_SUBTYPE, &subtype);
  24. if (SUCCEEDED(hr))
  25. {
  26. hr = MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &width, &height);
  27. }
  28. if (SUCCEEDED(hr))
  29. {
  30. hr = MFGetStrideForBitmapInfoHeader(subtype.Data1, width, &lStride);
  31. }
  32. // Set the attribute for later reference.
  33. if (SUCCEEDED(hr))
  34. {
  35. (void)pType->SetUINT32(MF_MT_DEFAULT_STRIDE, UINT32(lStride));
  36. }
  37. }
  38. if (SUCCEEDED(hr))
  39. {
  40. *plStride = lStride;
  41. }
  42. return hr;
  43. }
  44. MFTDecoder::MFTDecoder()
  45. {
  46. decoder = 0;
  47. stride = 0;
  48. width = 0;
  49. height = 0;
  50. }
  51. MFTDecoder::~MFTDecoder()
  52. {
  53. if (decoder) {
  54. decoder->Release();
  55. }
  56. }
  57. static HRESULT CreateInputMediaType(IMFMediaType **_media_type)
  58. {
  59. HRESULT hr=E_FAIL;
  60. IMFMediaType *media_type=0;
  61. do {
  62. hr = MFCreateMediaType(&media_type);
  63. if (FAILED(hr)) {
  64. break;
  65. }
  66. hr = media_type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
  67. if (FAILED(hr)) {
  68. break;
  69. }
  70. hr = media_type->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_H264);
  71. if (FAILED(hr)) {
  72. break;
  73. }
  74. *_media_type = media_type;
  75. return S_OK;
  76. } while(0);
  77. if (media_type) {
  78. media_type->Release();
  79. }
  80. return hr;
  81. }
  82. HRESULT MFTDecoder::Open()
  83. {
  84. HRESULT hr=E_FAIL;
  85. hr = CoCreateInstance(CLSID_CMSH264DecoderMFT, NULL, CLSCTX_INPROC_SERVER, __uuidof(IMFTransform), (void**)&decoder);
  86. if (FAILED(hr)) {
  87. return hr;
  88. }
  89. /* set input */
  90. IMFMediaType *media_type=0;
  91. hr = CreateInputMediaType(&media_type);
  92. if (FAILED(hr)) {
  93. return hr;
  94. }
  95. hr = decoder->SetInputType(0, media_type, 0);
  96. media_type->Release();
  97. if (FAILED(hr)) {
  98. return hr;
  99. }
  100. /* set output */
  101. hr = decoder->GetOutputAvailableType(0, 0, &media_type);
  102. if (FAILED(hr)) {
  103. return hr;
  104. }
  105. hr = decoder->SetOutputType(0, media_type, 0);
  106. media_type->Release();
  107. if (FAILED(hr)) {
  108. return hr;
  109. }
  110. decoder->ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0);
  111. decoder->ProcessMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0);
  112. return S_OK;
  113. }
  114. MFOffset MFTDecoder::MakeOffset(float v)
  115. {
  116. MFOffset offset{};
  117. offset.value = short(v);
  118. offset.fract = WORD(65536 * (v - offset.value));
  119. return offset;
  120. }
  121. MFVideoArea MFTDecoder::MakeArea(float x, float y, DWORD width, DWORD height)
  122. {
  123. MFVideoArea area{};
  124. area.OffsetX = MakeOffset(x);
  125. area.OffsetY = MakeOffset(y);
  126. area.Area.cx = width;
  127. area.Area.cy = height;
  128. return area;
  129. }
  130. HRESULT MFTDecoder::GetVideoDisplayArea(IMFMediaType* pType, MFVideoArea* pArea)
  131. {
  132. HRESULT hr = S_OK;
  133. BOOL bPanScan = FALSE;
  134. UINT32 width = 0, height = 0;
  135. bPanScan = MFGetAttributeUINT32(pType, MF_MT_PAN_SCAN_ENABLED, FALSE);
  136. // In pan-and-scan mode, try to get the pan-and-scan region.
  137. if (bPanScan)
  138. {
  139. hr = pType->GetBlob(MF_MT_PAN_SCAN_APERTURE, (UINT8*)pArea,
  140. sizeof(MFVideoArea), NULL);
  141. }
  142. // If not in pan-and-scan mode, or the pan-and-scan region is not set,
  143. // get the minimimum display aperture.
  144. if (!bPanScan || hr == MF_E_ATTRIBUTENOTFOUND)
  145. {
  146. hr = pType->GetBlob(MF_MT_MINIMUM_DISPLAY_APERTURE, (UINT8*)pArea,
  147. sizeof(MFVideoArea), NULL);
  148. if (hr == MF_E_ATTRIBUTENOTFOUND)
  149. {
  150. // Minimum display aperture is not set.
  151. // For backward compatibility with some components,
  152. // check for a geometric aperture.
  153. hr = pType->GetBlob(MF_MT_GEOMETRIC_APERTURE, (UINT8*)pArea,
  154. sizeof(MFVideoArea), NULL);
  155. }
  156. // Default: Use the entire video area.
  157. if (hr == MF_E_ATTRIBUTENOTFOUND)
  158. {
  159. hr = MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &width, &height);
  160. if (SUCCEEDED(hr))
  161. {
  162. *pArea = MakeArea(0.0, 0.0, width, height);
  163. }
  164. }
  165. }
  166. return hr;
  167. }
  168. HRESULT MFTDecoder::GetOutputFormat(UINT *width, UINT *height, bool *flip, double *aspect)
  169. {
  170. HRESULT hr=E_FAIL;
  171. IMFMediaType *media_type = 0;
  172. MFVideoArea pArea;
  173. do {
  174. hr = decoder->GetOutputCurrentType(0, &media_type);
  175. if (FAILED(hr)) {
  176. break;
  177. }
  178. //if (width && height) {
  179. // hr = MFGetAttributeSize(media_type, MF_MT_FRAME_SIZE, width, height);
  180. // if (FAILED(hr)) {
  181. // break;
  182. // }
  183. //}
  184. if (width && height) {
  185. hr = GetVideoDisplayArea(media_type, &pArea);
  186. if (FAILED(hr)) {
  187. break;
  188. }
  189. *width = pArea.Area.cx;
  190. *height = pArea.Area.cy;
  191. }
  192. if (flip) {
  193. LONG stride;
  194. hr = GetDefaultStride(media_type, &stride);
  195. if (FAILED(hr)) {
  196. break;
  197. }
  198. *flip = stride<0;
  199. }
  200. if (aspect) {
  201. MFRatio PAR = {0};
  202. hr = MFGetAttributeRatio(media_type, MF_MT_PIXEL_ASPECT_RATIO,
  203. (UINT32*)&PAR.Numerator,
  204. (UINT32*)&PAR.Denominator);
  205. if (FAILED(hr)) {
  206. *aspect = 1.0;
  207. } else {
  208. *aspect = (double)PAR.Numerator / (double)PAR.Denominator;
  209. }
  210. }
  211. } while(0);
  212. if (media_type) {
  213. media_type->Release();
  214. }
  215. return hr;
  216. }
  217. static HRESULT ConfigureOutput(IMFTransform *decoder, LONG *stride)
  218. {
  219. HRESULT hr = S_OK;
  220. IMFMediaType *media_type = 0;
  221. AM_MEDIA_TYPE *format = NULL;
  222. int index=0;
  223. while(SUCCEEDED(hr)) {
  224. hr = decoder->GetOutputAvailableType(0, index++, &media_type);
  225. if (FAILED(hr)) {
  226. break;
  227. }
  228. media_type->GetRepresentation(FORMAT_MFVideoFormat, (LPVOID*)&format);
  229. MFVIDEOFORMAT* z = (MFVIDEOFORMAT*)format->pbFormat;
  230. unsigned int surface_format = z->surfaceInfo.Format;
  231. media_type->FreeRepresentation(FORMAT_MFVideoFormat, (LPVOID)format);
  232. if (surface_format == '21VY') { // MFVideoFormat_YV12
  233. hr = GetDefaultStride(media_type, stride);
  234. hr = decoder->SetOutputType(0, media_type, 0);
  235. break;
  236. }
  237. }
  238. if(media_type) {
  239. media_type->Release();
  240. }
  241. return hr;
  242. }
  243. HRESULT MFTDecoder::Feed(const void *data, size_t data_size, uint64_t timestamp_hundred_nanos)
  244. {
  245. HRESULT hr=E_FAIL;
  246. const BYTE start_code[] = {0, 0, 0, 1};
  247. IMFMediaBuffer *buffer = 0;
  248. BYTE *buffer_pointer = 0;
  249. IMFSample *sample = 0;
  250. do {
  251. hr = MFCreateMemoryBuffer((DWORD)data_size+4, &buffer);
  252. if (FAILED(hr)) {
  253. break;
  254. }
  255. hr = buffer->Lock(&buffer_pointer, NULL, NULL);
  256. if (FAILED(hr)) {
  257. break;
  258. }
  259. memcpy(buffer_pointer, start_code, 4);
  260. memcpy(buffer_pointer+4, data, data_size);
  261. hr = buffer->Unlock();
  262. if (FAILED(hr)) {
  263. break;
  264. }
  265. hr = buffer->SetCurrentLength((DWORD)data_size+4);
  266. if (FAILED(hr)) {
  267. break;
  268. }
  269. hr = MFCreateSample(&sample);
  270. if (FAILED(hr)) {
  271. break;
  272. }
  273. hr = sample->AddBuffer(buffer);
  274. if (FAILED(hr)) {
  275. break;
  276. }
  277. hr = sample->SetSampleTime(timestamp_hundred_nanos);
  278. if (FAILED(hr)) {
  279. break;
  280. }
  281. hr = decoder->ProcessInput(0, sample, 0);
  282. if (FAILED(hr)) {
  283. break;
  284. }
  285. } while(0);
  286. if (buffer) {
  287. buffer->Release();
  288. }
  289. if (sample) {
  290. sample->Release();
  291. }
  292. return hr;
  293. }
  294. HRESULT MFTDecoder::FeedRaw(const void *data, size_t data_size, uint64_t timestamp_hundred_nanos)
  295. {
  296. HRESULT hr=E_FAIL;
  297. IMFMediaBuffer *buffer = 0;
  298. BYTE *buffer_pointer = 0;
  299. IMFSample *sample = 0;
  300. do {
  301. hr = MFCreateMemoryBuffer((DWORD)data_size, &buffer);
  302. if (FAILED(hr)) {
  303. break;
  304. }
  305. hr = buffer->Lock(&buffer_pointer, NULL, NULL);
  306. if (FAILED(hr)) {
  307. break;
  308. }
  309. memcpy(buffer_pointer, data, data_size);
  310. hr = buffer->Unlock();
  311. if (FAILED(hr)) {
  312. break;
  313. }
  314. hr = buffer->SetCurrentLength((DWORD)data_size);
  315. if (FAILED(hr)) {
  316. break;
  317. }
  318. hr = MFCreateSample(&sample);
  319. if (FAILED(hr)) {
  320. break;
  321. }
  322. hr = sample->AddBuffer(buffer);
  323. if (FAILED(hr)) {
  324. break;
  325. }
  326. hr = sample->SetSampleTime(timestamp_hundred_nanos);
  327. if (FAILED(hr)) {
  328. break;
  329. }
  330. hr = decoder->ProcessInput(0, sample, 0);
  331. if (FAILED(hr)) {
  332. break;
  333. }
  334. } while(0);
  335. if (buffer) {
  336. buffer->Release();
  337. }
  338. if (sample) {
  339. sample->Release();
  340. }
  341. return hr;
  342. }
  343. static HRESULT CreateOutputSample(IMFTransform *decoder, IMFSample **_output_sample)
  344. {
  345. HRESULT hr=E_FAIL;
  346. MFT_OUTPUT_STREAM_INFO stream_info;
  347. IMFMediaBuffer *media_buffer = 0;
  348. IMFSample *sample = 0;
  349. do {
  350. hr = MFCreateSample(&sample);
  351. if (FAILED(hr)) {
  352. break;
  353. }
  354. hr = decoder->GetOutputStreamInfo(0, &stream_info);
  355. if (FAILED(hr)) {
  356. break;
  357. }
  358. hr = MFCreateAlignedMemoryBuffer(stream_info.cbSize, MF_16_BYTE_ALIGNMENT, &media_buffer);
  359. if (FAILED(hr)) {
  360. break;
  361. }
  362. hr = sample->AddBuffer(media_buffer);
  363. if (FAILED(hr)) {
  364. break;
  365. }
  366. if (media_buffer) {
  367. media_buffer->Release();
  368. }
  369. *_output_sample = sample;
  370. return S_OK;
  371. } while(0);
  372. if (sample) {
  373. sample->Release();
  374. }
  375. if (media_buffer) {
  376. media_buffer->Release();
  377. }
  378. return hr;
  379. }
  380. // Release the events that an MFT might allocate in IMFTransform::ProcessOutput().
  381. static void ReleaseEventCollection(MFT_OUTPUT_DATA_BUFFER &pBuffers)
  382. {
  383. if (pBuffers.pEvents) {
  384. pBuffers.pEvents->Release();
  385. pBuffers.pEvents = NULL;
  386. }
  387. }
  388. HRESULT MFTDecoder::GetFrame(IMFMediaBuffer **out_buffer, uint64_t *hundrednanos)
  389. {
  390. HRESULT hr=E_FAIL;
  391. IMFSample *output_sample=0;
  392. DWORD mftStatus;
  393. do {
  394. hr = CreateOutputSample(decoder, &output_sample);
  395. if (FAILED(hr)) {
  396. break;
  397. }
  398. MFT_OUTPUT_DATA_BUFFER mftDataBuffer = {0, };
  399. mftDataBuffer.pSample = output_sample;
  400. mftStatus = 0;
  401. hr = decoder->ProcessOutput(0, 1, &mftDataBuffer, &mftStatus);
  402. if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
  403. break;
  404. }
  405. if (hr == MF_E_TRANSFORM_STREAM_CHANGE) {
  406. ConfigureOutput(decoder, &stride);
  407. width=0;
  408. height=0;
  409. } else if (FAILED(hr)) {
  410. break;
  411. } else {
  412. if (mftDataBuffer.pSample) {
  413. IMFMediaBuffer *mediaBuffer;
  414. hr = mftDataBuffer.pSample->GetBufferByIndex(0, &mediaBuffer);
  415. if (FAILED(hr)) {
  416. break;
  417. }
  418. LONGLONG sample_time;
  419. output_sample->GetSampleTime(&sample_time);
  420. if (hundrednanos) {
  421. *hundrednanos = sample_time;
  422. }
  423. *out_buffer = mediaBuffer;
  424. }
  425. ReleaseEventCollection(mftDataBuffer);
  426. }
  427. } while (0);
  428. if (output_sample) {
  429. output_sample->Release();
  430. }
  431. return hr;
  432. }
  433. HRESULT MFTDecoder::Flush()
  434. {
  435. return decoder->ProcessMessage(MFT_MESSAGE_COMMAND_FLUSH, 0);
  436. }
  437. HRESULT MFTDecoder::Drain()
  438. {
  439. return decoder->ProcessMessage(MFT_MESSAGE_COMMAND_DRAIN, 0);
  440. }
  441. HRESULT MFTDecoder::GetFrame(YV12_PLANES **data, void **decoder_data, uint64_t *mft_timestamp)
  442. {
  443. HRESULT hr=E_FAIL;
  444. IMFMediaBuffer *media_buffer = 0;
  445. IMFMediaType *media_type = 0;
  446. do {
  447. if (!height || !stride) {
  448. hr = decoder->GetOutputCurrentType(0, &media_type);
  449. if (FAILED(hr)) {
  450. break;
  451. }
  452. hr = MFGetAttributeSize(media_type, MF_MT_FRAME_SIZE, &width, &height);
  453. if (FAILED(hr)) {
  454. break;
  455. }
  456. hr = GetDefaultStride(media_type, &stride);
  457. if (FAILED(hr)) {
  458. break;
  459. }
  460. }
  461. hr = this->GetFrame(&media_buffer, mft_timestamp);
  462. if (FAILED(hr)) {
  463. break;
  464. }
  465. YV12_PLANES *planes = (YV12_PLANES *)malloc(sizeof(YV12_PLANES));
  466. IMF2DBuffer *buffer2d=0;
  467. if (SUCCEEDED(media_buffer->QueryInterface(&buffer2d))) {
  468. BYTE *pbScanline0;
  469. LONG pitch;
  470. buffer2d->Lock2D(&pbScanline0, &pitch);
  471. planes->y.baseAddr = pbScanline0;
  472. planes->y.rowBytes = pitch;
  473. pbScanline0 += pitch * height;
  474. planes->v.baseAddr = pbScanline0;
  475. planes->v.rowBytes = pitch/2;
  476. pbScanline0 += pitch * height/4;
  477. planes->u.baseAddr = pbScanline0;
  478. planes->u.rowBytes = pitch/2;
  479. buffer2d->Release();
  480. } else {
  481. DWORD length, max_length;
  482. BYTE *video_data;
  483. media_buffer->Lock(&video_data, &length, &max_length);
  484. planes->y.baseAddr = video_data;
  485. planes->y.rowBytes = stride;
  486. video_data += stride * height;
  487. planes->v.baseAddr = video_data;
  488. planes->v.rowBytes = stride/2;
  489. video_data += (stride/2) * (height/2);
  490. planes->u.baseAddr = video_data;
  491. planes->u.rowBytes = stride/2;
  492. }
  493. *data = planes;
  494. *decoder_data = media_buffer;
  495. } while(0);
  496. if (media_type) {
  497. media_type->Release();
  498. }
  499. return hr;
  500. }
  501. HRESULT MFTDecoder::FreeFrame(YV12_PLANES *data, void *decoder_data)
  502. {
  503. IMFMediaBuffer *buffer= (IMFMediaBuffer *)decoder_data;
  504. if (buffer) {
  505. IMF2DBuffer *buffer2d=0;
  506. if (SUCCEEDED(buffer->QueryInterface(&buffer2d))) {
  507. buffer2d->Unlock2D();
  508. buffer2d->Release();
  509. } else {
  510. buffer->Unlock();
  511. }
  512. buffer->Release();
  513. }
  514. free(data);
  515. return S_OK;
  516. }