nvenc.c 38 KB

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