scale-filter.c 16 KB

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