chroma-key-filter.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #include <obs-module.h>
  2. #include <graphics/matrix4.h>
  3. #include <graphics/vec2.h>
  4. #include <graphics/vec4.h>
  5. /* clang-format off */
  6. #define SETTING_OPACITY "opacity"
  7. #define SETTING_CONTRAST "contrast"
  8. #define SETTING_BRIGHTNESS "brightness"
  9. #define SETTING_GAMMA "gamma"
  10. #define SETTING_COLOR_TYPE "key_color_type"
  11. #define SETTING_KEY_COLOR "key_color"
  12. #define SETTING_SIMILARITY "similarity"
  13. #define SETTING_SMOOTHNESS "smoothness"
  14. #define SETTING_SPILL "spill"
  15. #define TEXT_OPACITY obs_module_text("Opacity")
  16. #define TEXT_CONTRAST obs_module_text("Contrast")
  17. #define TEXT_BRIGHTNESS obs_module_text("Brightness")
  18. #define TEXT_GAMMA obs_module_text("Gamma")
  19. #define TEXT_COLOR_TYPE obs_module_text("KeyColorType")
  20. #define TEXT_KEY_COLOR obs_module_text("KeyColor")
  21. #define TEXT_SIMILARITY obs_module_text("Similarity")
  22. #define TEXT_SMOOTHNESS obs_module_text("Smoothness")
  23. #define TEXT_SPILL obs_module_text("ColorSpillReduction")
  24. /* clang-format on */
  25. struct chroma_key_filter_data {
  26. obs_source_t *context;
  27. gs_effect_t *effect;
  28. gs_eparam_t *color_param;
  29. gs_eparam_t *contrast_param;
  30. gs_eparam_t *brightness_param;
  31. gs_eparam_t *gamma_param;
  32. gs_eparam_t *pixel_size_param;
  33. gs_eparam_t *chroma_param;
  34. gs_eparam_t *similarity_param;
  35. gs_eparam_t *smoothness_param;
  36. gs_eparam_t *spill_param;
  37. struct vec4 color;
  38. float contrast;
  39. float brightness;
  40. float gamma;
  41. struct vec2 chroma;
  42. float similarity;
  43. float smoothness;
  44. float spill;
  45. };
  46. struct chroma_key_filter_data_v2 {
  47. obs_source_t *context;
  48. gs_effect_t *effect;
  49. gs_eparam_t *opacity_param;
  50. gs_eparam_t *contrast_param;
  51. gs_eparam_t *brightness_param;
  52. gs_eparam_t *gamma_param;
  53. gs_eparam_t *pixel_size_param;
  54. gs_eparam_t *chroma_param;
  55. gs_eparam_t *similarity_param;
  56. gs_eparam_t *smoothness_param;
  57. gs_eparam_t *spill_param;
  58. float opacity;
  59. float contrast;
  60. float brightness;
  61. float gamma;
  62. struct vec2 chroma;
  63. float similarity;
  64. float smoothness;
  65. float spill;
  66. };
  67. static const char *chroma_key_name(void *unused)
  68. {
  69. UNUSED_PARAMETER(unused);
  70. return obs_module_text("ChromaKeyFilter");
  71. }
  72. static const float cb_vec[] = {-0.100644f, -0.338572f, 0.439216f, 0.501961f};
  73. static const float cr_vec[] = {0.439216f, -0.398942f, -0.040274f, 0.501961f};
  74. static inline void
  75. color_settings_update_v1(struct chroma_key_filter_data *filter,
  76. obs_data_t *settings)
  77. {
  78. uint32_t opacity =
  79. (uint32_t)obs_data_get_int(settings, SETTING_OPACITY);
  80. uint32_t color = 0xFFFFFF | (((opacity * 255) / 100) << 24);
  81. double contrast = obs_data_get_double(settings, SETTING_CONTRAST);
  82. double brightness = obs_data_get_double(settings, SETTING_BRIGHTNESS);
  83. double gamma = obs_data_get_double(settings, SETTING_GAMMA);
  84. contrast = (contrast < 0.0) ? (1.0 / (-contrast + 1.0))
  85. : (contrast + 1.0);
  86. brightness *= 0.5;
  87. gamma = (gamma < 0.0) ? (-gamma + 1.0) : (1.0 / (gamma + 1.0));
  88. filter->contrast = (float)contrast;
  89. filter->brightness = (float)brightness;
  90. filter->gamma = (float)gamma;
  91. vec4_from_rgba(&filter->color, color);
  92. }
  93. static inline void
  94. color_settings_update_v2(struct chroma_key_filter_data_v2 *filter,
  95. obs_data_t *settings)
  96. {
  97. filter->opacity =
  98. (float)obs_data_get_int(settings, SETTING_OPACITY) * 0.01f;
  99. double contrast = obs_data_get_double(settings, SETTING_CONTRAST);
  100. contrast = (contrast < 0.0) ? (1.0 / (-contrast + 1.0))
  101. : (contrast + 1.0);
  102. filter->contrast = (float)contrast;
  103. filter->brightness =
  104. (float)obs_data_get_double(settings, SETTING_BRIGHTNESS);
  105. double gamma = obs_data_get_double(settings, SETTING_GAMMA);
  106. gamma = (gamma < 0.0) ? (-gamma + 1.0) : (1.0 / (gamma + 1.0));
  107. filter->gamma = (float)gamma;
  108. }
  109. static inline void
  110. chroma_settings_update_v1(struct chroma_key_filter_data *filter,
  111. obs_data_t *settings)
  112. {
  113. int64_t similarity = obs_data_get_int(settings, SETTING_SIMILARITY);
  114. int64_t smoothness = obs_data_get_int(settings, SETTING_SMOOTHNESS);
  115. int64_t spill = obs_data_get_int(settings, SETTING_SPILL);
  116. uint32_t key_color =
  117. (uint32_t)obs_data_get_int(settings, SETTING_KEY_COLOR);
  118. const char *key_type =
  119. obs_data_get_string(settings, SETTING_COLOR_TYPE);
  120. struct vec4 key_rgb;
  121. struct vec4 cb_v4;
  122. struct vec4 cr_v4;
  123. if (strcmp(key_type, "green") == 0)
  124. key_color = 0x00FF00;
  125. else if (strcmp(key_type, "blue") == 0)
  126. key_color = 0xFF9900;
  127. else if (strcmp(key_type, "magenta") == 0)
  128. key_color = 0xFF00FF;
  129. vec4_from_rgba(&key_rgb, key_color | 0xFF000000);
  130. memcpy(&cb_v4, cb_vec, sizeof(cb_v4));
  131. memcpy(&cr_v4, cr_vec, sizeof(cr_v4));
  132. filter->chroma.x = vec4_dot(&key_rgb, &cb_v4);
  133. filter->chroma.y = vec4_dot(&key_rgb, &cr_v4);
  134. filter->similarity = (float)similarity / 1000.0f;
  135. filter->smoothness = (float)smoothness / 1000.0f;
  136. filter->spill = (float)spill / 1000.0f;
  137. }
  138. static inline void
  139. chroma_settings_update_v2(struct chroma_key_filter_data_v2 *filter,
  140. obs_data_t *settings)
  141. {
  142. int64_t similarity = obs_data_get_int(settings, SETTING_SIMILARITY);
  143. int64_t smoothness = obs_data_get_int(settings, SETTING_SMOOTHNESS);
  144. int64_t spill = obs_data_get_int(settings, SETTING_SPILL);
  145. uint32_t key_color =
  146. (uint32_t)obs_data_get_int(settings, SETTING_KEY_COLOR);
  147. const char *key_type =
  148. obs_data_get_string(settings, SETTING_COLOR_TYPE);
  149. struct vec4 key_rgb;
  150. struct vec4 cb_v4;
  151. struct vec4 cr_v4;
  152. if (strcmp(key_type, "green") == 0)
  153. key_color = 0x00FF00;
  154. else if (strcmp(key_type, "blue") == 0)
  155. key_color = 0xFF9900;
  156. else if (strcmp(key_type, "magenta") == 0)
  157. key_color = 0xFF00FF;
  158. vec4_from_rgba(&key_rgb, key_color | 0xFF000000);
  159. memcpy(&cb_v4, cb_vec, sizeof(cb_v4));
  160. memcpy(&cr_v4, cr_vec, sizeof(cr_v4));
  161. filter->chroma.x = vec4_dot(&key_rgb, &cb_v4);
  162. filter->chroma.y = vec4_dot(&key_rgb, &cr_v4);
  163. filter->similarity = (float)similarity / 1000.0f;
  164. filter->smoothness = (float)smoothness / 1000.0f;
  165. filter->spill = (float)spill / 1000.0f;
  166. }
  167. static void chroma_key_update_v1(void *data, obs_data_t *settings)
  168. {
  169. struct chroma_key_filter_data *filter = data;
  170. color_settings_update_v1(filter, settings);
  171. chroma_settings_update_v1(filter, settings);
  172. }
  173. static void chroma_key_update_v2(void *data, obs_data_t *settings)
  174. {
  175. struct chroma_key_filter_data_v2 *filter = data;
  176. color_settings_update_v2(filter, settings);
  177. chroma_settings_update_v2(filter, settings);
  178. }
  179. static void chroma_key_destroy_v1(void *data)
  180. {
  181. struct chroma_key_filter_data *filter = data;
  182. if (filter->effect) {
  183. obs_enter_graphics();
  184. gs_effect_destroy(filter->effect);
  185. obs_leave_graphics();
  186. }
  187. bfree(data);
  188. }
  189. static void chroma_key_destroy_v2(void *data)
  190. {
  191. struct chroma_key_filter_data_v2 *filter = data;
  192. if (filter->effect) {
  193. obs_enter_graphics();
  194. gs_effect_destroy(filter->effect);
  195. obs_leave_graphics();
  196. }
  197. bfree(data);
  198. }
  199. static void *chroma_key_create_v1(obs_data_t *settings, obs_source_t *context)
  200. {
  201. struct chroma_key_filter_data *filter =
  202. bzalloc(sizeof(struct chroma_key_filter_data));
  203. char *effect_path = obs_module_file("chroma_key_filter.effect");
  204. filter->context = context;
  205. obs_enter_graphics();
  206. filter->effect = gs_effect_create_from_file(effect_path, NULL);
  207. if (filter->effect) {
  208. filter->color_param =
  209. gs_effect_get_param_by_name(filter->effect, "color");
  210. filter->contrast_param =
  211. gs_effect_get_param_by_name(filter->effect, "contrast");
  212. filter->brightness_param = gs_effect_get_param_by_name(
  213. filter->effect, "brightness");
  214. filter->gamma_param =
  215. gs_effect_get_param_by_name(filter->effect, "gamma");
  216. filter->chroma_param = gs_effect_get_param_by_name(
  217. filter->effect, "chroma_key");
  218. filter->pixel_size_param = gs_effect_get_param_by_name(
  219. filter->effect, "pixel_size");
  220. filter->similarity_param = gs_effect_get_param_by_name(
  221. filter->effect, "similarity");
  222. filter->smoothness_param = gs_effect_get_param_by_name(
  223. filter->effect, "smoothness");
  224. filter->spill_param =
  225. gs_effect_get_param_by_name(filter->effect, "spill");
  226. }
  227. obs_leave_graphics();
  228. bfree(effect_path);
  229. if (!filter->effect) {
  230. chroma_key_destroy_v1(filter);
  231. return NULL;
  232. }
  233. chroma_key_update_v1(filter, settings);
  234. return filter;
  235. }
  236. static void *chroma_key_create_v2(obs_data_t *settings, obs_source_t *context)
  237. {
  238. struct chroma_key_filter_data_v2 *filter =
  239. bzalloc(sizeof(struct chroma_key_filter_data_v2));
  240. char *effect_path = obs_module_file("chroma_key_filter_v2.effect");
  241. filter->context = context;
  242. obs_enter_graphics();
  243. filter->effect = gs_effect_create_from_file(effect_path, NULL);
  244. if (filter->effect) {
  245. filter->opacity_param =
  246. gs_effect_get_param_by_name(filter->effect, "opacity");
  247. filter->contrast_param =
  248. gs_effect_get_param_by_name(filter->effect, "contrast");
  249. filter->brightness_param = gs_effect_get_param_by_name(
  250. filter->effect, "brightness");
  251. filter->gamma_param =
  252. gs_effect_get_param_by_name(filter->effect, "gamma");
  253. filter->chroma_param = gs_effect_get_param_by_name(
  254. filter->effect, "chroma_key");
  255. filter->pixel_size_param = gs_effect_get_param_by_name(
  256. filter->effect, "pixel_size");
  257. filter->similarity_param = gs_effect_get_param_by_name(
  258. filter->effect, "similarity");
  259. filter->smoothness_param = gs_effect_get_param_by_name(
  260. filter->effect, "smoothness");
  261. filter->spill_param =
  262. gs_effect_get_param_by_name(filter->effect, "spill");
  263. }
  264. obs_leave_graphics();
  265. bfree(effect_path);
  266. if (!filter->effect) {
  267. chroma_key_destroy_v2(filter);
  268. return NULL;
  269. }
  270. chroma_key_update_v2(filter, settings);
  271. return filter;
  272. }
  273. static void chroma_key_render_v1(void *data, gs_effect_t *effect)
  274. {
  275. struct chroma_key_filter_data *filter = data;
  276. obs_source_t *target = obs_filter_get_target(filter->context);
  277. uint32_t width = obs_source_get_base_width(target);
  278. uint32_t height = obs_source_get_base_height(target);
  279. struct vec2 pixel_size;
  280. if (!obs_source_process_filter_begin(filter->context, GS_RGBA,
  281. OBS_ALLOW_DIRECT_RENDERING))
  282. return;
  283. vec2_set(&pixel_size, 1.0f / (float)width, 1.0f / (float)height);
  284. gs_effect_set_vec4(filter->color_param, &filter->color);
  285. gs_effect_set_float(filter->contrast_param, filter->contrast);
  286. gs_effect_set_float(filter->brightness_param, filter->brightness);
  287. gs_effect_set_float(filter->gamma_param, filter->gamma);
  288. gs_effect_set_vec2(filter->chroma_param, &filter->chroma);
  289. gs_effect_set_vec2(filter->pixel_size_param, &pixel_size);
  290. gs_effect_set_float(filter->similarity_param, filter->similarity);
  291. gs_effect_set_float(filter->smoothness_param, filter->smoothness);
  292. gs_effect_set_float(filter->spill_param, filter->spill);
  293. obs_source_process_filter_end(filter->context, filter->effect, 0, 0);
  294. UNUSED_PARAMETER(effect);
  295. }
  296. static void chroma_key_render_v2(void *data, gs_effect_t *effect)
  297. {
  298. struct chroma_key_filter_data_v2 *filter = data;
  299. obs_source_t *target = obs_filter_get_target(filter->context);
  300. uint32_t width = obs_source_get_base_width(target);
  301. uint32_t height = obs_source_get_base_height(target);
  302. struct vec2 pixel_size;
  303. if (!obs_source_process_filter_begin(filter->context, GS_RGBA,
  304. OBS_ALLOW_DIRECT_RENDERING))
  305. return;
  306. vec2_set(&pixel_size, 1.0f / (float)width, 1.0f / (float)height);
  307. gs_effect_set_float(filter->opacity_param, filter->opacity);
  308. gs_effect_set_float(filter->contrast_param, filter->contrast);
  309. gs_effect_set_float(filter->brightness_param, filter->brightness);
  310. gs_effect_set_float(filter->gamma_param, filter->gamma);
  311. gs_effect_set_vec2(filter->chroma_param, &filter->chroma);
  312. gs_effect_set_vec2(filter->pixel_size_param, &pixel_size);
  313. gs_effect_set_float(filter->similarity_param, filter->similarity);
  314. gs_effect_set_float(filter->smoothness_param, filter->smoothness);
  315. gs_effect_set_float(filter->spill_param, filter->spill);
  316. obs_source_process_filter_end_srgb(filter->context, filter->effect, 0,
  317. 0);
  318. UNUSED_PARAMETER(effect);
  319. }
  320. static bool key_type_changed(obs_properties_t *props, obs_property_t *p,
  321. obs_data_t *settings)
  322. {
  323. const char *type = obs_data_get_string(settings, SETTING_COLOR_TYPE);
  324. bool custom = strcmp(type, "custom") == 0;
  325. obs_property_set_visible(obs_properties_get(props, SETTING_KEY_COLOR),
  326. custom);
  327. UNUSED_PARAMETER(p);
  328. return true;
  329. }
  330. static obs_properties_t *chroma_key_properties_v1(void *data)
  331. {
  332. obs_properties_t *props = obs_properties_create();
  333. obs_property_t *p = obs_properties_add_list(props, SETTING_COLOR_TYPE,
  334. TEXT_COLOR_TYPE,
  335. OBS_COMBO_TYPE_LIST,
  336. OBS_COMBO_FORMAT_STRING);
  337. obs_property_list_add_string(p, obs_module_text("Green"), "green");
  338. obs_property_list_add_string(p, obs_module_text("Blue"), "blue");
  339. obs_property_list_add_string(p, obs_module_text("Magenta"), "magenta");
  340. obs_property_list_add_string(p, obs_module_text("Custom"), "custom");
  341. obs_property_set_modified_callback(p, key_type_changed);
  342. obs_properties_add_color(props, SETTING_KEY_COLOR, TEXT_KEY_COLOR);
  343. obs_properties_add_int_slider(props, SETTING_SIMILARITY,
  344. TEXT_SIMILARITY, 1, 1000, 1);
  345. obs_properties_add_int_slider(props, SETTING_SMOOTHNESS,
  346. TEXT_SMOOTHNESS, 1, 1000, 1);
  347. obs_properties_add_int_slider(props, SETTING_SPILL, TEXT_SPILL, 1, 1000,
  348. 1);
  349. obs_properties_add_int_slider(props, SETTING_OPACITY, TEXT_OPACITY, 0,
  350. 100, 1);
  351. obs_properties_add_float_slider(props, SETTING_CONTRAST, TEXT_CONTRAST,
  352. -1.0, 1.0, 0.01);
  353. obs_properties_add_float_slider(props, SETTING_BRIGHTNESS,
  354. TEXT_BRIGHTNESS, -1.0, 1.0, 0.01);
  355. obs_properties_add_float_slider(props, SETTING_GAMMA, TEXT_GAMMA, -1.0,
  356. 1.0, 0.01);
  357. UNUSED_PARAMETER(data);
  358. return props;
  359. }
  360. static obs_properties_t *chroma_key_properties_v2(void *data)
  361. {
  362. obs_properties_t *props = obs_properties_create();
  363. obs_property_t *p = obs_properties_add_list(props, SETTING_COLOR_TYPE,
  364. TEXT_COLOR_TYPE,
  365. OBS_COMBO_TYPE_LIST,
  366. OBS_COMBO_FORMAT_STRING);
  367. obs_property_list_add_string(p, obs_module_text("Green"), "green");
  368. obs_property_list_add_string(p, obs_module_text("Blue"), "blue");
  369. obs_property_list_add_string(p, obs_module_text("Magenta"), "magenta");
  370. obs_property_list_add_string(p, obs_module_text("Custom"), "custom");
  371. obs_property_set_modified_callback(p, key_type_changed);
  372. obs_properties_add_color(props, SETTING_KEY_COLOR, TEXT_KEY_COLOR);
  373. obs_properties_add_int_slider(props, SETTING_SIMILARITY,
  374. TEXT_SIMILARITY, 1, 1000, 1);
  375. obs_properties_add_int_slider(props, SETTING_SMOOTHNESS,
  376. TEXT_SMOOTHNESS, 1, 1000, 1);
  377. obs_properties_add_int_slider(props, SETTING_SPILL, TEXT_SPILL, 1, 1000,
  378. 1);
  379. obs_properties_add_int_slider(props, SETTING_OPACITY, TEXT_OPACITY, 0,
  380. 100, 1);
  381. obs_properties_add_float_slider(props, SETTING_CONTRAST, TEXT_CONTRAST,
  382. -4.0, 4.0, 0.01);
  383. obs_properties_add_float_slider(props, SETTING_BRIGHTNESS,
  384. TEXT_BRIGHTNESS, -1.0, 1.0, 0.0001);
  385. obs_properties_add_float_slider(props, SETTING_GAMMA, TEXT_GAMMA, -1.0,
  386. 1.0, 0.01);
  387. UNUSED_PARAMETER(data);
  388. return props;
  389. }
  390. static void chroma_key_defaults(obs_data_t *settings)
  391. {
  392. obs_data_set_default_int(settings, SETTING_OPACITY, 100);
  393. obs_data_set_default_double(settings, SETTING_CONTRAST, 0.0);
  394. obs_data_set_default_double(settings, SETTING_BRIGHTNESS, 0.0);
  395. obs_data_set_default_double(settings, SETTING_GAMMA, 0.0);
  396. obs_data_set_default_int(settings, SETTING_KEY_COLOR, 0x00FF00);
  397. obs_data_set_default_string(settings, SETTING_COLOR_TYPE, "green");
  398. obs_data_set_default_int(settings, SETTING_SIMILARITY, 400);
  399. obs_data_set_default_int(settings, SETTING_SMOOTHNESS, 80);
  400. obs_data_set_default_int(settings, SETTING_SPILL, 100);
  401. }
  402. struct obs_source_info chroma_key_filter = {
  403. .id = "chroma_key_filter",
  404. .type = OBS_SOURCE_TYPE_FILTER,
  405. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CAP_OBSOLETE,
  406. .get_name = chroma_key_name,
  407. .create = chroma_key_create_v1,
  408. .destroy = chroma_key_destroy_v1,
  409. .video_render = chroma_key_render_v1,
  410. .update = chroma_key_update_v1,
  411. .get_properties = chroma_key_properties_v1,
  412. .get_defaults = chroma_key_defaults,
  413. };
  414. struct obs_source_info chroma_key_filter_v2 = {
  415. .id = "chroma_key_filter",
  416. .version = 2,
  417. .type = OBS_SOURCE_TYPE_FILTER,
  418. .output_flags = OBS_SOURCE_VIDEO,
  419. .get_name = chroma_key_name,
  420. .create = chroma_key_create_v2,
  421. .destroy = chroma_key_destroy_v2,
  422. .video_render = chroma_key_render_v2,
  423. .update = chroma_key_update_v2,
  424. .get_properties = chroma_key_properties_v2,
  425. .get_defaults = chroma_key_defaults,
  426. };