jim-nvenc.c 42 KB

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