nvenc.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. #include "nvenc-internal.h"
  2. #include <util/darray.h>
  3. #include <util/dstr.h>
  4. /* ========================================================================= */
  5. #define EXTRA_BUFFERS 5
  6. #ifndef _WIN32
  7. #define min(a, b) (((a) < (b)) ? (a) : (b))
  8. #define max(a, b) (((a) > (b)) ? (a) : (b))
  9. #endif
  10. /* ------------------------------------------------------------------------- */
  11. /* Bitstream Buffer */
  12. static bool nv_bitstream_init(struct nvenc_data *enc, struct nv_bitstream *bs)
  13. {
  14. NV_ENC_CREATE_BITSTREAM_BUFFER buf = {
  15. NV_ENC_CREATE_BITSTREAM_BUFFER_VER};
  16. if (NV_FAILED(nv.nvEncCreateBitstreamBuffer(enc->session, &buf))) {
  17. return false;
  18. }
  19. bs->ptr = buf.bitstreamBuffer;
  20. return true;
  21. }
  22. static void nv_bitstream_free(struct nvenc_data *enc, struct nv_bitstream *bs)
  23. {
  24. if (bs->ptr) {
  25. nv.nvEncDestroyBitstreamBuffer(enc->session, bs->ptr);
  26. }
  27. }
  28. /* ------------------------------------------------------------------------- */
  29. /* Implementation */
  30. static const char *h264_nvenc_get_name(void *type_data)
  31. {
  32. UNUSED_PARAMETER(type_data);
  33. return "NVIDIA NVENC H.264";
  34. }
  35. static const char *h264_nvenc_soft_get_name(void *type_data)
  36. {
  37. UNUSED_PARAMETER(type_data);
  38. return "NVIDIA NVENC H.264 (Fallback)";
  39. }
  40. #ifdef ENABLE_HEVC
  41. static const char *hevc_nvenc_get_name(void *type_data)
  42. {
  43. UNUSED_PARAMETER(type_data);
  44. return "NVIDIA NVENC HEVC";
  45. }
  46. static const char *hevc_nvenc_soft_get_name(void *type_data)
  47. {
  48. UNUSED_PARAMETER(type_data);
  49. return "NVIDIA NVENC HEVC (Fallback)";
  50. }
  51. #endif
  52. static const char *av1_nvenc_get_name(void *type_data)
  53. {
  54. UNUSED_PARAMETER(type_data);
  55. return "NVIDIA NVENC AV1";
  56. }
  57. static const char *av1_nvenc_soft_get_name(void *type_data)
  58. {
  59. UNUSED_PARAMETER(type_data);
  60. return "NVIDIA NVENC AV1 (Fallback)";
  61. }
  62. static inline int nv_get_cap(struct nvenc_data *enc, NV_ENC_CAPS cap)
  63. {
  64. if (!enc->session)
  65. return 0;
  66. NV_ENC_CAPS_PARAM param = {NV_ENC_CAPS_PARAM_VER};
  67. int v;
  68. param.capsToQuery = cap;
  69. nv.nvEncGetEncodeCaps(enc->session, enc->codec_guid, &param, &v);
  70. return v;
  71. }
  72. static bool nvenc_update(void *data, obs_data_t *settings)
  73. {
  74. struct nvenc_data *enc = data;
  75. /* Only support reconfiguration of CBR bitrate */
  76. if (enc->can_change_bitrate) {
  77. enc->props.bitrate = obs_data_get_int(settings, "bitrate");
  78. enc->props.max_bitrate =
  79. obs_data_get_int(settings, "max_bitrate");
  80. bool vbr = (enc->config.rcParams.rateControlMode ==
  81. NV_ENC_PARAMS_RC_VBR);
  82. enc->config.rcParams.averageBitRate =
  83. (uint32_t)enc->props.bitrate * 1000;
  84. enc->config.rcParams.maxBitRate =
  85. vbr ? (uint32_t)enc->props.max_bitrate * 1000
  86. : (uint32_t)enc->props.bitrate * 1000;
  87. NV_ENC_RECONFIGURE_PARAMS params = {0};
  88. params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
  89. params.reInitEncodeParams = enc->params;
  90. params.resetEncoder = 1;
  91. params.forceIDR = 1;
  92. if (NV_FAILED(nv.nvEncReconfigureEncoder(enc->session,
  93. &params))) {
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. static bool init_session(struct nvenc_data *enc)
  100. {
  101. NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = {
  102. NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER};
  103. params.apiVersion = NVENCAPI_VERSION;
  104. #ifdef _WIN32
  105. if (enc->non_texture) {
  106. params.device = enc->cu_ctx;
  107. params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
  108. } else {
  109. params.device = enc->device;
  110. params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
  111. }
  112. #else
  113. params.device = enc->cu_ctx;
  114. params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
  115. #endif
  116. if (NV_FAILED(nv.nvEncOpenEncodeSessionEx(&params, &enc->session))) {
  117. return false;
  118. }
  119. return true;
  120. }
  121. static void initialize_params(struct nvenc_data *enc, const GUID *nv_preset,
  122. NV_ENC_TUNING_INFO nv_tuning, uint32_t width,
  123. uint32_t height, uint32_t fps_num,
  124. uint32_t fps_den)
  125. {
  126. NV_ENC_INITIALIZE_PARAMS *params = &enc->params;
  127. memset(params, 0, sizeof(*params));
  128. params->version = NV_ENC_INITIALIZE_PARAMS_VER;
  129. params->encodeGUID = enc->codec_guid;
  130. params->presetGUID = *nv_preset;
  131. params->encodeWidth = width;
  132. params->encodeHeight = height;
  133. params->darWidth = width;
  134. params->darHeight = height;
  135. params->frameRateNum = fps_num;
  136. params->frameRateDen = fps_den;
  137. params->enableEncodeAsync = 0;
  138. params->enablePTD = 1;
  139. params->encodeConfig = &enc->config;
  140. params->tuningInfo = nv_tuning;
  141. #ifdef NVENC_12_1_OR_LATER
  142. params->splitEncodeMode =
  143. (NV_ENC_SPLIT_ENCODE_MODE)enc->props.split_encode;
  144. #endif
  145. }
  146. static inline GUID get_nv_preset(const char *preset2)
  147. {
  148. if (astrcmpi(preset2, "p1") == 0) {
  149. return NV_ENC_PRESET_P1_GUID;
  150. } else if (astrcmpi(preset2, "p2") == 0) {
  151. return NV_ENC_PRESET_P2_GUID;
  152. } else if (astrcmpi(preset2, "p3") == 0) {
  153. return NV_ENC_PRESET_P3_GUID;
  154. } else if (astrcmpi(preset2, "p4") == 0) {
  155. return NV_ENC_PRESET_P4_GUID;
  156. } else if (astrcmpi(preset2, "p6") == 0) {
  157. return NV_ENC_PRESET_P6_GUID;
  158. } else if (astrcmpi(preset2, "p7") == 0) {
  159. return NV_ENC_PRESET_P7_GUID;
  160. } else {
  161. return NV_ENC_PRESET_P5_GUID;
  162. }
  163. }
  164. static inline NV_ENC_TUNING_INFO get_nv_tuning(const char *tuning)
  165. {
  166. if (astrcmpi(tuning, "ll") == 0) {
  167. return NV_ENC_TUNING_INFO_LOW_LATENCY;
  168. } else if (astrcmpi(tuning, "ull") == 0) {
  169. return NV_ENC_TUNING_INFO_ULTRA_LOW_LATENCY;
  170. #ifdef NVENC_12_2_OR_LATER
  171. } else if (astrcmpi(tuning, "uhq") == 0) {
  172. return NV_ENC_TUNING_INFO_ULTRA_HIGH_QUALITY;
  173. #endif
  174. } else {
  175. return NV_ENC_TUNING_INFO_HIGH_QUALITY;
  176. }
  177. }
  178. static inline NV_ENC_MULTI_PASS get_nv_multipass(const char *multipass)
  179. {
  180. if (astrcmpi(multipass, "qres") == 0) {
  181. return NV_ENC_TWO_PASS_QUARTER_RESOLUTION;
  182. } else if (astrcmpi(multipass, "fullres") == 0) {
  183. return NV_ENC_TWO_PASS_FULL_RESOLUTION;
  184. } else {
  185. return NV_ENC_MULTI_PASS_DISABLED;
  186. }
  187. }
  188. static bool is_10_bit(const struct nvenc_data *enc)
  189. {
  190. return enc->non_texture ? enc->in_format == VIDEO_FORMAT_P010
  191. : obs_p010_tex_active();
  192. }
  193. static bool init_encoder_base(struct nvenc_data *enc, obs_data_t *settings)
  194. {
  195. UNUSED_PARAMETER(settings);
  196. int bitrate = (int)enc->props.bitrate;
  197. int max_bitrate = (int)enc->props.max_bitrate;
  198. int rc_lookahead = 0;
  199. bool cqvbr = astrcmpi(enc->props.rate_control, "CQVBR") == 0;
  200. bool vbr = cqvbr || astrcmpi(enc->props.rate_control, "VBR") == 0;
  201. bool lossless = strcmp(enc->props.rate_control, "lossless") == 0;
  202. NVENCSTATUS err;
  203. video_t *video = obs_encoder_video(enc->encoder);
  204. const struct video_output_info *voi = video_output_get_info(video);
  205. enc->cx = obs_encoder_get_width(enc->encoder);
  206. enc->cy = obs_encoder_get_height(enc->encoder);
  207. /* -------------------------- */
  208. /* get preset */
  209. GUID nv_preset = get_nv_preset(enc->props.preset);
  210. NV_ENC_TUNING_INFO nv_tuning = get_nv_tuning(enc->props.tune);
  211. NV_ENC_MULTI_PASS nv_multipass = get_nv_multipass(enc->props.multipass);
  212. if (lossless) {
  213. nv_tuning = NV_ENC_TUNING_INFO_LOSSLESS;
  214. nv_multipass = NV_ENC_MULTI_PASS_DISABLED;
  215. enc->props.adaptive_quantization = false;
  216. enc->props.cqp = 0;
  217. }
  218. /* -------------------------- */
  219. /* get preset default config */
  220. NV_ENC_PRESET_CONFIG preset_config = {0};
  221. preset_config.version = NV_ENC_PRESET_CONFIG_VER;
  222. preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
  223. err = nv.nvEncGetEncodePresetConfigEx(enc->session, enc->codec_guid,
  224. nv_preset, nv_tuning,
  225. &preset_config);
  226. if (nv_failed(enc->encoder, err, __FUNCTION__,
  227. "nvEncGetEncodePresetConfig")) {
  228. return false;
  229. }
  230. /* -------------------------- */
  231. /* main configuration */
  232. enc->config = preset_config.presetCfg;
  233. int keyint = (int)enc->props.keyint_sec * voi->fps_num / voi->fps_den;
  234. get_user_arg_int(enc, "keyint", &keyint);
  235. uint32_t gop_size = keyint > 0 ? keyint : 250;
  236. NV_ENC_CONFIG *config = &enc->config;
  237. initialize_params(enc, &nv_preset, nv_tuning, voi->width, voi->height,
  238. voi->fps_num, voi->fps_den);
  239. config->gopLength = gop_size;
  240. config->frameIntervalP = gop_size == 1 ? 0 : (int32_t)enc->props.bf + 1;
  241. /* lookahead */
  242. const bool use_profile_lookahead = config->rcParams.enableLookahead;
  243. bool lookahead = nv_get_cap(enc, NV_ENC_CAPS_SUPPORT_LOOKAHEAD) &&
  244. (enc->props.lookahead || use_profile_lookahead);
  245. if (lookahead) {
  246. rc_lookahead = use_profile_lookahead
  247. ? config->rcParams.lookaheadDepth
  248. : 8;
  249. /* Due to the additional calculations required to handle lookahead,
  250. * get the user override here (if any). */
  251. get_user_arg_int(enc, "lookaheadDepth", &rc_lookahead);
  252. }
  253. int buf_count = max(4, config->frameIntervalP * 2 * 2);
  254. if (lookahead) {
  255. buf_count =
  256. max(buf_count, config->frameIntervalP + rc_lookahead +
  257. EXTRA_BUFFERS);
  258. }
  259. buf_count = min(64, buf_count);
  260. enc->buf_count = buf_count;
  261. const int output_delay = buf_count - 1;
  262. enc->output_delay = output_delay;
  263. if (lookahead) {
  264. const int lkd_bound = output_delay - config->frameIntervalP - 4;
  265. if (lkd_bound >= 0) {
  266. config->rcParams.enableLookahead = 1;
  267. config->rcParams.lookaheadDepth =
  268. min(rc_lookahead, lkd_bound);
  269. config->rcParams.disableIadapt = 0;
  270. config->rcParams.disableBadapt = 0;
  271. } else {
  272. lookahead = false;
  273. }
  274. }
  275. enc->config.rcParams.disableIadapt = enc->props.disable_scenecut;
  276. /* psycho aq */
  277. if (enc->props.adaptive_quantization) {
  278. config->rcParams.enableAQ = 1;
  279. config->rcParams.aqStrength = 8;
  280. config->rcParams.enableTemporalAQ =
  281. nv_get_cap(enc, NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ);
  282. }
  283. /* -------------------------- */
  284. /* rate control */
  285. enc->can_change_bitrate =
  286. nv_get_cap(enc, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
  287. config->rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
  288. config->rcParams.averageBitRate = bitrate * 1000;
  289. config->rcParams.maxBitRate = vbr ? max_bitrate * 1000 : bitrate * 1000;
  290. config->rcParams.vbvBufferSize = bitrate * 1000;
  291. if (strcmp(enc->props.rate_control, "CQP") == 0 || lossless) {
  292. int cqp_val = enc->codec == CODEC_AV1 ? (int)enc->props.cqp * 4
  293. : (int)enc->props.cqp;
  294. config->rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
  295. config->rcParams.constQP.qpInterP = cqp_val;
  296. config->rcParams.constQP.qpInterB = cqp_val;
  297. config->rcParams.constQP.qpIntra = cqp_val;
  298. enc->can_change_bitrate = false;
  299. bitrate = 0;
  300. max_bitrate = 0;
  301. } else if (!vbr) { /* CBR by default */
  302. config->rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
  303. } else if (cqvbr) {
  304. config->rcParams.targetQuality =
  305. (uint8_t)enc->props.target_quality;
  306. config->rcParams.averageBitRate = 0;
  307. config->rcParams.vbvBufferSize = 0;
  308. }
  309. config->rcParams.multiPass = nv_multipass;
  310. config->rcParams.qpMapMode = NV_ENC_QP_MAP_DELTA;
  311. /* -------------------------- */
  312. /* initialize */
  313. info("settings:\n"
  314. "\tcodec: %s\n"
  315. "\trate_control: %s\n"
  316. "\tbitrate: %d\n"
  317. "\tmax_bitrate: %d\n"
  318. "\tcq/cqp: %ld\n"
  319. "\tkeyint: %d\n"
  320. "\tpreset: %s\n"
  321. "\ttuning: %s\n"
  322. "\tmultipass: %s\n"
  323. "\tprofile: %s\n"
  324. "\twidth: %d\n"
  325. "\theight: %d\n"
  326. "\tb-frames: %ld\n"
  327. "\tb-ref-mode: %ld\n"
  328. "\tlookahead: %s (%d)\n"
  329. "\taq: %s\n"
  330. "\tsplit encode: %ld\n"
  331. "\tuser opts: %s\n",
  332. get_codec_name(enc->codec), enc->props.rate_control, bitrate,
  333. max_bitrate, vbr ? enc->props.target_quality : enc->props.cqp,
  334. gop_size, enc->props.preset, enc->props.tune, enc->props.multipass,
  335. enc->props.profile, enc->cx, enc->cy, enc->props.bf,
  336. enc->props.bframe_ref_mode, lookahead ? "true" : "false",
  337. rc_lookahead, enc->props.adaptive_quantization ? "true" : "false",
  338. enc->props.split_encode, enc->props.opts_str);
  339. return true;
  340. }
  341. static bool init_encoder_h264(struct nvenc_data *enc, obs_data_t *settings)
  342. {
  343. bool lossless = strcmp(enc->props.rate_control, "lossless") == 0;
  344. if (!init_encoder_base(enc, settings)) {
  345. return false;
  346. }
  347. NV_ENC_CONFIG *config = &enc->config;
  348. NV_ENC_CONFIG_H264 *h264_config = &config->encodeCodecConfig.h264Config;
  349. NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui_params =
  350. &h264_config->h264VUIParameters;
  351. video_t *video = obs_encoder_video(enc->encoder);
  352. const struct video_output_info *voi = video_output_get_info(video);
  353. if (enc->props.repeat_headers) {
  354. h264_config->repeatSPSPPS = 1;
  355. h264_config->disableSPSPPS = 0;
  356. h264_config->outputAUD = 1;
  357. }
  358. h264_config->idrPeriod = config->gopLength;
  359. h264_config->sliceMode = 3;
  360. h264_config->sliceModeData = 1;
  361. h264_config->useBFramesAsRef =
  362. (NV_ENC_BFRAME_REF_MODE)enc->props.bframe_ref_mode;
  363. /* Enable CBR padding */
  364. if (config->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR)
  365. h264_config->enableFillerDataInsertion = 1;
  366. vui_params->videoSignalTypePresentFlag = 1;
  367. vui_params->videoFullRangeFlag = (voi->range == VIDEO_RANGE_FULL);
  368. vui_params->colourDescriptionPresentFlag = 1;
  369. switch (voi->colorspace) {
  370. case VIDEO_CS_601:
  371. vui_params->colourPrimaries = 6;
  372. vui_params->transferCharacteristics = 6;
  373. vui_params->colourMatrix = 6;
  374. break;
  375. case VIDEO_CS_DEFAULT:
  376. case VIDEO_CS_709:
  377. vui_params->colourPrimaries = 1;
  378. vui_params->transferCharacteristics = 1;
  379. vui_params->colourMatrix = 1;
  380. break;
  381. case VIDEO_CS_SRGB:
  382. vui_params->colourPrimaries = 1;
  383. vui_params->transferCharacteristics = 13;
  384. vui_params->colourMatrix = 1;
  385. break;
  386. default:
  387. break;
  388. }
  389. if (lossless) {
  390. h264_config->qpPrimeYZeroTransformBypassFlag = 1;
  391. } else if (strcmp(enc->props.rate_control, "CBR") == 0) { /* CBR */
  392. h264_config->outputBufferingPeriodSEI = 1;
  393. }
  394. h264_config->outputPictureTimingSEI = 1;
  395. /* -------------------------- */
  396. /* profile */
  397. if (enc->in_format == VIDEO_FORMAT_I444) {
  398. config->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
  399. h264_config->chromaFormatIDC = 3;
  400. } else if (astrcmpi(enc->props.profile, "main") == 0) {
  401. config->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
  402. } else if (astrcmpi(enc->props.profile, "baseline") == 0) {
  403. config->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
  404. } else if (!lossless) {
  405. config->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
  406. }
  407. apply_user_args(enc);
  408. if (NV_FAILED(nv.nvEncInitializeEncoder(enc->session, &enc->params))) {
  409. return false;
  410. }
  411. return true;
  412. }
  413. static bool init_encoder_hevc(struct nvenc_data *enc, obs_data_t *settings)
  414. {
  415. if (!init_encoder_base(enc, settings)) {
  416. return false;
  417. }
  418. NV_ENC_CONFIG *config = &enc->config;
  419. NV_ENC_CONFIG_HEVC *hevc_config = &config->encodeCodecConfig.hevcConfig;
  420. NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui_params =
  421. &hevc_config->hevcVUIParameters;
  422. video_t *video = obs_encoder_video(enc->encoder);
  423. const struct video_output_info *voi = video_output_get_info(video);
  424. if (enc->props.repeat_headers) {
  425. hevc_config->repeatSPSPPS = 1;
  426. hevc_config->disableSPSPPS = 0;
  427. hevc_config->outputAUD = 1;
  428. }
  429. hevc_config->idrPeriod = config->gopLength;
  430. hevc_config->sliceMode = 3;
  431. hevc_config->sliceModeData = 1;
  432. hevc_config->useBFramesAsRef =
  433. (NV_ENC_BFRAME_REF_MODE)enc->props.bframe_ref_mode;
  434. /* Enable CBR padding */
  435. if (config->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR)
  436. hevc_config->enableFillerDataInsertion = 1;
  437. vui_params->videoSignalTypePresentFlag = 1;
  438. vui_params->videoFullRangeFlag = (voi->range == VIDEO_RANGE_FULL);
  439. vui_params->colourDescriptionPresentFlag = 1;
  440. switch (voi->colorspace) {
  441. case VIDEO_CS_601:
  442. vui_params->colourPrimaries = 6;
  443. vui_params->transferCharacteristics = 6;
  444. vui_params->colourMatrix = 6;
  445. break;
  446. case VIDEO_CS_DEFAULT:
  447. case VIDEO_CS_709:
  448. vui_params->colourPrimaries = 1;
  449. vui_params->transferCharacteristics = 1;
  450. vui_params->colourMatrix = 1;
  451. break;
  452. case VIDEO_CS_SRGB:
  453. vui_params->colourPrimaries = 1;
  454. vui_params->transferCharacteristics = 13;
  455. vui_params->colourMatrix = 1;
  456. break;
  457. case VIDEO_CS_2100_PQ:
  458. vui_params->colourPrimaries = 9;
  459. vui_params->transferCharacteristics = 16;
  460. vui_params->colourMatrix = 9;
  461. vui_params->chromaSampleLocationFlag = 1;
  462. vui_params->chromaSampleLocationTop = 2;
  463. vui_params->chromaSampleLocationBot = 2;
  464. break;
  465. case VIDEO_CS_2100_HLG:
  466. vui_params->colourPrimaries = 9;
  467. vui_params->transferCharacteristics = 18;
  468. vui_params->colourMatrix = 9;
  469. vui_params->chromaSampleLocationFlag = 1;
  470. vui_params->chromaSampleLocationTop = 2;
  471. vui_params->chromaSampleLocationBot = 2;
  472. }
  473. if (astrcmpi(enc->props.rate_control, "cbr") == 0) {
  474. hevc_config->outputBufferingPeriodSEI = 1;
  475. }
  476. hevc_config->outputPictureTimingSEI = 1;
  477. /* -------------------------- */
  478. /* profile */
  479. bool profile_is_10bpc = false;
  480. if (enc->in_format == VIDEO_FORMAT_I444) {
  481. config->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
  482. hevc_config->chromaFormatIDC = 3;
  483. } else if (astrcmpi(enc->props.profile, "main10") == 0) {
  484. config->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
  485. profile_is_10bpc = true;
  486. } else if (is_10_bit(enc)) {
  487. blog(LOG_WARNING, "[obs-nvenc] Forcing main10 for P010");
  488. config->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
  489. profile_is_10bpc = true;
  490. } else {
  491. config->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
  492. }
  493. #ifndef NVENC_12_2_OR_LATER
  494. hevc_config->pixelBitDepthMinus8 = is_10_bit(enc) ? 2 : 0;
  495. #else
  496. hevc_config->inputBitDepth = is_10_bit(enc) ? NV_ENC_BIT_DEPTH_10
  497. : NV_ENC_BIT_DEPTH_8;
  498. hevc_config->outputBitDepth = profile_is_10bpc ? NV_ENC_BIT_DEPTH_10
  499. : NV_ENC_BIT_DEPTH_8;
  500. #endif
  501. apply_user_args(enc);
  502. if (NV_FAILED(nv.nvEncInitializeEncoder(enc->session, &enc->params))) {
  503. return false;
  504. }
  505. return true;
  506. }
  507. static bool init_encoder_av1(struct nvenc_data *enc, obs_data_t *settings)
  508. {
  509. if (!init_encoder_base(enc, settings)) {
  510. return false;
  511. }
  512. NV_ENC_CONFIG *config = &enc->config;
  513. NV_ENC_CONFIG_AV1 *av1_config = &config->encodeCodecConfig.av1Config;
  514. video_t *video = obs_encoder_video(enc->encoder);
  515. const struct video_output_info *voi = video_output_get_info(video);
  516. av1_config->idrPeriod = config->gopLength;
  517. av1_config->useBFramesAsRef =
  518. (NV_ENC_BFRAME_REF_MODE)enc->props.bframe_ref_mode;
  519. av1_config->colorRange = (voi->range == VIDEO_RANGE_FULL);
  520. /* Enable CBR padding */
  521. if (config->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR)
  522. av1_config->enableBitstreamPadding = 1;
  523. #define PIXELCOUNT_4K (3840 * 2160)
  524. /* If size is 4K+, set tiles to 2 uniform columns. */
  525. if ((voi->width * voi->height) >= PIXELCOUNT_4K)
  526. av1_config->numTileColumns = 2;
  527. switch (voi->colorspace) {
  528. case VIDEO_CS_601:
  529. av1_config->colorPrimaries = 6;
  530. av1_config->transferCharacteristics = 6;
  531. av1_config->matrixCoefficients = 6;
  532. break;
  533. case VIDEO_CS_DEFAULT:
  534. case VIDEO_CS_709:
  535. av1_config->colorPrimaries = 1;
  536. av1_config->transferCharacteristics = 1;
  537. av1_config->matrixCoefficients = 1;
  538. break;
  539. case VIDEO_CS_SRGB:
  540. av1_config->colorPrimaries = 1;
  541. av1_config->transferCharacteristics = 13;
  542. av1_config->matrixCoefficients = 1;
  543. break;
  544. case VIDEO_CS_2100_PQ:
  545. av1_config->colorPrimaries = 9;
  546. av1_config->transferCharacteristics = 16;
  547. av1_config->matrixCoefficients = 9;
  548. break;
  549. case VIDEO_CS_2100_HLG:
  550. av1_config->colorPrimaries = 9;
  551. av1_config->transferCharacteristics = 18;
  552. av1_config->matrixCoefficients = 9;
  553. }
  554. /* -------------------------- */
  555. /* profile */
  556. config->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
  557. av1_config->tier = NV_ENC_TIER_AV1_0;
  558. av1_config->level = NV_ENC_LEVEL_AV1_AUTOSELECT;
  559. av1_config->chromaFormatIDC = 1;
  560. #ifndef NVENC_12_2_OR_LATER
  561. av1_config->pixelBitDepthMinus8 = is_10_bit(enc) ? 2 : 0;
  562. av1_config->inputPixelBitDepthMinus8 = av1_config->pixelBitDepthMinus8;
  563. #else
  564. av1_config->inputBitDepth = is_10_bit(enc) ? NV_ENC_BIT_DEPTH_10
  565. : NV_ENC_BIT_DEPTH_8;
  566. av1_config->outputBitDepth = av1_config->inputBitDepth;
  567. #endif
  568. av1_config->numFwdRefs = 1;
  569. av1_config->numBwdRefs = 1;
  570. av1_config->repeatSeqHdr = 1;
  571. apply_user_args(enc);
  572. if (NV_FAILED(nv.nvEncInitializeEncoder(enc->session, &enc->params))) {
  573. return false;
  574. }
  575. return true;
  576. }
  577. static bool init_bitstreams(struct nvenc_data *enc)
  578. {
  579. da_reserve(enc->bitstreams, enc->buf_count);
  580. for (uint32_t i = 0; i < enc->buf_count; i++) {
  581. struct nv_bitstream bitstream;
  582. if (!nv_bitstream_init(enc, &bitstream)) {
  583. return false;
  584. }
  585. da_push_back(enc->bitstreams, &bitstream);
  586. }
  587. return true;
  588. }
  589. static enum video_format get_preferred_format(enum video_format format)
  590. {
  591. switch (format) {
  592. case VIDEO_FORMAT_I010:
  593. case VIDEO_FORMAT_P010:
  594. return VIDEO_FORMAT_P010;
  595. case VIDEO_FORMAT_RGBA:
  596. case VIDEO_FORMAT_BGRA:
  597. case VIDEO_FORMAT_BGRX:
  598. case VIDEO_FORMAT_I444:
  599. return VIDEO_FORMAT_I444;
  600. default:
  601. return VIDEO_FORMAT_NV12;
  602. }
  603. }
  604. static void nvenc_destroy(void *data);
  605. static bool init_encoder(struct nvenc_data *enc, enum codec_type codec,
  606. obs_data_t *settings, obs_encoder_t *encoder)
  607. {
  608. UNUSED_PARAMETER(codec);
  609. UNUSED_PARAMETER(encoder);
  610. const bool support_10bit =
  611. nv_get_cap(enc, NV_ENC_CAPS_SUPPORT_10BIT_ENCODE);
  612. const bool support_444 =
  613. nv_get_cap(enc, NV_ENC_CAPS_SUPPORT_YUV444_ENCODE);
  614. video_t *video = obs_encoder_video(enc->encoder);
  615. const struct video_output_info *voi = video_output_get_info(video);
  616. enum video_format pref_format =
  617. obs_encoder_get_preferred_video_format(enc->encoder);
  618. if (pref_format == VIDEO_FORMAT_NONE)
  619. pref_format = voi->format;
  620. enc->in_format = get_preferred_format(pref_format);
  621. if (enc->in_format == VIDEO_FORMAT_I444 && !support_444) {
  622. NV_FAIL(obs_module_text("NVENC.444Unsupported"));
  623. return false;
  624. }
  625. if (is_10_bit(enc) && !support_10bit) {
  626. NV_FAIL(obs_module_text("10bitUnsupported"));
  627. return false;
  628. }
  629. switch (voi->format) {
  630. case VIDEO_FORMAT_I010:
  631. case VIDEO_FORMAT_P010:
  632. break;
  633. default:
  634. switch (voi->colorspace) {
  635. case VIDEO_CS_2100_PQ:
  636. case VIDEO_CS_2100_HLG:
  637. NV_FAIL(obs_module_text("8bitUnsupportedHdr"));
  638. return false;
  639. default:
  640. break;
  641. }
  642. }
  643. switch (enc->codec) {
  644. case CODEC_HEVC:
  645. return init_encoder_hevc(enc, settings);
  646. case CODEC_H264:
  647. return init_encoder_h264(enc, settings);
  648. case CODEC_AV1:
  649. return init_encoder_av1(enc, settings);
  650. }
  651. return false;
  652. }
  653. static void *nvenc_create_internal(enum codec_type codec, obs_data_t *settings,
  654. obs_encoder_t *encoder, bool texture)
  655. {
  656. struct nvenc_data *enc = bzalloc(sizeof(*enc));
  657. enc->encoder = encoder;
  658. enc->codec = codec;
  659. enc->first_packet = true;
  660. enc->non_texture = !texture;
  661. nvenc_properties_read(&enc->props, settings);
  662. NV_ENCODE_API_FUNCTION_LIST init = {NV_ENCODE_API_FUNCTION_LIST_VER};
  663. switch (enc->codec) {
  664. case CODEC_H264:
  665. enc->codec_guid = NV_ENC_CODEC_H264_GUID;
  666. break;
  667. case CODEC_HEVC:
  668. enc->codec_guid = NV_ENC_CODEC_HEVC_GUID;
  669. break;
  670. case CODEC_AV1:
  671. enc->codec_guid = NV_ENC_CODEC_AV1_GUID;
  672. break;
  673. }
  674. if (!init_nvenc(encoder))
  675. goto fail;
  676. #ifdef _WIN32
  677. if (texture ? !d3d11_init(enc, settings) : !init_cuda(encoder))
  678. goto fail;
  679. #else
  680. if (!init_cuda(encoder))
  681. goto fail;
  682. #endif
  683. if (NV_FAILED(nv_create_instance(&init)))
  684. goto fail;
  685. if (!cuda_ctx_init(enc, settings, texture))
  686. goto fail;
  687. if (!init_session(enc)) {
  688. goto fail;
  689. }
  690. if (!init_encoder(enc, codec, settings, encoder)) {
  691. goto fail;
  692. }
  693. if (!init_bitstreams(enc)) {
  694. goto fail;
  695. }
  696. #ifdef _WIN32
  697. if (texture ? !d3d11_init_textures(enc) : !cuda_init_surfaces(enc))
  698. goto fail;
  699. #else
  700. if (!cuda_init_surfaces(enc))
  701. goto fail;
  702. #endif
  703. enc->codec = codec;
  704. return enc;
  705. fail:
  706. nvenc_destroy(enc);
  707. return NULL;
  708. }
  709. static void *nvenc_create_base(enum codec_type codec, obs_data_t *settings,
  710. obs_encoder_t *encoder, bool texture)
  711. {
  712. /* This encoder requires shared textures, this cannot be used on a
  713. * gpu other than the one OBS is currently running on.
  714. *
  715. * 2024 Amendment: On Linux when using CUDA<->OpenGL interop we can
  716. * in fact use shared textures even when using a different GPU, this
  717. * will still copy data through the CPU, but much more efficiently than
  718. * our native non-texture encoder. For now allow this via a hidden
  719. * option as it may cause issues for people.
  720. */
  721. const int gpu = (int)obs_data_get_int(settings, "device");
  722. #ifndef _WIN32
  723. const bool force_tex = obs_data_get_bool(settings, "force_cuda_tex");
  724. #else
  725. const bool force_tex = false;
  726. #endif
  727. if (gpu != -1 && texture && !force_tex) {
  728. blog(LOG_INFO,
  729. "[obs-nvenc] different GPU selected by user, falling back "
  730. "to non-texture encoder");
  731. goto reroute;
  732. }
  733. if (obs_encoder_scaling_enabled(encoder)) {
  734. if (obs_encoder_gpu_scaling_enabled(encoder)) {
  735. blog(LOG_INFO, "[obs-nvenc] GPU scaling enabled");
  736. } else if (texture) {
  737. blog(LOG_INFO,
  738. "[obs-nvenc] CPU scaling enabled, falling back to"
  739. " non-texture encoder");
  740. goto reroute;
  741. }
  742. }
  743. if (texture && !obs_p010_tex_active() && !obs_nv12_tex_active()) {
  744. blog(LOG_INFO,
  745. "[obs-nvenc] nv12/p010 not active, falling back to "
  746. "non-texture encoder");
  747. goto reroute;
  748. }
  749. struct nvenc_data *enc =
  750. nvenc_create_internal(codec, settings, encoder, texture);
  751. if (enc) {
  752. return enc;
  753. }
  754. reroute:
  755. if (!texture) {
  756. blog(LOG_ERROR,
  757. "Already in non_texture encoder, can't fall back further!");
  758. return NULL;
  759. }
  760. switch (codec) {
  761. case CODEC_H264:
  762. return obs_encoder_create_rerouted(encoder,
  763. "obs_nvenc_h264_soft");
  764. case CODEC_HEVC:
  765. return obs_encoder_create_rerouted(encoder,
  766. "obs_nvenc_hevc_soft");
  767. case CODEC_AV1:
  768. return obs_encoder_create_rerouted(encoder,
  769. "obs_nvenc_av1_soft");
  770. }
  771. return NULL;
  772. }
  773. static void *h264_nvenc_create(obs_data_t *settings, obs_encoder_t *encoder)
  774. {
  775. return nvenc_create_base(CODEC_H264, settings, encoder, true);
  776. }
  777. #ifdef ENABLE_HEVC
  778. static void *hevc_nvenc_create(obs_data_t *settings, obs_encoder_t *encoder)
  779. {
  780. return nvenc_create_base(CODEC_HEVC, settings, encoder, true);
  781. }
  782. #endif
  783. static void *av1_nvenc_create(obs_data_t *settings, obs_encoder_t *encoder)
  784. {
  785. return nvenc_create_base(CODEC_AV1, settings, encoder, true);
  786. }
  787. static void *h264_nvenc_soft_create(obs_data_t *settings,
  788. obs_encoder_t *encoder)
  789. {
  790. return nvenc_create_base(CODEC_H264, settings, encoder, false);
  791. }
  792. #ifdef ENABLE_HEVC
  793. static void *hevc_nvenc_soft_create(obs_data_t *settings,
  794. obs_encoder_t *encoder)
  795. {
  796. return nvenc_create_base(CODEC_HEVC, settings, encoder, false);
  797. }
  798. #endif
  799. static void *av1_nvenc_soft_create(obs_data_t *settings, obs_encoder_t *encoder)
  800. {
  801. return nvenc_create_base(CODEC_AV1, settings, encoder, false);
  802. }
  803. static bool get_encoded_packet(struct nvenc_data *enc, bool finalize);
  804. static void nvenc_destroy(void *data)
  805. {
  806. struct nvenc_data *enc = data;
  807. if (enc->encode_started) {
  808. NV_ENC_PIC_PARAMS params = {NV_ENC_PIC_PARAMS_VER};
  809. params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
  810. nv.nvEncEncodePicture(enc->session, &params);
  811. get_encoded_packet(enc, true);
  812. }
  813. for (size_t i = 0; i < enc->bitstreams.num; i++) {
  814. nv_bitstream_free(enc, &enc->bitstreams.array[i]);
  815. }
  816. if (enc->session)
  817. nv.nvEncDestroyEncoder(enc->session);
  818. #ifdef _WIN32
  819. d3d11_free_textures(enc);
  820. d3d11_free(enc);
  821. #else
  822. cuda_opengl_free(enc);
  823. #endif
  824. cuda_free_surfaces(enc);
  825. cuda_ctx_free(enc);
  826. bfree(enc->header);
  827. bfree(enc->sei);
  828. bfree(enc->roi_map);
  829. deque_free(&enc->dts_list);
  830. da_free(enc->surfaces);
  831. da_free(enc->input_textures);
  832. da_free(enc->bitstreams);
  833. #ifdef _WIN32
  834. da_free(enc->textures);
  835. #endif
  836. da_free(enc->packet_data);
  837. obs_free_options(enc->props.opts);
  838. obs_data_release(enc->props.data);
  839. bfree(enc);
  840. }
  841. static bool get_encoded_packet(struct nvenc_data *enc, bool finalize)
  842. {
  843. void *s = enc->session;
  844. da_resize(enc->packet_data, 0);
  845. if (!enc->buffers_queued)
  846. return true;
  847. if (!finalize && enc->buffers_queued < enc->output_delay)
  848. return true;
  849. size_t count = finalize ? enc->buffers_queued : 1;
  850. for (size_t i = 0; i < count; i++) {
  851. size_t cur_bs_idx = enc->cur_bitstream;
  852. struct nv_bitstream *bs = &enc->bitstreams.array[cur_bs_idx];
  853. #ifdef _WIN32
  854. struct nv_texture *nvtex =
  855. enc->non_texture ? NULL
  856. : &enc->textures.array[cur_bs_idx];
  857. struct nv_cuda_surface *surf =
  858. enc->non_texture ? &enc->surfaces.array[cur_bs_idx]
  859. : NULL;
  860. #else
  861. struct nv_cuda_surface *surf = &enc->surfaces.array[cur_bs_idx];
  862. #endif
  863. /* ---------------- */
  864. NV_ENC_LOCK_BITSTREAM lock = {NV_ENC_LOCK_BITSTREAM_VER};
  865. lock.outputBitstream = bs->ptr;
  866. lock.doNotWait = false;
  867. if (NV_FAILED(nv.nvEncLockBitstream(s, &lock))) {
  868. return false;
  869. }
  870. if (enc->first_packet) {
  871. NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = {0};
  872. uint8_t buf[256];
  873. uint32_t size = 0;
  874. payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
  875. payload.spsppsBuffer = buf;
  876. payload.inBufferSize = sizeof(buf);
  877. payload.outSPSPPSPayloadSize = &size;
  878. nv.nvEncGetSequenceParams(s, &payload);
  879. enc->header = bmemdup(buf, size);
  880. enc->header_size = size;
  881. enc->first_packet = false;
  882. }
  883. da_copy_array(enc->packet_data, lock.bitstreamBufferPtr,
  884. lock.bitstreamSizeInBytes);
  885. enc->packet_pts = (int64_t)lock.outputTimeStamp;
  886. enc->packet_keyframe = lock.pictureType == NV_ENC_PIC_TYPE_IDR;
  887. if (NV_FAILED(nv.nvEncUnlockBitstream(s, bs->ptr))) {
  888. return false;
  889. }
  890. /* ---------------- */
  891. #ifdef _WIN32
  892. if (nvtex && nvtex->mapped_res) {
  893. NVENCSTATUS err;
  894. err = nv.nvEncUnmapInputResource(s, nvtex->mapped_res);
  895. if (nv_failed(enc->encoder, err, __FUNCTION__,
  896. "unmap")) {
  897. return false;
  898. }
  899. nvtex->mapped_res = NULL;
  900. }
  901. #endif
  902. /* ---------------- */
  903. if (surf && surf->mapped_res) {
  904. NVENCSTATUS err;
  905. err = nv.nvEncUnmapInputResource(s, surf->mapped_res);
  906. if (nv_failed(enc->encoder, err, __FUNCTION__,
  907. "unmap")) {
  908. return false;
  909. }
  910. surf->mapped_res = NULL;
  911. }
  912. /* ---------------- */
  913. if (++enc->cur_bitstream == enc->buf_count)
  914. enc->cur_bitstream = 0;
  915. enc->buffers_queued--;
  916. }
  917. return true;
  918. }
  919. struct roi_params {
  920. uint32_t mb_width;
  921. uint32_t mb_height;
  922. uint32_t mb_size;
  923. bool av1;
  924. int8_t *map;
  925. };
  926. static void roi_cb(void *param, struct obs_encoder_roi *roi)
  927. {
  928. const struct roi_params *rp = param;
  929. int8_t qp_val;
  930. /* AV1 has a larger QP range than HEVC/H.264 */
  931. if (rp->av1) {
  932. qp_val = (int8_t)(-128.0f * roi->priority);
  933. } else {
  934. qp_val = (int8_t)(-51.0f * roi->priority);
  935. }
  936. const uint32_t roi_left = roi->left / rp->mb_size;
  937. const uint32_t roi_top = roi->top / rp->mb_size;
  938. const uint32_t roi_right = (roi->right - 1) / rp->mb_size;
  939. const uint32_t roi_bottom = (roi->bottom - 1) / rp->mb_size;
  940. for (uint32_t mb_y = 0; mb_y < rp->mb_height; mb_y++) {
  941. if (mb_y < roi_top || mb_y > roi_bottom)
  942. continue;
  943. for (uint32_t mb_x = 0; mb_x < rp->mb_width; mb_x++) {
  944. if (mb_x < roi_left || mb_x > roi_right)
  945. continue;
  946. rp->map[mb_y * rp->mb_width + mb_x] = qp_val;
  947. }
  948. }
  949. }
  950. static void add_roi(struct nvenc_data *enc, NV_ENC_PIC_PARAMS *params)
  951. {
  952. const uint32_t increment = obs_encoder_get_roi_increment(enc->encoder);
  953. if (enc->roi_map && enc->roi_increment == increment) {
  954. params->qpDeltaMap = enc->roi_map;
  955. params->qpDeltaMapSize = (uint32_t)enc->roi_map_size;
  956. return;
  957. }
  958. uint32_t mb_size = 0;
  959. switch (enc->codec) {
  960. case CODEC_H264:
  961. /* H.264 is always 16x16 */
  962. mb_size = 16;
  963. break;
  964. case CODEC_HEVC:
  965. /* HEVC can be 16x16, 32x32, or 64x64, but NVENC is always 32x32 */
  966. mb_size = 32;
  967. break;
  968. case CODEC_AV1:
  969. /* AV1 can be 64x64 or 128x128, but NVENC is always 64x64 */
  970. mb_size = 64;
  971. break;
  972. }
  973. const uint32_t mb_width = (enc->cx + mb_size - 1) / mb_size;
  974. const uint32_t mb_height = (enc->cy + mb_size - 1) / mb_size;
  975. const size_t map_size = mb_width * mb_height * sizeof(int8_t);
  976. if (map_size != enc->roi_map_size) {
  977. enc->roi_map = brealloc(enc->roi_map, map_size);
  978. enc->roi_map_size = map_size;
  979. }
  980. memset(enc->roi_map, 0, enc->roi_map_size);
  981. struct roi_params par = {
  982. .mb_width = mb_width,
  983. .mb_height = mb_height,
  984. .mb_size = mb_size,
  985. .av1 = enc->codec == CODEC_AV1,
  986. .map = enc->roi_map,
  987. };
  988. obs_encoder_enum_roi(enc->encoder, roi_cb, &par);
  989. enc->roi_increment = increment;
  990. params->qpDeltaMap = enc->roi_map;
  991. params->qpDeltaMapSize = (uint32_t)map_size;
  992. }
  993. bool nvenc_encode_base(struct nvenc_data *enc, struct nv_bitstream *bs,
  994. void *pic, int64_t pts, struct encoder_packet *packet,
  995. bool *received_packet)
  996. {
  997. NV_ENC_PIC_PARAMS params = {0};
  998. params.version = NV_ENC_PIC_PARAMS_VER;
  999. params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
  1000. params.inputBuffer = pic;
  1001. params.inputTimeStamp = (uint64_t)pts;
  1002. params.inputWidth = enc->cx;
  1003. params.inputHeight = enc->cy;
  1004. params.inputPitch = enc->cx;
  1005. params.outputBitstream = bs->ptr;
  1006. params.frameIdx = (uint32_t)pts;
  1007. if (enc->non_texture) {
  1008. params.bufferFmt = enc->surface_format;
  1009. } else {
  1010. params.bufferFmt = obs_p010_tex_active()
  1011. ? NV_ENC_BUFFER_FORMAT_YUV420_10BIT
  1012. : NV_ENC_BUFFER_FORMAT_NV12;
  1013. }
  1014. /* Add ROI map if enabled */
  1015. if (obs_encoder_has_roi(enc->encoder))
  1016. add_roi(enc, &params);
  1017. NVENCSTATUS err = nv.nvEncEncodePicture(enc->session, &params);
  1018. if (err != NV_ENC_SUCCESS && err != NV_ENC_ERR_NEED_MORE_INPUT) {
  1019. nv_failed(enc->encoder, err, __FUNCTION__,
  1020. "nvEncEncodePicture");
  1021. return false;
  1022. }
  1023. enc->encode_started = true;
  1024. enc->buffers_queued++;
  1025. if (++enc->next_bitstream == enc->buf_count) {
  1026. enc->next_bitstream = 0;
  1027. }
  1028. /* ------------------------------------ */
  1029. /* check for encoded packet and parse */
  1030. if (!get_encoded_packet(enc, false)) {
  1031. return false;
  1032. }
  1033. /* ------------------------------------ */
  1034. /* output encoded packet */
  1035. if (enc->packet_data.num) {
  1036. int64_t dts;
  1037. deque_pop_front(&enc->dts_list, &dts, sizeof(dts));
  1038. /* subtract bframe delay from dts for H.264/HEVC */
  1039. if (enc->codec != CODEC_AV1)
  1040. dts -= enc->props.bf * packet->timebase_num;
  1041. *received_packet = true;
  1042. packet->data = enc->packet_data.array;
  1043. packet->size = enc->packet_data.num;
  1044. packet->type = OBS_ENCODER_VIDEO;
  1045. packet->pts = enc->packet_pts;
  1046. packet->dts = dts;
  1047. packet->keyframe = enc->packet_keyframe;
  1048. } else {
  1049. *received_packet = false;
  1050. }
  1051. return true;
  1052. }
  1053. static void nvenc_soft_video_info(void *data, struct video_scale_info *info)
  1054. {
  1055. struct nvenc_data *enc = data;
  1056. info->format = enc->in_format;
  1057. }
  1058. static bool nvenc_extra_data(void *data, uint8_t **header, size_t *size)
  1059. {
  1060. struct nvenc_data *enc = data;
  1061. if (!enc->header) {
  1062. return false;
  1063. }
  1064. *header = enc->header;
  1065. *size = enc->header_size;
  1066. return true;
  1067. }
  1068. static bool nvenc_sei_data(void *data, uint8_t **sei, size_t *size)
  1069. {
  1070. struct nvenc_data *enc = data;
  1071. if (!enc->sei) {
  1072. return false;
  1073. }
  1074. *sei = enc->sei;
  1075. *size = enc->sei_size;
  1076. return true;
  1077. }
  1078. struct obs_encoder_info h264_nvenc_info = {
  1079. .id = "obs_nvenc_h264_tex",
  1080. .codec = "h264",
  1081. .type = OBS_ENCODER_VIDEO,
  1082. .caps = OBS_ENCODER_CAP_PASS_TEXTURE | OBS_ENCODER_CAP_DYN_BITRATE |
  1083. OBS_ENCODER_CAP_ROI,
  1084. .get_name = h264_nvenc_get_name,
  1085. .create = h264_nvenc_create,
  1086. .destroy = nvenc_destroy,
  1087. .update = nvenc_update,
  1088. #ifdef _WIN32
  1089. .encode_texture2 = d3d11_encode,
  1090. #else
  1091. .encode_texture2 = cuda_opengl_encode,
  1092. #endif
  1093. .get_defaults = h264_nvenc_defaults,
  1094. .get_properties = h264_nvenc_properties,
  1095. .get_extra_data = nvenc_extra_data,
  1096. .get_sei_data = nvenc_sei_data,
  1097. };
  1098. #ifdef ENABLE_HEVC
  1099. struct obs_encoder_info hevc_nvenc_info = {
  1100. .id = "obs_nvenc_hevc_tex",
  1101. .codec = "hevc",
  1102. .type = OBS_ENCODER_VIDEO,
  1103. .caps = OBS_ENCODER_CAP_PASS_TEXTURE | OBS_ENCODER_CAP_DYN_BITRATE |
  1104. OBS_ENCODER_CAP_ROI,
  1105. .get_name = hevc_nvenc_get_name,
  1106. .create = hevc_nvenc_create,
  1107. .destroy = nvenc_destroy,
  1108. .update = nvenc_update,
  1109. #ifdef _WIN32
  1110. .encode_texture2 = d3d11_encode,
  1111. #else
  1112. .encode_texture2 = cuda_opengl_encode,
  1113. #endif
  1114. .get_defaults = hevc_nvenc_defaults,
  1115. .get_properties = hevc_nvenc_properties,
  1116. .get_extra_data = nvenc_extra_data,
  1117. .get_sei_data = nvenc_sei_data,
  1118. };
  1119. #endif
  1120. struct obs_encoder_info av1_nvenc_info = {
  1121. .id = "obs_nvenc_av1_tex",
  1122. .codec = "av1",
  1123. .type = OBS_ENCODER_VIDEO,
  1124. .caps = OBS_ENCODER_CAP_PASS_TEXTURE | OBS_ENCODER_CAP_DYN_BITRATE |
  1125. OBS_ENCODER_CAP_ROI,
  1126. .get_name = av1_nvenc_get_name,
  1127. .create = av1_nvenc_create,
  1128. .destroy = nvenc_destroy,
  1129. .update = nvenc_update,
  1130. #ifdef _WIN32
  1131. .encode_texture2 = d3d11_encode,
  1132. #else
  1133. .encode_texture2 = cuda_opengl_encode,
  1134. #endif
  1135. .get_defaults = av1_nvenc_defaults,
  1136. .get_properties = av1_nvenc_properties,
  1137. .get_extra_data = nvenc_extra_data,
  1138. };
  1139. struct obs_encoder_info h264_nvenc_soft_info = {
  1140. .id = "obs_nvenc_h264_soft",
  1141. .codec = "h264",
  1142. .type = OBS_ENCODER_VIDEO,
  1143. .caps = OBS_ENCODER_CAP_DYN_BITRATE | OBS_ENCODER_CAP_ROI |
  1144. OBS_ENCODER_CAP_INTERNAL,
  1145. .get_name = h264_nvenc_soft_get_name,
  1146. .create = h264_nvenc_soft_create,
  1147. .destroy = nvenc_destroy,
  1148. .update = nvenc_update,
  1149. .encode = cuda_encode,
  1150. .get_defaults = h264_nvenc_defaults,
  1151. .get_properties = h264_nvenc_properties,
  1152. .get_extra_data = nvenc_extra_data,
  1153. .get_sei_data = nvenc_sei_data,
  1154. .get_video_info = nvenc_soft_video_info,
  1155. };
  1156. #ifdef ENABLE_HEVC
  1157. struct obs_encoder_info hevc_nvenc_soft_info = {
  1158. .id = "obs_nvenc_hevc_soft",
  1159. .codec = "hevc",
  1160. .type = OBS_ENCODER_VIDEO,
  1161. .caps = OBS_ENCODER_CAP_DYN_BITRATE | OBS_ENCODER_CAP_ROI |
  1162. OBS_ENCODER_CAP_INTERNAL,
  1163. .get_name = hevc_nvenc_soft_get_name,
  1164. .create = hevc_nvenc_soft_create,
  1165. .destroy = nvenc_destroy,
  1166. .update = nvenc_update,
  1167. .encode = cuda_encode,
  1168. .get_defaults = hevc_nvenc_defaults,
  1169. .get_properties = hevc_nvenc_properties,
  1170. .get_extra_data = nvenc_extra_data,
  1171. .get_sei_data = nvenc_sei_data,
  1172. .get_video_info = nvenc_soft_video_info,
  1173. };
  1174. #endif
  1175. struct obs_encoder_info av1_nvenc_soft_info = {
  1176. .id = "obs_nvenc_av1_soft",
  1177. .codec = "av1",
  1178. .type = OBS_ENCODER_VIDEO,
  1179. .caps = OBS_ENCODER_CAP_DYN_BITRATE | OBS_ENCODER_CAP_ROI |
  1180. OBS_ENCODER_CAP_INTERNAL,
  1181. .get_name = av1_nvenc_soft_get_name,
  1182. .create = av1_nvenc_soft_create,
  1183. .destroy = nvenc_destroy,
  1184. .update = nvenc_update,
  1185. .encode = cuda_encode,
  1186. .get_defaults = av1_nvenc_defaults,
  1187. .get_properties = av1_nvenc_properties,
  1188. .get_extra_data = nvenc_extra_data,
  1189. .get_video_info = nvenc_soft_video_info,
  1190. };
  1191. void register_encoders(void)
  1192. {
  1193. obs_register_encoder(&h264_nvenc_info);
  1194. obs_register_encoder(&h264_nvenc_soft_info);
  1195. #ifdef ENABLE_HEVC
  1196. obs_register_encoder(&hevc_nvenc_info);
  1197. obs_register_encoder(&hevc_nvenc_soft_info);
  1198. #endif
  1199. if (is_codec_supported(CODEC_AV1)) {
  1200. obs_register_encoder(&av1_nvenc_info);
  1201. obs_register_encoder(&av1_nvenc_soft_info);
  1202. }
  1203. }