jim-nvenc.c 42 KB

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