obs-nvenc.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  1. #include "obs-nvenc.h"
  2. #include <util/circlebuf.h>
  3. #include <util/darray.h>
  4. #include <util/dstr.h>
  5. #include <obs-avc.h>
  6. #include <libavutil/rational.h>
  7. #define INITGUID
  8. #include <dxgi.h>
  9. #include <d3d11.h>
  10. #include <d3d11_1.h>
  11. #include <obs-hevc.h>
  12. /* ========================================================================= */
  13. /* a hack of the ages: nvenc backward compatibility */
  14. #define CONFIGURED_NVENC_MAJOR 12
  15. #define CONFIGURED_NVENC_MINOR 0
  16. #define CONFIGURED_NVENC_VER \
  17. (CONFIGURED_NVENC_MAJOR | (CONFIGURED_NVENC_MINOR << 24))
  18. /* we cannot guarantee structures haven't changed, so purposely break on
  19. * version change to force the programmer to update or remove backward
  20. * compatibility NVENC code. */
  21. #if CONFIGURED_NVENC_VER != NVENCAPI_VERSION
  22. #error NVENC version changed, update or remove NVENC compatibility code
  23. #endif
  24. #undef NVENCAPI_STRUCT_VERSION
  25. #define NVENCAPI_STRUCT_VERSION(ver) \
  26. ((uint32_t)(enc->codec == CODEC_AV1 ? NVENCAPI_VERSION \
  27. : NVENC_COMPAT_VER) | \
  28. ((ver) << 16) | (0x7 << 28))
  29. #define NV_ENC_CONFIG_COMPAT_VER (NVENCAPI_STRUCT_VERSION(7) | (1 << 31))
  30. #define NV_ENC_PIC_PARAMS_COMPAT_VER (NVENCAPI_STRUCT_VERSION(4) | (1 << 31))
  31. #define NV_ENC_LOCK_BITSTREAM_COMPAT_VER NVENCAPI_STRUCT_VERSION(1)
  32. #define NV_ENC_REGISTER_RESOURCE_COMPAT_VER NVENCAPI_STRUCT_VERSION(3)
  33. /* ========================================================================= */
  34. #define EXTRA_BUFFERS 5
  35. #define do_log(level, format, ...) \
  36. blog(level, "[obs-nvenc: '%s'] " format, \
  37. obs_encoder_get_name(enc->encoder), ##__VA_ARGS__)
  38. #define error(format, ...) do_log(LOG_ERROR, format, ##__VA_ARGS__)
  39. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  40. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  41. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  42. #define error_hr(msg) error("%s: %s: 0x%08lX", __FUNCTION__, msg, (uint32_t)hr);
  43. struct nv_bitstream;
  44. struct nv_texture;
  45. struct handle_tex {
  46. uint32_t handle;
  47. ID3D11Texture2D *tex;
  48. IDXGIKeyedMutex *km;
  49. };
  50. /* ------------------------------------------------------------------------- */
  51. /* Main Implementation Structure */
  52. enum codec_type {
  53. CODEC_H264,
  54. CODEC_HEVC,
  55. CODEC_AV1,
  56. };
  57. static const char *get_codec_name(enum codec_type type)
  58. {
  59. switch (type) {
  60. case CODEC_H264:
  61. return "H264";
  62. case CODEC_HEVC:
  63. return "HEVC";
  64. case CODEC_AV1:
  65. return "AV1";
  66. }
  67. return "Unknown";
  68. }
  69. struct nvenc_data {
  70. obs_encoder_t *encoder;
  71. enum codec_type codec;
  72. GUID codec_guid;
  73. void *session;
  74. NV_ENC_INITIALIZE_PARAMS params;
  75. NV_ENC_CONFIG config;
  76. int rc_lookahead;
  77. int buf_count;
  78. int output_delay;
  79. int buffers_queued;
  80. size_t next_bitstream;
  81. size_t cur_bitstream;
  82. bool encode_started;
  83. bool first_packet;
  84. bool can_change_bitrate;
  85. int32_t bframes;
  86. DARRAY(struct nv_bitstream) bitstreams;
  87. DARRAY(struct nv_texture) textures;
  88. DARRAY(struct handle_tex) input_textures;
  89. struct circlebuf dts_list;
  90. DARRAY(uint8_t) packet_data;
  91. int64_t packet_pts;
  92. bool packet_keyframe;
  93. ID3D11Device *device;
  94. ID3D11DeviceContext *context;
  95. uint32_t cx;
  96. uint32_t cy;
  97. uint8_t *header;
  98. size_t header_size;
  99. uint8_t *sei;
  100. size_t sei_size;
  101. };
  102. /* ------------------------------------------------------------------------- */
  103. /* Bitstream Buffer */
  104. struct nv_bitstream {
  105. void *ptr;
  106. };
  107. #define NV_FAIL(format, ...) nv_fail(enc->encoder, format, ##__VA_ARGS__)
  108. #define NV_FAILED(x) nv_failed(enc->encoder, x, __FUNCTION__, #x)
  109. static bool nv_bitstream_init(struct nvenc_data *enc, struct nv_bitstream *bs)
  110. {
  111. NV_ENC_CREATE_BITSTREAM_BUFFER buf = {
  112. NV_ENC_CREATE_BITSTREAM_BUFFER_VER};
  113. if (NV_FAILED(nv.nvEncCreateBitstreamBuffer(enc->session, &buf))) {
  114. return false;
  115. }
  116. bs->ptr = buf.bitstreamBuffer;
  117. return true;
  118. }
  119. static void nv_bitstream_free(struct nvenc_data *enc, struct nv_bitstream *bs)
  120. {
  121. if (bs->ptr) {
  122. nv.nvEncDestroyBitstreamBuffer(enc->session, bs->ptr);
  123. }
  124. }
  125. /* ------------------------------------------------------------------------- */
  126. /* Texture Resource */
  127. struct nv_texture {
  128. void *res;
  129. ID3D11Texture2D *tex;
  130. void *mapped_res;
  131. };
  132. static bool nv_texture_init(struct nvenc_data *enc, struct nv_texture *nvtex)
  133. {
  134. const bool p010 = obs_p010_tex_active();
  135. D3D11_TEXTURE2D_DESC desc = {0};
  136. desc.Width = enc->cx;
  137. desc.Height = enc->cy;
  138. desc.MipLevels = 1;
  139. desc.ArraySize = 1;
  140. desc.Format = p010 ? DXGI_FORMAT_P010 : DXGI_FORMAT_NV12;
  141. desc.SampleDesc.Count = 1;
  142. desc.BindFlags = D3D11_BIND_RENDER_TARGET;
  143. ID3D11Device *const device = enc->device;
  144. ID3D11Texture2D *tex;
  145. HRESULT hr = device->lpVtbl->CreateTexture2D(device, &desc, NULL, &tex);
  146. if (FAILED(hr)) {
  147. error_hr("Failed to create texture");
  148. return false;
  149. }
  150. tex->lpVtbl->SetEvictionPriority(tex, DXGI_RESOURCE_PRIORITY_MAXIMUM);
  151. uint32_t struct_ver = enc->codec == CODEC_AV1
  152. ? NV_ENC_REGISTER_RESOURCE_VER
  153. : NV_ENC_REGISTER_RESOURCE_COMPAT_VER;
  154. NV_ENC_REGISTER_RESOURCE res = {struct_ver};
  155. res.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
  156. res.resourceToRegister = tex;
  157. res.width = enc->cx;
  158. res.height = enc->cy;
  159. res.bufferFormat = p010 ? NV_ENC_BUFFER_FORMAT_YUV420_10BIT
  160. : NV_ENC_BUFFER_FORMAT_NV12;
  161. if (NV_FAILED(nv.nvEncRegisterResource(enc->session, &res))) {
  162. tex->lpVtbl->Release(tex);
  163. return false;
  164. }
  165. nvtex->res = res.registeredResource;
  166. nvtex->tex = tex;
  167. nvtex->mapped_res = NULL;
  168. return true;
  169. }
  170. static void nv_texture_free(struct nvenc_data *enc, struct nv_texture *nvtex)
  171. {
  172. if (nvtex->res) {
  173. if (nvtex->mapped_res) {
  174. nv.nvEncUnmapInputResource(enc->session,
  175. nvtex->mapped_res);
  176. }
  177. nv.nvEncUnregisterResource(enc->session, nvtex->res);
  178. nvtex->tex->lpVtbl->Release(nvtex->tex);
  179. }
  180. }
  181. /* ------------------------------------------------------------------------- */
  182. /* Implementation */
  183. static const char *h264_nvenc_get_name(void *type_data)
  184. {
  185. UNUSED_PARAMETER(type_data);
  186. return "NVIDIA NVENC H.264";
  187. }
  188. #ifdef ENABLE_HEVC
  189. static const char *hevc_nvenc_get_name(void *type_data)
  190. {
  191. UNUSED_PARAMETER(type_data);
  192. return "NVIDIA NVENC HEVC";
  193. }
  194. #endif
  195. static const char *av1_nvenc_get_name(void *type_data)
  196. {
  197. UNUSED_PARAMETER(type_data);
  198. return "NVIDIA NVENC AV1";
  199. }
  200. static inline int nv_get_cap(struct nvenc_data *enc, NV_ENC_CAPS cap)
  201. {
  202. if (!enc->session)
  203. return 0;
  204. NV_ENC_CAPS_PARAM param = {NV_ENC_CAPS_PARAM_VER};
  205. int v;
  206. param.capsToQuery = cap;
  207. nv.nvEncGetEncodeCaps(enc->session, enc->codec_guid, &param, &v);
  208. return v;
  209. }
  210. static bool nvenc_update(void *data, obs_data_t *settings)
  211. {
  212. struct nvenc_data *enc = data;
  213. /* Only support reconfiguration of CBR bitrate */
  214. if (enc->can_change_bitrate) {
  215. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  216. int max_bitrate =
  217. (int)obs_data_get_int(settings, "max_bitrate");
  218. bool vbr = (enc->config.rcParams.rateControlMode ==
  219. NV_ENC_PARAMS_RC_VBR);
  220. enc->config.rcParams.averageBitRate = bitrate * 1000;
  221. enc->config.rcParams.maxBitRate = vbr ? max_bitrate * 1000
  222. : bitrate * 1000;
  223. NV_ENC_RECONFIGURE_PARAMS params = {0};
  224. params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
  225. params.reInitEncodeParams = enc->params;
  226. params.resetEncoder = 1;
  227. params.forceIDR = 1;
  228. if (NV_FAILED(nv.nvEncReconfigureEncoder(enc->session,
  229. &params))) {
  230. return false;
  231. }
  232. }
  233. return true;
  234. }
  235. static HANDLE get_lib(struct nvenc_data *enc, const char *lib)
  236. {
  237. HMODULE mod = GetModuleHandleA(lib);
  238. if (mod)
  239. return mod;
  240. mod = LoadLibraryA(lib);
  241. if (!mod)
  242. error("Failed to load %s", lib);
  243. return mod;
  244. }
  245. typedef HRESULT(WINAPI *CREATEDXGIFACTORY1PROC)(REFIID, void **);
  246. static bool init_d3d11(struct nvenc_data *enc, obs_data_t *settings)
  247. {
  248. HMODULE dxgi = get_lib(enc, "DXGI.dll");
  249. HMODULE d3d11 = get_lib(enc, "D3D11.dll");
  250. CREATEDXGIFACTORY1PROC create_dxgi;
  251. PFN_D3D11_CREATE_DEVICE create_device;
  252. IDXGIFactory1 *factory;
  253. IDXGIAdapter *adapter;
  254. ID3D11Device *device;
  255. ID3D11DeviceContext *context;
  256. HRESULT hr;
  257. if (!dxgi || !d3d11) {
  258. return false;
  259. }
  260. create_dxgi = (CREATEDXGIFACTORY1PROC)GetProcAddress(
  261. dxgi, "CreateDXGIFactory1");
  262. create_device = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(
  263. d3d11, "D3D11CreateDevice");
  264. if (!create_dxgi || !create_device) {
  265. error("Failed to load D3D11/DXGI procedures");
  266. return false;
  267. }
  268. hr = create_dxgi(&IID_IDXGIFactory1, &factory);
  269. if (FAILED(hr)) {
  270. error_hr("CreateDXGIFactory1 failed");
  271. return false;
  272. }
  273. hr = factory->lpVtbl->EnumAdapters(factory, 0, &adapter);
  274. factory->lpVtbl->Release(factory);
  275. if (FAILED(hr)) {
  276. error_hr("EnumAdapters failed");
  277. return false;
  278. }
  279. hr = create_device(adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, 0, NULL, 0,
  280. D3D11_SDK_VERSION, &device, NULL, &context);
  281. adapter->lpVtbl->Release(adapter);
  282. if (FAILED(hr)) {
  283. error_hr("D3D11CreateDevice failed");
  284. return false;
  285. }
  286. enc->device = device;
  287. enc->context = context;
  288. return true;
  289. }
  290. static bool init_session(struct nvenc_data *enc)
  291. {
  292. NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = {
  293. NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER};
  294. params.device = enc->device;
  295. params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
  296. params.apiVersion = enc->codec == CODEC_AV1 ? NVENCAPI_VERSION
  297. : NVENC_COMPAT_VER;
  298. if (NV_FAILED(nv.nvEncOpenEncodeSessionEx(&params, &enc->session))) {
  299. return false;
  300. }
  301. return true;
  302. }
  303. static void initialize_params(struct nvenc_data *enc, const GUID *nv_preset,
  304. NV_ENC_TUNING_INFO nv_tuning, uint32_t width,
  305. uint32_t height, uint32_t fps_num,
  306. uint32_t fps_den)
  307. {
  308. int darWidth, darHeight;
  309. av_reduce(&darWidth, &darHeight, width, height, 1024 * 1024);
  310. NV_ENC_INITIALIZE_PARAMS *params = &enc->params;
  311. memset(params, 0, sizeof(*params));
  312. params->version = NV_ENC_INITIALIZE_PARAMS_VER;
  313. params->encodeGUID = enc->codec_guid;
  314. params->presetGUID = *nv_preset;
  315. params->encodeWidth = width;
  316. params->encodeHeight = height;
  317. params->darWidth = enc->codec == CODEC_AV1 ? width : darWidth;
  318. params->darHeight = enc->codec == CODEC_AV1 ? height : darHeight;
  319. params->frameRateNum = fps_num;
  320. params->frameRateDen = fps_den;
  321. params->enableEncodeAsync = 0;
  322. params->enablePTD = 1;
  323. params->encodeConfig = &enc->config;
  324. params->tuningInfo = nv_tuning;
  325. }
  326. static inline GUID get_nv_preset2(const char *preset2)
  327. {
  328. if (astrcmpi(preset2, "p1") == 0) {
  329. return NV_ENC_PRESET_P1_GUID;
  330. } else if (astrcmpi(preset2, "p2") == 0) {
  331. return NV_ENC_PRESET_P2_GUID;
  332. } else if (astrcmpi(preset2, "p3") == 0) {
  333. return NV_ENC_PRESET_P3_GUID;
  334. } else if (astrcmpi(preset2, "p4") == 0) {
  335. return NV_ENC_PRESET_P4_GUID;
  336. } else if (astrcmpi(preset2, "p6") == 0) {
  337. return NV_ENC_PRESET_P6_GUID;
  338. } else if (astrcmpi(preset2, "p7") == 0) {
  339. return NV_ENC_PRESET_P7_GUID;
  340. } else {
  341. return NV_ENC_PRESET_P5_GUID;
  342. }
  343. }
  344. static inline NV_ENC_TUNING_INFO get_nv_tuning(const char *tuning)
  345. {
  346. if (astrcmpi(tuning, "ll") == 0) {
  347. return NV_ENC_TUNING_INFO_LOW_LATENCY;
  348. } else if (astrcmpi(tuning, "ull") == 0) {
  349. return NV_ENC_TUNING_INFO_ULTRA_LOW_LATENCY;
  350. } else {
  351. return NV_ENC_TUNING_INFO_HIGH_QUALITY;
  352. }
  353. }
  354. static inline NV_ENC_MULTI_PASS get_nv_multipass(const char *multipass)
  355. {
  356. if (astrcmpi(multipass, "qres") == 0) {
  357. return NV_ENC_TWO_PASS_QUARTER_RESOLUTION;
  358. } else if (astrcmpi(multipass, "fullres") == 0) {
  359. return NV_ENC_TWO_PASS_FULL_RESOLUTION;
  360. } else {
  361. return NV_ENC_MULTI_PASS_DISABLED;
  362. }
  363. }
  364. static bool init_encoder_base(struct nvenc_data *enc, obs_data_t *settings,
  365. int bf, bool compatibility, bool *lossless)
  366. {
  367. const char *rc = obs_data_get_string(settings, "rate_control");
  368. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  369. int max_bitrate = (int)obs_data_get_int(settings, "max_bitrate");
  370. int cqp = (int)obs_data_get_int(settings, "cqp");
  371. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  372. const char *preset = obs_data_get_string(settings, "preset");
  373. const char *preset2 = obs_data_get_string(settings, "preset2");
  374. const char *tuning = obs_data_get_string(settings, "tune");
  375. const char *multipass = obs_data_get_string(settings, "multipass");
  376. const char *profile = obs_data_get_string(settings, "profile");
  377. bool lookahead = obs_data_get_bool(settings, "lookahead");
  378. bool vbr = astrcmpi(rc, "VBR") == 0;
  379. bool psycho_aq = !compatibility &&
  380. obs_data_get_bool(settings, "psycho_aq");
  381. NVENCSTATUS err;
  382. video_t *video = obs_encoder_video(enc->encoder);
  383. const struct video_output_info *voi = video_output_get_info(video);
  384. enc->cx = obs_encoder_get_width(enc->encoder);
  385. enc->cy = obs_encoder_get_height(enc->encoder);
  386. /* -------------------------- */
  387. /* get preset */
  388. GUID nv_preset = get_nv_preset2(preset2);
  389. NV_ENC_TUNING_INFO nv_tuning = get_nv_tuning(tuning);
  390. NV_ENC_MULTI_PASS nv_multipass = compatibility
  391. ? NV_ENC_MULTI_PASS_DISABLED
  392. : get_nv_multipass(multipass);
  393. if (obs_data_has_user_value(settings, "preset") &&
  394. !obs_data_has_user_value(settings, "preset2") &&
  395. enc->codec == CODEC_H264) {
  396. if (astrcmpi(preset, "mq") == 0) {
  397. nv_preset = NV_ENC_PRESET_P5_GUID;
  398. nv_tuning = NV_ENC_TUNING_INFO_HIGH_QUALITY;
  399. nv_multipass = NV_ENC_TWO_PASS_QUARTER_RESOLUTION;
  400. } else if (astrcmpi(preset, "hq") == 0) {
  401. nv_preset = NV_ENC_PRESET_P5_GUID;
  402. nv_tuning = NV_ENC_TUNING_INFO_HIGH_QUALITY;
  403. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  404. } else if (astrcmpi(preset, "default") == 0) {
  405. nv_preset = NV_ENC_PRESET_P3_GUID;
  406. nv_tuning = NV_ENC_TUNING_INFO_HIGH_QUALITY;
  407. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  408. } else if (astrcmpi(preset, "hp") == 0) {
  409. nv_preset = NV_ENC_PRESET_P1_GUID;
  410. nv_tuning = NV_ENC_TUNING_INFO_HIGH_QUALITY;
  411. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  412. } else if (astrcmpi(preset, "ll") == 0) {
  413. nv_preset = NV_ENC_PRESET_P3_GUID;
  414. nv_tuning = NV_ENC_TUNING_INFO_LOW_LATENCY;
  415. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  416. } else if (astrcmpi(preset, "llhq") == 0) {
  417. nv_preset = NV_ENC_PRESET_P4_GUID;
  418. nv_tuning = NV_ENC_TUNING_INFO_LOW_LATENCY;
  419. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  420. } else if (astrcmpi(preset, "llhp") == 0) {
  421. nv_preset = NV_ENC_PRESET_P2_GUID;
  422. nv_tuning = NV_ENC_TUNING_INFO_LOW_LATENCY;
  423. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  424. }
  425. } else if (obs_data_has_user_value(settings, "preset") &&
  426. !obs_data_has_user_value(settings, "preset2") &&
  427. enc->codec == CODEC_HEVC) {
  428. if (astrcmpi(preset, "mq") == 0) {
  429. nv_preset = NV_ENC_PRESET_P6_GUID;
  430. nv_tuning = NV_ENC_TUNING_INFO_HIGH_QUALITY;
  431. nv_multipass = NV_ENC_TWO_PASS_QUARTER_RESOLUTION;
  432. } else if (astrcmpi(preset, "hq") == 0) {
  433. nv_preset = NV_ENC_PRESET_P6_GUID;
  434. nv_tuning = NV_ENC_TUNING_INFO_HIGH_QUALITY;
  435. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  436. } else if (astrcmpi(preset, "default") == 0) {
  437. nv_preset = NV_ENC_PRESET_P5_GUID;
  438. nv_tuning = NV_ENC_TUNING_INFO_HIGH_QUALITY;
  439. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  440. } else if (astrcmpi(preset, "hp") == 0) {
  441. nv_preset = NV_ENC_PRESET_P1_GUID;
  442. nv_tuning = NV_ENC_TUNING_INFO_HIGH_QUALITY;
  443. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  444. } else if (astrcmpi(preset, "ll") == 0) {
  445. nv_preset = NV_ENC_PRESET_P3_GUID;
  446. nv_tuning = NV_ENC_TUNING_INFO_LOW_LATENCY;
  447. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  448. } else if (astrcmpi(preset, "llhq") == 0) {
  449. nv_preset = NV_ENC_PRESET_P4_GUID;
  450. nv_tuning = NV_ENC_TUNING_INFO_LOW_LATENCY;
  451. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  452. } else if (astrcmpi(preset, "llhp") == 0) {
  453. nv_preset = NV_ENC_PRESET_P2_GUID;
  454. nv_tuning = NV_ENC_TUNING_INFO_LOW_LATENCY;
  455. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  456. }
  457. }
  458. const bool rc_lossless = astrcmpi(rc, "lossless") == 0;
  459. *lossless = rc_lossless;
  460. if (rc_lossless) {
  461. *lossless =
  462. nv_get_cap(enc, NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE);
  463. if (*lossless) {
  464. nv_tuning = NV_ENC_TUNING_INFO_LOSSLESS;
  465. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  466. } else {
  467. warn("lossless encode is not supported, ignoring");
  468. nv_preset = NV_ENC_PRESET_P5_GUID;
  469. nv_tuning = NV_ENC_TUNING_INFO_HIGH_QUALITY;
  470. nv_multipass = NV_ENC_TWO_PASS_QUARTER_RESOLUTION;
  471. }
  472. }
  473. /* -------------------------- */
  474. /* get preset default config */
  475. uint32_t config_ver = enc->codec == CODEC_AV1
  476. ? NV_ENC_CONFIG_VER
  477. : NV_ENC_CONFIG_COMPAT_VER;
  478. NV_ENC_PRESET_CONFIG preset_config = {NV_ENC_PRESET_CONFIG_VER,
  479. {config_ver}};
  480. err = nv.nvEncGetEncodePresetConfigEx(enc->session, enc->codec_guid,
  481. nv_preset, nv_tuning,
  482. &preset_config);
  483. if (nv_failed(enc->encoder, err, __FUNCTION__,
  484. "nvEncGetEncodePresetConfig")) {
  485. return false;
  486. }
  487. /* -------------------------- */
  488. /* main configuration */
  489. enc->config = preset_config.presetCfg;
  490. uint32_t gop_size =
  491. (keyint_sec) ? keyint_sec * voi->fps_num / voi->fps_den : 250;
  492. NV_ENC_CONFIG *config = &enc->config;
  493. initialize_params(enc, &nv_preset, nv_tuning, voi->width, voi->height,
  494. voi->fps_num, voi->fps_den);
  495. config->gopLength = gop_size;
  496. config->frameIntervalP = 1 + bf;
  497. enc->bframes = bf;
  498. /* lookahead */
  499. const bool use_profile_lookahead = config->rcParams.enableLookahead;
  500. lookahead = nv_get_cap(enc, NV_ENC_CAPS_SUPPORT_LOOKAHEAD) &&
  501. (lookahead || use_profile_lookahead);
  502. if (lookahead) {
  503. enc->rc_lookahead = use_profile_lookahead
  504. ? config->rcParams.lookaheadDepth
  505. : 8;
  506. }
  507. int buf_count = max(4, config->frameIntervalP * 2 * 2);
  508. if (lookahead) {
  509. buf_count = max(buf_count, config->frameIntervalP +
  510. enc->rc_lookahead +
  511. EXTRA_BUFFERS);
  512. }
  513. buf_count = min(64, buf_count);
  514. enc->buf_count = buf_count;
  515. const int output_delay = buf_count - 1;
  516. enc->output_delay = output_delay;
  517. if (lookahead) {
  518. const int lkd_bound = output_delay - config->frameIntervalP - 4;
  519. if (lkd_bound >= 0) {
  520. config->rcParams.enableLookahead = 1;
  521. config->rcParams.lookaheadDepth =
  522. max(enc->rc_lookahead, lkd_bound);
  523. config->rcParams.disableIadapt = 0;
  524. config->rcParams.disableBadapt = 0;
  525. } else {
  526. lookahead = false;
  527. }
  528. }
  529. /* psycho aq */
  530. if (!compatibility) {
  531. if (nv_get_cap(enc, NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ)) {
  532. config->rcParams.enableAQ = psycho_aq;
  533. config->rcParams.aqStrength = 8;
  534. config->rcParams.enableTemporalAQ = psycho_aq;
  535. } else {
  536. warn("Ignoring Psycho Visual Tuning request since GPU is not capable");
  537. }
  538. }
  539. /* -------------------------- */
  540. /* rate control */
  541. enc->can_change_bitrate =
  542. nv_get_cap(enc, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
  543. config->rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
  544. if (astrcmpi(rc, "cqp") == 0 || rc_lossless) {
  545. if (*lossless)
  546. cqp = 0;
  547. int cqp_val = enc->codec == CODEC_AV1 ? cqp * 4 : cqp;
  548. config->rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
  549. config->rcParams.constQP.qpInterP = cqp_val;
  550. config->rcParams.constQP.qpInterB = cqp_val;
  551. config->rcParams.constQP.qpIntra = cqp_val;
  552. enc->can_change_bitrate = false;
  553. bitrate = 0;
  554. max_bitrate = 0;
  555. } else if (astrcmpi(rc, "vbr") != 0) { /* CBR by default */
  556. config->rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
  557. }
  558. config->rcParams.averageBitRate = bitrate * 1000;
  559. config->rcParams.maxBitRate = vbr ? max_bitrate * 1000 : bitrate * 1000;
  560. config->rcParams.vbvBufferSize = bitrate * 1000;
  561. config->rcParams.multiPass = nv_multipass;
  562. /* -------------------------- */
  563. /* initialize */
  564. info("settings:\n"
  565. "\tcodec: %s\n"
  566. "\trate_control: %s\n"
  567. "\tbitrate: %d\n"
  568. "\tcqp: %d\n"
  569. "\tkeyint: %d\n"
  570. "\tpreset: %s\n"
  571. "\ttuning: %s\n"
  572. "\tmultipass: %s\n"
  573. "\tprofile: %s\n"
  574. "\twidth: %d\n"
  575. "\theight: %d\n"
  576. "\tb-frames: %d\n"
  577. "\tlookahead: %s\n"
  578. "\tpsycho_aq: %s\n",
  579. get_codec_name(enc->codec), rc, bitrate, cqp, gop_size, preset2,
  580. tuning, multipass, profile, enc->cx, enc->cy, bf,
  581. lookahead ? "true" : "false", psycho_aq ? "true" : "false");
  582. return true;
  583. }
  584. static bool init_encoder_h264(struct nvenc_data *enc, obs_data_t *settings,
  585. int bf, bool compatibility)
  586. {
  587. const char *rc = obs_data_get_string(settings, "rate_control");
  588. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  589. const char *profile = obs_data_get_string(settings, "profile");
  590. bool lossless;
  591. if (!init_encoder_base(enc, settings, bf, compatibility, &lossless)) {
  592. return false;
  593. }
  594. NV_ENC_CONFIG *config = &enc->config;
  595. NV_ENC_CONFIG_H264 *h264_config = &config->encodeCodecConfig.h264Config;
  596. NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui_params =
  597. &h264_config->h264VUIParameters;
  598. video_t *video = obs_encoder_video(enc->encoder);
  599. const struct video_output_info *voi = video_output_get_info(video);
  600. uint32_t gop_size =
  601. (keyint_sec) ? keyint_sec * voi->fps_num / voi->fps_den : 250;
  602. h264_config->idrPeriod = gop_size;
  603. bool repeat_headers = obs_data_get_bool(settings, "repeat_headers");
  604. if (repeat_headers) {
  605. h264_config->repeatSPSPPS = 1;
  606. h264_config->disableSPSPPS = 0;
  607. h264_config->outputAUD = 1;
  608. }
  609. h264_config->sliceMode = 3;
  610. h264_config->sliceModeData = 1;
  611. h264_config->useBFramesAsRef = NV_ENC_BFRAME_REF_MODE_DISABLED;
  612. /* Enable CBR padding */
  613. if (config->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR)
  614. h264_config->enableFillerDataInsertion = 1;
  615. vui_params->videoSignalTypePresentFlag = 1;
  616. vui_params->videoFullRangeFlag = (voi->range == VIDEO_RANGE_FULL);
  617. vui_params->colourDescriptionPresentFlag = 1;
  618. switch (voi->colorspace) {
  619. case VIDEO_CS_601:
  620. vui_params->colourPrimaries = 6;
  621. vui_params->transferCharacteristics = 6;
  622. vui_params->colourMatrix = 6;
  623. break;
  624. case VIDEO_CS_DEFAULT:
  625. case VIDEO_CS_709:
  626. vui_params->colourPrimaries = 1;
  627. vui_params->transferCharacteristics = 1;
  628. vui_params->colourMatrix = 1;
  629. break;
  630. case VIDEO_CS_SRGB:
  631. vui_params->colourPrimaries = 1;
  632. vui_params->transferCharacteristics = 13;
  633. vui_params->colourMatrix = 1;
  634. break;
  635. }
  636. if (astrcmpi(rc, "lossless") == 0) {
  637. h264_config->qpPrimeYZeroTransformBypassFlag = 1;
  638. } else if (astrcmpi(rc, "vbr") != 0) { /* CBR */
  639. h264_config->outputBufferingPeriodSEI = 1;
  640. }
  641. h264_config->outputPictureTimingSEI = 1;
  642. /* -------------------------- */
  643. /* profile */
  644. if (astrcmpi(profile, "main") == 0) {
  645. config->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
  646. } else if (astrcmpi(profile, "baseline") == 0) {
  647. config->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
  648. } else if (!lossless) {
  649. config->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
  650. }
  651. if (NV_FAILED(nv.nvEncInitializeEncoder(enc->session, &enc->params))) {
  652. return false;
  653. }
  654. return true;
  655. }
  656. static bool init_encoder_hevc(struct nvenc_data *enc, obs_data_t *settings,
  657. int bf, bool compatibility)
  658. {
  659. const char *rc = obs_data_get_string(settings, "rate_control");
  660. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  661. const char *profile = obs_data_get_string(settings, "profile");
  662. bool lossless;
  663. if (!init_encoder_base(enc, settings, bf, compatibility, &lossless)) {
  664. return false;
  665. }
  666. NV_ENC_CONFIG *config = &enc->config;
  667. NV_ENC_CONFIG_HEVC *hevc_config = &config->encodeCodecConfig.hevcConfig;
  668. NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui_params =
  669. &hevc_config->hevcVUIParameters;
  670. video_t *video = obs_encoder_video(enc->encoder);
  671. const struct video_output_info *voi = video_output_get_info(video);
  672. uint32_t gop_size =
  673. (keyint_sec) ? keyint_sec * voi->fps_num / voi->fps_den : 250;
  674. hevc_config->idrPeriod = gop_size;
  675. bool repeat_headers = obs_data_get_bool(settings, "repeat_headers");
  676. if (repeat_headers) {
  677. hevc_config->repeatSPSPPS = 1;
  678. hevc_config->disableSPSPPS = 0;
  679. hevc_config->outputAUD = 1;
  680. }
  681. hevc_config->sliceMode = 3;
  682. hevc_config->sliceModeData = 1;
  683. hevc_config->useBFramesAsRef = NV_ENC_BFRAME_REF_MODE_DISABLED;
  684. /* Enable CBR padding */
  685. if (config->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR)
  686. hevc_config->enableFillerDataInsertion = 1;
  687. vui_params->videoSignalTypePresentFlag = 1;
  688. vui_params->videoFullRangeFlag = (voi->range == VIDEO_RANGE_FULL);
  689. vui_params->colourDescriptionPresentFlag = 1;
  690. switch (voi->colorspace) {
  691. case VIDEO_CS_601:
  692. vui_params->colourPrimaries = 6;
  693. vui_params->transferCharacteristics = 6;
  694. vui_params->colourMatrix = 6;
  695. break;
  696. case VIDEO_CS_DEFAULT:
  697. case VIDEO_CS_709:
  698. vui_params->colourPrimaries = 1;
  699. vui_params->transferCharacteristics = 1;
  700. vui_params->colourMatrix = 1;
  701. break;
  702. case VIDEO_CS_SRGB:
  703. vui_params->colourPrimaries = 1;
  704. vui_params->transferCharacteristics = 13;
  705. vui_params->colourMatrix = 1;
  706. break;
  707. case VIDEO_CS_2100_PQ:
  708. vui_params->colourPrimaries = 9;
  709. vui_params->transferCharacteristics = 16;
  710. vui_params->colourMatrix = 9;
  711. vui_params->chromaSampleLocationFlag = 1;
  712. vui_params->chromaSampleLocationTop = 2;
  713. vui_params->chromaSampleLocationBot = 2;
  714. break;
  715. case VIDEO_CS_2100_HLG:
  716. vui_params->colourPrimaries = 9;
  717. vui_params->transferCharacteristics = 18;
  718. vui_params->colourMatrix = 9;
  719. vui_params->chromaSampleLocationFlag = 1;
  720. vui_params->chromaSampleLocationTop = 2;
  721. vui_params->chromaSampleLocationBot = 2;
  722. }
  723. hevc_config->pixelBitDepthMinus8 = obs_p010_tex_active() ? 2 : 0;
  724. if (astrcmpi(rc, "cbr") == 0) {
  725. hevc_config->outputBufferingPeriodSEI = 1;
  726. }
  727. hevc_config->outputPictureTimingSEI = 1;
  728. /* -------------------------- */
  729. /* profile */
  730. if (astrcmpi(profile, "main10") == 0) {
  731. config->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
  732. } else if (obs_p010_tex_active()) {
  733. blog(LOG_WARNING, "[obs-nvenc] Forcing main10 for P010");
  734. config->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
  735. } else {
  736. config->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
  737. }
  738. if (NV_FAILED(nv.nvEncInitializeEncoder(enc->session, &enc->params))) {
  739. return false;
  740. }
  741. return true;
  742. }
  743. static bool init_encoder_av1(struct nvenc_data *enc, obs_data_t *settings,
  744. int bf, bool compatibility)
  745. {
  746. const char *rc = obs_data_get_string(settings, "rate_control");
  747. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  748. bool lossless;
  749. if (!init_encoder_base(enc, settings, bf, compatibility, &lossless)) {
  750. return false;
  751. }
  752. NV_ENC_INITIALIZE_PARAMS *params = &enc->params;
  753. NV_ENC_CONFIG *config = &enc->config;
  754. NV_ENC_CONFIG_AV1 *av1_config = &config->encodeCodecConfig.av1Config;
  755. video_t *video = obs_encoder_video(enc->encoder);
  756. const struct video_output_info *voi = video_output_get_info(video);
  757. uint32_t gop_size =
  758. (keyint_sec) ? keyint_sec * voi->fps_num / voi->fps_den : 250;
  759. av1_config->idrPeriod = gop_size;
  760. av1_config->useBFramesAsRef = NV_ENC_BFRAME_REF_MODE_DISABLED;
  761. av1_config->colorRange = (voi->range == VIDEO_RANGE_FULL);
  762. /* Enable CBR padding */
  763. if (config->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR)
  764. av1_config->enableBitstreamPadding = 1;
  765. switch (voi->colorspace) {
  766. case VIDEO_CS_601:
  767. av1_config->colorPrimaries = 6;
  768. av1_config->transferCharacteristics = 6;
  769. av1_config->matrixCoefficients = 6;
  770. break;
  771. case VIDEO_CS_DEFAULT:
  772. case VIDEO_CS_709:
  773. av1_config->colorPrimaries = 1;
  774. av1_config->transferCharacteristics = 1;
  775. av1_config->matrixCoefficients = 1;
  776. break;
  777. case VIDEO_CS_SRGB:
  778. av1_config->colorPrimaries = 1;
  779. av1_config->transferCharacteristics = 13;
  780. av1_config->matrixCoefficients = 1;
  781. break;
  782. case VIDEO_CS_2100_PQ:
  783. av1_config->colorPrimaries = 9;
  784. av1_config->transferCharacteristics = 16;
  785. av1_config->matrixCoefficients = 9;
  786. break;
  787. case VIDEO_CS_2100_HLG:
  788. av1_config->colorPrimaries = 9;
  789. av1_config->transferCharacteristics = 18;
  790. av1_config->matrixCoefficients = 9;
  791. }
  792. /* -------------------------- */
  793. /* profile */
  794. config->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
  795. av1_config->tier = NV_ENC_TIER_AV1_0;
  796. av1_config->level = NV_ENC_LEVEL_AV1_AUTOSELECT;
  797. av1_config->chromaFormatIDC = 1;
  798. av1_config->pixelBitDepthMinus8 = obs_p010_tex_active() ? 2 : 0;
  799. av1_config->inputPixelBitDepthMinus8 = av1_config->pixelBitDepthMinus8;
  800. av1_config->numFwdRefs = 1;
  801. av1_config->numBwdRefs = 1;
  802. av1_config->repeatSeqHdr = 1;
  803. if (NV_FAILED(nv.nvEncInitializeEncoder(enc->session, &enc->params))) {
  804. return false;
  805. }
  806. return true;
  807. }
  808. static bool init_bitstreams(struct nvenc_data *enc)
  809. {
  810. da_reserve(enc->bitstreams, enc->buf_count);
  811. for (int i = 0; i < enc->buf_count; i++) {
  812. struct nv_bitstream bitstream;
  813. if (!nv_bitstream_init(enc, &bitstream)) {
  814. return false;
  815. }
  816. da_push_back(enc->bitstreams, &bitstream);
  817. }
  818. return true;
  819. }
  820. static bool init_textures(struct nvenc_data *enc)
  821. {
  822. da_reserve(enc->textures, enc->buf_count);
  823. for (int i = 0; i < enc->buf_count; i++) {
  824. struct nv_texture texture;
  825. if (!nv_texture_init(enc, &texture)) {
  826. return false;
  827. }
  828. da_push_back(enc->textures, &texture);
  829. }
  830. return true;
  831. }
  832. static void nvenc_destroy(void *data);
  833. static bool init_specific_encoder(struct nvenc_data *enc, obs_data_t *settings,
  834. int bf, bool compatibility)
  835. {
  836. switch (enc->codec) {
  837. case CODEC_HEVC:
  838. return init_encoder_hevc(enc, settings, bf, compatibility);
  839. case CODEC_H264:
  840. return init_encoder_h264(enc, settings, bf, compatibility);
  841. case CODEC_AV1:
  842. return init_encoder_av1(enc, settings, bf, compatibility);
  843. }
  844. return false;
  845. }
  846. static bool init_encoder(struct nvenc_data *enc, enum codec_type codec,
  847. obs_data_t *settings, obs_encoder_t *encoder)
  848. {
  849. int bf = (int)obs_data_get_int(settings, "bf");
  850. const bool support_10bit =
  851. nv_get_cap(enc, NV_ENC_CAPS_SUPPORT_10BIT_ENCODE);
  852. const int bf_max = nv_get_cap(enc, NV_ENC_CAPS_NUM_MAX_BFRAMES);
  853. if (obs_p010_tex_active() && !support_10bit) {
  854. NV_FAIL(obs_module_text("NVENC.10bitUnsupported"));
  855. return false;
  856. }
  857. video_t *video = obs_encoder_video(enc->encoder);
  858. const struct video_output_info *voi = video_output_get_info(video);
  859. switch (voi->format) {
  860. case VIDEO_FORMAT_I010:
  861. case VIDEO_FORMAT_P010:
  862. break;
  863. default:
  864. switch (voi->colorspace) {
  865. case VIDEO_CS_2100_PQ:
  866. case VIDEO_CS_2100_HLG:
  867. NV_FAIL(obs_module_text("NVENC.8bitUnsupportedHdr"));
  868. return false;
  869. }
  870. }
  871. if (bf > bf_max) {
  872. blog(LOG_WARNING,
  873. "[obs-nvenc] Max B-frames setting (%d) is more than encoder supports (%d).\n"
  874. "Setting B-frames to %d",
  875. bf, bf_max, bf_max);
  876. bf = bf_max;
  877. }
  878. if (!init_specific_encoder(enc, settings, bf, false)) {
  879. blog(LOG_WARNING, "[obs-nvenc] init_specific_encoder failed, "
  880. "trying again with compatibility options");
  881. nv.nvEncDestroyEncoder(enc->session);
  882. enc->session = NULL;
  883. if (!init_session(enc)) {
  884. return false;
  885. }
  886. /* try without multipass and psycho aq */
  887. if (!init_specific_encoder(enc, settings, bf, true)) {
  888. return false;
  889. }
  890. }
  891. return true;
  892. }
  893. static void *nvenc_create_internal(enum codec_type codec, obs_data_t *settings,
  894. obs_encoder_t *encoder)
  895. {
  896. struct nvenc_data *enc = bzalloc(sizeof(*enc));
  897. enc->encoder = encoder;
  898. enc->codec = codec;
  899. enc->first_packet = true;
  900. NV_ENCODE_API_FUNCTION_LIST init = {NV_ENCODE_API_FUNCTION_LIST_VER};
  901. switch (enc->codec) {
  902. case CODEC_H264:
  903. enc->codec_guid = NV_ENC_CODEC_H264_GUID;
  904. break;
  905. case CODEC_HEVC:
  906. enc->codec_guid = NV_ENC_CODEC_HEVC_GUID;
  907. break;
  908. case CODEC_AV1:
  909. enc->codec_guid = NV_ENC_CODEC_AV1_GUID;
  910. break;
  911. }
  912. if (!init_nvenc(encoder)) {
  913. goto fail;
  914. }
  915. if (NV_FAILED(nv_create_instance(&init))) {
  916. goto fail;
  917. }
  918. if (!init_d3d11(enc, settings)) {
  919. goto fail;
  920. }
  921. if (!init_session(enc)) {
  922. goto fail;
  923. }
  924. if (!init_encoder(enc, codec, settings, encoder)) {
  925. goto fail;
  926. }
  927. if (!init_bitstreams(enc)) {
  928. goto fail;
  929. }
  930. if (!init_textures(enc)) {
  931. goto fail;
  932. }
  933. #ifdef ENABLE_HEVC
  934. enc->codec = codec;
  935. #endif
  936. return enc;
  937. fail:
  938. nvenc_destroy(enc);
  939. return NULL;
  940. }
  941. static void *nvenc_create_base(enum codec_type codec, obs_data_t *settings,
  942. obs_encoder_t *encoder)
  943. {
  944. /* this encoder requires shared textures, this cannot be used on a
  945. * gpu other than the one OBS is currently running on. */
  946. const int gpu = (int)obs_data_get_int(settings, "gpu");
  947. if (gpu != 0) {
  948. blog(LOG_INFO,
  949. "[obs-nvenc] different GPU selected by user, falling back to ffmpeg");
  950. goto reroute;
  951. }
  952. if (obs_encoder_scaling_enabled(encoder)) {
  953. if (!obs_encoder_gpu_scaling_enabled(encoder)) {
  954. blog(LOG_INFO,
  955. "[obs-nvenc] CPU scaling enabled, falling back to ffmpeg");
  956. goto reroute;
  957. }
  958. blog(LOG_INFO, "[obs-nvenc] GPU scaling enabled");
  959. }
  960. if (!obs_p010_tex_active() && !obs_nv12_tex_active()) {
  961. blog(LOG_INFO,
  962. "[obs-nvenc] nv12/p010 not active, falling back to ffmpeg");
  963. goto reroute;
  964. }
  965. struct nvenc_data *enc =
  966. nvenc_create_internal(codec, settings, encoder);
  967. if (enc) {
  968. return enc;
  969. }
  970. reroute:
  971. switch (codec) {
  972. case CODEC_H264:
  973. return obs_encoder_create_rerouted(encoder, "ffmpeg_nvenc");
  974. case CODEC_HEVC:
  975. return obs_encoder_create_rerouted(encoder,
  976. "ffmpeg_hevc_nvenc");
  977. case CODEC_AV1:
  978. obs_encoder_set_last_error(
  979. encoder,
  980. obs_module_text("NVENC.NoAV1FallbackPossible"));
  981. break;
  982. }
  983. return NULL;
  984. }
  985. static void *h264_nvenc_create(obs_data_t *settings, obs_encoder_t *encoder)
  986. {
  987. return nvenc_create_base(CODEC_H264, settings, encoder);
  988. }
  989. #ifdef ENABLE_HEVC
  990. static void *hevc_nvenc_create(obs_data_t *settings, obs_encoder_t *encoder)
  991. {
  992. return nvenc_create_base(CODEC_HEVC, settings, encoder);
  993. }
  994. #endif
  995. static void *av1_nvenc_create(obs_data_t *settings, obs_encoder_t *encoder)
  996. {
  997. return nvenc_create_base(CODEC_AV1, settings, encoder);
  998. }
  999. static bool get_encoded_packet(struct nvenc_data *enc, bool finalize);
  1000. static void nvenc_destroy(void *data)
  1001. {
  1002. struct nvenc_data *enc = data;
  1003. if (enc->encode_started) {
  1004. size_t next_bitstream = enc->next_bitstream;
  1005. uint32_t struct_ver = enc->codec == CODEC_AV1
  1006. ? NV_ENC_PIC_PARAMS_VER
  1007. : NV_ENC_PIC_PARAMS_COMPAT_VER;
  1008. NV_ENC_PIC_PARAMS params = {struct_ver};
  1009. params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
  1010. nv.nvEncEncodePicture(enc->session, &params);
  1011. get_encoded_packet(enc, true);
  1012. }
  1013. for (size_t i = 0; i < enc->textures.num; i++) {
  1014. nv_texture_free(enc, &enc->textures.array[i]);
  1015. }
  1016. for (size_t i = 0; i < enc->bitstreams.num; i++) {
  1017. nv_bitstream_free(enc, &enc->bitstreams.array[i]);
  1018. }
  1019. if (enc->session) {
  1020. nv.nvEncDestroyEncoder(enc->session);
  1021. }
  1022. for (size_t i = 0; i < enc->input_textures.num; i++) {
  1023. ID3D11Texture2D *tex = enc->input_textures.array[i].tex;
  1024. IDXGIKeyedMutex *km = enc->input_textures.array[i].km;
  1025. tex->lpVtbl->Release(tex);
  1026. km->lpVtbl->Release(km);
  1027. }
  1028. if (enc->context) {
  1029. enc->context->lpVtbl->Release(enc->context);
  1030. }
  1031. if (enc->device) {
  1032. enc->device->lpVtbl->Release(enc->device);
  1033. }
  1034. bfree(enc->header);
  1035. bfree(enc->sei);
  1036. circlebuf_free(&enc->dts_list);
  1037. da_free(enc->textures);
  1038. da_free(enc->bitstreams);
  1039. da_free(enc->input_textures);
  1040. da_free(enc->packet_data);
  1041. bfree(enc);
  1042. }
  1043. static ID3D11Texture2D *get_tex_from_handle(struct nvenc_data *enc,
  1044. uint32_t handle,
  1045. IDXGIKeyedMutex **km_out)
  1046. {
  1047. ID3D11Device *device = enc->device;
  1048. IDXGIKeyedMutex *km;
  1049. ID3D11Texture2D *input_tex;
  1050. HRESULT hr;
  1051. for (size_t i = 0; i < enc->input_textures.num; i++) {
  1052. struct handle_tex *ht = &enc->input_textures.array[i];
  1053. if (ht->handle == handle) {
  1054. *km_out = ht->km;
  1055. return ht->tex;
  1056. }
  1057. }
  1058. hr = device->lpVtbl->OpenSharedResource(device,
  1059. (HANDLE)(uintptr_t)handle,
  1060. &IID_ID3D11Texture2D,
  1061. &input_tex);
  1062. if (FAILED(hr)) {
  1063. error_hr("OpenSharedResource failed");
  1064. return NULL;
  1065. }
  1066. hr = input_tex->lpVtbl->QueryInterface(input_tex, &IID_IDXGIKeyedMutex,
  1067. &km);
  1068. if (FAILED(hr)) {
  1069. error_hr("QueryInterface(IDXGIKeyedMutex) failed");
  1070. input_tex->lpVtbl->Release(input_tex);
  1071. return NULL;
  1072. }
  1073. input_tex->lpVtbl->SetEvictionPriority(input_tex,
  1074. DXGI_RESOURCE_PRIORITY_MAXIMUM);
  1075. *km_out = km;
  1076. struct handle_tex new_ht = {handle, input_tex, km};
  1077. da_push_back(enc->input_textures, &new_ht);
  1078. return input_tex;
  1079. }
  1080. static bool get_encoded_packet(struct nvenc_data *enc, bool finalize)
  1081. {
  1082. void *s = enc->session;
  1083. da_resize(enc->packet_data, 0);
  1084. if (!enc->buffers_queued)
  1085. return true;
  1086. if (!finalize && enc->buffers_queued < enc->output_delay)
  1087. return true;
  1088. size_t count = finalize ? enc->buffers_queued : 1;
  1089. for (size_t i = 0; i < count; i++) {
  1090. size_t cur_bs_idx = enc->cur_bitstream;
  1091. struct nv_bitstream *bs = &enc->bitstreams.array[cur_bs_idx];
  1092. struct nv_texture *nvtex = &enc->textures.array[cur_bs_idx];
  1093. /* ---------------- */
  1094. uint32_t struct_ver =
  1095. enc->codec == CODEC_AV1
  1096. ? NV_ENC_LOCK_BITSTREAM_VER
  1097. : NV_ENC_LOCK_BITSTREAM_COMPAT_VER;
  1098. NV_ENC_LOCK_BITSTREAM lock = {struct_ver};
  1099. lock.outputBitstream = bs->ptr;
  1100. lock.doNotWait = false;
  1101. if (NV_FAILED(nv.nvEncLockBitstream(s, &lock))) {
  1102. return false;
  1103. }
  1104. if (enc->first_packet) {
  1105. NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = {0};
  1106. uint8_t buf[256];
  1107. uint32_t size = 0;
  1108. payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
  1109. payload.spsppsBuffer = buf;
  1110. payload.inBufferSize = sizeof(buf);
  1111. payload.outSPSPPSPayloadSize = &size;
  1112. nv.nvEncGetSequenceParams(s, &payload);
  1113. enc->header = bmemdup(buf, size);
  1114. enc->header_size = size;
  1115. enc->first_packet = false;
  1116. }
  1117. da_copy_array(enc->packet_data, lock.bitstreamBufferPtr,
  1118. lock.bitstreamSizeInBytes);
  1119. enc->packet_pts = (int64_t)lock.outputTimeStamp;
  1120. enc->packet_keyframe = lock.pictureType == NV_ENC_PIC_TYPE_IDR;
  1121. if (NV_FAILED(nv.nvEncUnlockBitstream(s, bs->ptr))) {
  1122. return false;
  1123. }
  1124. /* ---------------- */
  1125. if (nvtex->mapped_res) {
  1126. NVENCSTATUS err;
  1127. err = nv.nvEncUnmapInputResource(s, nvtex->mapped_res);
  1128. if (nv_failed(enc->encoder, err, __FUNCTION__,
  1129. "unmap")) {
  1130. return false;
  1131. }
  1132. nvtex->mapped_res = NULL;
  1133. }
  1134. /* ---------------- */
  1135. if (++enc->cur_bitstream == enc->buf_count)
  1136. enc->cur_bitstream = 0;
  1137. enc->buffers_queued--;
  1138. }
  1139. return true;
  1140. }
  1141. static bool nvenc_encode_tex(void *data, uint32_t handle, int64_t pts,
  1142. uint64_t lock_key, uint64_t *next_key,
  1143. struct encoder_packet *packet,
  1144. bool *received_packet)
  1145. {
  1146. struct nvenc_data *enc = data;
  1147. ID3D11Device *device = enc->device;
  1148. ID3D11DeviceContext *context = enc->context;
  1149. ID3D11Texture2D *input_tex;
  1150. ID3D11Texture2D *output_tex;
  1151. IDXGIKeyedMutex *km;
  1152. struct nv_texture *nvtex;
  1153. struct nv_bitstream *bs;
  1154. NVENCSTATUS err;
  1155. if (handle == GS_INVALID_HANDLE) {
  1156. error("Encode failed: bad texture handle");
  1157. *next_key = lock_key;
  1158. return false;
  1159. }
  1160. bs = &enc->bitstreams.array[enc->next_bitstream];
  1161. nvtex = &enc->textures.array[enc->next_bitstream];
  1162. input_tex = get_tex_from_handle(enc, handle, &km);
  1163. output_tex = nvtex->tex;
  1164. if (!input_tex) {
  1165. *next_key = lock_key;
  1166. return false;
  1167. }
  1168. circlebuf_push_back(&enc->dts_list, &pts, sizeof(pts));
  1169. /* ------------------------------------ */
  1170. /* copy to output tex */
  1171. km->lpVtbl->AcquireSync(km, lock_key, INFINITE);
  1172. context->lpVtbl->CopyResource(context, (ID3D11Resource *)output_tex,
  1173. (ID3D11Resource *)input_tex);
  1174. km->lpVtbl->ReleaseSync(km, *next_key);
  1175. /* ------------------------------------ */
  1176. /* map output tex so nvenc can use it */
  1177. NV_ENC_MAP_INPUT_RESOURCE map = {NV_ENC_MAP_INPUT_RESOURCE_VER};
  1178. map.registeredResource = nvtex->res;
  1179. if (NV_FAILED(nv.nvEncMapInputResource(enc->session, &map))) {
  1180. return false;
  1181. }
  1182. nvtex->mapped_res = map.mappedResource;
  1183. /* ------------------------------------ */
  1184. /* do actual encode call */
  1185. NV_ENC_PIC_PARAMS params = {0};
  1186. params.version = enc->codec == CODEC_AV1 ? NV_ENC_PIC_PARAMS_VER
  1187. : NV_ENC_PIC_PARAMS_COMPAT_VER;
  1188. params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
  1189. params.inputBuffer = nvtex->mapped_res;
  1190. params.bufferFmt = obs_p010_tex_active()
  1191. ? NV_ENC_BUFFER_FORMAT_YUV420_10BIT
  1192. : NV_ENC_BUFFER_FORMAT_NV12;
  1193. params.inputTimeStamp = (uint64_t)pts;
  1194. params.inputWidth = enc->cx;
  1195. params.inputHeight = enc->cy;
  1196. params.inputPitch = enc->cx;
  1197. params.outputBitstream = bs->ptr;
  1198. err = nv.nvEncEncodePicture(enc->session, &params);
  1199. if (err != NV_ENC_SUCCESS && err != NV_ENC_ERR_NEED_MORE_INPUT) {
  1200. nv_failed(enc->encoder, err, __FUNCTION__,
  1201. "nvEncEncodePicture");
  1202. return false;
  1203. }
  1204. enc->encode_started = true;
  1205. enc->buffers_queued++;
  1206. if (++enc->next_bitstream == enc->buf_count) {
  1207. enc->next_bitstream = 0;
  1208. }
  1209. /* ------------------------------------ */
  1210. /* check for encoded packet and parse */
  1211. if (!get_encoded_packet(enc, false)) {
  1212. return false;
  1213. }
  1214. /* ------------------------------------ */
  1215. /* output encoded packet */
  1216. if (enc->packet_data.num) {
  1217. int64_t dts;
  1218. circlebuf_pop_front(&enc->dts_list, &dts, sizeof(dts));
  1219. /* subtract bframe delay from dts */
  1220. dts -= (int64_t)enc->bframes * packet->timebase_num;
  1221. *received_packet = true;
  1222. packet->data = enc->packet_data.array;
  1223. packet->size = enc->packet_data.num;
  1224. packet->type = OBS_ENCODER_VIDEO;
  1225. packet->pts = enc->packet_pts;
  1226. packet->dts = dts;
  1227. packet->keyframe = enc->packet_keyframe;
  1228. } else {
  1229. *received_packet = false;
  1230. }
  1231. return true;
  1232. }
  1233. extern void h264_nvenc_defaults(obs_data_t *settings);
  1234. extern obs_properties_t *h264_nvenc_properties(void *unused);
  1235. #ifdef ENABLE_HEVC
  1236. extern void hevc_nvenc_defaults(obs_data_t *settings);
  1237. extern obs_properties_t *hevc_nvenc_properties(void *unused);
  1238. #endif
  1239. extern obs_properties_t *av1_nvenc_properties(void *unused);
  1240. extern void av1_nvenc_defaults(obs_data_t *settings);
  1241. static bool nvenc_extra_data(void *data, uint8_t **header, size_t *size)
  1242. {
  1243. struct nvenc_data *enc = data;
  1244. if (!enc->header) {
  1245. return false;
  1246. }
  1247. *header = enc->header;
  1248. *size = enc->header_size;
  1249. return true;
  1250. }
  1251. static bool nvenc_sei_data(void *data, uint8_t **sei, size_t *size)
  1252. {
  1253. struct nvenc_data *enc = data;
  1254. if (!enc->sei) {
  1255. return false;
  1256. }
  1257. *sei = enc->sei;
  1258. *size = enc->sei_size;
  1259. return true;
  1260. }
  1261. struct obs_encoder_info h264_nvenc_info = {
  1262. .id = "jim_nvenc",
  1263. .codec = "h264",
  1264. .type = OBS_ENCODER_VIDEO,
  1265. .caps = OBS_ENCODER_CAP_PASS_TEXTURE | OBS_ENCODER_CAP_DYN_BITRATE,
  1266. .get_name = h264_nvenc_get_name,
  1267. .create = h264_nvenc_create,
  1268. .destroy = nvenc_destroy,
  1269. .update = nvenc_update,
  1270. .encode_texture = nvenc_encode_tex,
  1271. .get_defaults = h264_nvenc_defaults,
  1272. .get_properties = h264_nvenc_properties,
  1273. .get_extra_data = nvenc_extra_data,
  1274. .get_sei_data = nvenc_sei_data,
  1275. };
  1276. #ifdef ENABLE_HEVC
  1277. struct obs_encoder_info hevc_nvenc_info = {
  1278. .id = "jim_hevc_nvenc",
  1279. .codec = "hevc",
  1280. .type = OBS_ENCODER_VIDEO,
  1281. .caps = OBS_ENCODER_CAP_PASS_TEXTURE | OBS_ENCODER_CAP_DYN_BITRATE,
  1282. .get_name = hevc_nvenc_get_name,
  1283. .create = hevc_nvenc_create,
  1284. .destroy = nvenc_destroy,
  1285. .update = nvenc_update,
  1286. .encode_texture = nvenc_encode_tex,
  1287. .get_defaults = hevc_nvenc_defaults,
  1288. .get_properties = hevc_nvenc_properties,
  1289. .get_extra_data = nvenc_extra_data,
  1290. .get_sei_data = nvenc_sei_data,
  1291. };
  1292. #endif
  1293. struct obs_encoder_info av1_nvenc_info = {
  1294. .id = "jim_av1_nvenc",
  1295. .codec = "av1",
  1296. .type = OBS_ENCODER_VIDEO,
  1297. .caps = OBS_ENCODER_CAP_PASS_TEXTURE | OBS_ENCODER_CAP_DYN_BITRATE,
  1298. .get_name = av1_nvenc_get_name,
  1299. .create = av1_nvenc_create,
  1300. .destroy = nvenc_destroy,
  1301. .update = nvenc_update,
  1302. .encode_texture = nvenc_encode_tex,
  1303. .get_defaults = av1_nvenc_defaults,
  1304. .get_properties = av1_nvenc_properties,
  1305. .get_extra_data = nvenc_extra_data,
  1306. };