obs-ffmpeg-nvenc.c 19 KB

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