QSV_Encoder_Internal.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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 <vpl/mfxstructures.h>
  45. #include <vpl/mfxvideo++.h>
  46. #include <vpl/mfxdispatcher.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(mfxVersion &version, bool isDGPU)
  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. m_isDGPU(isDGPU),
  66. m_sessionData(NULL)
  67. {
  68. mfxVariant tempImpl;
  69. mfxStatus sts;
  70. mfxLoader loader = MFXLoad();
  71. mfxConfig cfg = MFXCreateConfig(loader);
  72. tempImpl.Type = MFX_VARIANT_TYPE_U32;
  73. tempImpl.Data.U32 = MFX_IMPL_TYPE_HARDWARE;
  74. MFXSetConfigFilterProperty(
  75. cfg, (const mfxU8 *)"mfxImplDescription.Impl", tempImpl);
  76. tempImpl.Type = MFX_VARIANT_TYPE_U32;
  77. tempImpl.Data.U32 = INTEL_VENDOR_ID;
  78. MFXSetConfigFilterProperty(
  79. cfg, (const mfxU8 *)"mfxImplDescription.VendorID", tempImpl);
  80. #if defined(_WIN32)
  81. m_bUseD3D11 = true;
  82. m_bUseTexAlloc = true;
  83. tempImpl.Type = MFX_VARIANT_TYPE_U32;
  84. tempImpl.Data.U32 = MFX_ACCEL_MODE_VIA_D3D11;
  85. MFXSetConfigFilterProperty(
  86. cfg, (const mfxU8 *)"mfxImplDescription.AccelerationMode",
  87. tempImpl);
  88. #else
  89. m_bUseTexAlloc = false;
  90. tempImpl.Type = MFX_VARIANT_TYPE_U32;
  91. tempImpl.Data.U32 = MFX_ACCEL_MODE_VIA_VAAPI;
  92. MFXSetConfigFilterProperty(
  93. cfg, (const mfxU8 *)"mfxImplDescription.AccelerationMode",
  94. tempImpl);
  95. #endif
  96. sts = MFXCreateSession(loader, 0, &m_session);
  97. if (sts == MFX_ERR_NONE) {
  98. MFXQueryVersion(m_session, &version);
  99. MFXClose(m_session);
  100. MFXUnload(loader);
  101. blog(LOG_INFO, "\tsurf: %s",
  102. m_bUseTexAlloc ? "Texture" : "SysMem");
  103. m_ver = version;
  104. return;
  105. }
  106. }
  107. QSV_Encoder_Internal::~QSV_Encoder_Internal()
  108. {
  109. if (m_pmfxENC)
  110. ClearData();
  111. }
  112. mfxStatus QSV_Encoder_Internal::Open(qsv_param_t *pParams, enum qsv_codec codec)
  113. {
  114. mfxStatus sts = MFX_ERR_NONE;
  115. #if defined(_WIN32)
  116. if (m_bUseD3D11)
  117. // Use D3D11 surface
  118. sts = Initialize(m_ver, &m_session, &m_mfxAllocator,
  119. &g_DX_Handle, false, codec, &m_sessionData);
  120. else
  121. sts = Initialize(m_ver, &m_session, NULL, NULL, NULL, codec,
  122. &m_sessionData);
  123. #else
  124. sts = Initialize(m_ver, &m_session, NULL, NULL, false, codec,
  125. &m_sessionData);
  126. #endif
  127. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  128. m_pmfxENC = new MFXVideoENCODE(m_session);
  129. InitParams(pParams, codec);
  130. sts = m_pmfxENC->Query(&m_mfxEncParams, &m_mfxEncParams);
  131. MSDK_IGNORE_MFX_STS(sts, MFX_WRN_INCOMPATIBLE_VIDEO_PARAM);
  132. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  133. sts = AllocateSurfaces();
  134. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  135. sts = m_pmfxENC->Init(&m_mfxEncParams);
  136. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  137. sts = GetVideoParam(codec);
  138. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  139. sts = InitBitstream();
  140. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  141. if (sts >= MFX_ERR_NONE) {
  142. g_numEncodersOpen++;
  143. }
  144. return sts;
  145. }
  146. mfxStatus QSV_Encoder_Internal::InitParams(qsv_param_t *pParams,
  147. enum qsv_codec codec)
  148. {
  149. memset(&m_mfxEncParams, 0, sizeof(m_mfxEncParams));
  150. if (codec == QSV_CODEC_AVC)
  151. m_mfxEncParams.mfx.CodecId = MFX_CODEC_AVC;
  152. else if (codec == QSV_CODEC_AV1)
  153. m_mfxEncParams.mfx.CodecId = MFX_CODEC_AV1;
  154. else if (codec == QSV_CODEC_HEVC)
  155. m_mfxEncParams.mfx.CodecId = MFX_CODEC_HEVC;
  156. if (codec == QSV_CODEC_HEVC) {
  157. m_mfxEncParams.mfx.NumSlice = 0;
  158. m_mfxEncParams.mfx.IdrInterval = 1;
  159. } else {
  160. m_mfxEncParams.mfx.NumSlice = 1;
  161. }
  162. m_mfxEncParams.mfx.TargetUsage = pParams->nTargetUsage;
  163. m_mfxEncParams.mfx.CodecProfile = pParams->nCodecProfile;
  164. m_mfxEncParams.mfx.FrameInfo.FrameRateExtN = pParams->nFpsNum;
  165. m_mfxEncParams.mfx.FrameInfo.FrameRateExtD = pParams->nFpsDen;
  166. if (pParams->video_fmt_10bit) {
  167. m_mfxEncParams.mfx.FrameInfo.FourCC = MFX_FOURCC_P010;
  168. m_mfxEncParams.mfx.FrameInfo.BitDepthChroma = 10;
  169. m_mfxEncParams.mfx.FrameInfo.BitDepthLuma = 10;
  170. m_mfxEncParams.mfx.FrameInfo.Shift = 1;
  171. } else {
  172. m_mfxEncParams.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
  173. }
  174. m_mfxEncParams.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
  175. m_mfxEncParams.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
  176. m_mfxEncParams.mfx.FrameInfo.CropX = 0;
  177. m_mfxEncParams.mfx.FrameInfo.CropY = 0;
  178. m_mfxEncParams.mfx.FrameInfo.CropW = pParams->nWidth;
  179. m_mfxEncParams.mfx.FrameInfo.CropH = pParams->nHeight;
  180. m_mfxEncParams.mfx.GopRefDist = pParams->nbFrames + 1;
  181. mfxPlatform platform;
  182. MFXVideoCORE_QueryPlatform(m_session, &platform);
  183. PRAGMA_WARN_PUSH
  184. PRAGMA_WARN_DEPRECATION
  185. if (codec == QSV_CODEC_AVC || codec == QSV_CODEC_HEVC) {
  186. if (platform.CodeName >= MFX_PLATFORM_DG2)
  187. m_mfxEncParams.mfx.LowPower = MFX_CODINGOPTION_ON;
  188. } else if (codec == QSV_CODEC_AV1) {
  189. m_mfxEncParams.mfx.LowPower = MFX_CODINGOPTION_ON;
  190. }
  191. PRAGMA_WARN_POP
  192. m_mfxEncParams.mfx.RateControlMethod = pParams->nRateControl;
  193. switch (pParams->nRateControl) {
  194. case MFX_RATECONTROL_CBR:
  195. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  196. m_mfxEncParams.mfx.BufferSizeInKB =
  197. (pParams->nTargetBitRate / 8) * 2;
  198. m_mfxEncParams.mfx.InitialDelayInKB =
  199. (pParams->nTargetBitRate / 8) * 1;
  200. break;
  201. case MFX_RATECONTROL_VBR:
  202. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  203. m_mfxEncParams.mfx.MaxKbps = pParams->nMaxBitRate;
  204. m_mfxEncParams.mfx.BufferSizeInKB =
  205. (pParams->nTargetBitRate / 8) * 2;
  206. m_mfxEncParams.mfx.InitialDelayInKB =
  207. (pParams->nTargetBitRate / 8) * 1;
  208. break;
  209. case MFX_RATECONTROL_CQP:
  210. m_mfxEncParams.mfx.QPI = pParams->nQPI;
  211. m_mfxEncParams.mfx.QPB = pParams->nQPB;
  212. m_mfxEncParams.mfx.QPP = pParams->nQPP;
  213. break;
  214. case MFX_RATECONTROL_ICQ:
  215. m_mfxEncParams.mfx.ICQQuality = pParams->nICQQuality;
  216. break;
  217. default:
  218. break;
  219. }
  220. m_mfxEncParams.AsyncDepth = pParams->nAsyncDepth;
  221. m_mfxEncParams.mfx.GopPicSize =
  222. (pParams->nKeyIntSec)
  223. ? (mfxU16)(pParams->nKeyIntSec * pParams->nFpsNum /
  224. (float)pParams->nFpsDen)
  225. : 240;
  226. memset(&m_co2, 0, sizeof(mfxExtCodingOption2));
  227. m_co2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
  228. m_co2.Header.BufferSz = sizeof(m_co2);
  229. if (pParams->bRepeatHeaders)
  230. m_co2.RepeatPPS = MFX_CODINGOPTION_ON;
  231. else
  232. m_co2.RepeatPPS = MFX_CODINGOPTION_OFF;
  233. memset(&m_co3, 0, sizeof(mfxExtCodingOption3));
  234. m_co3.Header.BufferId = MFX_EXTBUFF_CODING_OPTION3;
  235. m_co3.Header.BufferSz = sizeof(m_co3);
  236. if (pParams->nbFrames > 1)
  237. m_co2.BRefType = MFX_B_REF_PYRAMID;
  238. PRAGMA_WARN_PUSH
  239. PRAGMA_WARN_DEPRECATION
  240. // LA VME/ENC case for older platforms
  241. if (pParams->nLADEPTH && codec == QSV_CODEC_AVC &&
  242. m_mfxEncParams.mfx.LowPower != MFX_CODINGOPTION_ON &&
  243. platform.CodeName >= MFX_PLATFORM_ICELAKE) {
  244. if (pParams->nRateControl == MFX_RATECONTROL_CBR) {
  245. pParams->nRateControl = MFX_RATECONTROL_LA_HRD;
  246. } else if (pParams->nRateControl == MFX_RATECONTROL_VBR) {
  247. pParams->nRateControl = MFX_RATECONTROL_LA;
  248. } else if (pParams->nRateControl == MFX_RATECONTROL_ICQ) {
  249. pParams->nRateControl = MFX_RATECONTROL_LA_ICQ;
  250. }
  251. m_co2.LookAheadDepth = pParams->nLADEPTH;
  252. }
  253. PRAGMA_WARN_POP
  254. // LA VDENC case for newer platform, works only under CBR / VBR
  255. if (pParams->nRateControl == MFX_RATECONTROL_CBR ||
  256. pParams->nRateControl == MFX_RATECONTROL_VBR) {
  257. if (pParams->nLADEPTH &&
  258. m_mfxEncParams.mfx.LowPower == MFX_CODINGOPTION_ON) {
  259. m_co2.LookAheadDepth = pParams->nLADEPTH;
  260. }
  261. // CQM to follow UI setting
  262. if (pParams->bCQM && !pParams->bRepeatHeaders) {
  263. m_co3.AdaptiveCQM = MFX_CODINGOPTION_ON;
  264. } else {
  265. m_co3.AdaptiveCQM = MFX_CODINGOPTION_OFF;
  266. }
  267. }
  268. extendedBuffers.push_back((mfxExtBuffer *)&m_co2);
  269. extendedBuffers.push_back((mfxExtBuffer *)&m_co3);
  270. if (codec == QSV_CODEC_HEVC) {
  271. if ((pParams->nWidth & 15) || (pParams->nHeight & 15)) {
  272. memset(&m_ExtHEVCParam, 0, sizeof(m_ExtHEVCParam));
  273. m_ExtHEVCParam.Header.BufferId = MFX_EXTBUFF_HEVC_PARAM;
  274. m_ExtHEVCParam.Header.BufferSz = sizeof(m_ExtHEVCParam);
  275. m_ExtHEVCParam.PicWidthInLumaSamples = pParams->nWidth;
  276. m_ExtHEVCParam.PicHeightInLumaSamples =
  277. pParams->nHeight;
  278. extendedBuffers.push_back(
  279. (mfxExtBuffer *)&m_ExtHEVCParam);
  280. }
  281. }
  282. #if defined(_WIN32)
  283. // TODO: Ask about this one on VAAPI too.
  284. memset(&m_ExtVideoSignalInfo, 0, sizeof(m_ExtVideoSignalInfo));
  285. m_ExtVideoSignalInfo.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO;
  286. m_ExtVideoSignalInfo.Header.BufferSz = sizeof(m_ExtVideoSignalInfo);
  287. m_ExtVideoSignalInfo.VideoFormat = pParams->VideoFormat;
  288. m_ExtVideoSignalInfo.VideoFullRange = pParams->VideoFullRange;
  289. m_ExtVideoSignalInfo.ColourDescriptionPresent = 1;
  290. m_ExtVideoSignalInfo.ColourPrimaries = pParams->ColourPrimaries;
  291. m_ExtVideoSignalInfo.TransferCharacteristics =
  292. pParams->TransferCharacteristics;
  293. m_ExtVideoSignalInfo.MatrixCoefficients = pParams->MatrixCoefficients;
  294. extendedBuffers.push_back((mfxExtBuffer *)&m_ExtVideoSignalInfo);
  295. #endif
  296. // CLL and Chroma location in HEVC only supported by VPL
  297. if (m_ver.Major >= 2) {
  298. // Chroma location is HEVC only
  299. if (codec == QSV_CODEC_HEVC) {
  300. memset(&m_ExtChromaLocInfo, 0,
  301. sizeof(m_ExtChromaLocInfo));
  302. m_ExtChromaLocInfo.Header.BufferId =
  303. MFX_EXTBUFF_CHROMA_LOC_INFO;
  304. m_ExtChromaLocInfo.Header.BufferSz =
  305. sizeof(m_ExtChromaLocInfo);
  306. m_ExtChromaLocInfo.ChromaLocInfoPresentFlag = 1;
  307. m_ExtChromaLocInfo.ChromaSampleLocTypeTopField =
  308. pParams->ChromaSampleLocTypeTopField;
  309. m_ExtChromaLocInfo.ChromaSampleLocTypeBottomField =
  310. pParams->ChromaSampleLocTypeBottomField;
  311. extendedBuffers.push_back(
  312. (mfxExtBuffer *)&m_ExtChromaLocInfo);
  313. }
  314. }
  315. // AV1 HDR meta data is now supported by VPL.
  316. if (pParams->MaxContentLightLevel > 0) {
  317. memset(&m_ExtMasteringDisplayColourVolume, 0,
  318. sizeof(m_ExtMasteringDisplayColourVolume));
  319. m_ExtMasteringDisplayColourVolume.Header.BufferId =
  320. MFX_EXTBUFF_MASTERING_DISPLAY_COLOUR_VOLUME;
  321. m_ExtMasteringDisplayColourVolume.Header.BufferSz =
  322. sizeof(m_ExtMasteringDisplayColourVolume);
  323. m_ExtMasteringDisplayColourVolume.InsertPayloadToggle =
  324. MFX_PAYLOAD_IDR;
  325. m_ExtMasteringDisplayColourVolume.DisplayPrimariesX[0] =
  326. pParams->DisplayPrimariesX[0];
  327. m_ExtMasteringDisplayColourVolume.DisplayPrimariesX[1] =
  328. pParams->DisplayPrimariesX[1];
  329. m_ExtMasteringDisplayColourVolume.DisplayPrimariesX[2] =
  330. pParams->DisplayPrimariesX[2];
  331. m_ExtMasteringDisplayColourVolume.DisplayPrimariesY[0] =
  332. pParams->DisplayPrimariesY[0];
  333. m_ExtMasteringDisplayColourVolume.DisplayPrimariesY[1] =
  334. pParams->DisplayPrimariesY[1];
  335. m_ExtMasteringDisplayColourVolume.DisplayPrimariesY[2] =
  336. pParams->DisplayPrimariesY[2];
  337. m_ExtMasteringDisplayColourVolume.WhitePointX =
  338. pParams->WhitePointX;
  339. m_ExtMasteringDisplayColourVolume.WhitePointY =
  340. pParams->WhitePointY;
  341. m_ExtMasteringDisplayColourVolume.MaxDisplayMasteringLuminance =
  342. pParams->MaxDisplayMasteringLuminance;
  343. m_ExtMasteringDisplayColourVolume.MinDisplayMasteringLuminance =
  344. pParams->MinDisplayMasteringLuminance;
  345. extendedBuffers.push_back(
  346. (mfxExtBuffer *)&m_ExtMasteringDisplayColourVolume);
  347. memset(&m_ExtContentLightLevelInfo, 0,
  348. sizeof(m_ExtContentLightLevelInfo));
  349. m_ExtContentLightLevelInfo.Header.BufferId =
  350. MFX_EXTBUFF_CONTENT_LIGHT_LEVEL_INFO;
  351. m_ExtContentLightLevelInfo.Header.BufferSz =
  352. sizeof(m_ExtContentLightLevelInfo);
  353. m_ExtContentLightLevelInfo.InsertPayloadToggle =
  354. MFX_PAYLOAD_IDR;
  355. m_ExtContentLightLevelInfo.MaxContentLightLevel =
  356. pParams->MaxContentLightLevel;
  357. m_ExtContentLightLevelInfo.MaxPicAverageLightLevel =
  358. pParams->MaxPicAverageLightLevel;
  359. extendedBuffers.push_back(
  360. (mfxExtBuffer *)&m_ExtContentLightLevelInfo);
  361. }
  362. // Width must be a multiple of 16
  363. // Height must be a multiple of 16 in case of frame picture and a
  364. // multiple of 32 in case of field picture
  365. m_mfxEncParams.mfx.FrameInfo.Width = MSDK_ALIGN16(pParams->nWidth);
  366. m_mfxEncParams.mfx.FrameInfo.Height = MSDK_ALIGN16(pParams->nHeight);
  367. if (m_bUseTexAlloc)
  368. m_mfxEncParams.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
  369. else
  370. m_mfxEncParams.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
  371. m_mfxEncParams.ExtParam = extendedBuffers.data();
  372. m_mfxEncParams.NumExtParam = (mfxU16)extendedBuffers.size();
  373. // We don't check what was valid or invalid here, just try changing LowPower.
  374. // Ensure set values are not overwritten so in case it wasn't lowPower we fail
  375. // during the parameter check.
  376. mfxVideoParam validParams = {0};
  377. memcpy(&validParams, &m_mfxEncParams, sizeof(validParams));
  378. mfxStatus sts = m_pmfxENC->Query(&m_mfxEncParams, &validParams);
  379. if (sts == MFX_ERR_UNSUPPORTED || sts == MFX_ERR_UNDEFINED_BEHAVIOR) {
  380. if (m_mfxEncParams.mfx.LowPower == MFX_CODINGOPTION_ON) {
  381. m_mfxEncParams.mfx.LowPower = MFX_CODINGOPTION_OFF;
  382. m_co2.LookAheadDepth = 0;
  383. }
  384. }
  385. return sts;
  386. }
  387. bool QSV_Encoder_Internal::UpdateParams(qsv_param_t *pParams)
  388. {
  389. switch (pParams->nRateControl) {
  390. case MFX_RATECONTROL_CBR:
  391. m_mfxEncParams.mfx.TargetKbps = pParams->nTargetBitRate;
  392. default:
  393. break;
  394. }
  395. return true;
  396. }
  397. mfxStatus QSV_Encoder_Internal::ReconfigureEncoder()
  398. {
  399. return m_pmfxENC->Reset(&m_mfxEncParams);
  400. }
  401. mfxStatus QSV_Encoder_Internal::AllocateSurfaces()
  402. {
  403. // Query number of required surfaces for encoder
  404. mfxFrameAllocRequest EncRequest;
  405. memset(&EncRequest, 0, sizeof(EncRequest));
  406. mfxStatus sts = m_pmfxENC->QueryIOSurf(&m_mfxEncParams, &EncRequest);
  407. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  408. EncRequest.Type |= WILL_WRITE;
  409. // SNB hack. On some SNB, it seems to require more surfaces
  410. EncRequest.NumFrameSuggested += m_mfxEncParams.AsyncDepth;
  411. // Allocate required surfaces
  412. if (m_bUseTexAlloc) {
  413. sts = m_mfxAllocator.Alloc(m_mfxAllocator.pthis, &EncRequest,
  414. &m_mfxResponse);
  415. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  416. m_nSurfNum = m_mfxResponse.NumFrameActual;
  417. m_pmfxSurfaces = new mfxFrameSurface1 *[m_nSurfNum];
  418. MSDK_CHECK_POINTER(m_pmfxSurfaces, MFX_ERR_MEMORY_ALLOC);
  419. for (int i = 0; i < m_nSurfNum; i++) {
  420. m_pmfxSurfaces[i] = new mfxFrameSurface1;
  421. memset(m_pmfxSurfaces[i], 0, sizeof(mfxFrameSurface1));
  422. memcpy(&(m_pmfxSurfaces[i]->Info),
  423. &(m_mfxEncParams.mfx.FrameInfo),
  424. sizeof(mfxFrameInfo));
  425. m_pmfxSurfaces[i]->Data.MemId = m_mfxResponse.mids[i];
  426. }
  427. } else {
  428. mfxU16 width = (mfxU16)MSDK_ALIGN32(EncRequest.Info.Width);
  429. mfxU16 height = (mfxU16)MSDK_ALIGN32(EncRequest.Info.Height);
  430. mfxU8 bitsPerPixel = 12;
  431. mfxU32 surfaceSize = width * height * bitsPerPixel / 8;
  432. m_nSurfNum = EncRequest.NumFrameSuggested;
  433. m_pmfxSurfaces = new mfxFrameSurface1 *[m_nSurfNum];
  434. for (int i = 0; i < m_nSurfNum; i++) {
  435. m_pmfxSurfaces[i] = new mfxFrameSurface1;
  436. memset(m_pmfxSurfaces[i], 0, sizeof(mfxFrameSurface1));
  437. memcpy(&(m_pmfxSurfaces[i]->Info),
  438. &(m_mfxEncParams.mfx.FrameInfo),
  439. sizeof(mfxFrameInfo));
  440. mfxU8 *pSurface = (mfxU8 *)new mfxU8[surfaceSize];
  441. m_pmfxSurfaces[i]->Data.Y = pSurface;
  442. m_pmfxSurfaces[i]->Data.U = pSurface + width * height;
  443. m_pmfxSurfaces[i]->Data.V =
  444. pSurface + width * height + 1;
  445. m_pmfxSurfaces[i]->Data.Pitch = width;
  446. }
  447. }
  448. blog(LOG_INFO, "\tm_nSurfNum: %d", m_nSurfNum);
  449. return sts;
  450. }
  451. mfxStatus QSV_Encoder_Internal::GetVideoParam(enum qsv_codec codec)
  452. {
  453. memset(&m_parameter, 0, sizeof(m_parameter));
  454. mfxExtCodingOptionSPSPPS opt;
  455. memset(&m_parameter, 0, sizeof(m_parameter));
  456. opt.Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS;
  457. opt.Header.BufferSz = sizeof(mfxExtCodingOptionSPSPPS);
  458. std::vector<mfxExtBuffer *> extendedBuffers;
  459. extendedBuffers.reserve(2);
  460. opt.SPSBuffer = m_SPSBuffer;
  461. opt.PPSBuffer = m_PPSBuffer;
  462. opt.SPSBufSize = 1024; // m_nSPSBufferSize;
  463. opt.PPSBufSize = 1024; // m_nPPSBufferSize;
  464. mfxExtCodingOptionVPS opt_vps{};
  465. if (codec == QSV_CODEC_HEVC) {
  466. opt_vps.Header.BufferId = MFX_EXTBUFF_CODING_OPTION_VPS;
  467. opt_vps.Header.BufferSz = sizeof(mfxExtCodingOptionVPS);
  468. opt_vps.VPSBuffer = m_VPSBuffer;
  469. opt_vps.VPSBufSize = 1024;
  470. extendedBuffers.push_back((mfxExtBuffer *)&opt_vps);
  471. }
  472. extendedBuffers.push_back((mfxExtBuffer *)&opt);
  473. m_parameter.ExtParam = extendedBuffers.data();
  474. m_parameter.NumExtParam = (mfxU16)extendedBuffers.size();
  475. mfxStatus sts = m_pmfxENC->GetVideoParam(&m_parameter);
  476. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  477. if (codec == QSV_CODEC_HEVC)
  478. m_nVPSBufferSize = opt_vps.VPSBufSize;
  479. m_nSPSBufferSize = opt.SPSBufSize;
  480. m_nPPSBufferSize = opt.PPSBufSize;
  481. return sts;
  482. }
  483. void QSV_Encoder_Internal::GetSPSPPS(mfxU8 **pSPSBuf, mfxU8 **pPPSBuf,
  484. mfxU16 *pnSPSBuf, mfxU16 *pnPPSBuf)
  485. {
  486. *pSPSBuf = m_SPSBuffer;
  487. *pPPSBuf = m_PPSBuffer;
  488. *pnSPSBuf = m_nSPSBufferSize;
  489. *pnPPSBuf = m_nPPSBufferSize;
  490. }
  491. void QSV_Encoder_Internal::GetVpsSpsPps(mfxU8 **pVPSBuf, mfxU8 **pSPSBuf,
  492. mfxU8 **pPPSBuf, mfxU16 *pnVPSBuf,
  493. mfxU16 *pnSPSBuf, mfxU16 *pnPPSBuf)
  494. {
  495. *pVPSBuf = m_VPSBuffer;
  496. *pnVPSBuf = m_nVPSBufferSize;
  497. *pSPSBuf = m_SPSBuffer;
  498. *pnSPSBuf = m_nSPSBufferSize;
  499. *pPPSBuf = m_PPSBuffer;
  500. *pnPPSBuf = m_nPPSBufferSize;
  501. }
  502. mfxStatus QSV_Encoder_Internal::InitBitstream()
  503. {
  504. m_nTaskPool = m_parameter.AsyncDepth;
  505. m_nFirstSyncTask = 0;
  506. m_pTaskPool = new Task[m_nTaskPool];
  507. memset(m_pTaskPool, 0, sizeof(Task) * m_nTaskPool);
  508. for (int i = 0; i < m_nTaskPool; i++) {
  509. m_pTaskPool[i].mfxBS.MaxLength =
  510. m_parameter.mfx.BufferSizeInKB * 1000;
  511. m_pTaskPool[i].mfxBS.Data =
  512. new mfxU8[m_pTaskPool[i].mfxBS.MaxLength];
  513. m_pTaskPool[i].mfxBS.DataOffset = 0;
  514. m_pTaskPool[i].mfxBS.DataLength = 0;
  515. MSDK_CHECK_POINTER(m_pTaskPool[i].mfxBS.Data,
  516. MFX_ERR_MEMORY_ALLOC);
  517. }
  518. memset(&m_outBitstream, 0, sizeof(mfxBitstream));
  519. m_outBitstream.MaxLength = m_parameter.mfx.BufferSizeInKB * 1000;
  520. m_outBitstream.Data = new mfxU8[m_outBitstream.MaxLength];
  521. m_outBitstream.DataOffset = 0;
  522. m_outBitstream.DataLength = 0;
  523. blog(LOG_INFO, "\tm_nTaskPool: %d", m_nTaskPool);
  524. return MFX_ERR_NONE;
  525. }
  526. mfxStatus QSV_Encoder_Internal::LoadP010(mfxFrameSurface1 *pSurface,
  527. uint8_t *pDataY, uint8_t *pDataUV,
  528. uint32_t strideY, uint32_t strideUV)
  529. {
  530. mfxU16 w, h, i, pitch;
  531. mfxU8 *ptr;
  532. mfxFrameInfo *pInfo = &pSurface->Info;
  533. mfxFrameData *pData = &pSurface->Data;
  534. if (pInfo->CropH > 0 && pInfo->CropW > 0) {
  535. w = pInfo->CropW;
  536. h = pInfo->CropH;
  537. } else {
  538. w = pInfo->Width;
  539. h = pInfo->Height;
  540. }
  541. pitch = pData->Pitch;
  542. ptr = pData->Y + pInfo->CropX + pInfo->CropY * pData->Pitch;
  543. const size_t line_size = w * 2;
  544. // load Y plane
  545. for (i = 0; i < h; i++)
  546. memcpy(ptr + i * pitch, pDataY + i * strideY, line_size);
  547. // load UV plane
  548. h /= 2;
  549. ptr = pData->UV + pInfo->CropX + (pInfo->CropY / 2) * pitch;
  550. for (i = 0; i < h; i++)
  551. memcpy(ptr + i * pitch, pDataUV + i * strideUV, line_size);
  552. return MFX_ERR_NONE;
  553. }
  554. mfxStatus QSV_Encoder_Internal::LoadNV12(mfxFrameSurface1 *pSurface,
  555. uint8_t *pDataY, uint8_t *pDataUV,
  556. uint32_t strideY, uint32_t strideUV)
  557. {
  558. mfxU16 w, h, i, pitch;
  559. mfxU8 *ptr;
  560. mfxFrameInfo *pInfo = &pSurface->Info;
  561. mfxFrameData *pData = &pSurface->Data;
  562. if (pInfo->CropH > 0 && pInfo->CropW > 0) {
  563. w = pInfo->CropW;
  564. h = pInfo->CropH;
  565. } else {
  566. w = pInfo->Width;
  567. h = pInfo->Height;
  568. }
  569. pitch = pData->Pitch;
  570. ptr = pData->Y + pInfo->CropX + pInfo->CropY * pData->Pitch;
  571. // load Y plane
  572. for (i = 0; i < h; i++)
  573. memcpy(ptr + i * pitch, pDataY + i * strideY, w);
  574. // load UV plane
  575. h /= 2;
  576. ptr = pData->UV + pInfo->CropX + (pInfo->CropY / 2) * pitch;
  577. for (i = 0; i < h; i++)
  578. memcpy(ptr + i * pitch, pDataUV + i * strideUV, w);
  579. return MFX_ERR_NONE;
  580. }
  581. int QSV_Encoder_Internal::GetFreeTaskIndex(Task *pTaskPool, mfxU16 nPoolSize)
  582. {
  583. if (pTaskPool)
  584. for (int i = 0; i < nPoolSize; i++)
  585. if (!pTaskPool[i].syncp)
  586. return i;
  587. return MFX_ERR_NOT_FOUND;
  588. }
  589. mfxStatus QSV_Encoder_Internal::Encode(uint64_t ts, uint8_t *pDataY,
  590. uint8_t *pDataUV, uint32_t strideY,
  591. uint32_t strideUV, mfxBitstream **pBS)
  592. {
  593. mfxStatus sts = MFX_ERR_NONE;
  594. *pBS = NULL;
  595. int nTaskIdx = GetFreeTaskIndex(m_pTaskPool, m_nTaskPool);
  596. #if 0
  597. info("MSDK Encode:\n"
  598. "\tTaskIndex: %d",
  599. nTaskIdx);
  600. #endif
  601. int nSurfIdx = GetFreeSurfaceIndex(m_pmfxSurfaces, m_nSurfNum);
  602. #if 0
  603. info("MSDK Encode:\n"
  604. "\tnSurfIdx: %d",
  605. nSurfIdx);
  606. #endif
  607. while (MFX_ERR_NOT_FOUND == nTaskIdx || MFX_ERR_NOT_FOUND == nSurfIdx) {
  608. // No more free tasks or surfaces, need to sync
  609. sts = MFXVideoCORE_SyncOperation(
  610. m_session, m_pTaskPool[m_nFirstSyncTask].syncp, 60000);
  611. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  612. mfxU8 *pTemp = m_outBitstream.Data;
  613. memcpy(&m_outBitstream, &m_pTaskPool[m_nFirstSyncTask].mfxBS,
  614. sizeof(mfxBitstream));
  615. m_pTaskPool[m_nFirstSyncTask].mfxBS.Data = pTemp;
  616. m_pTaskPool[m_nFirstSyncTask].mfxBS.DataLength = 0;
  617. m_pTaskPool[m_nFirstSyncTask].mfxBS.DataOffset = 0;
  618. m_pTaskPool[m_nFirstSyncTask].syncp = NULL;
  619. nTaskIdx = m_nFirstSyncTask;
  620. m_nFirstSyncTask = (m_nFirstSyncTask + 1) % m_nTaskPool;
  621. *pBS = &m_outBitstream;
  622. #if 0
  623. info("MSDK Encode:\n"
  624. "\tnew FirstSyncTask: %d\n"
  625. "\tTaskIndex: %d",
  626. m_nFirstSyncTask,
  627. nTaskIdx);
  628. #endif
  629. nSurfIdx = GetFreeSurfaceIndex(m_pmfxSurfaces, m_nSurfNum);
  630. #if 0
  631. info("MSDK Encode:\n"
  632. "\tnSurfIdx: %d",
  633. nSurfIdx);
  634. #endif
  635. }
  636. mfxFrameSurface1 *pSurface = m_pmfxSurfaces[nSurfIdx];
  637. if (m_bUseTexAlloc) {
  638. sts = m_mfxAllocator.Lock(m_mfxAllocator.pthis,
  639. pSurface->Data.MemId,
  640. &(pSurface->Data));
  641. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  642. }
  643. sts = (pSurface->Info.FourCC == MFX_FOURCC_P010)
  644. ? LoadP010(pSurface, pDataY, pDataUV, strideY, strideUV)
  645. : LoadNV12(pSurface, pDataY, pDataUV, strideY, strideUV);
  646. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  647. pSurface->Data.TimeStamp = ts;
  648. if (m_bUseTexAlloc) {
  649. sts = m_mfxAllocator.Unlock(m_mfxAllocator.pthis,
  650. pSurface->Data.MemId,
  651. &(pSurface->Data));
  652. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  653. }
  654. for (;;) {
  655. // Encode a frame asynchronously (returns immediately)
  656. sts = m_pmfxENC->EncodeFrameAsync(NULL, pSurface,
  657. &m_pTaskPool[nTaskIdx].mfxBS,
  658. &m_pTaskPool[nTaskIdx].syncp);
  659. if (MFX_ERR_NONE < sts && !m_pTaskPool[nTaskIdx].syncp) {
  660. // Repeat the call if warning and no output
  661. if (MFX_WRN_DEVICE_BUSY == sts)
  662. MSDK_SLEEP(
  663. 1); // Wait if device is busy, then repeat the same call
  664. } else if (MFX_ERR_NONE < sts && m_pTaskPool[nTaskIdx].syncp) {
  665. sts = MFX_ERR_NONE; // Ignore warnings if output is available
  666. break;
  667. } else if (MFX_ERR_NOT_ENOUGH_BUFFER == sts) {
  668. // Allocate more bitstream buffer memory here if needed...
  669. break;
  670. } else
  671. break;
  672. }
  673. return sts;
  674. }
  675. mfxStatus QSV_Encoder_Internal::Encode_tex(uint64_t ts, uint32_t tex_handle,
  676. uint64_t lock_key,
  677. uint64_t *next_key,
  678. mfxBitstream **pBS)
  679. {
  680. mfxStatus sts = MFX_ERR_NONE;
  681. *pBS = NULL;
  682. int nTaskIdx = GetFreeTaskIndex(m_pTaskPool, m_nTaskPool);
  683. int nSurfIdx = GetFreeSurfaceIndex(m_pmfxSurfaces, m_nSurfNum);
  684. while (MFX_ERR_NOT_FOUND == nTaskIdx || MFX_ERR_NOT_FOUND == nSurfIdx) {
  685. // No more free tasks or surfaces, need to sync
  686. sts = MFXVideoCORE_SyncOperation(
  687. m_session, m_pTaskPool[m_nFirstSyncTask].syncp, 60000);
  688. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  689. mfxU8 *pTemp = m_outBitstream.Data;
  690. memcpy(&m_outBitstream, &m_pTaskPool[m_nFirstSyncTask].mfxBS,
  691. sizeof(mfxBitstream));
  692. m_pTaskPool[m_nFirstSyncTask].mfxBS.Data = pTemp;
  693. m_pTaskPool[m_nFirstSyncTask].mfxBS.DataLength = 0;
  694. m_pTaskPool[m_nFirstSyncTask].mfxBS.DataOffset = 0;
  695. m_pTaskPool[m_nFirstSyncTask].syncp = NULL;
  696. nTaskIdx = m_nFirstSyncTask;
  697. m_nFirstSyncTask = (m_nFirstSyncTask + 1) % m_nTaskPool;
  698. *pBS = &m_outBitstream;
  699. nSurfIdx = GetFreeSurfaceIndex(m_pmfxSurfaces, m_nSurfNum);
  700. }
  701. mfxFrameSurface1 *pSurface = m_pmfxSurfaces[nSurfIdx];
  702. //copy to default surface directly
  703. pSurface->Data.TimeStamp = ts;
  704. if (m_bUseTexAlloc) {
  705. // mfxU64 isn't consistent with stdint, requiring a cast to be multi-platform.
  706. sts = simple_copytex(m_mfxAllocator.pthis, pSurface->Data.MemId,
  707. tex_handle, lock_key, (mfxU64 *)next_key);
  708. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  709. }
  710. for (;;) {
  711. // Encode a frame asynchronously (returns immediately)
  712. sts = m_pmfxENC->EncodeFrameAsync(NULL, pSurface,
  713. &m_pTaskPool[nTaskIdx].mfxBS,
  714. &m_pTaskPool[nTaskIdx].syncp);
  715. if (MFX_ERR_NONE < sts && !m_pTaskPool[nTaskIdx].syncp) {
  716. // Repeat the call if warning and no output
  717. if (MFX_WRN_DEVICE_BUSY == sts)
  718. MSDK_SLEEP(
  719. 1); // Wait if device is busy, then repeat the same call
  720. } else if (MFX_ERR_NONE < sts && m_pTaskPool[nTaskIdx].syncp) {
  721. sts = MFX_ERR_NONE; // Ignore warnings if output is available
  722. break;
  723. } else if (MFX_ERR_NOT_ENOUGH_BUFFER == sts) {
  724. // Allocate more bitstream buffer memory here if needed...
  725. break;
  726. } else
  727. break;
  728. }
  729. return sts;
  730. }
  731. mfxStatus QSV_Encoder_Internal::Drain()
  732. {
  733. mfxStatus sts = MFX_ERR_NONE;
  734. while (m_pTaskPool && m_pTaskPool[m_nFirstSyncTask].syncp) {
  735. sts = MFXVideoCORE_SyncOperation(
  736. m_session, m_pTaskPool[m_nFirstSyncTask].syncp, 60000);
  737. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  738. m_pTaskPool[m_nFirstSyncTask].syncp = NULL;
  739. m_nFirstSyncTask = (m_nFirstSyncTask + 1) % m_nTaskPool;
  740. }
  741. return sts;
  742. }
  743. mfxStatus QSV_Encoder_Internal::ClearData()
  744. {
  745. mfxStatus sts = MFX_ERR_NONE;
  746. sts = Drain();
  747. if (m_pmfxENC) {
  748. sts = m_pmfxENC->Close();
  749. delete m_pmfxENC;
  750. m_pmfxENC = NULL;
  751. }
  752. if (m_bUseTexAlloc)
  753. m_mfxAllocator.Free(m_mfxAllocator.pthis, &m_mfxResponse);
  754. if (m_pmfxSurfaces) {
  755. for (int i = 0; i < m_nSurfNum; i++) {
  756. if (!m_bUseTexAlloc)
  757. delete m_pmfxSurfaces[i]->Data.Y;
  758. delete m_pmfxSurfaces[i];
  759. }
  760. MSDK_SAFE_DELETE_ARRAY(m_pmfxSurfaces);
  761. }
  762. if (m_pTaskPool) {
  763. for (int i = 0; i < m_nTaskPool; i++)
  764. delete m_pTaskPool[i].mfxBS.Data;
  765. MSDK_SAFE_DELETE_ARRAY(m_pTaskPool);
  766. }
  767. if (m_outBitstream.Data) {
  768. delete[] m_outBitstream.Data;
  769. m_outBitstream.Data = NULL;
  770. }
  771. if (sts >= MFX_ERR_NONE) {
  772. g_numEncodersOpen--;
  773. }
  774. if ((m_bUseTexAlloc) && (g_numEncodersOpen <= 0)) {
  775. Release();
  776. g_DX_Handle = NULL;
  777. }
  778. MFXVideoENCODE_Close(m_session);
  779. ReleaseSessionData(m_sessionData);
  780. m_sessionData = NULL;
  781. return sts;
  782. }
  783. mfxStatus QSV_Encoder_Internal::Reset(qsv_param_t *pParams,
  784. enum qsv_codec codec)
  785. {
  786. mfxStatus sts = ClearData();
  787. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  788. sts = Open(pParams, codec);
  789. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  790. return sts;
  791. }