nvenc.c 41 KB

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