obs-x264.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /******************************************************************************
  2. Copyright (C) 2014 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 <stdio.h>
  15. #include <string.h>
  16. #include <util/bmem.h>
  17. #include <util/dstr.h>
  18. #include <util/darray.h>
  19. #include <util/platform.h>
  20. #include <obs-module.h>
  21. #include <opts-parser.h>
  22. #ifndef _STDINT_H_INCLUDED
  23. #define _STDINT_H_INCLUDED
  24. #endif
  25. #include <x264.h>
  26. #define do_log_enc(level, encoder, format, ...) \
  27. blog(level, "[x264 encoder: '%s'] " format, \
  28. obs_encoder_get_name(encoder), ##__VA_ARGS__)
  29. #define do_log(level, format, ...) \
  30. do_log_enc(level, obsx264->encoder, format, ##__VA_ARGS__)
  31. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  32. #define warn_enc(encoder, format, ...) \
  33. do_log_enc(LOG_WARNING, encoder, format, ##__VA_ARGS__)
  34. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  35. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  36. //#define ENABLE_VFR
  37. /* ------------------------------------------------------------------------- */
  38. struct obs_x264 {
  39. obs_encoder_t *encoder;
  40. x264_param_t params;
  41. x264_t *context;
  42. DARRAY(uint8_t) packet_data;
  43. uint8_t *extra_data;
  44. uint8_t *sei;
  45. size_t extra_data_size;
  46. size_t sei_size;
  47. os_performance_token_t *performance_token;
  48. };
  49. /* ------------------------------------------------------------------------- */
  50. static const char *obs_x264_getname(void *unused)
  51. {
  52. UNUSED_PARAMETER(unused);
  53. return "x264";
  54. }
  55. static void obs_x264_stop(void *data);
  56. static void clear_data(struct obs_x264 *obsx264)
  57. {
  58. if (obsx264->context) {
  59. x264_encoder_close(obsx264->context);
  60. bfree(obsx264->sei);
  61. bfree(obsx264->extra_data);
  62. obsx264->context = NULL;
  63. obsx264->sei = NULL;
  64. obsx264->extra_data = NULL;
  65. }
  66. }
  67. static void obs_x264_destroy(void *data)
  68. {
  69. struct obs_x264 *obsx264 = data;
  70. if (obsx264) {
  71. os_end_high_performance(obsx264->performance_token);
  72. clear_data(obsx264);
  73. da_free(obsx264->packet_data);
  74. bfree(obsx264);
  75. }
  76. }
  77. static void obs_x264_defaults(obs_data_t *settings)
  78. {
  79. obs_data_set_default_int(settings, "bitrate", 2500);
  80. obs_data_set_default_bool(settings, "use_bufsize", false);
  81. obs_data_set_default_int(settings, "buffer_size", 2500);
  82. obs_data_set_default_int(settings, "keyint_sec", 0);
  83. obs_data_set_default_int(settings, "crf", 23);
  84. #ifdef ENABLE_VFR
  85. obs_data_set_default_bool(settings, "vfr", false);
  86. #endif
  87. obs_data_set_default_string(settings, "rate_control", "CBR");
  88. obs_data_set_default_string(settings, "preset", "veryfast");
  89. obs_data_set_default_string(settings, "profile", "");
  90. obs_data_set_default_string(settings, "tune", "");
  91. obs_data_set_default_string(settings, "x264opts", "");
  92. obs_data_set_default_bool(settings, "repeat_headers", false);
  93. }
  94. static inline void add_strings(obs_property_t *list, const char *const *strings)
  95. {
  96. while (*strings) {
  97. obs_property_list_add_string(list, *strings, *strings);
  98. strings++;
  99. }
  100. }
  101. #define TEXT_RATE_CONTROL obs_module_text("RateControl")
  102. #define TEXT_BITRATE obs_module_text("Bitrate")
  103. #define TEXT_CUSTOM_BUF obs_module_text("CustomBufsize")
  104. #define TEXT_BUF_SIZE obs_module_text("BufferSize")
  105. #define TEXT_VFR obs_module_text("VFR")
  106. #define TEXT_CRF obs_module_text("CRF")
  107. #define TEXT_KEYINT_SEC obs_module_text("KeyframeIntervalSec")
  108. #define TEXT_PRESET obs_module_text("CPUPreset")
  109. #define TEXT_PROFILE obs_module_text("Profile")
  110. #define TEXT_TUNE obs_module_text("Tune")
  111. #define TEXT_NONE obs_module_text("None")
  112. #define TEXT_X264_OPTS obs_module_text("EncoderOptions")
  113. static bool use_bufsize_modified(obs_properties_t *ppts, obs_property_t *p,
  114. obs_data_t *settings)
  115. {
  116. bool use_bufsize = obs_data_get_bool(settings, "use_bufsize");
  117. const char *rc = obs_data_get_string(settings, "rate_control");
  118. bool rc_crf = astrcmpi(rc, "CRF") == 0;
  119. p = obs_properties_get(ppts, "buffer_size");
  120. obs_property_set_visible(p, use_bufsize && !rc_crf);
  121. return true;
  122. }
  123. static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
  124. obs_data_t *settings)
  125. {
  126. const char *rc = obs_data_get_string(settings, "rate_control");
  127. bool use_bufsize = obs_data_get_bool(settings, "use_bufsize");
  128. bool abr = astrcmpi(rc, "CBR") == 0 || astrcmpi(rc, "ABR") == 0;
  129. bool rc_crf = astrcmpi(rc, "CRF") == 0;
  130. p = obs_properties_get(ppts, "crf");
  131. obs_property_set_visible(p, !abr);
  132. p = obs_properties_get(ppts, "bitrate");
  133. obs_property_set_visible(p, !rc_crf);
  134. p = obs_properties_get(ppts, "use_bufsize");
  135. obs_property_set_visible(p, !rc_crf);
  136. p = obs_properties_get(ppts, "buffer_size");
  137. obs_property_set_visible(p, !rc_crf && use_bufsize);
  138. return true;
  139. }
  140. static obs_properties_t *obs_x264_props(void *unused)
  141. {
  142. UNUSED_PARAMETER(unused);
  143. obs_properties_t *props = obs_properties_create();
  144. obs_property_t *list;
  145. obs_property_t *p;
  146. obs_property_t *headers;
  147. list = obs_properties_add_list(props, "rate_control", TEXT_RATE_CONTROL,
  148. OBS_COMBO_TYPE_LIST,
  149. OBS_COMBO_FORMAT_STRING);
  150. obs_property_list_add_string(list, "CBR", "CBR");
  151. obs_property_list_add_string(list, "ABR", "ABR");
  152. obs_property_list_add_string(list, "VBR", "VBR");
  153. obs_property_list_add_string(list, "CRF", "CRF");
  154. obs_property_set_modified_callback(list, rate_control_modified);
  155. p = obs_properties_add_int(props, "bitrate", TEXT_BITRATE, 50, 10000000,
  156. 50);
  157. obs_property_int_set_suffix(p, " Kbps");
  158. p = obs_properties_add_bool(props, "use_bufsize", TEXT_CUSTOM_BUF);
  159. obs_property_set_modified_callback(p, use_bufsize_modified);
  160. obs_properties_add_int(props, "buffer_size", TEXT_BUF_SIZE, 0, 10000000,
  161. 1);
  162. obs_properties_add_int(props, "crf", TEXT_CRF, 0, 51, 1);
  163. p = obs_properties_add_int(props, "keyint_sec", TEXT_KEYINT_SEC, 0, 20,
  164. 1);
  165. obs_property_int_set_suffix(p, " s");
  166. list = obs_properties_add_list(props, "preset", TEXT_PRESET,
  167. OBS_COMBO_TYPE_LIST,
  168. OBS_COMBO_FORMAT_STRING);
  169. add_strings(list, x264_preset_names);
  170. list = obs_properties_add_list(props, "profile", TEXT_PROFILE,
  171. OBS_COMBO_TYPE_LIST,
  172. OBS_COMBO_FORMAT_STRING);
  173. obs_property_list_add_string(list, TEXT_NONE, "");
  174. obs_property_list_add_string(list, "baseline", "baseline");
  175. obs_property_list_add_string(list, "main", "main");
  176. obs_property_list_add_string(list, "high", "high");
  177. list = obs_properties_add_list(props, "tune", TEXT_TUNE,
  178. OBS_COMBO_TYPE_LIST,
  179. OBS_COMBO_FORMAT_STRING);
  180. obs_property_list_add_string(list, TEXT_NONE, "");
  181. add_strings(list, x264_tune_names);
  182. #ifdef ENABLE_VFR
  183. obs_properties_add_bool(props, "vfr", TEXT_VFR);
  184. #endif
  185. obs_properties_add_text(props, "x264opts", TEXT_X264_OPTS,
  186. OBS_TEXT_DEFAULT);
  187. headers = obs_properties_add_bool(props, "repeat_headers",
  188. "repeat_headers");
  189. obs_property_set_visible(headers, false);
  190. return props;
  191. }
  192. static bool getparam(const char *param, char **name, const char **value)
  193. {
  194. const char *assign;
  195. if (!param || !*param || (*param == '='))
  196. return false;
  197. assign = strchr(param, '=');
  198. if (!assign || !*assign || !*(assign + 1))
  199. return false;
  200. *name = bstrdup_n(param, assign - param);
  201. *value = assign + 1;
  202. return true;
  203. }
  204. static const char *validate(struct obs_x264 *obsx264, const char *val,
  205. const char *name, const char *const *list)
  206. {
  207. if (!val || !*val)
  208. return val;
  209. while (*list) {
  210. if (strcmp(val, *list) == 0)
  211. return val;
  212. list++;
  213. }
  214. warn("Invalid %s: %s", name, val);
  215. return NULL;
  216. }
  217. static void override_base_param(struct obs_x264 *obsx264,
  218. struct obs_option option, char **preset,
  219. char **profile, char **tune)
  220. {
  221. const char *name = option.name;
  222. const char *val = option.value;
  223. if (astrcmpi(name, "preset") == 0) {
  224. const char *valid_name =
  225. validate(obsx264, val, "preset", x264_preset_names);
  226. if (valid_name) {
  227. bfree(*preset);
  228. *preset = bstrdup(val);
  229. }
  230. } else if (astrcmpi(name, "profile") == 0) {
  231. const char *valid_name =
  232. validate(obsx264, val, "profile", x264_profile_names);
  233. if (valid_name) {
  234. bfree(*profile);
  235. *profile = bstrdup(val);
  236. }
  237. } else if (astrcmpi(name, "tune") == 0) {
  238. const char *valid_name =
  239. validate(obsx264, val, "tune", x264_tune_names);
  240. if (valid_name) {
  241. bfree(*tune);
  242. *tune = bstrdup(val);
  243. }
  244. }
  245. }
  246. static inline void override_base_params(struct obs_x264 *obsx264,
  247. const struct obs_options *options,
  248. char **preset, char **profile,
  249. char **tune)
  250. {
  251. for (size_t i = 0; i < options->count; ++i)
  252. override_base_param(obsx264, options->options[i], preset,
  253. profile, tune);
  254. }
  255. #define OPENCL_ALIAS "opencl_is_experimental_and_potentially_unstable"
  256. static inline void set_param(struct obs_x264 *obsx264, struct obs_option option)
  257. {
  258. const char *name = option.name;
  259. const char *val = option.value;
  260. if (strcmp(name, "preset") != 0 && strcmp(name, "profile") != 0 &&
  261. strcmp(name, "tune") != 0 && strcmp(name, "fps") != 0 &&
  262. strcmp(name, "force-cfr") != 0 && strcmp(name, "width") != 0 &&
  263. strcmp(name, "height") != 0 && strcmp(name, "opencl") != 0) {
  264. if (strcmp(option.name, OPENCL_ALIAS) == 0)
  265. name = "opencl";
  266. if (x264_param_parse(&obsx264->params, name, val) != 0)
  267. warn("x264 param: %s=%s failed", name, val);
  268. }
  269. }
  270. static inline void apply_x264_profile(struct obs_x264 *obsx264,
  271. const char *profile)
  272. {
  273. if (!obsx264->context && profile && *profile) {
  274. int ret = x264_param_apply_profile(&obsx264->params, profile);
  275. if (ret != 0)
  276. warn("Failed to set x264 profile '%s'", profile);
  277. }
  278. }
  279. static inline const char *validate_preset(struct obs_x264 *obsx264,
  280. const char *preset)
  281. {
  282. const char *new_preset =
  283. validate(obsx264, preset, "preset", x264_preset_names);
  284. return new_preset ? new_preset : "veryfast";
  285. }
  286. static bool reset_x264_params(struct obs_x264 *obsx264, const char *preset,
  287. const char *tune)
  288. {
  289. int ret = x264_param_default_preset(
  290. &obsx264->params, validate_preset(obsx264, preset),
  291. validate(obsx264, tune, "tune", x264_tune_names));
  292. return ret == 0;
  293. }
  294. static void log_x264(void *param, int level, const char *format, va_list args)
  295. {
  296. struct obs_x264 *obsx264 = param;
  297. char str[1024];
  298. vsnprintf(str, sizeof(str), format, args);
  299. info("%s", str);
  300. UNUSED_PARAMETER(level);
  301. }
  302. static inline int get_x264_cs_val(const char *const name,
  303. const char *const names[])
  304. {
  305. int idx = 0;
  306. do {
  307. if (strcmp(names[idx], name) == 0)
  308. return idx;
  309. } while (!!names[++idx]);
  310. return 0;
  311. }
  312. static void obs_x264_video_info(void *data, struct video_scale_info *info);
  313. enum rate_control {
  314. RATE_CONTROL_CBR,
  315. RATE_CONTROL_VBR,
  316. RATE_CONTROL_ABR,
  317. RATE_CONTROL_CRF
  318. };
  319. static void update_params(struct obs_x264 *obsx264, obs_data_t *settings,
  320. const struct obs_options *options, bool update)
  321. {
  322. video_t *video = obs_encoder_video(obsx264->encoder);
  323. const struct video_output_info *voi = video_output_get_info(video);
  324. struct video_scale_info info;
  325. info.format = voi->format;
  326. info.colorspace = voi->colorspace;
  327. info.range = voi->range;
  328. obs_x264_video_info(obsx264, &info);
  329. const char *rate_control =
  330. obs_data_get_string(settings, "rate_control");
  331. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  332. int buffer_size = (int)obs_data_get_int(settings, "buffer_size");
  333. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  334. int crf = (int)obs_data_get_int(settings, "crf");
  335. int width = (int)obs_encoder_get_width(obsx264->encoder);
  336. int height = (int)obs_encoder_get_height(obsx264->encoder);
  337. int bf = (int)obs_data_get_int(settings, "bf");
  338. bool use_bufsize = obs_data_get_bool(settings, "use_bufsize");
  339. bool cbr_override = obs_data_get_bool(settings, "cbr");
  340. enum rate_control rc;
  341. #ifdef ENABLE_VFR
  342. bool vfr = obs_data_get_bool(settings, "vfr");
  343. #endif
  344. /* XXX: "cbr" setting has been deprecated */
  345. if (cbr_override) {
  346. warn("\"cbr\" setting has been deprecated for all encoders! "
  347. "Please set \"rate_control\" to \"CBR\" instead. "
  348. "Forcing CBR mode. "
  349. "(Note to all: this is why you shouldn't use strings for "
  350. "common settings)");
  351. rate_control = "CBR";
  352. }
  353. if (astrcmpi(rate_control, "ABR") == 0) {
  354. rc = RATE_CONTROL_ABR;
  355. crf = 0;
  356. } else if (astrcmpi(rate_control, "VBR") == 0) {
  357. rc = RATE_CONTROL_VBR;
  358. } else if (astrcmpi(rate_control, "CRF") == 0) {
  359. rc = RATE_CONTROL_CRF;
  360. bitrate = 0;
  361. buffer_size = 0;
  362. } else { /* CBR */
  363. rc = RATE_CONTROL_CBR;
  364. crf = 0;
  365. }
  366. if (keyint_sec)
  367. obsx264->params.i_keyint_max =
  368. keyint_sec * voi->fps_num / voi->fps_den;
  369. if (!use_bufsize)
  370. buffer_size = bitrate;
  371. #ifdef ENABLE_VFR
  372. obsx264->params.b_vfr_input = vfr;
  373. #else
  374. obsx264->params.b_vfr_input = false;
  375. #endif
  376. obsx264->params.rc.i_vbv_max_bitrate = bitrate;
  377. obsx264->params.rc.i_vbv_buffer_size = buffer_size;
  378. obsx264->params.rc.i_bitrate = bitrate;
  379. obsx264->params.i_width = width;
  380. obsx264->params.i_height = height;
  381. obsx264->params.i_fps_num = voi->fps_num;
  382. obsx264->params.i_fps_den = voi->fps_den;
  383. obsx264->params.i_timebase_num = voi->fps_den;
  384. obsx264->params.i_timebase_den = voi->fps_num;
  385. obsx264->params.pf_log = log_x264;
  386. obsx264->params.p_log_private = obsx264;
  387. obsx264->params.i_log_level = X264_LOG_WARNING;
  388. if (obs_data_has_user_value(settings, "bf"))
  389. obsx264->params.i_bframe = bf;
  390. static const char *const smpte170m = "smpte170m";
  391. static const char *const bt709 = "bt709";
  392. const char *colorprim = bt709;
  393. const char *transfer = bt709;
  394. const char *colmatrix = bt709;
  395. switch (info.colorspace) {
  396. case VIDEO_CS_DEFAULT:
  397. case VIDEO_CS_709:
  398. colorprim = bt709;
  399. transfer = bt709;
  400. colmatrix = bt709;
  401. break;
  402. case VIDEO_CS_601:
  403. colorprim = smpte170m;
  404. transfer = smpte170m;
  405. colmatrix = smpte170m;
  406. break;
  407. case VIDEO_CS_SRGB:
  408. colorprim = bt709;
  409. transfer = "iec61966-2-1";
  410. colmatrix = bt709;
  411. }
  412. obsx264->params.vui.i_sar_height = 1;
  413. obsx264->params.vui.i_sar_width = 1;
  414. obsx264->params.vui.b_fullrange = info.range == VIDEO_RANGE_FULL;
  415. obsx264->params.vui.i_colorprim =
  416. get_x264_cs_val(colorprim, x264_colorprim_names);
  417. obsx264->params.vui.i_transfer =
  418. get_x264_cs_val(transfer, x264_transfer_names);
  419. obsx264->params.vui.i_colmatrix =
  420. get_x264_cs_val(colmatrix, x264_colmatrix_names);
  421. /* use the new filler method for CBR to allow real-time adjusting of
  422. * the bitrate */
  423. if (rc == RATE_CONTROL_CBR || rc == RATE_CONTROL_ABR) {
  424. obsx264->params.rc.i_rc_method = X264_RC_ABR;
  425. if (rc == RATE_CONTROL_CBR) {
  426. #if X264_BUILD >= 139
  427. obsx264->params.rc.b_filler = true;
  428. #else
  429. obsx264->params.i_nal_hrd = X264_NAL_HRD_CBR;
  430. #endif
  431. }
  432. } else {
  433. obsx264->params.rc.i_rc_method = X264_RC_CRF;
  434. obsx264->params.rc.f_rf_constant = (float)crf;
  435. }
  436. if (info.format == VIDEO_FORMAT_NV12)
  437. obsx264->params.i_csp = X264_CSP_NV12;
  438. else if (info.format == VIDEO_FORMAT_I420)
  439. obsx264->params.i_csp = X264_CSP_I420;
  440. else if (info.format == VIDEO_FORMAT_I444)
  441. obsx264->params.i_csp = X264_CSP_I444;
  442. else
  443. obsx264->params.i_csp = X264_CSP_NV12;
  444. for (size_t i = 0; i < options->ignored_word_count; ++i)
  445. warn("ignoring invalid x264 option: %s",
  446. options->ignored_words[i]);
  447. for (size_t i = 0; i < options->count; ++i)
  448. set_param(obsx264, options->options[i]);
  449. if (!update) {
  450. info("settings:\n"
  451. "\trate_control: %s\n"
  452. "\tbitrate: %d\n"
  453. "\tbuffer size: %d\n"
  454. "\tcrf: %d\n"
  455. "\tfps_num: %d\n"
  456. "\tfps_den: %d\n"
  457. "\twidth: %d\n"
  458. "\theight: %d\n"
  459. "\tkeyint: %d\n",
  460. rate_control, obsx264->params.rc.i_vbv_max_bitrate,
  461. obsx264->params.rc.i_vbv_buffer_size,
  462. (int)obsx264->params.rc.f_rf_constant, voi->fps_num,
  463. voi->fps_den, width, height, obsx264->params.i_keyint_max);
  464. }
  465. }
  466. static void log_custom_options(struct obs_x264 *obsx264,
  467. const struct obs_options *options)
  468. {
  469. if (options->count == 0) {
  470. return;
  471. }
  472. size_t settings_string_length = 0;
  473. for (size_t i = 0; i < options->count; ++i)
  474. settings_string_length += strlen(options->options[i].name) +
  475. strlen(options->options[i].value) + 5;
  476. size_t buffer_size = settings_string_length + 1;
  477. char *settings_string = bmalloc(settings_string_length + 1);
  478. char *p = settings_string;
  479. size_t remaining_buffer_size = buffer_size;
  480. for (size_t i = 0; i < options->count; ++i) {
  481. int chars_written = snprintf(p, remaining_buffer_size,
  482. "\n\t%s = %s",
  483. options->options[i].name,
  484. options->options[i].value);
  485. assert(chars_written >= 0);
  486. assert((size_t)chars_written <= remaining_buffer_size);
  487. p += chars_written;
  488. remaining_buffer_size -= chars_written;
  489. }
  490. assert(remaining_buffer_size == 1);
  491. assert(*p == '\0');
  492. info("custom settings: %s", settings_string);
  493. bfree(settings_string);
  494. }
  495. static bool update_settings(struct obs_x264 *obsx264, obs_data_t *settings,
  496. bool update)
  497. {
  498. char *preset = bstrdup(obs_data_get_string(settings, "preset"));
  499. char *profile = bstrdup(obs_data_get_string(settings, "profile"));
  500. char *tune = bstrdup(obs_data_get_string(settings, "tune"));
  501. struct obs_options options =
  502. obs_parse_options(obs_data_get_string(settings, "x264opts"));
  503. bool repeat_headers = obs_data_get_bool(settings, "repeat_headers");
  504. bool success = true;
  505. if (!update)
  506. blog(LOG_INFO, "---------------------------------");
  507. if (!obsx264->context) {
  508. override_base_params(obsx264, &options, &preset, &profile,
  509. &tune);
  510. if (preset && *preset)
  511. info("preset: %s", preset);
  512. if (profile && *profile)
  513. info("profile: %s", profile);
  514. if (tune && *tune)
  515. info("tune: %s", tune);
  516. success = reset_x264_params(obsx264, preset, tune);
  517. }
  518. if (repeat_headers) {
  519. obsx264->params.b_repeat_headers = 1;
  520. obsx264->params.b_annexb = 1;
  521. obsx264->params.b_aud = 1;
  522. }
  523. if (success) {
  524. update_params(obsx264, settings, &options, update);
  525. if (!update) {
  526. log_custom_options(obsx264, &options);
  527. }
  528. if (!obsx264->context)
  529. apply_x264_profile(obsx264, profile);
  530. }
  531. obs_free_options(options);
  532. bfree(preset);
  533. bfree(profile);
  534. bfree(tune);
  535. return success;
  536. }
  537. static bool obs_x264_update(void *data, obs_data_t *settings)
  538. {
  539. struct obs_x264 *obsx264 = data;
  540. bool success = update_settings(obsx264, settings, true);
  541. int ret;
  542. if (success) {
  543. ret = x264_encoder_reconfig(obsx264->context, &obsx264->params);
  544. if (ret != 0)
  545. warn("Failed to reconfigure: %d", ret);
  546. return ret == 0;
  547. }
  548. return false;
  549. }
  550. static void load_headers(struct obs_x264 *obsx264)
  551. {
  552. x264_nal_t *nals;
  553. int nal_count;
  554. DARRAY(uint8_t) header;
  555. DARRAY(uint8_t) sei;
  556. da_init(header);
  557. da_init(sei);
  558. x264_encoder_headers(obsx264->context, &nals, &nal_count);
  559. for (int i = 0; i < nal_count; i++) {
  560. x264_nal_t *nal = nals + i;
  561. if (nal->i_type == NAL_SEI)
  562. da_push_back_array(sei, nal->p_payload, nal->i_payload);
  563. else
  564. da_push_back_array(header, nal->p_payload,
  565. nal->i_payload);
  566. }
  567. obsx264->extra_data = header.array;
  568. obsx264->extra_data_size = header.num;
  569. obsx264->sei = sei.array;
  570. obsx264->sei_size = sei.num;
  571. }
  572. static void *obs_x264_create(obs_data_t *settings, obs_encoder_t *encoder)
  573. {
  574. video_t *video = obs_encoder_video(encoder);
  575. const struct video_output_info *voi = video_output_get_info(video);
  576. switch (voi->format) {
  577. case VIDEO_FORMAT_I010:
  578. case VIDEO_FORMAT_P010:
  579. obs_encoder_set_last_error(encoder,
  580. obs_module_text("10bitUnsupported"));
  581. warn_enc(encoder,
  582. "OBS does not support using x264 with 10-bit formats");
  583. return NULL;
  584. default:
  585. switch (voi->colorspace) {
  586. case VIDEO_CS_2100_PQ:
  587. case VIDEO_CS_2100_HLG:
  588. obs_encoder_set_last_error(
  589. encoder, obs_module_text("HdrUnsupported"));
  590. warn_enc(
  591. encoder,
  592. "OBS does not support using x264 with Rec. 2100");
  593. return NULL;
  594. }
  595. }
  596. struct obs_x264 *obsx264 = bzalloc(sizeof(struct obs_x264));
  597. obsx264->encoder = encoder;
  598. if (update_settings(obsx264, settings, false)) {
  599. obsx264->context = x264_encoder_open(&obsx264->params);
  600. if (obsx264->context == NULL)
  601. warn("x264 failed to load");
  602. else
  603. load_headers(obsx264);
  604. } else {
  605. warn("bad settings specified");
  606. }
  607. if (!obsx264->context) {
  608. bfree(obsx264);
  609. return NULL;
  610. }
  611. obsx264->performance_token =
  612. os_request_high_performance("x264 encoding");
  613. return obsx264;
  614. }
  615. static void parse_packet(struct obs_x264 *obsx264,
  616. struct encoder_packet *packet, x264_nal_t *nals,
  617. int nal_count, x264_picture_t *pic_out)
  618. {
  619. if (!nal_count)
  620. return;
  621. da_resize(obsx264->packet_data, 0);
  622. for (int i = 0; i < nal_count; i++) {
  623. x264_nal_t *nal = nals + i;
  624. da_push_back_array(obsx264->packet_data, nal->p_payload,
  625. nal->i_payload);
  626. }
  627. packet->data = obsx264->packet_data.array;
  628. packet->size = obsx264->packet_data.num;
  629. packet->type = OBS_ENCODER_VIDEO;
  630. packet->pts = pic_out->i_pts;
  631. packet->dts = pic_out->i_dts;
  632. packet->keyframe = pic_out->b_keyframe != 0;
  633. }
  634. static inline void init_pic_data(struct obs_x264 *obsx264, x264_picture_t *pic,
  635. struct encoder_frame *frame)
  636. {
  637. x264_picture_init(pic);
  638. pic->i_pts = frame->pts;
  639. pic->img.i_csp = obsx264->params.i_csp;
  640. if (obsx264->params.i_csp == X264_CSP_NV12)
  641. pic->img.i_plane = 2;
  642. else if (obsx264->params.i_csp == X264_CSP_I420)
  643. pic->img.i_plane = 3;
  644. else if (obsx264->params.i_csp == X264_CSP_I444)
  645. pic->img.i_plane = 3;
  646. for (int i = 0; i < pic->img.i_plane; i++) {
  647. pic->img.i_stride[i] = (int)frame->linesize[i];
  648. pic->img.plane[i] = frame->data[i];
  649. }
  650. }
  651. static bool obs_x264_encode(void *data, struct encoder_frame *frame,
  652. struct encoder_packet *packet,
  653. bool *received_packet)
  654. {
  655. struct obs_x264 *obsx264 = data;
  656. x264_nal_t *nals;
  657. int nal_count;
  658. int ret;
  659. x264_picture_t pic, pic_out;
  660. if (!frame || !packet || !received_packet)
  661. return false;
  662. if (frame)
  663. init_pic_data(obsx264, &pic, frame);
  664. ret = x264_encoder_encode(obsx264->context, &nals, &nal_count,
  665. (frame ? &pic : NULL), &pic_out);
  666. if (ret < 0) {
  667. warn("encode failed");
  668. return false;
  669. }
  670. *received_packet = (nal_count != 0);
  671. parse_packet(obsx264, packet, nals, nal_count, &pic_out);
  672. return true;
  673. }
  674. static bool obs_x264_extra_data(void *data, uint8_t **extra_data, size_t *size)
  675. {
  676. struct obs_x264 *obsx264 = data;
  677. if (!obsx264->context)
  678. return false;
  679. *extra_data = obsx264->extra_data;
  680. *size = obsx264->extra_data_size;
  681. return true;
  682. }
  683. static bool obs_x264_sei(void *data, uint8_t **sei, size_t *size)
  684. {
  685. struct obs_x264 *obsx264 = data;
  686. if (!obsx264->context)
  687. return false;
  688. *sei = obsx264->sei;
  689. *size = obsx264->sei_size;
  690. return true;
  691. }
  692. static inline bool valid_format(enum video_format format)
  693. {
  694. return format == VIDEO_FORMAT_I420 || format == VIDEO_FORMAT_NV12 ||
  695. format == VIDEO_FORMAT_I444;
  696. }
  697. static void obs_x264_video_info(void *data, struct video_scale_info *info)
  698. {
  699. struct obs_x264 *obsx264 = data;
  700. enum video_format pref_format;
  701. pref_format = obs_encoder_get_preferred_video_format(obsx264->encoder);
  702. if (!valid_format(pref_format)) {
  703. pref_format = valid_format(info->format) ? info->format
  704. : VIDEO_FORMAT_NV12;
  705. }
  706. info->format = pref_format;
  707. }
  708. struct obs_encoder_info obs_x264_encoder = {
  709. .id = "obs_x264",
  710. .type = OBS_ENCODER_VIDEO,
  711. .codec = "h264",
  712. .get_name = obs_x264_getname,
  713. .create = obs_x264_create,
  714. .destroy = obs_x264_destroy,
  715. .encode = obs_x264_encode,
  716. .update = obs_x264_update,
  717. .get_properties = obs_x264_props,
  718. .get_defaults = obs_x264_defaults,
  719. .get_extra_data = obs_x264_extra_data,
  720. .get_sei_data = obs_x264_sei,
  721. .get_video_info = obs_x264_video_info,
  722. .caps = OBS_ENCODER_CAP_DYN_BITRATE,
  723. };