nvenc.c 38 KB

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