QSV_Encoder_Internal.cpp 31 KB

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