nvenc.c 38 KB

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