QSV_Encoder_Internal.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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, "msdk_impl", ##__VA_ARGS__)
  50. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  51. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  52. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  53. mfxHDL QSV_Encoder_Internal::g_DX_Handle = NULL;
  54. mfxU16 QSV_Encoder_Internal::g_numEncodersOpen = 0;
  55. QSV_Encoder_Internal::QSV_Encoder_Internal(mfxIMPL &impl, mfxVersion &version)
  56. : m_pmfxSurfaces(NULL),
  57. m_pmfxENC(NULL),
  58. m_nSPSBufferSize(1024),
  59. m_nPPSBufferSize(1024),
  60. m_nTaskPool(0),
  61. m_pTaskPool(NULL),
  62. m_nTaskIdx(0),
  63. m_nFirstSyncTask(0),
  64. m_outBitstream()
  65. {
  66. mfxIMPL tempImpl;
  67. mfxStatus sts;
  68. m_bIsWindows8OrGreater = IsWindows8OrGreater();
  69. m_bUseD3D11 = false;
  70. m_bD3D9HACK = true;
  71. if (m_bIsWindows8OrGreater) {
  72. tempImpl = impl | MFX_IMPL_VIA_D3D11;
  73. sts = m_session.Init(tempImpl, &version);
  74. if (sts == MFX_ERR_NONE) {
  75. m_session.QueryVersion(&version);
  76. m_session.Close();
  77. // Use D3D11 surface
  78. // m_bUseD3D11 = ((version.Major > 1) ||
  79. // (version.Major == 1 && version.Minor >= 8));
  80. m_bUseD3D11 = true;
  81. if (m_bUseD3D11)
  82. blog(LOG_INFO, "\timpl: D3D11\n"
  83. "\tsurf: D3D11");
  84. else
  85. blog(LOG_INFO, "\timpl: D3D11\n"
  86. "\tsurf: SysMem");
  87. m_impl = tempImpl;
  88. m_ver = version;
  89. return;
  90. }
  91. } else if (m_bD3D9HACK) {
  92. tempImpl = impl | MFX_IMPL_VIA_D3D9;
  93. sts = m_session.Init(tempImpl, &version);
  94. if (sts == MFX_ERR_NONE) {
  95. m_session.QueryVersion(&version);
  96. m_session.Close();
  97. blog(LOG_INFO, "\timpl: D3D09\n"
  98. "\tsurf: Hack");
  99. m_impl = tempImpl;
  100. m_ver = version;
  101. return;
  102. }
  103. }
  104. // Either windows 7 or D3D11 failed at this point.
  105. tempImpl = impl | MFX_IMPL_VIA_D3D9;
  106. sts = m_session.Init(tempImpl, &version);
  107. if (sts == MFX_ERR_NONE) {
  108. m_session.QueryVersion(&version);
  109. m_session.Close();
  110. blog(LOG_INFO, "\timpl: D3D09\n"
  111. "\tsurf: SysMem");
  112. m_impl = tempImpl;
  113. m_ver = version;
  114. }
  115. }
  116. QSV_Encoder_Internal::~QSV_Encoder_Internal()
  117. {
  118. if (m_pmfxENC)
  119. ClearData();
  120. }
  121. mfxStatus QSV_Encoder_Internal::Open(qsv_param_t *pParams)
  122. {
  123. mfxStatus sts = MFX_ERR_NONE;
  124. if (m_bUseD3D11)
  125. // Use D3D11 surface
  126. sts = Initialize(m_impl, m_ver, &m_session, &m_mfxAllocator,
  127. &g_DX_Handle, false, false);
  128. else if (m_bD3D9HACK)
  129. // Use hack
  130. sts = Initialize(m_impl, m_ver, &m_session, &m_mfxAllocator,
  131. &g_DX_Handle, false, true);
  132. else
  133. sts = Initialize(m_impl, m_ver, &m_session, NULL);
  134. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  135. m_pmfxENC = new MFXVideoENCODE(m_session);
  136. InitParams(pParams);
  137. sts = m_pmfxENC->Query(&m_mfxEncParams, &m_mfxEncParams);
  138. MSDK_IGNORE_MFX_STS(sts, MFX_WRN_INCOMPATIBLE_VIDEO_PARAM);
  139. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  140. sts = AllocateSurfaces();
  141. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  142. sts = m_pmfxENC->Init(&m_mfxEncParams);
  143. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  144. sts = GetVideoParam();
  145. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  146. sts = InitBitstream();
  147. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  148. if (sts >= MFX_ERR_NONE) {
  149. g_numEncodersOpen++;
  150. }
  151. return sts;
  152. }
  153. bool QSV_Encoder_Internal::InitParams(qsv_param_t *pParams)
  154. {
  155. memset(&m_mfxEncParams, 0, sizeof(m_mfxEncParams));
  156. m_mfxEncParams.mfx.CodecId = MFX_CODEC_AVC;
  157. m_mfxEncParams.mfx.GopOptFlag = MFX_GOP_STRICT;
  158. m_mfxEncParams.mfx.NumSlice = 1;
  159. m_mfxEncParams.mfx.TargetUsage = pParams->nTargetUsage;
  160. m_mfxEncParams.mfx.CodecProfile = pParams->nCodecProfile;
  161. m_mfxEncParams.mfx.FrameInfo.FrameRateExtN = pParams->nFpsNum;
  162. m_mfxEncParams.mfx.FrameInfo.FrameRateExtD = pParams->nFpsDen;
  163. m_mfxEncParams.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
  164. m_mfxEncParams.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  165. m_mfxEncParams.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
  166. m_mfxEncParams.mfx.FrameInfo.CropX = 0;
  167. m_mfxEncParams.mfx.FrameInfo.CropY = 0;
  168. m_mfxEncParams.mfx.FrameInfo.CropW = pParams->nWidth;
  169. m_mfxEncParams.mfx.FrameInfo.CropH = pParams->nHeight;
  170. m_mfxEncParams.mfx.GopRefDist = pParams->nbFrames + 1;
  171. m_mfxEncParams.mfx.RateControlMethod = pParams->nRateControl;
  172. switch (pParams->nRateControl) {
  173. case MFX_RATECONTROL_CBR:
  174. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  175. break;
  176. case MFX_RATECONTROL_VBR:
  177. case MFX_RATECONTROL_VCM:
  178. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  179. m_mfxEncParams.mfx.MaxKbps = pParams->nMaxBitRate;
  180. break;
  181. case MFX_RATECONTROL_CQP:
  182. m_mfxEncParams.mfx.QPI = pParams->nQPI;
  183. m_mfxEncParams.mfx.QPB = pParams->nQPB;
  184. m_mfxEncParams.mfx.QPP = pParams->nQPP;
  185. break;
  186. case MFX_RATECONTROL_AVBR:
  187. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  188. m_mfxEncParams.mfx.Accuracy = pParams->nAccuracy;
  189. m_mfxEncParams.mfx.Convergence = pParams->nConvergence;
  190. break;
  191. case MFX_RATECONTROL_ICQ:
  192. m_mfxEncParams.mfx.ICQQuality = pParams->nICQQuality;
  193. break;
  194. case MFX_RATECONTROL_LA:
  195. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  196. break;
  197. case MFX_RATECONTROL_LA_ICQ:
  198. m_mfxEncParams.mfx.ICQQuality = pParams->nICQQuality;
  199. break;
  200. case MFX_RATECONTROL_LA_HRD:
  201. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  202. m_mfxEncParams.mfx.MaxKbps = pParams->nTargetBitRate;
  203. break;
  204. default:
  205. break;
  206. }
  207. m_mfxEncParams.AsyncDepth = pParams->nAsyncDepth;
  208. m_mfxEncParams.mfx.GopPicSize =
  209. (mfxU16)(pParams->nKeyIntSec * pParams->nFpsNum /
  210. (float)pParams->nFpsDen);
  211. static mfxExtBuffer *extendedBuffers[3];
  212. int iBuffers = 0;
  213. if (m_ver.Major == 1 && m_ver.Minor >= 8) {
  214. memset(&m_co2, 0, sizeof(mfxExtCodingOption2));
  215. m_co2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
  216. m_co2.Header.BufferSz = sizeof(m_co2);
  217. if (pParams->nRateControl == MFX_RATECONTROL_LA_ICQ ||
  218. pParams->nRateControl == MFX_RATECONTROL_LA)
  219. m_co2.LookAheadDepth = pParams->nLADEPTH;
  220. if (pParams->bMBBRC)
  221. m_co2.MBBRC = MFX_CODINGOPTION_ON;
  222. if (pParams->nbFrames > 1)
  223. m_co2.BRefType = MFX_B_REF_PYRAMID;
  224. extendedBuffers[iBuffers++] = (mfxExtBuffer *)&m_co2;
  225. }
  226. if (pParams->bCQM) {
  227. if (m_ver.Major == 1 && m_ver.Minor >= 16) {
  228. memset(&m_co3, 0, sizeof(mfxExtCodingOption3));
  229. m_co3.Header.BufferId = MFX_EXTBUFF_CODING_OPTION3;
  230. m_co3.Header.BufferSz = sizeof(m_co3);
  231. m_co3.ScenarioInfo = 7; // MFX_SCENARIO_GAME_STREAMING
  232. extendedBuffers[iBuffers++] = (mfxExtBuffer *)&m_co3;
  233. }
  234. }
  235. if (iBuffers > 0) {
  236. m_mfxEncParams.ExtParam = extendedBuffers;
  237. m_mfxEncParams.NumExtParam = (mfxU16)iBuffers;
  238. }
  239. // Width must be a multiple of 16
  240. // Height must be a multiple of 16 in case of frame picture and a
  241. // multiple of 32 in case of field picture
  242. m_mfxEncParams.mfx.FrameInfo.Width = MSDK_ALIGN16(pParams->nWidth);
  243. m_mfxEncParams.mfx.FrameInfo.Height = MSDK_ALIGN16(pParams->nHeight);
  244. if (m_bUseD3D11 || m_bD3D9HACK)
  245. m_mfxEncParams.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
  246. else
  247. m_mfxEncParams.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
  248. return true;
  249. }
  250. mfxStatus QSV_Encoder_Internal::AllocateSurfaces()
  251. {
  252. // Query number of required surfaces for encoder
  253. mfxFrameAllocRequest EncRequest;
  254. memset(&EncRequest, 0, sizeof(EncRequest));
  255. mfxStatus sts = m_pmfxENC->QueryIOSurf(&m_mfxEncParams, &EncRequest);
  256. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  257. EncRequest.Type |= WILL_WRITE;
  258. // SNB hack. On some SNB, it seems to require more surfaces
  259. EncRequest.NumFrameSuggested += m_mfxEncParams.AsyncDepth;
  260. // Allocate required surfaces
  261. if (m_bUseD3D11 || m_bD3D9HACK) {
  262. sts = m_mfxAllocator.Alloc(m_mfxAllocator.pthis, &EncRequest,
  263. &m_mfxResponse);
  264. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  265. m_nSurfNum = m_mfxResponse.NumFrameActual;
  266. m_pmfxSurfaces = new mfxFrameSurface1 *[m_nSurfNum];
  267. MSDK_CHECK_POINTER(m_pmfxSurfaces, MFX_ERR_MEMORY_ALLOC);
  268. for (int i = 0; i < m_nSurfNum; i++) {
  269. m_pmfxSurfaces[i] = new mfxFrameSurface1;
  270. memset(m_pmfxSurfaces[i], 0, sizeof(mfxFrameSurface1));
  271. memcpy(&(m_pmfxSurfaces[i]->Info),
  272. &(m_mfxEncParams.mfx.FrameInfo),
  273. sizeof(mfxFrameInfo));
  274. m_pmfxSurfaces[i]->Data.MemId = m_mfxResponse.mids[i];
  275. }
  276. } else {
  277. mfxU16 width = (mfxU16)MSDK_ALIGN32(EncRequest.Info.Width);
  278. mfxU16 height = (mfxU16)MSDK_ALIGN32(EncRequest.Info.Height);
  279. mfxU8 bitsPerPixel = 12;
  280. mfxU32 surfaceSize = width * height * bitsPerPixel / 8;
  281. m_nSurfNum = EncRequest.NumFrameSuggested;
  282. m_pmfxSurfaces = new mfxFrameSurface1 *[m_nSurfNum];
  283. for (int i = 0; i < m_nSurfNum; i++) {
  284. m_pmfxSurfaces[i] = new mfxFrameSurface1;
  285. memset(m_pmfxSurfaces[i], 0, sizeof(mfxFrameSurface1));
  286. memcpy(&(m_pmfxSurfaces[i]->Info),
  287. &(m_mfxEncParams.mfx.FrameInfo),
  288. sizeof(mfxFrameInfo));
  289. mfxU8 *pSurface = (mfxU8 *)new mfxU8[surfaceSize];
  290. m_pmfxSurfaces[i]->Data.Y = pSurface;
  291. m_pmfxSurfaces[i]->Data.U = pSurface + width * height;
  292. m_pmfxSurfaces[i]->Data.V =
  293. pSurface + width * height + 1;
  294. m_pmfxSurfaces[i]->Data.Pitch = width;
  295. }
  296. }
  297. blog(LOG_INFO, "\tm_nSurfNum: %d", m_nSurfNum);
  298. return sts;
  299. }
  300. mfxStatus QSV_Encoder_Internal::GetVideoParam()
  301. {
  302. memset(&m_parameter, 0, sizeof(m_parameter));
  303. mfxExtCodingOptionSPSPPS opt;
  304. memset(&m_parameter, 0, sizeof(m_parameter));
  305. opt.Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS;
  306. opt.Header.BufferSz = sizeof(mfxExtCodingOptionSPSPPS);
  307. static mfxExtBuffer *extendedBuffers[1];
  308. extendedBuffers[0] = (mfxExtBuffer *)&opt;
  309. m_parameter.ExtParam = extendedBuffers;
  310. m_parameter.NumExtParam = 1;
  311. opt.SPSBuffer = m_SPSBuffer;
  312. opt.PPSBuffer = m_PPSBuffer;
  313. opt.SPSBufSize = 1024; // m_nSPSBufferSize;
  314. opt.PPSBufSize = 1024; // m_nPPSBufferSize;
  315. mfxStatus sts = m_pmfxENC->GetVideoParam(&m_parameter);
  316. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  317. m_nSPSBufferSize = opt.SPSBufSize;
  318. m_nPPSBufferSize = opt.PPSBufSize;
  319. return sts;
  320. }
  321. void QSV_Encoder_Internal::GetSPSPPS(mfxU8 **pSPSBuf, mfxU8 **pPPSBuf,
  322. mfxU16 *pnSPSBuf, mfxU16 *pnPPSBuf)
  323. {
  324. *pSPSBuf = m_SPSBuffer;
  325. *pPPSBuf = m_PPSBuffer;
  326. *pnSPSBuf = m_nSPSBufferSize;
  327. *pnPPSBuf = m_nPPSBufferSize;
  328. }
  329. mfxStatus QSV_Encoder_Internal::InitBitstream()
  330. {
  331. m_nTaskPool = m_parameter.AsyncDepth;
  332. m_nFirstSyncTask = 0;
  333. m_pTaskPool = new Task[m_nTaskPool];
  334. memset(m_pTaskPool, 0, sizeof(Task) * m_nTaskPool);
  335. for (int i = 0; i < m_nTaskPool; i++) {
  336. m_pTaskPool[i].mfxBS.MaxLength =
  337. m_parameter.mfx.BufferSizeInKB * 1000;
  338. m_pTaskPool[i].mfxBS.Data =
  339. new mfxU8[m_pTaskPool[i].mfxBS.MaxLength];
  340. m_pTaskPool[i].mfxBS.DataOffset = 0;
  341. m_pTaskPool[i].mfxBS.DataLength = 0;
  342. MSDK_CHECK_POINTER(m_pTaskPool[i].mfxBS.Data,
  343. MFX_ERR_MEMORY_ALLOC);
  344. }
  345. memset(&m_outBitstream, 0, sizeof(mfxBitstream));
  346. m_outBitstream.MaxLength = m_parameter.mfx.BufferSizeInKB * 1000;
  347. m_outBitstream.Data = new mfxU8[m_outBitstream.MaxLength];
  348. m_outBitstream.DataOffset = 0;
  349. m_outBitstream.DataLength = 0;
  350. blog(LOG_INFO, "\tm_nTaskPool: %d", m_nTaskPool);
  351. return MFX_ERR_NONE;
  352. }
  353. mfxStatus QSV_Encoder_Internal::LoadNV12(mfxFrameSurface1 *pSurface,
  354. uint8_t *pDataY, uint8_t *pDataUV,
  355. uint32_t strideY, uint32_t strideUV)
  356. {
  357. mfxU16 w, h, i, pitch;
  358. mfxU8 *ptr;
  359. mfxFrameInfo *pInfo = &pSurface->Info;
  360. mfxFrameData *pData = &pSurface->Data;
  361. if (pInfo->CropH > 0 && pInfo->CropW > 0) {
  362. w = pInfo->CropW;
  363. h = pInfo->CropH;
  364. } else {
  365. w = pInfo->Width;
  366. h = pInfo->Height;
  367. }
  368. pitch = pData->Pitch;
  369. ptr = pData->Y + pInfo->CropX + pInfo->CropY * pData->Pitch;
  370. // load Y plane
  371. for (i = 0; i < h; i++)
  372. memcpy(ptr + i * pitch, pDataY + i * strideY, w);
  373. // load UV plane
  374. h /= 2;
  375. ptr = pData->UV + pInfo->CropX + (pInfo->CropY / 2) * pitch;
  376. for (i = 0; i < h; i++)
  377. memcpy(ptr + i * pitch, pDataUV + i * strideUV, w);
  378. return MFX_ERR_NONE;
  379. }
  380. int QSV_Encoder_Internal::GetFreeTaskIndex(Task *pTaskPool, mfxU16 nPoolSize)
  381. {
  382. if (pTaskPool)
  383. for (int i = 0; i < nPoolSize; i++)
  384. if (!pTaskPool[i].syncp)
  385. return i;
  386. return MFX_ERR_NOT_FOUND;
  387. }
  388. mfxStatus QSV_Encoder_Internal::Encode(uint64_t ts, uint8_t *pDataY,
  389. uint8_t *pDataUV, uint32_t strideY,
  390. uint32_t strideUV, mfxBitstream **pBS)
  391. {
  392. mfxStatus sts = MFX_ERR_NONE;
  393. *pBS = NULL;
  394. int nTaskIdx = GetFreeTaskIndex(m_pTaskPool, m_nTaskPool);
  395. #if 0
  396. info("MSDK Encode:\n"
  397. "\tTaskIndex: %d",
  398. nTaskIdx);
  399. #endif
  400. int nSurfIdx = GetFreeSurfaceIndex(m_pmfxSurfaces, m_nSurfNum);
  401. #if 0
  402. info("MSDK Encode:\n"
  403. "\tnSurfIdx: %d",
  404. nSurfIdx);
  405. #endif
  406. while (MFX_ERR_NOT_FOUND == nTaskIdx || MFX_ERR_NOT_FOUND == nSurfIdx) {
  407. // No more free tasks or surfaces, need to sync
  408. sts = m_session.SyncOperation(
  409. m_pTaskPool[m_nFirstSyncTask].syncp, 60000);
  410. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  411. mfxU8 *pTemp = m_outBitstream.Data;
  412. memcpy(&m_outBitstream, &m_pTaskPool[m_nFirstSyncTask].mfxBS,
  413. sizeof(mfxBitstream));
  414. m_pTaskPool[m_nFirstSyncTask].mfxBS.Data = pTemp;
  415. m_pTaskPool[m_nFirstSyncTask].mfxBS.DataLength = 0;
  416. m_pTaskPool[m_nFirstSyncTask].mfxBS.DataOffset = 0;
  417. m_pTaskPool[m_nFirstSyncTask].syncp = NULL;
  418. nTaskIdx = m_nFirstSyncTask;
  419. m_nFirstSyncTask = (m_nFirstSyncTask + 1) % m_nTaskPool;
  420. *pBS = &m_outBitstream;
  421. #if 0
  422. info("MSDK Encode:\n"
  423. "\tnew FirstSyncTask: %d\n"
  424. "\tTaskIndex: %d",
  425. m_nFirstSyncTask,
  426. nTaskIdx);
  427. #endif
  428. nSurfIdx = GetFreeSurfaceIndex(m_pmfxSurfaces, m_nSurfNum);
  429. #if 0
  430. info("MSDK Encode:\n"
  431. "\tnSurfIdx: %d",
  432. nSurfIdx);
  433. #endif
  434. }
  435. mfxFrameSurface1 *pSurface = m_pmfxSurfaces[nSurfIdx];
  436. if (m_bUseD3D11 || m_bD3D9HACK) {
  437. sts = m_mfxAllocator.Lock(m_mfxAllocator.pthis,
  438. pSurface->Data.MemId,
  439. &(pSurface->Data));
  440. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  441. }
  442. sts = LoadNV12(pSurface, pDataY, pDataUV, strideY, strideUV);
  443. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  444. pSurface->Data.TimeStamp = ts;
  445. if (m_bUseD3D11 || m_bD3D9HACK) {
  446. sts = m_mfxAllocator.Unlock(m_mfxAllocator.pthis,
  447. pSurface->Data.MemId,
  448. &(pSurface->Data));
  449. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  450. }
  451. for (;;) {
  452. // Encode a frame asynchronously (returns immediately)
  453. sts = m_pmfxENC->EncodeFrameAsync(NULL, pSurface,
  454. &m_pTaskPool[nTaskIdx].mfxBS,
  455. &m_pTaskPool[nTaskIdx].syncp);
  456. if (MFX_ERR_NONE < sts && !m_pTaskPool[nTaskIdx].syncp) {
  457. // Repeat the call if warning and no output
  458. if (MFX_WRN_DEVICE_BUSY == sts)
  459. MSDK_SLEEP(
  460. 1); // Wait if device is busy, then repeat the same call
  461. } else if (MFX_ERR_NONE < sts && m_pTaskPool[nTaskIdx].syncp) {
  462. sts = MFX_ERR_NONE; // Ignore warnings if output is available
  463. break;
  464. } else if (MFX_ERR_NOT_ENOUGH_BUFFER == sts) {
  465. // Allocate more bitstream buffer memory here if needed...
  466. break;
  467. } else
  468. break;
  469. }
  470. return sts;
  471. }
  472. mfxStatus QSV_Encoder_Internal::Drain()
  473. {
  474. mfxStatus sts = MFX_ERR_NONE;
  475. while (m_pTaskPool && m_pTaskPool[m_nFirstSyncTask].syncp) {
  476. sts = m_session.SyncOperation(
  477. m_pTaskPool[m_nFirstSyncTask].syncp, 60000);
  478. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  479. m_pTaskPool[m_nFirstSyncTask].syncp = NULL;
  480. m_nFirstSyncTask = (m_nFirstSyncTask + 1) % m_nTaskPool;
  481. }
  482. return sts;
  483. }
  484. mfxStatus QSV_Encoder_Internal::ClearData()
  485. {
  486. mfxStatus sts = MFX_ERR_NONE;
  487. sts = Drain();
  488. if (m_pmfxENC) {
  489. sts = m_pmfxENC->Close();
  490. delete m_pmfxENC;
  491. m_pmfxENC = NULL;
  492. }
  493. if (m_bUseD3D11 || m_bD3D9HACK)
  494. m_mfxAllocator.Free(m_mfxAllocator.pthis, &m_mfxResponse);
  495. if (m_pmfxSurfaces) {
  496. for (int i = 0; i < m_nSurfNum; i++) {
  497. if (!m_bUseD3D11 && !m_bD3D9HACK)
  498. delete m_pmfxSurfaces[i]->Data.Y;
  499. delete m_pmfxSurfaces[i];
  500. }
  501. MSDK_SAFE_DELETE_ARRAY(m_pmfxSurfaces);
  502. }
  503. if (m_pTaskPool) {
  504. for (int i = 0; i < m_nTaskPool; i++)
  505. delete m_pTaskPool[i].mfxBS.Data;
  506. MSDK_SAFE_DELETE_ARRAY(m_pTaskPool);
  507. }
  508. if (m_outBitstream.Data) {
  509. delete m_outBitstream.Data;
  510. m_outBitstream.Data = NULL;
  511. }
  512. if (sts >= MFX_ERR_NONE) {
  513. g_numEncodersOpen--;
  514. }
  515. if ((m_bUseD3D11 || m_bD3D9HACK) && (g_numEncodersOpen <= 0)) {
  516. Release();
  517. g_DX_Handle = NULL;
  518. }
  519. m_session.Close();
  520. return sts;
  521. }
  522. mfxStatus QSV_Encoder_Internal::Reset(qsv_param_t *pParams)
  523. {
  524. mfxStatus sts = ClearData();
  525. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  526. sts = Open(pParams);
  527. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  528. return sts;
  529. }