scale-filter.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <util/dstr.h>
  4. #include <obs-module.h>
  5. #include <util/platform.h>
  6. #include <graphics/vec2.h>
  7. #include <graphics/math-defs.h>
  8. /* clang-format off */
  9. #define S_RESOLUTION "resolution"
  10. #define S_SAMPLING "sampling"
  11. #define S_UNDISTORT "undistort"
  12. #define T_RESOLUTION obs_module_text("Resolution")
  13. #define T_NONE obs_module_text("None")
  14. #define T_SAMPLING obs_module_text("ScaleFiltering")
  15. #define T_SAMPLING_POINT obs_module_text("ScaleFiltering.Point")
  16. #define T_SAMPLING_BILINEAR obs_module_text("ScaleFiltering.Bilinear")
  17. #define T_SAMPLING_BICUBIC obs_module_text("ScaleFiltering.Bicubic")
  18. #define T_SAMPLING_LANCZOS obs_module_text("ScaleFiltering.Lanczos")
  19. #define T_SAMPLING_AREA obs_module_text("ScaleFiltering.Area")
  20. #define T_UNDISTORT obs_module_text("UndistortCenter")
  21. #define T_BASE obs_module_text("Base.Canvas")
  22. #define S_SAMPLING_POINT "point"
  23. #define S_SAMPLING_BILINEAR "bilinear"
  24. #define S_SAMPLING_BICUBIC "bicubic"
  25. #define S_SAMPLING_LANCZOS "lanczos"
  26. #define S_SAMPLING_AREA "area"
  27. /* clang-format on */
  28. struct scale_filter_data {
  29. obs_source_t *context;
  30. gs_effect_t *effect;
  31. gs_eparam_t *image_param;
  32. gs_eparam_t *dimension_param;
  33. gs_eparam_t *dimension_i_param;
  34. gs_eparam_t *undistort_factor_param;
  35. gs_eparam_t *multiplier_param;
  36. struct vec2 dimension;
  37. struct vec2 dimension_i;
  38. double undistort_factor;
  39. int cx_in;
  40. int cy_in;
  41. int cx_out;
  42. int cy_out;
  43. enum obs_scale_type sampling;
  44. gs_samplerstate_t *point_sampler;
  45. bool aspect_ratio_only;
  46. bool target_valid;
  47. bool valid;
  48. bool can_undistort;
  49. bool undistort;
  50. bool upscale;
  51. bool base_canvas_resolution;
  52. };
  53. static const char *scale_filter_name(void *unused)
  54. {
  55. UNUSED_PARAMETER(unused);
  56. return obs_module_text("ScaleFilter");
  57. }
  58. static void scale_filter_update(void *data, obs_data_t *settings)
  59. {
  60. struct scale_filter_data *filter = data;
  61. int ret;
  62. const char *res_str = obs_data_get_string(settings, S_RESOLUTION);
  63. const char *sampling = obs_data_get_string(settings, S_SAMPLING);
  64. filter->valid = true;
  65. filter->base_canvas_resolution = false;
  66. if (strcmp(res_str, T_BASE) == 0) {
  67. struct obs_video_info ovi;
  68. obs_get_video_info(&ovi);
  69. filter->aspect_ratio_only = false;
  70. filter->base_canvas_resolution = true;
  71. filter->cx_in = ovi.base_width;
  72. filter->cy_in = ovi.base_height;
  73. } else {
  74. ret = sscanf(res_str, "%dx%d", &filter->cx_in, &filter->cy_in);
  75. if (ret == 2) {
  76. filter->aspect_ratio_only = false;
  77. } else {
  78. ret = sscanf(res_str, "%d:%d", &filter->cx_in, &filter->cy_in);
  79. if (ret != 2) {
  80. filter->valid = false;
  81. return;
  82. }
  83. filter->aspect_ratio_only = true;
  84. }
  85. }
  86. if (astrcmpi(sampling, S_SAMPLING_POINT) == 0) {
  87. filter->sampling = OBS_SCALE_POINT;
  88. } else if (astrcmpi(sampling, S_SAMPLING_BILINEAR) == 0) {
  89. filter->sampling = OBS_SCALE_BILINEAR;
  90. } else if (astrcmpi(sampling, S_SAMPLING_LANCZOS) == 0) {
  91. filter->sampling = OBS_SCALE_LANCZOS;
  92. } else if (astrcmpi(sampling, S_SAMPLING_AREA) == 0) {
  93. filter->sampling = OBS_SCALE_AREA;
  94. } else { /* S_SAMPLING_BICUBIC */
  95. filter->sampling = OBS_SCALE_BICUBIC;
  96. }
  97. filter->can_undistort = obs_data_get_bool(settings, S_UNDISTORT);
  98. }
  99. static void scale_filter_destroy(void *data)
  100. {
  101. struct scale_filter_data *filter = data;
  102. obs_enter_graphics();
  103. gs_samplerstate_destroy(filter->point_sampler);
  104. obs_leave_graphics();
  105. bfree(data);
  106. }
  107. static void *scale_filter_create(obs_data_t *settings, obs_source_t *context)
  108. {
  109. struct scale_filter_data *filter = bzalloc(sizeof(struct scale_filter_data));
  110. struct gs_sampler_info sampler_info = {0};
  111. filter->context = context;
  112. obs_enter_graphics();
  113. filter->point_sampler = gs_samplerstate_create(&sampler_info);
  114. obs_leave_graphics();
  115. scale_filter_update(filter, settings);
  116. return filter;
  117. }
  118. static void scale_filter_tick(void *data, float seconds)
  119. {
  120. struct scale_filter_data *filter = data;
  121. enum obs_base_effect type;
  122. obs_source_t *target;
  123. bool lower_than_2x;
  124. double cx_f;
  125. double cy_f;
  126. int cx;
  127. int cy;
  128. if (filter->base_canvas_resolution) {
  129. struct obs_video_info ovi;
  130. obs_get_video_info(&ovi);
  131. filter->cx_in = ovi.base_width;
  132. filter->cy_in = ovi.base_height;
  133. }
  134. target = obs_filter_get_target(filter->context);
  135. filter->cx_out = 0;
  136. filter->cy_out = 0;
  137. filter->target_valid = !!target;
  138. if (!filter->target_valid)
  139. return;
  140. cx = obs_source_get_base_width(target);
  141. cy = obs_source_get_base_height(target);
  142. if (!cx || !cy) {
  143. filter->target_valid = false;
  144. return;
  145. }
  146. filter->cx_out = cx;
  147. filter->cy_out = cy;
  148. if (!filter->valid)
  149. return;
  150. /* ------------------------- */
  151. cx_f = (double)cx;
  152. cy_f = (double)cy;
  153. double old_aspect = cx_f / cy_f;
  154. double new_aspect = (double)filter->cx_in / (double)filter->cy_in;
  155. if (filter->aspect_ratio_only) {
  156. if (fabs(old_aspect - new_aspect) <= EPSILON) {
  157. filter->target_valid = false;
  158. return;
  159. } else {
  160. if (new_aspect > old_aspect) {
  161. filter->cx_out = (int)(cy_f * new_aspect);
  162. } else {
  163. filter->cy_out = (int)(cx_f / new_aspect);
  164. }
  165. }
  166. } else {
  167. filter->cx_out = filter->cx_in;
  168. filter->cy_out = filter->cy_in;
  169. }
  170. vec2_set(&filter->dimension, (float)cx, (float)cy);
  171. vec2_set(&filter->dimension_i, 1.0f / (float)cx, 1.0f / (float)cy);
  172. filter->undistort = false;
  173. filter->upscale = false;
  174. /* ------------------------- */
  175. lower_than_2x = filter->cx_out < cx / 2 || filter->cy_out < cy / 2;
  176. if (lower_than_2x && filter->sampling != OBS_SCALE_POINT) {
  177. type = OBS_EFFECT_BILINEAR_LOWRES;
  178. } else {
  179. switch (filter->sampling) {
  180. default:
  181. case OBS_SCALE_POINT:
  182. case OBS_SCALE_BILINEAR:
  183. type = OBS_EFFECT_DEFAULT;
  184. break;
  185. case OBS_SCALE_BICUBIC:
  186. type = OBS_EFFECT_BICUBIC;
  187. filter->undistort = filter->can_undistort;
  188. break;
  189. case OBS_SCALE_LANCZOS:
  190. type = OBS_EFFECT_LANCZOS;
  191. filter->undistort = filter->can_undistort;
  192. break;
  193. case OBS_SCALE_AREA:
  194. type = OBS_EFFECT_AREA;
  195. if ((filter->cx_out >= cx) && (filter->cy_out >= cy))
  196. filter->upscale = true;
  197. break;
  198. }
  199. }
  200. filter->undistort_factor = filter->undistort ? (new_aspect / old_aspect) : 1.0;
  201. filter->effect = obs_get_base_effect(type);
  202. filter->image_param = gs_effect_get_param_by_name(filter->effect, "image");
  203. if (type != OBS_EFFECT_DEFAULT) {
  204. filter->dimension_param = gs_effect_get_param_by_name(filter->effect, "base_dimension");
  205. filter->dimension_i_param = gs_effect_get_param_by_name(filter->effect, "base_dimension_i");
  206. } else {
  207. filter->dimension_param = NULL;
  208. filter->dimension_i_param = NULL;
  209. }
  210. if (type == OBS_EFFECT_BICUBIC || type == OBS_EFFECT_LANCZOS) {
  211. filter->undistort_factor_param = gs_effect_get_param_by_name(filter->effect, "undistort_factor");
  212. } else {
  213. filter->undistort_factor_param = NULL;
  214. }
  215. filter->multiplier_param = gs_effect_get_param_by_name(filter->effect, "multiplier");
  216. UNUSED_PARAMETER(seconds);
  217. }
  218. static const char *get_tech_name_and_multiplier(const struct scale_filter_data *filter,
  219. enum gs_color_space current_space, enum gs_color_space source_space,
  220. float *multiplier)
  221. {
  222. *multiplier = 1.f;
  223. switch (source_space) {
  224. case GS_CS_SRGB:
  225. case GS_CS_SRGB_16F:
  226. case GS_CS_709_EXTENDED:
  227. if (current_space == GS_CS_709_SCRGB)
  228. *multiplier = obs_get_video_sdr_white_level() / 80.f;
  229. break;
  230. case GS_CS_709_SCRGB:
  231. switch (current_space) {
  232. case GS_CS_SRGB:
  233. case GS_CS_SRGB_16F:
  234. case GS_CS_709_EXTENDED:
  235. *multiplier = 80.f / obs_get_video_sdr_white_level();
  236. break;
  237. case GS_CS_709_SCRGB:
  238. break;
  239. }
  240. }
  241. const char *tech_name = "Draw";
  242. if (filter->undistort) {
  243. tech_name = "DrawUndistort";
  244. switch (source_space) {
  245. case GS_CS_SRGB:
  246. case GS_CS_SRGB_16F:
  247. if (current_space == GS_CS_709_SCRGB)
  248. tech_name = "DrawUndistortMultiply";
  249. break;
  250. case GS_CS_709_EXTENDED:
  251. switch (current_space) {
  252. case GS_CS_SRGB:
  253. case GS_CS_SRGB_16F:
  254. tech_name = "DrawUndistortTonemap";
  255. break;
  256. case GS_CS_709_SCRGB:
  257. tech_name = "DrawUndistortMultiply";
  258. break;
  259. case GS_CS_709_EXTENDED:
  260. break;
  261. }
  262. break;
  263. case GS_CS_709_SCRGB:
  264. switch (current_space) {
  265. case GS_CS_SRGB:
  266. case GS_CS_SRGB_16F:
  267. tech_name = "DrawUndistortMultiplyTonemap";
  268. break;
  269. case GS_CS_709_EXTENDED:
  270. tech_name = "DrawUndistortMultiply";
  271. break;
  272. case GS_CS_709_SCRGB:
  273. break;
  274. }
  275. }
  276. } else if (filter->upscale) {
  277. tech_name = "DrawUpscale";
  278. switch (source_space) {
  279. case GS_CS_SRGB:
  280. case GS_CS_SRGB_16F:
  281. if (current_space == GS_CS_709_SCRGB)
  282. tech_name = "DrawUpscaleMultiply";
  283. break;
  284. case GS_CS_709_EXTENDED:
  285. switch (current_space) {
  286. case GS_CS_SRGB:
  287. case GS_CS_SRGB_16F:
  288. tech_name = "DrawUpscaleTonemap";
  289. break;
  290. case GS_CS_709_SCRGB:
  291. tech_name = "DrawUpscaleMultiply";
  292. break;
  293. case GS_CS_709_EXTENDED:
  294. break;
  295. }
  296. break;
  297. case GS_CS_709_SCRGB:
  298. switch (current_space) {
  299. case GS_CS_SRGB:
  300. case GS_CS_SRGB_16F:
  301. tech_name = "DrawUpscaleMultiplyTonemap";
  302. break;
  303. case GS_CS_709_EXTENDED:
  304. tech_name = "DrawUpscaleMultiply";
  305. break;
  306. case GS_CS_709_SCRGB:
  307. break;
  308. }
  309. }
  310. } else {
  311. switch (source_space) {
  312. case GS_CS_SRGB:
  313. case GS_CS_SRGB_16F:
  314. if (current_space == GS_CS_709_SCRGB)
  315. tech_name = "DrawMultiply";
  316. break;
  317. case GS_CS_709_EXTENDED:
  318. switch (current_space) {
  319. case GS_CS_SRGB:
  320. case GS_CS_SRGB_16F:
  321. tech_name = "DrawTonemap";
  322. break;
  323. case GS_CS_709_SCRGB:
  324. tech_name = "DrawMultiply";
  325. break;
  326. case GS_CS_709_EXTENDED:
  327. break;
  328. }
  329. break;
  330. case GS_CS_709_SCRGB:
  331. switch (current_space) {
  332. case GS_CS_SRGB:
  333. case GS_CS_SRGB_16F:
  334. tech_name = "DrawMultiplyTonemap";
  335. break;
  336. case GS_CS_709_EXTENDED:
  337. tech_name = "DrawMultiply";
  338. break;
  339. case GS_CS_709_SCRGB:
  340. break;
  341. }
  342. }
  343. }
  344. return tech_name;
  345. }
  346. static void scale_filter_render(void *data, gs_effect_t *effect)
  347. {
  348. UNUSED_PARAMETER(effect);
  349. struct scale_filter_data *filter = data;
  350. if (!filter->valid || !filter->target_valid) {
  351. obs_source_skip_video_filter(filter->context);
  352. return;
  353. }
  354. const enum gs_color_space preferred_spaces[] = {
  355. GS_CS_SRGB,
  356. GS_CS_SRGB_16F,
  357. GS_CS_709_EXTENDED,
  358. };
  359. const enum gs_color_space source_space = obs_source_get_color_space(
  360. obs_filter_get_target(filter->context), OBS_COUNTOF(preferred_spaces), preferred_spaces);
  361. float multiplier;
  362. const char *technique = get_tech_name_and_multiplier(filter, gs_get_color_space(), source_space, &multiplier);
  363. const enum gs_color_format format = gs_get_format_from_space(source_space);
  364. if (obs_source_process_filter_begin_with_color_space(filter->context, format, source_space,
  365. OBS_NO_DIRECT_RENDERING)) {
  366. if (filter->dimension_param)
  367. gs_effect_set_vec2(filter->dimension_param, &filter->dimension);
  368. if (filter->dimension_i_param)
  369. gs_effect_set_vec2(filter->dimension_i_param, &filter->dimension_i);
  370. if (filter->undistort_factor_param)
  371. gs_effect_set_float(filter->undistort_factor_param, (float)filter->undistort_factor);
  372. if (filter->multiplier_param)
  373. gs_effect_set_float(filter->multiplier_param, multiplier);
  374. if (filter->sampling == OBS_SCALE_POINT)
  375. gs_effect_set_next_sampler(filter->image_param, filter->point_sampler);
  376. gs_blend_state_push();
  377. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  378. obs_source_process_filter_tech_end(filter->context, filter->effect, filter->cx_out, filter->cy_out,
  379. technique);
  380. gs_blend_state_pop();
  381. }
  382. }
  383. static const double downscale_vals[] = {1.0, 1.25, (1.0 / 0.75), 1.5, (1.0 / 0.6), 1.75, 2.0, 2.25, 2.5, 2.75, 3.0};
  384. #define NUM_DOWNSCALES (sizeof(downscale_vals) / sizeof(double))
  385. static const char *aspects[] = {"16:9", "16:10", "4:3", "1:1"};
  386. #define NUM_ASPECTS (sizeof(aspects) / sizeof(const char *))
  387. static bool sampling_modified(obs_properties_t *props, obs_property_t *p, obs_data_t *settings)
  388. {
  389. const char *sampling = obs_data_get_string(settings, S_SAMPLING);
  390. bool has_undistort;
  391. if (astrcmpi(sampling, S_SAMPLING_POINT) == 0) {
  392. has_undistort = false;
  393. } else if (astrcmpi(sampling, S_SAMPLING_BILINEAR) == 0) {
  394. has_undistort = false;
  395. } else if (astrcmpi(sampling, S_SAMPLING_LANCZOS) == 0) {
  396. has_undistort = true;
  397. } else if (astrcmpi(sampling, S_SAMPLING_AREA) == 0) {
  398. has_undistort = false;
  399. } else { /* S_SAMPLING_BICUBIC */
  400. has_undistort = true;
  401. }
  402. obs_property_set_visible(obs_properties_get(props, S_UNDISTORT), has_undistort);
  403. UNUSED_PARAMETER(p);
  404. return true;
  405. }
  406. static obs_properties_t *scale_filter_properties(void *data)
  407. {
  408. obs_properties_t *props = obs_properties_create();
  409. struct obs_video_info ovi;
  410. obs_property_t *p;
  411. uint32_t cx;
  412. uint32_t cy;
  413. struct {
  414. int cx;
  415. int cy;
  416. } downscales[NUM_DOWNSCALES];
  417. /* ----------------- */
  418. obs_get_video_info(&ovi);
  419. cx = ovi.base_width;
  420. cy = ovi.base_height;
  421. for (size_t i = 0; i < NUM_DOWNSCALES; i++) {
  422. downscales[i].cx = (int)((double)cx / downscale_vals[i]);
  423. downscales[i].cy = (int)((double)cy / downscale_vals[i]);
  424. }
  425. p = obs_properties_add_list(props, S_SAMPLING, T_SAMPLING, OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  426. obs_property_set_modified_callback(p, sampling_modified);
  427. obs_property_list_add_string(p, T_SAMPLING_POINT, S_SAMPLING_POINT);
  428. obs_property_list_add_string(p, T_SAMPLING_BILINEAR, S_SAMPLING_BILINEAR);
  429. obs_property_list_add_string(p, T_SAMPLING_BICUBIC, S_SAMPLING_BICUBIC);
  430. obs_property_list_add_string(p, T_SAMPLING_LANCZOS, S_SAMPLING_LANCZOS);
  431. obs_property_list_add_string(p, T_SAMPLING_AREA, S_SAMPLING_AREA);
  432. /* ----------------- */
  433. p = obs_properties_add_list(props, S_RESOLUTION, T_RESOLUTION, OBS_COMBO_TYPE_EDITABLE,
  434. OBS_COMBO_FORMAT_STRING);
  435. obs_property_list_add_string(p, T_NONE, T_NONE);
  436. obs_property_list_add_string(p, T_BASE, T_BASE);
  437. for (size_t i = 0; i < NUM_ASPECTS; i++)
  438. obs_property_list_add_string(p, aspects[i], aspects[i]);
  439. for (size_t i = 0; i < NUM_DOWNSCALES; i++) {
  440. char str[32];
  441. snprintf(str, sizeof(str), "%dx%d", downscales[i].cx, downscales[i].cy);
  442. obs_property_list_add_string(p, str, str);
  443. }
  444. obs_properties_add_bool(props, S_UNDISTORT, T_UNDISTORT);
  445. /* ----------------- */
  446. UNUSED_PARAMETER(data);
  447. return props;
  448. }
  449. static void scale_filter_defaults(obs_data_t *settings)
  450. {
  451. obs_data_set_default_string(settings, S_SAMPLING, S_SAMPLING_BICUBIC);
  452. obs_data_set_default_string(settings, S_RESOLUTION, T_NONE);
  453. obs_data_set_default_bool(settings, S_UNDISTORT, 0);
  454. }
  455. static uint32_t scale_filter_width(void *data)
  456. {
  457. struct scale_filter_data *filter = data;
  458. return (uint32_t)filter->cx_out;
  459. }
  460. static uint32_t scale_filter_height(void *data)
  461. {
  462. struct scale_filter_data *filter = data;
  463. return (uint32_t)filter->cy_out;
  464. }
  465. static enum gs_color_space scale_filter_get_color_space(void *data, size_t count,
  466. const enum gs_color_space *preferred_spaces)
  467. {
  468. const enum gs_color_space potential_spaces[] = {
  469. GS_CS_SRGB,
  470. GS_CS_SRGB_16F,
  471. GS_CS_709_EXTENDED,
  472. };
  473. struct scale_filter_data *const filter = data;
  474. const enum gs_color_space source_space = obs_source_get_color_space(
  475. obs_filter_get_target(filter->context), OBS_COUNTOF(potential_spaces), potential_spaces);
  476. enum gs_color_space space = source_space;
  477. for (size_t i = 0; i < count; ++i) {
  478. space = preferred_spaces[i];
  479. if (space == source_space)
  480. break;
  481. }
  482. return space;
  483. }
  484. struct obs_source_info scale_filter = {
  485. .id = "scale_filter",
  486. .type = OBS_SOURCE_TYPE_FILTER,
  487. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_SRGB,
  488. .get_name = scale_filter_name,
  489. .create = scale_filter_create,
  490. .destroy = scale_filter_destroy,
  491. .video_tick = scale_filter_tick,
  492. .video_render = scale_filter_render,
  493. .update = scale_filter_update,
  494. .get_properties = scale_filter_properties,
  495. .get_defaults = scale_filter_defaults,
  496. .get_width = scale_filter_width,
  497. .get_height = scale_filter_height,
  498. .video_get_color_space = scale_filter_get_color_space,
  499. };