QSV_Encoder_Internal.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. This file is provided under a dual BSD/GPLv2 license. When using or
  3. redistributing this file, you may do so under either license.
  4. GPL LICENSE SUMMARY
  5. Copyright(c) Oct. 2015 Intel Corporation.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. Contact Information:
  14. Seung-Woo Kim, [email protected]
  15. 705 5th Ave S #500, Seattle, WA 98104
  16. BSD LICENSE
  17. Copyright(c) <date> Intel Corporation.
  18. Redistribution and use in source and binary forms, with or without
  19. modification, are permitted provided that the following conditions
  20. are met:
  21. * Redistributions of source code must retain the above copyright
  22. notice, this list of conditions and the following disclaimer.
  23. * Redistributions in binary form must reproduce the above copyright
  24. notice, this list of conditions and the following disclaimer in
  25. the documentation and/or other materials provided with the
  26. distribution.
  27. * Neither the name of Intel Corporation nor the names of its
  28. contributors may be used to endorse or promote products derived
  29. from this software without specific prior written permission.
  30. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  33. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  36. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  37. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  38. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  40. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. #include "QSV_Encoder_Internal.h"
  43. #include "QSV_Encoder.h"
  44. #include "mfxastructures.h"
  45. #include "mfxvideo++.h"
  46. #include <VersionHelpers.h>
  47. #include <obs-module.h>
  48. #define do_log(level, format, ...) \
  49. blog(level, "[qsv encoder: '%s'] " format, \
  50. "msdk_impl", ##__VA_ARGS__)
  51. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  52. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  53. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  54. QSV_Encoder_Internal::QSV_Encoder_Internal(mfxIMPL& impl, mfxVersion& version) :
  55. m_pmfxENC(NULL),
  56. m_nSPSBufferSize(100),
  57. m_nPPSBufferSize(100),
  58. m_nTaskPool(0),
  59. m_pTaskPool(NULL),
  60. m_nTaskIdx(0),
  61. m_nFirstSyncTask(0)
  62. {
  63. mfxIMPL tempImpl;
  64. mfxStatus sts;
  65. m_bIsWindows8OrGreater = IsWindows8OrGreater();
  66. m_bUseD3D11 = false;
  67. m_bD3D9HACK = true;
  68. if (m_bIsWindows8OrGreater) {
  69. tempImpl = impl | MFX_IMPL_VIA_D3D11;
  70. sts = m_session.Init(tempImpl, &version);
  71. if (sts == MFX_ERR_NONE) {
  72. m_session.QueryVersion(&version);
  73. m_session.Close();
  74. // Use D3D11 surface
  75. // m_bUseD3D11 = ((version.Major > 1) ||
  76. // (version.Major == 1 && version.Minor >= 8));
  77. m_bUseD3D11 = true;
  78. if (m_bUseD3D11)
  79. blog(LOG_INFO, "\timpl: D3D11\n"
  80. "\tsurf: D3D11");
  81. else
  82. blog(LOG_INFO, "\timpl: D3D11\n"
  83. "\tsurf: SysMem");
  84. m_impl = tempImpl;
  85. m_ver = version;
  86. return;
  87. }
  88. }
  89. else if (m_bD3D9HACK) {
  90. tempImpl = impl | MFX_IMPL_VIA_D3D9;
  91. sts = m_session.Init(tempImpl, &version);
  92. if (sts == MFX_ERR_NONE) {
  93. m_session.QueryVersion(&version);
  94. m_session.Close();
  95. blog(LOG_INFO, "\timpl: D3D09\n"
  96. "\tsurf: Hack");
  97. m_impl = tempImpl;
  98. m_ver = version;
  99. return;
  100. }
  101. }
  102. // Either windows 7 or D3D11 failed at this point.
  103. tempImpl = impl | MFX_IMPL_VIA_D3D9;
  104. sts = m_session.Init(tempImpl, &version);
  105. if (sts == MFX_ERR_NONE) {
  106. m_session.QueryVersion(&version);
  107. m_session.Close();
  108. blog(LOG_INFO, "\timpl: D3D09\n"
  109. "\tsurf: SysMem");
  110. m_impl = tempImpl;
  111. m_ver = version;
  112. }
  113. }
  114. QSV_Encoder_Internal::~QSV_Encoder_Internal()
  115. {
  116. if (m_pmfxENC)
  117. ClearData();
  118. }
  119. mfxStatus QSV_Encoder_Internal::Open(qsv_param_t * pParams)
  120. {
  121. mfxStatus sts = MFX_ERR_NONE;
  122. if (m_bUseD3D11)
  123. // Use D3D11 surface
  124. sts = Initialize(m_impl, m_ver, &m_session, &m_mfxAllocator, false, false);
  125. else if (m_bD3D9HACK)
  126. // Use hack
  127. sts = Initialize(m_impl, m_ver, &m_session, &m_mfxAllocator, false, true);
  128. else
  129. sts = Initialize(m_impl, m_ver, &m_session, NULL);
  130. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  131. m_pmfxENC = new MFXVideoENCODE(m_session);
  132. InitParams(pParams);
  133. sts = m_pmfxENC->Query(&m_mfxEncParams, &m_mfxEncParams);
  134. MSDK_IGNORE_MFX_STS(sts, MFX_WRN_INCOMPATIBLE_VIDEO_PARAM);
  135. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  136. sts = AllocateSurfaces();
  137. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  138. sts = m_pmfxENC->Init(&m_mfxEncParams);
  139. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  140. sts = GetVideoParam();
  141. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  142. sts = InitBitstream();
  143. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  144. return sts;
  145. }
  146. bool QSV_Encoder_Internal::InitParams(qsv_param_t * pParams)
  147. {
  148. memset(&m_mfxEncParams, 0, sizeof(m_mfxEncParams));
  149. m_mfxEncParams.mfx.CodecId = MFX_CODEC_AVC;
  150. m_mfxEncParams.mfx.GopOptFlag = MFX_GOP_STRICT;
  151. m_mfxEncParams.mfx.NumSlice = 1;
  152. m_mfxEncParams.mfx.TargetUsage = pParams->nTargetUsage;
  153. m_mfxEncParams.mfx.CodecProfile = pParams->nCodecProfile;
  154. m_mfxEncParams.mfx.FrameInfo.FrameRateExtN = pParams->nFpsNum;
  155. m_mfxEncParams.mfx.FrameInfo.FrameRateExtD = pParams->nFpsDen;
  156. m_mfxEncParams.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
  157. m_mfxEncParams.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  158. m_mfxEncParams.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
  159. m_mfxEncParams.mfx.FrameInfo.CropX = 0;
  160. m_mfxEncParams.mfx.FrameInfo.CropY = 0;
  161. m_mfxEncParams.mfx.FrameInfo.CropW = pParams->nWidth;
  162. m_mfxEncParams.mfx.FrameInfo.CropH = pParams->nHeight;
  163. m_mfxEncParams.mfx.RateControlMethod = pParams->nRateControl;
  164. switch (pParams->nRateControl) {
  165. case MFX_RATECONTROL_CBR:
  166. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  167. break;
  168. case MFX_RATECONTROL_VBR:
  169. case MFX_RATECONTROL_VCM:
  170. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  171. m_mfxEncParams.mfx.MaxKbps = pParams->nMaxBitRate;
  172. break;
  173. case MFX_RATECONTROL_CQP:
  174. m_mfxEncParams.mfx.QPI = pParams->nQPI;
  175. m_mfxEncParams.mfx.QPB = pParams->nQPB;
  176. m_mfxEncParams.mfx.QPP = pParams->nQPP;
  177. break;
  178. case MFX_RATECONTROL_AVBR:
  179. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  180. m_mfxEncParams.mfx.Accuracy = pParams->nAccuracy;
  181. m_mfxEncParams.mfx.Convergence = pParams->nConvergence;
  182. break;
  183. case MFX_RATECONTROL_ICQ:
  184. m_mfxEncParams.mfx.ICQQuality = pParams->nICQQuality;
  185. break;
  186. case MFX_RATECONTROL_LA:
  187. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  188. break;
  189. case MFX_RATECONTROL_LA_ICQ:
  190. m_mfxEncParams.mfx.ICQQuality = pParams->nICQQuality;
  191. break;
  192. default:
  193. break;
  194. }
  195. m_mfxEncParams.AsyncDepth = pParams->nAsyncDepth;
  196. m_mfxEncParams.mfx.GopPicSize = (mfxU16)(pParams->nKeyIntSec *
  197. pParams->nFpsNum / (float)pParams->nFpsDen);
  198. static mfxExtBuffer* extendedBuffers[2];
  199. int iBuffers = 0;
  200. if (pParams->nAsyncDepth == 1) {
  201. m_mfxEncParams.mfx.NumRefFrame = 1;
  202. // low latency, I and P frames only
  203. m_mfxEncParams.mfx.GopRefDist = 1;
  204. memset(&m_co, 0, sizeof(mfxExtCodingOption));
  205. m_co.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
  206. m_co.Header.BufferSz = sizeof(mfxExtCodingOption);
  207. m_co.MaxDecFrameBuffering = 1;
  208. extendedBuffers[iBuffers++] = (mfxExtBuffer*)&m_co;
  209. }
  210. else
  211. m_mfxEncParams.mfx.GopRefDist = pParams->nbFrames + 1;
  212. if (pParams->nRateControl == MFX_RATECONTROL_LA_ICQ ||
  213. pParams->nRateControl == MFX_RATECONTROL_LA) {
  214. memset(&m_co2, 0, sizeof(mfxExtCodingOption2));
  215. m_co2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
  216. m_co2.Header.BufferSz = sizeof(m_co2);
  217. m_co2.LookAheadDepth = pParams->nLADEPTH;
  218. extendedBuffers[iBuffers++] = (mfxExtBuffer*)& m_co2;
  219. }
  220. if (iBuffers > 0) {
  221. m_mfxEncParams.ExtParam = extendedBuffers;
  222. m_mfxEncParams.NumExtParam = (mfxU16)iBuffers;
  223. }
  224. // Width must be a multiple of 16
  225. // Height must be a multiple of 16 in case of frame picture and a
  226. // multiple of 32 in case of field picture
  227. m_mfxEncParams.mfx.FrameInfo.Width = MSDK_ALIGN16(pParams->nWidth);
  228. m_mfxEncParams.mfx.FrameInfo.Height = MSDK_ALIGN16(pParams->nHeight);
  229. if (m_bUseD3D11 || m_bD3D9HACK)
  230. m_mfxEncParams.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
  231. else
  232. m_mfxEncParams.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
  233. return true;
  234. }
  235. mfxStatus QSV_Encoder_Internal::AllocateSurfaces()
  236. {
  237. // Query number of required surfaces for encoder
  238. mfxFrameAllocRequest EncRequest;
  239. memset(&EncRequest, 0, sizeof(EncRequest));
  240. mfxStatus sts = m_pmfxENC->QueryIOSurf(&m_mfxEncParams, &EncRequest);
  241. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  242. EncRequest.Type |= WILL_WRITE;
  243. // SNB hack. On some SNB, it seems to require more surfaces
  244. EncRequest.NumFrameSuggested += m_mfxEncParams.AsyncDepth;
  245. // Allocate required surfaces
  246. if (m_bUseD3D11 || m_bD3D9HACK) {
  247. sts = m_mfxAllocator.Alloc(m_mfxAllocator.pthis, &EncRequest,
  248. &m_mfxResponse);
  249. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  250. m_nSurfNum = m_mfxResponse.NumFrameActual;
  251. m_pmfxSurfaces = new mfxFrameSurface1 *[m_nSurfNum];
  252. MSDK_CHECK_POINTER(m_pmfxSurfaces, MFX_ERR_MEMORY_ALLOC);
  253. for (int i = 0; i < m_nSurfNum; i++) {
  254. m_pmfxSurfaces[i] = new mfxFrameSurface1;
  255. memset(m_pmfxSurfaces[i], 0, sizeof(mfxFrameSurface1));
  256. memcpy(&(m_pmfxSurfaces[i]->Info),
  257. &(m_mfxEncParams.mfx.FrameInfo),
  258. sizeof(mfxFrameInfo));
  259. m_pmfxSurfaces[i]->Data.MemId = m_mfxResponse.mids[i];
  260. }
  261. }
  262. else {
  263. mfxU16 width = (mfxU16)MSDK_ALIGN32(EncRequest.Info.Width);
  264. mfxU16 height = (mfxU16)MSDK_ALIGN32(EncRequest.Info.Height);
  265. mfxU8 bitsPerPixel = 12;
  266. mfxU32 surfaceSize = width * height * bitsPerPixel / 8;
  267. m_nSurfNum = EncRequest.NumFrameSuggested;
  268. m_pmfxSurfaces = new mfxFrameSurface1 *[m_nSurfNum];
  269. for (int i = 0; i < m_nSurfNum; i++) {
  270. m_pmfxSurfaces[i] = new mfxFrameSurface1;
  271. memset(m_pmfxSurfaces[i], 0, sizeof(mfxFrameSurface1));
  272. memcpy(&(m_pmfxSurfaces[i]->Info),
  273. &(m_mfxEncParams.mfx.FrameInfo),
  274. sizeof(mfxFrameInfo));
  275. mfxU8* pSurface = (mfxU8*) new mfxU8[surfaceSize];
  276. m_pmfxSurfaces[i]->Data.Y = pSurface;
  277. m_pmfxSurfaces[i]->Data.U = pSurface + width * height;
  278. m_pmfxSurfaces[i]->Data.V = pSurface + width * height + 1;
  279. m_pmfxSurfaces[i]->Data.Pitch = width;
  280. }
  281. }
  282. blog(LOG_INFO, "\tm_nSurfNum: %d", m_nSurfNum);
  283. return sts;
  284. }
  285. mfxStatus QSV_Encoder_Internal::GetVideoParam()
  286. {
  287. memset(&m_parameter, 0, sizeof(m_parameter));
  288. mfxExtCodingOptionSPSPPS opt;
  289. memset(&m_parameter, 0, sizeof(m_parameter));
  290. opt.Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS;
  291. opt.Header.BufferSz = sizeof(mfxExtCodingOptionSPSPPS);
  292. static mfxExtBuffer* extendedBuffers[1];
  293. extendedBuffers[0] = (mfxExtBuffer*)& opt;
  294. m_parameter.ExtParam = extendedBuffers;
  295. m_parameter.NumExtParam = 1;
  296. opt.SPSBuffer = m_SPSBuffer;
  297. opt.PPSBuffer = m_PPSBuffer;
  298. opt.SPSBufSize = 100; // m_nSPSBufferSize;
  299. opt.PPSBufSize = 100; // m_nPPSBufferSize;
  300. mfxStatus sts = m_pmfxENC->GetVideoParam(&m_parameter);
  301. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  302. m_nSPSBufferSize = opt.SPSBufSize;
  303. m_nPPSBufferSize = opt.PPSBufSize;
  304. return sts;
  305. }
  306. void QSV_Encoder_Internal::GetSPSPPS(mfxU8 **pSPSBuf, mfxU8 **pPPSBuf,
  307. mfxU16 *pnSPSBuf, mfxU16 *pnPPSBuf)
  308. {
  309. *pSPSBuf = m_SPSBuffer;
  310. *pPPSBuf = m_PPSBuffer;
  311. *pnSPSBuf = m_nSPSBufferSize;
  312. *pnPPSBuf = m_nPPSBufferSize;
  313. }
  314. mfxStatus QSV_Encoder_Internal::InitBitstream()
  315. {
  316. m_nTaskPool = m_parameter.AsyncDepth;
  317. m_nFirstSyncTask = 0;
  318. m_pTaskPool = new Task[m_nTaskPool];
  319. memset(m_pTaskPool, 0, sizeof(Task) * m_nTaskPool);
  320. for (int i = 0; i < m_nTaskPool; i++) {
  321. m_pTaskPool[i].mfxBS.MaxLength =
  322. m_parameter.mfx.BufferSizeInKB * 1000;
  323. m_pTaskPool[i].mfxBS.Data =
  324. new mfxU8[m_pTaskPool[i].mfxBS.MaxLength];
  325. m_pTaskPool[i].mfxBS.DataOffset = 0;
  326. m_pTaskPool[i].mfxBS.DataLength = 0;
  327. MSDK_CHECK_POINTER(m_pTaskPool[i].mfxBS.Data,
  328. MFX_ERR_MEMORY_ALLOC);
  329. }
  330. memset(&m_outBitstream, 0, sizeof(mfxBitstream));
  331. m_outBitstream.MaxLength = m_parameter.mfx.BufferSizeInKB * 1000;
  332. m_outBitstream.Data = new mfxU8[m_outBitstream.MaxLength];
  333. m_outBitstream.DataOffset = 0;
  334. m_outBitstream.DataLength = 0;
  335. blog(LOG_INFO, "\tm_nTaskPool: %d", m_nTaskPool);
  336. return MFX_ERR_NONE;
  337. }
  338. mfxStatus QSV_Encoder_Internal::LoadNV12(mfxFrameSurface1 *pSurface,
  339. uint8_t *pDataY, uint8_t *pDataUV, uint32_t strideY,
  340. uint32_t strideUV)
  341. {
  342. mfxU16 w, h, i, pitch;
  343. mfxU8* ptr;
  344. mfxFrameInfo* pInfo = &pSurface->Info;
  345. mfxFrameData* pData = &pSurface->Data;
  346. if (pInfo->CropH > 0 && pInfo->CropW > 0)
  347. {
  348. w = pInfo->CropW;
  349. h = pInfo->CropH;
  350. }
  351. else
  352. {
  353. w = pInfo->Width;
  354. h = pInfo->Height;
  355. }
  356. pitch = pData->Pitch;
  357. ptr = pData->Y + pInfo->CropX + pInfo->CropY * pData->Pitch;
  358. // load Y plane
  359. for (i = 0; i < h; i++)
  360. memcpy(ptr + i * pitch, pDataY + i * strideY, w);
  361. // load UV plane
  362. h /= 2;
  363. ptr = pData->UV + pInfo->CropX + (pInfo->CropY / 2) * pitch;
  364. for (i = 0; i < h; i++)
  365. memcpy(ptr + i * pitch, pDataUV + i * strideUV, w);
  366. return MFX_ERR_NONE;
  367. }
  368. int QSV_Encoder_Internal::GetFreeTaskIndex(Task* pTaskPool, mfxU16 nPoolSize)
  369. {
  370. if (pTaskPool)
  371. for (int i = 0; i < nPoolSize; i++)
  372. if (!pTaskPool[i].syncp)
  373. return i;
  374. return MFX_ERR_NOT_FOUND;
  375. }
  376. mfxStatus QSV_Encoder_Internal::Encode(uint64_t ts, uint8_t *pDataY,
  377. uint8_t *pDataUV, uint32_t strideY, uint32_t strideUV,
  378. mfxBitstream **pBS)
  379. {
  380. mfxStatus sts = MFX_ERR_NONE;
  381. *pBS = NULL;
  382. int nTaskIdx = GetFreeTaskIndex(m_pTaskPool, m_nTaskPool);
  383. #if 0
  384. info("MSDK Encode:\n"
  385. "\tTaskIndex: %d",
  386. nTaskIdx);
  387. #endif
  388. int nSurfIdx = GetFreeSurfaceIndex(m_pmfxSurfaces, m_nSurfNum);
  389. #if 0
  390. info("MSDK Encode:\n"
  391. "\tnSurfIdx: %d",
  392. nSurfIdx);
  393. #endif
  394. while (MFX_ERR_NOT_FOUND == nTaskIdx || MFX_ERR_NOT_FOUND == nSurfIdx) {
  395. // No more free tasks or surfaces, need to sync
  396. sts = m_session.SyncOperation(m_pTaskPool[m_nFirstSyncTask].syncp,
  397. 60000);
  398. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  399. mfxU8 *pTemp = m_outBitstream.Data;
  400. memcpy(&m_outBitstream, &m_pTaskPool[m_nFirstSyncTask].mfxBS,
  401. sizeof(mfxBitstream));
  402. m_pTaskPool[m_nFirstSyncTask].mfxBS.Data = pTemp;
  403. m_pTaskPool[m_nFirstSyncTask].mfxBS.DataLength = 0;
  404. m_pTaskPool[m_nFirstSyncTask].mfxBS.DataOffset = 0;
  405. m_pTaskPool[m_nFirstSyncTask].syncp = NULL;
  406. nTaskIdx = m_nFirstSyncTask;
  407. m_nFirstSyncTask = (m_nFirstSyncTask + 1) % m_nTaskPool;
  408. *pBS = &m_outBitstream;
  409. #if 0
  410. info("MSDK Encode:\n"
  411. "\tnew FirstSyncTask: %d\n"
  412. "\tTaskIndex: %d",
  413. m_nFirstSyncTask,
  414. nTaskIdx);
  415. #endif
  416. nSurfIdx = GetFreeSurfaceIndex(m_pmfxSurfaces, m_nSurfNum);
  417. #if 0
  418. info("MSDK Encode:\n"
  419. "\tnSurfIdx: %d",
  420. nSurfIdx);
  421. #endif
  422. }
  423. mfxFrameSurface1 *pSurface = m_pmfxSurfaces[nSurfIdx];
  424. if (m_bUseD3D11 || m_bD3D9HACK) {
  425. sts = m_mfxAllocator.Lock(m_mfxAllocator.pthis,
  426. pSurface->Data.MemId, &(pSurface->Data));
  427. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  428. }
  429. sts = LoadNV12(pSurface, pDataY, pDataUV, strideY, strideUV);
  430. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  431. pSurface->Data.TimeStamp = ts;
  432. if (m_bUseD3D11 || m_bD3D9HACK) {
  433. sts = m_mfxAllocator.Unlock(m_mfxAllocator.pthis,
  434. pSurface->Data.MemId, &(pSurface->Data));
  435. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  436. }
  437. for (;;) {
  438. // Encode a frame asynchronously (returns immediately)
  439. sts = m_pmfxENC->EncodeFrameAsync(NULL, pSurface,
  440. &m_pTaskPool[nTaskIdx].mfxBS,
  441. &m_pTaskPool[nTaskIdx].syncp);
  442. if (MFX_ERR_NONE < sts && !m_pTaskPool[nTaskIdx].syncp) {
  443. // Repeat the call if warning and no output
  444. if (MFX_WRN_DEVICE_BUSY == sts)
  445. MSDK_SLEEP(1); // Wait if device is busy, then repeat the same call
  446. } else if (MFX_ERR_NONE < sts && m_pTaskPool[nTaskIdx].syncp) {
  447. sts = MFX_ERR_NONE; // Ignore warnings if output is available
  448. break;
  449. } else if (MFX_ERR_NOT_ENOUGH_BUFFER == sts) {
  450. // Allocate more bitstream buffer memory here if needed...
  451. break;
  452. } else
  453. break;
  454. }
  455. return sts;
  456. }
  457. mfxStatus QSV_Encoder_Internal::Drain()
  458. {
  459. mfxStatus sts = MFX_ERR_NONE;
  460. while (m_pTaskPool && m_pTaskPool[m_nFirstSyncTask].syncp) {
  461. sts = m_session.SyncOperation(m_pTaskPool[m_nFirstSyncTask].syncp, 60000);
  462. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  463. m_pTaskPool[m_nFirstSyncTask].syncp = NULL;
  464. m_nFirstSyncTask = (m_nFirstSyncTask + 1) % m_nTaskPool;
  465. }
  466. return sts;
  467. }
  468. mfxStatus QSV_Encoder_Internal::ClearData()
  469. {
  470. mfxStatus sts = MFX_ERR_NONE;
  471. sts = Drain();
  472. sts = m_pmfxENC->Close();
  473. if (m_bUseD3D11 || m_bD3D9HACK)
  474. m_mfxAllocator.Free(m_mfxAllocator.pthis, &m_mfxResponse);
  475. for (int i = 0; i < m_nSurfNum; i++) {
  476. if (!m_bUseD3D11 && !m_bD3D9HACK)
  477. delete m_pmfxSurfaces[i]->Data.Y;
  478. delete m_pmfxSurfaces[i];
  479. }
  480. MSDK_SAFE_DELETE_ARRAY(m_pmfxSurfaces);
  481. for (int i = 0; i < m_nTaskPool; i++)
  482. delete m_pTaskPool[i].mfxBS.Data;
  483. MSDK_SAFE_DELETE_ARRAY(m_pTaskPool);
  484. delete m_outBitstream.Data;
  485. delete m_pmfxENC;
  486. m_pmfxENC = NULL;
  487. if (m_bUseD3D11 || m_bD3D9HACK)
  488. Release();
  489. m_session.Close();
  490. return sts;
  491. }
  492. mfxStatus QSV_Encoder_Internal::Reset(qsv_param_t *pParams)
  493. {
  494. mfxStatus sts = ClearData();
  495. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  496. sts = Open(pParams);
  497. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  498. return sts;
  499. }