obs-ffmpeg-nvenc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /******************************************************************************
  2. Copyright (C) 2016 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <util/darray.h>
  15. #include <util/dstr.h>
  16. #include <util/base.h>
  17. #include <media-io/video-io.h>
  18. #include <obs-module.h>
  19. #include <obs-avc.h>
  20. #include <libavutil/opt.h>
  21. #include <libavutil/pixdesc.h>
  22. #include <libavformat/avformat.h>
  23. #include "obs-ffmpeg-formats.h"
  24. #define do_log(level, format, ...) \
  25. blog(level, "[NVENC encoder: '%s'] " format, \
  26. obs_encoder_get_name(enc->encoder), ##__VA_ARGS__)
  27. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  28. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  29. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  30. struct nvenc_encoder {
  31. obs_encoder_t *encoder;
  32. AVCodec *nvenc;
  33. AVCodecContext *context;
  34. AVFrame *vframe;
  35. DARRAY(uint8_t) buffer;
  36. uint8_t *header;
  37. size_t header_size;
  38. uint8_t *sei;
  39. size_t sei_size;
  40. int height;
  41. bool first_packet;
  42. bool initialized;
  43. };
  44. static const char *nvenc_getname(void *unused)
  45. {
  46. UNUSED_PARAMETER(unused);
  47. return "NVIDIA NVENC H.264";
  48. }
  49. static inline bool valid_format(enum video_format format)
  50. {
  51. return format == VIDEO_FORMAT_I420 || format == VIDEO_FORMAT_NV12 ||
  52. format == VIDEO_FORMAT_I444;
  53. }
  54. static void nvenc_video_info(void *data, struct video_scale_info *info)
  55. {
  56. struct nvenc_encoder *enc = data;
  57. enum video_format pref_format;
  58. pref_format = obs_encoder_get_preferred_video_format(enc->encoder);
  59. if (!valid_format(pref_format)) {
  60. pref_format = valid_format(info->format) ? info->format
  61. : VIDEO_FORMAT_NV12;
  62. }
  63. info->format = pref_format;
  64. }
  65. static void set_psycho_aq(struct nvenc_encoder *enc, bool psycho_aq)
  66. {
  67. av_opt_set_int(enc->context->priv_data, "spatial-aq", psycho_aq, 0);
  68. av_opt_set_int(enc->context->priv_data, "temporal-aq", psycho_aq, 0);
  69. }
  70. static bool nvenc_init_codec(struct nvenc_encoder *enc, bool psycho_aq)
  71. {
  72. UNUSED_PARAMETER(psycho_aq);
  73. int ret;
  74. // avcodec_open2 will overwrite priv_data, we call this to get a
  75. // local copy of the "gpu" setting for improved error messages.
  76. int64_t gpu;
  77. if (av_opt_get_int(enc->context->priv_data, "gpu", 0, &gpu) < 0) {
  78. gpu = -1;
  79. }
  80. ret = avcodec_open2(enc->context, enc->nvenc, NULL);
  81. if (ret < 0) {
  82. // if we were a fallback from jim-nvenc, there may already be a
  83. // more useful error returned from that, so don't overwrite.
  84. // this can be removed if / when ffmpeg fallback is removed.
  85. if (!obs_encoder_get_last_error(enc->encoder)) {
  86. struct dstr error_message = {0};
  87. dstr_copy(&error_message,
  88. obs_module_text("NVENC.Error"));
  89. dstr_replace(&error_message, "%1", av_err2str(ret));
  90. dstr_cat(&error_message, "\r\n\r\n");
  91. if (gpu > 0) {
  92. // if a non-zero GPU failed, almost always
  93. // user error. tell then to fix it.
  94. char gpu_str[16];
  95. snprintf(gpu_str, sizeof(gpu_str) - 1, "%d",
  96. (int)gpu);
  97. gpu_str[sizeof(gpu_str) - 1] = 0;
  98. dstr_cat(&error_message,
  99. obs_module_text("NVENC.BadGPUIndex"));
  100. dstr_replace(&error_message, "%1", gpu_str);
  101. } else if (ret == AVERROR_EXTERNAL) {
  102. // special case for common NVENC error
  103. dstr_cat(&error_message,
  104. obs_module_text("NVENC.GenericError"));
  105. } else {
  106. dstr_cat(&error_message,
  107. obs_module_text("NVENC.CheckDrivers"));
  108. }
  109. obs_encoder_set_last_error(enc->encoder,
  110. error_message.array);
  111. dstr_free(&error_message);
  112. }
  113. warn("Failed to open NVENC codec: %s", av_err2str(ret));
  114. return false;
  115. }
  116. enc->vframe = av_frame_alloc();
  117. if (!enc->vframe) {
  118. warn("Failed to allocate video frame");
  119. return false;
  120. }
  121. enc->vframe->format = enc->context->pix_fmt;
  122. enc->vframe->width = enc->context->width;
  123. enc->vframe->height = enc->context->height;
  124. enc->vframe->colorspace = enc->context->colorspace;
  125. enc->vframe->color_range = enc->context->color_range;
  126. ret = av_frame_get_buffer(enc->vframe, base_get_alignment());
  127. if (ret < 0) {
  128. warn("Failed to allocate vframe: %s", av_err2str(ret));
  129. return false;
  130. }
  131. enc->initialized = true;
  132. return true;
  133. }
  134. enum RC_MODE { RC_MODE_CBR, RC_MODE_VBR, RC_MODE_CQP, RC_MODE_LOSSLESS };
  135. static bool nvenc_update(struct nvenc_encoder *enc, obs_data_t *settings,
  136. bool psycho_aq)
  137. {
  138. const char *rc = obs_data_get_string(settings, "rate_control");
  139. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  140. int cqp = (int)obs_data_get_int(settings, "cqp");
  141. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  142. const char *preset = obs_data_get_string(settings, "preset");
  143. const char *profile = obs_data_get_string(settings, "profile");
  144. int gpu = (int)obs_data_get_int(settings, "gpu");
  145. bool cbr_override = obs_data_get_bool(settings, "cbr");
  146. int bf = (int)obs_data_get_int(settings, "bf");
  147. video_t *video = obs_encoder_video(enc->encoder);
  148. const struct video_output_info *voi = video_output_get_info(video);
  149. struct video_scale_info info;
  150. /* XXX: "cbr" setting has been deprecated */
  151. if (cbr_override) {
  152. warn("\"cbr\" setting has been deprecated for all encoders! "
  153. "Please set \"rate_control\" to \"CBR\" instead. "
  154. "Forcing CBR mode. "
  155. "(Note to all: this is why you shouldn't use strings for "
  156. "common settings)");
  157. rc = "CBR";
  158. }
  159. info.format = voi->format;
  160. info.colorspace = voi->colorspace;
  161. info.range = voi->range;
  162. bool twopass = false;
  163. if (astrcmpi(preset, "mq") == 0) {
  164. twopass = true;
  165. preset = "hq";
  166. }
  167. nvenc_video_info(enc, &info);
  168. av_opt_set_int(enc->context->priv_data, "cbr", false, 0);
  169. av_opt_set(enc->context->priv_data, "profile", profile, 0);
  170. av_opt_set(enc->context->priv_data, "preset", preset, 0);
  171. if (astrcmpi(rc, "cqp") == 0) {
  172. bitrate = 0;
  173. enc->context->global_quality = cqp;
  174. } else if (astrcmpi(rc, "lossless") == 0) {
  175. bitrate = 0;
  176. cqp = 0;
  177. bool hp = (astrcmpi(preset, "hp") == 0 ||
  178. astrcmpi(preset, "llhp") == 0);
  179. av_opt_set(enc->context->priv_data, "preset",
  180. hp ? "losslesshp" : "lossless", 0);
  181. } else if (astrcmpi(rc, "vbr") != 0) { /* CBR by default */
  182. av_opt_set_int(enc->context->priv_data, "cbr", true, 0);
  183. const int64_t rate = bitrate * INT64_C(1000);
  184. enc->context->rc_max_rate = rate;
  185. enc->context->rc_min_rate = rate;
  186. cqp = 0;
  187. }
  188. av_opt_set(enc->context->priv_data, "level", "auto", 0);
  189. av_opt_set_int(enc->context->priv_data, "2pass", twopass, 0);
  190. av_opt_set_int(enc->context->priv_data, "gpu", gpu, 0);
  191. set_psycho_aq(enc, psycho_aq);
  192. const int rate = bitrate * 1000;
  193. enc->context->bit_rate = rate;
  194. enc->context->rc_buffer_size = rate;
  195. enc->context->width = obs_encoder_get_width(enc->encoder);
  196. enc->context->height = obs_encoder_get_height(enc->encoder);
  197. enc->context->time_base = (AVRational){voi->fps_den, voi->fps_num};
  198. enc->context->pix_fmt = obs_to_ffmpeg_video_format(info.format);
  199. enc->context->color_range = info.range == VIDEO_RANGE_FULL
  200. ? AVCOL_RANGE_JPEG
  201. : AVCOL_RANGE_MPEG;
  202. enc->context->max_b_frames = bf;
  203. switch (info.colorspace) {
  204. case VIDEO_CS_601:
  205. enc->context->color_trc = AVCOL_TRC_SMPTE170M;
  206. enc->context->color_primaries = AVCOL_PRI_SMPTE170M;
  207. enc->context->colorspace = AVCOL_SPC_SMPTE170M;
  208. break;
  209. case VIDEO_CS_DEFAULT:
  210. case VIDEO_CS_709:
  211. enc->context->color_trc = AVCOL_TRC_BT709;
  212. enc->context->color_primaries = AVCOL_PRI_BT709;
  213. enc->context->colorspace = AVCOL_SPC_BT709;
  214. break;
  215. case VIDEO_CS_SRGB:
  216. enc->context->color_trc = AVCOL_TRC_IEC61966_2_1;
  217. enc->context->color_primaries = AVCOL_PRI_BT709;
  218. enc->context->colorspace = AVCOL_SPC_BT709;
  219. break;
  220. }
  221. if (keyint_sec)
  222. enc->context->gop_size =
  223. keyint_sec * voi->fps_num / voi->fps_den;
  224. else
  225. enc->context->gop_size = 250;
  226. enc->height = enc->context->height;
  227. info("settings:\n"
  228. "\trate_control: %s\n"
  229. "\tbitrate: %d\n"
  230. "\tcqp: %d\n"
  231. "\tkeyint: %d\n"
  232. "\tpreset: %s\n"
  233. "\tprofile: %s\n"
  234. "\twidth: %d\n"
  235. "\theight: %d\n"
  236. "\t2-pass: %s\n"
  237. "\tb-frames: %d\n"
  238. "\tpsycho-aq: %d\n"
  239. "\tGPU: %d\n",
  240. rc, bitrate, cqp, enc->context->gop_size, preset, profile,
  241. enc->context->width, enc->context->height,
  242. twopass ? "true" : "false", enc->context->max_b_frames, psycho_aq,
  243. gpu);
  244. return nvenc_init_codec(enc, psycho_aq);
  245. }
  246. static bool nvenc_reconfigure(void *data, obs_data_t *settings)
  247. {
  248. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 19, 101)
  249. struct nvenc_encoder *enc = data;
  250. const int64_t bitrate = obs_data_get_int(settings, "bitrate");
  251. const char *rc = obs_data_get_string(settings, "rate_control");
  252. bool cbr = astrcmpi(rc, "CBR") == 0;
  253. bool vbr = astrcmpi(rc, "VBR") == 0;
  254. if (cbr || vbr) {
  255. const int64_t rate = bitrate * 1000;
  256. enc->context->bit_rate = rate;
  257. enc->context->rc_max_rate = rate;
  258. }
  259. #endif
  260. return true;
  261. }
  262. static void nvenc_destroy(void *data)
  263. {
  264. struct nvenc_encoder *enc = data;
  265. if (enc->initialized) {
  266. AVPacket pkt = {0};
  267. int r_pkt = 1;
  268. while (r_pkt) {
  269. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  270. if (avcodec_receive_packet(enc->context, &pkt) < 0)
  271. break;
  272. #else
  273. if (avcodec_encode_video2(enc->context, &pkt, NULL,
  274. &r_pkt) < 0)
  275. break;
  276. #endif
  277. if (r_pkt)
  278. av_packet_unref(&pkt);
  279. }
  280. }
  281. avcodec_close(enc->context);
  282. av_frame_unref(enc->vframe);
  283. av_frame_free(&enc->vframe);
  284. da_free(enc->buffer);
  285. bfree(enc->header);
  286. bfree(enc->sei);
  287. bfree(enc);
  288. }
  289. static void *nvenc_create_internal(obs_data_t *settings, obs_encoder_t *encoder,
  290. bool psycho_aq)
  291. {
  292. struct nvenc_encoder *enc;
  293. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  294. avcodec_register_all();
  295. #endif
  296. enc = bzalloc(sizeof(*enc));
  297. enc->encoder = encoder;
  298. enc->nvenc = avcodec_find_encoder_by_name("h264_nvenc");
  299. if (!enc->nvenc)
  300. enc->nvenc = avcodec_find_encoder_by_name("nvenc_h264");
  301. enc->first_packet = true;
  302. blog(LOG_INFO, "---------------------------------");
  303. if (!enc->nvenc) {
  304. obs_encoder_set_last_error(encoder,
  305. "Couldn't find NVENC encoder");
  306. warn("Couldn't find encoder");
  307. goto fail;
  308. }
  309. enc->context = avcodec_alloc_context3(enc->nvenc);
  310. if (!enc->context) {
  311. warn("Failed to create codec context");
  312. goto fail;
  313. }
  314. if (!nvenc_update(enc, settings, psycho_aq))
  315. goto fail;
  316. return enc;
  317. fail:
  318. nvenc_destroy(enc);
  319. return NULL;
  320. }
  321. static void *nvenc_create(obs_data_t *settings, obs_encoder_t *encoder)
  322. {
  323. bool psycho_aq = obs_data_get_bool(settings, "psycho_aq");
  324. void *enc = nvenc_create_internal(settings, encoder, psycho_aq);
  325. if ((enc == NULL) && psycho_aq) {
  326. blog(LOG_WARNING,
  327. "[NVENC encoder] nvenc_create_internal failed, "
  328. "trying again without Psycho Visual Tuning");
  329. enc = nvenc_create_internal(settings, encoder, false);
  330. }
  331. return enc;
  332. }
  333. static inline void copy_data(AVFrame *pic, const struct encoder_frame *frame,
  334. int height, enum AVPixelFormat format)
  335. {
  336. int h_chroma_shift, v_chroma_shift;
  337. av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
  338. &v_chroma_shift);
  339. for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
  340. if (!frame->data[plane])
  341. continue;
  342. int frame_rowsize = (int)frame->linesize[plane];
  343. int pic_rowsize = pic->linesize[plane];
  344. int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
  345. : pic_rowsize;
  346. int plane_height = height >> (plane ? v_chroma_shift : 0);
  347. for (int y = 0; y < plane_height; y++) {
  348. int pos_frame = y * frame_rowsize;
  349. int pos_pic = y * pic_rowsize;
  350. memcpy(pic->data[plane] + pos_pic,
  351. frame->data[plane] + pos_frame, bytes);
  352. }
  353. }
  354. }
  355. static bool nvenc_encode(void *data, struct encoder_frame *frame,
  356. struct encoder_packet *packet, bool *received_packet)
  357. {
  358. struct nvenc_encoder *enc = data;
  359. AVPacket av_pkt = {0};
  360. int got_packet;
  361. int ret;
  362. av_init_packet(&av_pkt);
  363. copy_data(enc->vframe, frame, enc->height, enc->context->pix_fmt);
  364. enc->vframe->pts = frame->pts;
  365. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  366. ret = avcodec_send_frame(enc->context, enc->vframe);
  367. if (ret == 0)
  368. ret = avcodec_receive_packet(enc->context, &av_pkt);
  369. got_packet = (ret == 0);
  370. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  371. ret = 0;
  372. #else
  373. ret = avcodec_encode_video2(enc->context, &av_pkt, enc->vframe,
  374. &got_packet);
  375. #endif
  376. if (ret < 0) {
  377. warn("nvenc_encode: Error encoding: %s", av_err2str(ret));
  378. return false;
  379. }
  380. if (got_packet && av_pkt.size) {
  381. if (enc->first_packet) {
  382. uint8_t *new_packet;
  383. size_t size;
  384. enc->first_packet = false;
  385. obs_extract_avc_headers(av_pkt.data, av_pkt.size,
  386. &new_packet, &size,
  387. &enc->header, &enc->header_size,
  388. &enc->sei, &enc->sei_size);
  389. da_copy_array(enc->buffer, new_packet, size);
  390. bfree(new_packet);
  391. } else {
  392. da_copy_array(enc->buffer, av_pkt.data, av_pkt.size);
  393. }
  394. packet->pts = av_pkt.pts;
  395. packet->dts = av_pkt.dts;
  396. packet->data = enc->buffer.array;
  397. packet->size = enc->buffer.num;
  398. packet->type = OBS_ENCODER_VIDEO;
  399. packet->keyframe = obs_avc_keyframe(packet->data, packet->size);
  400. *received_packet = true;
  401. } else {
  402. *received_packet = false;
  403. }
  404. av_packet_unref(&av_pkt);
  405. return true;
  406. }
  407. void nvenc_defaults(obs_data_t *settings)
  408. {
  409. obs_data_set_default_int(settings, "bitrate", 2500);
  410. obs_data_set_default_int(settings, "max_bitrate", 5000);
  411. obs_data_set_default_int(settings, "keyint_sec", 0);
  412. obs_data_set_default_int(settings, "cqp", 20);
  413. obs_data_set_default_string(settings, "rate_control", "CBR");
  414. obs_data_set_default_string(settings, "preset", "hq");
  415. obs_data_set_default_string(settings, "profile", "high");
  416. obs_data_set_default_bool(settings, "psycho_aq", true);
  417. obs_data_set_default_int(settings, "gpu", 0);
  418. obs_data_set_default_int(settings, "bf", 2);
  419. obs_data_set_default_bool(settings, "repeat_headers", false);
  420. }
  421. static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
  422. obs_data_t *settings)
  423. {
  424. const char *rc = obs_data_get_string(settings, "rate_control");
  425. bool cqp = astrcmpi(rc, "CQP") == 0;
  426. bool vbr = astrcmpi(rc, "VBR") == 0;
  427. bool lossless = astrcmpi(rc, "lossless") == 0;
  428. size_t count;
  429. p = obs_properties_get(ppts, "bitrate");
  430. obs_property_set_visible(p, !cqp && !lossless);
  431. p = obs_properties_get(ppts, "max_bitrate");
  432. obs_property_set_visible(p, vbr);
  433. p = obs_properties_get(ppts, "cqp");
  434. obs_property_set_visible(p, cqp);
  435. p = obs_properties_get(ppts, "preset");
  436. count = obs_property_list_item_count(p);
  437. for (size_t i = 0; i < count; i++) {
  438. bool compatible = (i == 0 || i == 3);
  439. obs_property_list_item_disable(p, i, lossless && !compatible);
  440. }
  441. return true;
  442. }
  443. obs_properties_t *nvenc_properties_internal(bool ffmpeg)
  444. {
  445. obs_properties_t *props = obs_properties_create();
  446. obs_property_t *p;
  447. p = obs_properties_add_list(props, "rate_control",
  448. obs_module_text("RateControl"),
  449. OBS_COMBO_TYPE_LIST,
  450. OBS_COMBO_FORMAT_STRING);
  451. obs_property_list_add_string(p, "CBR", "CBR");
  452. obs_property_list_add_string(p, "CQP", "CQP");
  453. obs_property_list_add_string(p, "VBR", "VBR");
  454. obs_property_list_add_string(p, obs_module_text("Lossless"),
  455. "lossless");
  456. obs_property_set_modified_callback(p, rate_control_modified);
  457. p = obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"),
  458. 50, 300000, 50);
  459. obs_property_int_set_suffix(p, " Kbps");
  460. p = obs_properties_add_int(props, "max_bitrate",
  461. obs_module_text("MaxBitrate"), 50, 300000,
  462. 50);
  463. obs_property_int_set_suffix(p, " Kbps");
  464. obs_properties_add_int(props, "cqp", obs_module_text("NVENC.CQLevel"),
  465. 1, 30, 1);
  466. obs_properties_add_int(props, "keyint_sec",
  467. obs_module_text("KeyframeIntervalSec"), 0, 10,
  468. 1);
  469. p = obs_properties_add_list(props, "preset", obs_module_text("Preset"),
  470. OBS_COMBO_TYPE_LIST,
  471. OBS_COMBO_FORMAT_STRING);
  472. #define add_preset(val) \
  473. obs_property_list_add_string(p, obs_module_text("NVENC.Preset." val), \
  474. val)
  475. add_preset("mq");
  476. add_preset("hq");
  477. add_preset("default");
  478. add_preset("hp");
  479. add_preset("ll");
  480. add_preset("llhq");
  481. add_preset("llhp");
  482. #undef add_preset
  483. p = obs_properties_add_list(props, "profile",
  484. obs_module_text("Profile"),
  485. OBS_COMBO_TYPE_LIST,
  486. OBS_COMBO_FORMAT_STRING);
  487. #define add_profile(val) obs_property_list_add_string(p, val, val)
  488. add_profile("high");
  489. add_profile("main");
  490. add_profile("baseline");
  491. #undef add_profile
  492. if (!ffmpeg) {
  493. p = obs_properties_add_bool(props, "lookahead",
  494. obs_module_text("NVENC.LookAhead"));
  495. obs_property_set_long_description(
  496. p, obs_module_text("NVENC.LookAhead.ToolTip"));
  497. p = obs_properties_add_bool(props, "repeat_headers",
  498. "repeat_headers");
  499. obs_property_set_visible(p, false);
  500. }
  501. p = obs_properties_add_bool(
  502. props, "psycho_aq",
  503. obs_module_text("NVENC.PsychoVisualTuning"));
  504. obs_property_set_long_description(
  505. p, obs_module_text("NVENC.PsychoVisualTuning.ToolTip"));
  506. obs_properties_add_int(props, "gpu", obs_module_text("GPU"), 0, 8, 1);
  507. obs_properties_add_int(props, "bf", obs_module_text("BFrames"), 0, 4,
  508. 1);
  509. return props;
  510. }
  511. obs_properties_t *nvenc_properties(void *unused)
  512. {
  513. UNUSED_PARAMETER(unused);
  514. return nvenc_properties_internal(false);
  515. }
  516. obs_properties_t *nvenc_properties_ffmpeg(void *unused)
  517. {
  518. UNUSED_PARAMETER(unused);
  519. return nvenc_properties_internal(true);
  520. }
  521. static bool nvenc_extra_data(void *data, uint8_t **extra_data, size_t *size)
  522. {
  523. struct nvenc_encoder *enc = data;
  524. *extra_data = enc->header;
  525. *size = enc->header_size;
  526. return true;
  527. }
  528. static bool nvenc_sei_data(void *data, uint8_t **extra_data, size_t *size)
  529. {
  530. struct nvenc_encoder *enc = data;
  531. *extra_data = enc->sei;
  532. *size = enc->sei_size;
  533. return true;
  534. }
  535. struct obs_encoder_info nvenc_encoder_info = {
  536. .id = "ffmpeg_nvenc",
  537. .type = OBS_ENCODER_VIDEO,
  538. .codec = "h264",
  539. .get_name = nvenc_getname,
  540. .create = nvenc_create,
  541. .destroy = nvenc_destroy,
  542. .encode = nvenc_encode,
  543. .update = nvenc_reconfigure,
  544. .get_defaults = nvenc_defaults,
  545. .get_properties = nvenc_properties_ffmpeg,
  546. .get_extra_data = nvenc_extra_data,
  547. .get_sei_data = nvenc_sei_data,
  548. .get_video_info = nvenc_video_info,
  549. #ifdef _WIN32
  550. .caps = OBS_ENCODER_CAP_DYN_BITRATE | OBS_ENCODER_CAP_INTERNAL,
  551. #else
  552. .caps = OBS_ENCODER_CAP_DYN_BITRATE,
  553. #endif
  554. };