Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.28 KB | None | 0 0
  1. // decode next frame and put result to output surface
  2. mfxStatus CDecodingPipeline::DecodeOneFrame(int Width, int Height, IDirect3DSurface9 *pDstSurface, IDirect3DDevice9* pd3dDevice)
  3.  
  4. {
  5. mfxStatus stsOut = MFX_ERR_NONE;
  6. // check if previouse submitted task exists
  7. if(m_Tasks[m_TaskIndex].m_DecodeSync || m_Tasks[m_TaskIndex].m_OCLSync)
  8. {// wait task is finished and copy result texture to back buffer
  9. mfxStatus sts = MFX_ERR_NONE;
  10. mfxFrameSurface1_OCL* pOutSurface = NULL; // output surface.
  11. //wait the previous submitted tasks
  12. if(m_Tasks[m_TaskIndex].m_DecodeSync)
  13. {
  14. sts = m_mfxSession.SyncOperation(m_Tasks[m_TaskIndex].m_DecodeSync, MSDK_DEC_WAIT_INTERVAL);
  15. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  16. pOutSurface = m_Tasks[m_TaskIndex].m_pDecodeOutSurface;
  17. }
  18. if(m_Tasks[m_TaskIndex].m_OCLSync)
  19. {
  20. sts = m_mfxSession.SyncOperation(m_Tasks[m_TaskIndex].m_OCLSync, MSDK_VPP_WAIT_INTERVAL);
  21. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  22. pOutSurface = m_Tasks[m_TaskIndex].m_pOCLOutSurface;
  23. }
  24. if(pOutSurface)
  25. {/* copy YUV texture to screen */
  26.  
  27. HRESULT hr;
  28.  
  29. IDirect3DSurface9* pSrcSurface = (IDirect3DSurface9*)(pOutSurface->Data.MemId);
  30. assert(pDstSurface && pSrcSurface);
  31. if(pSrcSurface && pDstSurface)
  32. {
  33. RECT r;
  34. r.left = 0;
  35. r.top = 0;
  36. r.right = min(Width,m_mfxDecodeVideoParams.vpp.In.Width);
  37. r.bottom = min(Height,m_mfxDecodeVideoParams.vpp.In.Height);
  38.  
  39. r.right -= r.right&1;
  40. r.bottom -= r.bottom&1;
  41.  
  42. V(pd3dDevice->StretchRect(pSrcSurface, &r, pDstSurface, &r,D3DTEXF_POINT));
  43. }
  44.  
  45. }
  46. if(m_Tasks[m_TaskIndex].m_pDecodeOutSurface && m_Tasks[m_TaskIndex].m_pDecodeOutSurface->Data.Locked)
  47. _InterlockedDecrement16((short*)&m_Tasks[m_TaskIndex].m_pDecodeOutSurface->Data.Locked);
  48. if(m_Tasks[m_TaskIndex].m_pOCLOutSurface && m_Tasks[m_TaskIndex].m_pOCLOutSurface->Data.Locked)
  49. _InterlockedDecrement16((short*)&m_Tasks[m_TaskIndex].m_pOCLOutSurface->Data.Locked);
  50.  
  51. }
  52.  
  53. // clear sync task for further using
  54. m_Tasks[m_TaskIndex].m_OCLSync = 0;
  55. m_Tasks[m_TaskIndex].m_pOCLOutSurface = 0;
  56. m_Tasks[m_TaskIndex].m_DecodeSync = 0;
  57. m_Tasks[m_TaskIndex].m_pDecodeOutSurface = 0;
  58.  
  59.  
  60.  
  61. if(m_DECODEFlag)
  62. {// feed decoder
  63. mfxSyncPoint DecodeSyncPoint = 0;
  64. static mfxU16 nDecoderSurfIndex = 0; // index of free surface
  65. mfxStatus sts = MFX_ERR_NONE;
  66. m_pmfxDecodeSurfaceLast = NULL; // reset curretn decoder surface to get new one from Decoder
  67. while(MFX_ERR_NONE <= sts || MFX_ERR_MORE_DATA == sts || MFX_ERR_MORE_SURFACE == sts || MFX_WRN_DEVICE_BUSY == sts)
  68. {// loop until decoder report that it get request for new frame
  69. if (MFX_WRN_DEVICE_BUSY == sts)
  70. {
  71. Sleep(1); // just wait and then repeat the same call to DecodeFrameAsync
  72. }
  73. else if (MFX_ERR_MORE_DATA == sts)
  74. { // read more data to input bit stream
  75. sts = m_FileReader.ReadNextFrame(&m_mfxBS);
  76. MSDK_BREAK_ON_ERROR(sts);
  77. }
  78. else if (MFX_ERR_MORE_SURFACE == sts || MFX_ERR_NONE == sts)
  79. {// find new working-output surface in m_pmfxDecodeSurfaces
  80. //nDecoderSurfIndex = 0;
  81. nDecoderSurfIndex = GetFreeSurfaceIndex(m_pmfxDecodeSurfaces, m_mfxDecoderResponse.NumFrameActual,nDecoderSurfIndex);
  82. if (MSDK_INVALID_SURF_IDX == nDecoderSurfIndex)
  83. {
  84. return MFX_ERR_MEMORY_ALLOC;
  85. }
  86. }
  87.  
  88. // send request to decoder
  89. sts = m_pmfxDEC->DecodeFrameAsync(
  90. &m_mfxBS,
  91. (mfxFrameSurface1*)&(m_pmfxDecodeSurfaces[nDecoderSurfIndex]),
  92. (mfxFrameSurface1**)&m_pmfxDecodeSurfaceLast,
  93. &DecodeSyncPoint);
  94. // ignore warnings if output is available,
  95. // if no output and no action required just repeat the same call
  96. if (MFX_ERR_NONE < sts && DecodeSyncPoint)
  97. {
  98. sts = MFX_ERR_NONE;
  99. }
  100.  
  101. if (MFX_ERR_NONE == sts)
  102. {// decoder return sync point then fill the curretn task nad switch to OCL Plugin feeding
  103. m_Tasks[m_TaskIndex].m_DecodeSync = DecodeSyncPoint;
  104. m_Tasks[m_TaskIndex].m_pDecodeOutSurface = m_pmfxDecodeSurfaceLast;
  105. // look for output process
  106. if(m_Tasks[m_TaskIndex].m_pDecodeOutSurface)
  107. _InterlockedIncrement16((short*)&m_Tasks[m_TaskIndex].m_pDecodeOutSurface->Data.Locked);
  108. break;
  109. }
  110. }
  111. if(MFX_ERR_NONE != sts)
  112. {
  113. printf("ERROR: Decoder returns error %d!n",sts);
  114. stsOut = sts;
  115. }
  116. }//if(m_DECODEFlag)
  117.  
  118. if(m_pOCLPlugin && m_pOCLPlugin->m_OCLFlag && begin)
  119. {// OPENCL part
  120. mfxSyncPoint OCLSyncPoint = 0;
  121. mfxStatus sts = MFX_ERR_NONE;
  122. // get index for output surface for OCL plugin
  123. mfxU16 nOCLSurfIndex = GetFreeSurfaceIndex(m_pmfxOCLSurfaces, m_mfxOCLResponse.NumFrameActual);
  124. MSDK_CHECK_ERROR(nOCLSurfIndex, MSDK_INVALID_SURF_IDX, MFX_ERR_MEMORY_ALLOC);
  125. mfxHDL pOutSurf = &m_pmfxOCLSurfaces[nOCLSurfIndex];
  126. mfxHDL inp = m_pmfxDecodeSurfaceLast;
  127.  
  128. // OCL filter
  129. for(;;)
  130. {
  131. sts = MFXVideoUSER_ProcessFrameAsync(m_mfxSession, &inp, 1, &pOutSurf, 1, &OCLSyncPoint);
  132.  
  133. if (MFX_WRN_DEVICE_BUSY == sts)
  134. {
  135. Sleep(1); // just wait and then repeat the same call
  136. }
  137. else
  138. {
  139. break;
  140. }
  141. }
  142.  
  143. // ignore warnings if output is available,
  144. if (MFX_ERR_NONE < sts && OCLSyncPoint)
  145. {
  146. sts = MFX_ERR_NONE;
  147. }
  148.  
  149. if(MFX_ERR_NONE!=sts)
  150. {
  151. printf("ERROR: OpenCL filter return error %d!n",sts);
  152. return sts;
  153. }
  154.  
  155. {
  156. m_Tasks[m_TaskIndex].m_OCLSync = OCLSyncPoint;
  157. m_Tasks[m_TaskIndex].m_pOCLOutSurface = &m_pmfxOCLSurfaces[nOCLSurfIndex];
  158. // look for output process
  159. _InterlockedIncrement16((short*)&m_Tasks[m_TaskIndex].m_pOCLOutSurface->Data.Locked);
  160. }
  161. }
  162.  
  163. // increase task index to point to next task.
  164. m_TaskIndex = (m_TaskIndex+1)%SYNC_BUF_SIZE;
  165. return stsOut;
  166. }//CDecodingPipeline::DecodeOneFrame
  167.  
  168. if(m_pOCLPlugin && m_pOCLPlugin->m_OCLFlag)
  169.  
  170. // decode next frame and put result to output surface
  171. mfxStatus CDecodingPipeline::DecodeOneFrame(int Width, int Height, IDirect3DSurface9 *pDstSurface, IDirect3DDevice9* pd3dDevice)
  172.  
  173. {
  174. mfxStatus stsOut = MFX_ERR_NONE;
  175. int begin =0; //flag to indicate start of opencl
  176. // check if previouse submitted task exists
  177. if(m_Tasks[m_TaskIndex].m_DecodeSync || m_Tasks[m_TaskIndex].m_OCLSync)
  178. {// wait task is finished and copy result texture to back buffer
  179. mfxStatus sts = MFX_ERR_NONE;
  180. mfxFrameSurface1_OCL* pOutSurface = NULL; // output surface.
  181. //wait the previous submitted tasks
  182. if(m_Tasks[m_TaskIndex].m_DecodeSync)
  183. {
  184. sts = m_mfxSession.SyncOperation(m_Tasks[m_TaskIndex].m_DecodeSync, MSDK_DEC_WAIT_INTERVAL);
  185. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  186. pOutSurface = m_Tasks[m_TaskIndex].m_pDecodeOutSurface;
  187. }
  188. if(m_Tasks[m_TaskIndex].m_OCLSync)
  189. {
  190. sts = m_mfxSession.SyncOperation(m_Tasks[m_TaskIndex].m_OCLSync, MSDK_VPP_WAIT_INTERVAL);
  191. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  192. pOutSurface = m_Tasks[m_TaskIndex].m_pOCLOutSurface;
  193. begin =1; // start opencl processing. now decoder has completed the submitted task.
  194. }
  195. if(pOutSurface)
  196. {/* copy YUV texture to screen */
  197.  
  198. HRESULT hr;
  199.  
  200. IDirect3DSurface9* pSrcSurface = (IDirect3DSurface9*)(pOutSurface->Data.MemId);
  201. assert(pDstSurface && pSrcSurface);
  202. if(pSrcSurface && pDstSurface)
  203. {
  204. RECT r;
  205. r.left = 0;
  206. r.top = 0;
  207. r.right = min(Width,m_mfxDecodeVideoParams.vpp.In.Width);
  208. r.bottom = min(Height,m_mfxDecodeVideoParams.vpp.In.Height);
  209.  
  210. r.right -= r.right&1;
  211. r.bottom -= r.bottom&1;
  212.  
  213. V(pd3dDevice->StretchRect(pSrcSurface, &r, pDstSurface, &r,D3DTEXF_POINT));
  214. }
  215.  
  216. }
  217. if(m_Tasks[m_TaskIndex].m_pDecodeOutSurface && m_Tasks[m_TaskIndex].m_pDecodeOutSurface->Data.Locked)
  218. _InterlockedDecrement16((short*)&m_Tasks[m_TaskIndex].m_pDecodeOutSurface->Data.Locked);
  219. if(m_Tasks[m_TaskIndex].m_pOCLOutSurface && m_Tasks[m_TaskIndex].m_pOCLOutSurface->Data.Locked)
  220. _InterlockedDecrement16((short*)&m_Tasks[m_TaskIndex].m_pOCLOutSurface->Data.Locked);
  221.  
  222. }
  223.  
  224. // clear sync task for further using
  225. m_Tasks[m_TaskIndex].m_OCLSync = 0;
  226. m_Tasks[m_TaskIndex].m_pOCLOutSurface = 0;
  227. m_Tasks[m_TaskIndex].m_DecodeSync = 0;
  228. m_Tasks[m_TaskIndex].m_pDecodeOutSurface = 0;
  229.  
  230.  
  231.  
  232. if(m_DECODEFlag)
  233. {// feed decoder
  234. mfxSyncPoint DecodeSyncPoint = 0;
  235. static mfxU16 nDecoderSurfIndex = 0; // index of free surface
  236. mfxStatus sts = MFX_ERR_NONE;
  237. m_pmfxDecodeSurfaceLast = NULL; // reset curretn decoder surface to get new one from Decoder
  238. while(MFX_ERR_NONE <= sts || MFX_ERR_MORE_DATA == sts || MFX_ERR_MORE_SURFACE == sts || MFX_WRN_DEVICE_BUSY == sts)
  239. {// loop until decoder report that it get request for new frame
  240. if (MFX_WRN_DEVICE_BUSY == sts)
  241. {
  242. Sleep(1); // just wait and then repeat the same call to DecodeFrameAsync
  243. }
  244. else if (MFX_ERR_MORE_DATA == sts)
  245. { // read more data to input bit stream
  246. sts = m_FileReader.ReadNextFrame(&m_mfxBS);
  247. MSDK_BREAK_ON_ERROR(sts);
  248. }
  249. else if (MFX_ERR_MORE_SURFACE == sts || MFX_ERR_NONE == sts)
  250. {// find new working-output surface in m_pmfxDecodeSurfaces
  251. //nDecoderSurfIndex = 0;
  252. nDecoderSurfIndex = GetFreeSurfaceIndex(m_pmfxDecodeSurfaces, m_mfxDecoderResponse.NumFrameActual,nDecoderSurfIndex);
  253. if (MSDK_INVALID_SURF_IDX == nDecoderSurfIndex)
  254. {
  255. return MFX_ERR_MEMORY_ALLOC;
  256. }
  257. }
  258.  
  259. // send request to decoder
  260. sts = m_pmfxDEC->DecodeFrameAsync(
  261. &m_mfxBS,
  262. (mfxFrameSurface1*)&(m_pmfxDecodeSurfaces[nDecoderSurfIndex]),
  263. (mfxFrameSurface1**)&m_pmfxDecodeSurfaceLast,
  264. &DecodeSyncPoint);
  265. // ignore warnings if output is available,
  266. // if no output and no action required just repeat the same call
  267. if (MFX_ERR_NONE < sts && DecodeSyncPoint)
  268. {
  269. sts = MFX_ERR_NONE;
  270. }
  271.  
  272. if (MFX_ERR_NONE == sts)
  273. {// decoder return sync point then fill the curretn task nad switch to OCL Plugin feeding
  274. m_Tasks[m_TaskIndex].m_DecodeSync = DecodeSyncPoint;
  275. m_Tasks[m_TaskIndex].m_pDecodeOutSurface = m_pmfxDecodeSurfaceLast;
  276. // look for output process
  277. if(m_Tasks[m_TaskIndex].m_pDecodeOutSurface)
  278. _InterlockedIncrement16((short*)&m_Tasks[m_TaskIndex].m_pDecodeOutSurface->Data.Locked);
  279. break;
  280. }
  281. }
  282. if(MFX_ERR_NONE != sts)
  283. {
  284. printf("ERROR: Decoder returns error %d!n",sts);
  285. stsOut = sts;
  286. }
  287. }//if(m_DECODEFlag)
  288.  
  289. if(m_pOCLPlugin && m_pOCLPlugin->m_OCLFlag)
  290. {// OPENCL part
  291. mfxSyncPoint OCLSyncPoint = 0;
  292. mfxStatus sts = MFX_ERR_NONE;
  293. // get index for output surface for OCL plugin
  294. mfxU16 nOCLSurfIndex = GetFreeSurfaceIndex(m_pmfxOCLSurfaces, m_mfxOCLResponse.NumFrameActual);
  295. MSDK_CHECK_ERROR(nOCLSurfIndex, MSDK_INVALID_SURF_IDX, MFX_ERR_MEMORY_ALLOC);
  296. mfxHDL pOutSurf = &m_pmfxOCLSurfaces[nOCLSurfIndex];
  297. mfxHDL inp = m_pmfxDecodeSurfaceLast;
  298.  
  299. // OCL filter
  300. for(;;)
  301. {
  302. sts = MFXVideoUSER_ProcessFrameAsync(m_mfxSession, &inp, 1, &pOutSurf, 1, &OCLSyncPoint);
  303.  
  304. if (MFX_WRN_DEVICE_BUSY == sts)
  305. {
  306. Sleep(1); // just wait and then repeat the same call
  307. }
  308. else
  309. {
  310. break;
  311. }
  312. }
  313.  
  314. // ignore warnings if output is available,
  315. if (MFX_ERR_NONE < sts && OCLSyncPoint)
  316. {
  317. sts = MFX_ERR_NONE;
  318. }
  319.  
  320. if(MFX_ERR_NONE!=sts)
  321. {
  322. printf("ERROR: OpenCL filter return error %d!n",sts);
  323. return sts;
  324. }
  325.  
  326. {
  327. m_Tasks[m_TaskIndex].m_OCLSync = OCLSyncPoint;
  328. m_Tasks[m_TaskIndex].m_pOCLOutSurface = &m_pmfxOCLSurfaces[nOCLSurfIndex];
  329. // look for output process
  330. _InterlockedIncrement16((short*)&m_Tasks[m_TaskIndex].m_pOCLOutSurface->Data.Locked);
  331. }
  332. }
  333.  
  334. // increase task index to point to next task.
  335. m_TaskIndex = (m_TaskIndex+1)%SYNC_BUF_SIZE;
  336. return stsOut;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement