scale-filter.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. #define S_RESOLUTION "resolution"
  9. #define S_SAMPLING "sampling"
  10. #define S_UNDISTORT "undistort"
  11. #define T_RESOLUTION obs_module_text("Resolution")
  12. #define T_NONE obs_module_text("None")
  13. #define T_SAMPLING obs_module_text("ScaleFiltering")
  14. #define T_SAMPLING_POINT obs_module_text("ScaleFiltering.Point")
  15. #define T_SAMPLING_BILINEAR obs_module_text("ScaleFiltering.Bilinear")
  16. #define T_SAMPLING_BICUBIC obs_module_text("ScaleFiltering.Bicubic")
  17. #define T_SAMPLING_LANCZOS obs_module_text("ScaleFiltering.Lanczos")
  18. #define T_SAMPLING_AREA obs_module_text("ScaleFiltering.Area")
  19. #define T_UNDISTORT obs_module_text("UndistortCenter")
  20. #define T_BASE obs_module_text("Base.Canvas")
  21. #define S_SAMPLING_POINT "point"
  22. #define S_SAMPLING_BILINEAR "bilinear"
  23. #define S_SAMPLING_BICUBIC "bicubic"
  24. #define S_SAMPLING_LANCZOS "lanczos"
  25. #define S_SAMPLING_AREA "area"
  26. struct scale_filter_data {
  27. obs_source_t *context;
  28. gs_effect_t *effect;
  29. gs_eparam_t *image_param;
  30. gs_eparam_t *dimension_param;
  31. gs_eparam_t *undistort_factor_param;
  32. struct vec2 dimension_i;
  33. double undistort_factor;
  34. int cx_in;
  35. int cy_in;
  36. int cx_out;
  37. int cy_out;
  38. enum obs_scale_type sampling;
  39. gs_samplerstate_t *point_sampler;
  40. bool aspect_ratio_only;
  41. bool target_valid;
  42. bool valid;
  43. bool undistort;
  44. bool base_canvas_resolution;
  45. };
  46. static const char *scale_filter_name(void *unused)
  47. {
  48. UNUSED_PARAMETER(unused);
  49. return obs_module_text("ScaleFilter");
  50. }
  51. static void scale_filter_update(void *data, obs_data_t *settings)
  52. {
  53. struct scale_filter_data *filter = data;
  54. int ret;
  55. const char *res_str = obs_data_get_string(settings, S_RESOLUTION);
  56. const char *sampling = obs_data_get_string(settings, S_SAMPLING);
  57. filter->valid = true;
  58. filter->base_canvas_resolution = false;
  59. if (strcmp(res_str, T_BASE) == 0) {
  60. struct obs_video_info ovi;
  61. obs_get_video_info(&ovi);
  62. filter->aspect_ratio_only = false;
  63. filter->base_canvas_resolution = true;
  64. filter->cx_in = ovi.base_width;
  65. filter->cy_in = ovi.base_height;
  66. } else {
  67. ret = sscanf(res_str, "%dx%d", &filter->cx_in, &filter->cy_in);
  68. if (ret == 2) {
  69. filter->aspect_ratio_only = false;
  70. } else {
  71. ret = sscanf(res_str, "%d:%d", &filter->cx_in,
  72. &filter->cy_in);
  73. if (ret != 2) {
  74. filter->valid = false;
  75. return;
  76. }
  77. filter->aspect_ratio_only = true;
  78. }
  79. }
  80. if (astrcmpi(sampling, S_SAMPLING_POINT) == 0) {
  81. filter->sampling = OBS_SCALE_POINT;
  82. } else if (astrcmpi(sampling, S_SAMPLING_BILINEAR) == 0) {
  83. filter->sampling = OBS_SCALE_BILINEAR;
  84. } else if (astrcmpi(sampling, S_SAMPLING_LANCZOS) == 0) {
  85. filter->sampling = OBS_SCALE_LANCZOS;
  86. } else if (astrcmpi(sampling, S_SAMPLING_AREA) == 0) {
  87. filter->sampling = OBS_SCALE_AREA;
  88. } else { /* S_SAMPLING_BICUBIC */
  89. filter->sampling = OBS_SCALE_BICUBIC;
  90. }
  91. filter->undistort = obs_data_get_bool(settings, S_UNDISTORT);
  92. }
  93. static void scale_filter_destroy(void *data)
  94. {
  95. struct scale_filter_data *filter = data;
  96. obs_enter_graphics();
  97. gs_samplerstate_destroy(filter->point_sampler);
  98. obs_leave_graphics();
  99. bfree(data);
  100. }
  101. static void *scale_filter_create(obs_data_t *settings, obs_source_t *context)
  102. {
  103. struct scale_filter_data *filter =
  104. bzalloc(sizeof(struct scale_filter_data));
  105. struct gs_sampler_info sampler_info = {0};
  106. filter->context = context;
  107. obs_enter_graphics();
  108. filter->point_sampler = gs_samplerstate_create(&sampler_info);
  109. obs_leave_graphics();
  110. scale_filter_update(filter, settings);
  111. return filter;
  112. }
  113. static void scale_filter_tick(void *data, float seconds)
  114. {
  115. struct scale_filter_data *filter = data;
  116. enum obs_base_effect type;
  117. obs_source_t *target;
  118. bool lower_than_2x;
  119. double cx_f;
  120. double cy_f;
  121. int cx;
  122. int cy;
  123. if (filter->base_canvas_resolution) {
  124. struct obs_video_info ovi;
  125. obs_get_video_info(&ovi);
  126. filter->cx_in = ovi.base_width;
  127. filter->cy_in = ovi.base_height;
  128. }
  129. target = obs_filter_get_target(filter->context);
  130. filter->cx_out = 0;
  131. filter->cy_out = 0;
  132. filter->target_valid = !!target;
  133. if (!filter->target_valid)
  134. return;
  135. cx = obs_source_get_base_width(target);
  136. cy = obs_source_get_base_height(target);
  137. if (!cx || !cy) {
  138. filter->target_valid = false;
  139. return;
  140. }
  141. filter->cx_out = cx;
  142. filter->cy_out = cy;
  143. if (!filter->valid)
  144. return;
  145. /* ------------------------- */
  146. cx_f = (double)cx;
  147. cy_f = (double)cy;
  148. double old_aspect = cx_f / cy_f;
  149. double new_aspect =
  150. (double)filter->cx_in / (double)filter->cy_in;
  151. if (filter->aspect_ratio_only) {
  152. if (fabs(old_aspect - new_aspect) <= EPSILON) {
  153. filter->target_valid = false;
  154. return;
  155. } else {
  156. if (new_aspect > old_aspect) {
  157. filter->cx_out = (int)(cy_f * new_aspect);
  158. filter->cy_out = cy;
  159. } else {
  160. filter->cx_out = cx;
  161. filter->cy_out = (int)(cx_f / new_aspect);
  162. }
  163. }
  164. } else {
  165. filter->cx_out = filter->cx_in;
  166. filter->cy_out = filter->cy_in;
  167. }
  168. vec2_set(&filter->dimension_i,
  169. 1.0f / (float)cx,
  170. 1.0f / (float)cy);
  171. if (filter->undistort) {
  172. filter->undistort_factor = new_aspect / old_aspect;
  173. } else {
  174. filter->undistort_factor = 1.0;
  175. }
  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: type = OBS_EFFECT_DEFAULT; break;
  185. case OBS_SCALE_BICUBIC: type = OBS_EFFECT_BICUBIC; break;
  186. case OBS_SCALE_LANCZOS: type = OBS_EFFECT_LANCZOS; break;
  187. case OBS_SCALE_AREA: type = OBS_EFFECT_AREA; break;
  188. }
  189. }
  190. filter->effect = obs_get_base_effect(type);
  191. filter->image_param = gs_effect_get_param_by_name(filter->effect,
  192. "image");
  193. if (type != OBS_EFFECT_DEFAULT) {
  194. filter->dimension_param = gs_effect_get_param_by_name(
  195. filter->effect, "base_dimension_i");
  196. } else {
  197. filter->dimension_param = NULL;
  198. }
  199. if (type == OBS_EFFECT_BICUBIC || type == OBS_EFFECT_LANCZOS) {
  200. filter->undistort_factor_param = gs_effect_get_param_by_name(
  201. filter->effect, "undistort_factor");
  202. }
  203. else {
  204. filter->undistort_factor_param = NULL;
  205. }
  206. UNUSED_PARAMETER(seconds);
  207. }
  208. static void scale_filter_render(void *data, gs_effect_t *effect)
  209. {
  210. struct scale_filter_data *filter = data;
  211. const char *technique = filter->undistort ?
  212. "DrawUndistort" : "Draw";
  213. if (!filter->valid || !filter->target_valid) {
  214. obs_source_skip_video_filter(filter->context);
  215. return;
  216. }
  217. if (!obs_source_process_filter_begin(filter->context, GS_RGBA,
  218. OBS_NO_DIRECT_RENDERING))
  219. return;
  220. if (filter->dimension_param)
  221. gs_effect_set_vec2(filter->dimension_param,
  222. &filter->dimension_i);
  223. if (filter->undistort_factor_param)
  224. gs_effect_set_float(filter->undistort_factor_param,
  225. (float)filter->undistort_factor);
  226. if (filter->sampling == OBS_SCALE_POINT)
  227. gs_effect_set_next_sampler(filter->image_param,
  228. filter->point_sampler);
  229. obs_source_process_filter_tech_end(filter->context, filter->effect,
  230. filter->cx_out, filter->cy_out, technique);
  231. UNUSED_PARAMETER(effect);
  232. }
  233. static const double downscale_vals[] = {
  234. 1.0,
  235. 1.25,
  236. (1.0/0.75),
  237. 1.5,
  238. (1.0/0.6),
  239. 1.75,
  240. 2.0,
  241. 2.25,
  242. 2.5,
  243. 2.75,
  244. 3.0
  245. };
  246. #define NUM_DOWNSCALES (sizeof(downscale_vals) / sizeof(double))
  247. static const char *aspects[] = {
  248. "16:9",
  249. "16:10",
  250. "4:3",
  251. "1:1"
  252. };
  253. #define NUM_ASPECTS (sizeof(aspects) / sizeof(const char *))
  254. static bool sampling_modified(obs_properties_t *props, obs_property_t *p,
  255. obs_data_t *settings)
  256. {
  257. const char *sampling = obs_data_get_string(settings, S_SAMPLING);
  258. bool has_undistort;
  259. if (astrcmpi(sampling, S_SAMPLING_POINT) == 0) {
  260. has_undistort = false;
  261. }
  262. else if (astrcmpi(sampling, S_SAMPLING_BILINEAR) == 0) {
  263. has_undistort = false;
  264. }
  265. else if (astrcmpi(sampling, S_SAMPLING_LANCZOS) == 0) {
  266. has_undistort = true;
  267. }
  268. else if (astrcmpi(sampling, S_SAMPLING_AREA) == 0) {
  269. has_undistort = false;
  270. }
  271. else { /* S_SAMPLING_BICUBIC */
  272. has_undistort = true;
  273. }
  274. obs_property_set_visible(obs_properties_get(props, S_UNDISTORT), has_undistort);
  275. UNUSED_PARAMETER(p);
  276. return true;
  277. }
  278. static obs_properties_t *scale_filter_properties(void *data)
  279. {
  280. obs_properties_t *props = obs_properties_create();
  281. struct obs_video_info ovi;
  282. obs_property_t *p;
  283. uint32_t cx;
  284. uint32_t cy;
  285. struct {
  286. int cx;
  287. int cy;
  288. } downscales[NUM_DOWNSCALES];
  289. /* ----------------- */
  290. obs_get_video_info(&ovi);
  291. cx = ovi.base_width;
  292. cy = ovi.base_height;
  293. for (size_t i = 0; i < NUM_DOWNSCALES; i++) {
  294. downscales[i].cx = (int)((double)cx / downscale_vals[i]);
  295. downscales[i].cy = (int)((double)cy / downscale_vals[i]);
  296. }
  297. p = obs_properties_add_list(props, S_SAMPLING, T_SAMPLING,
  298. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  299. obs_property_set_modified_callback(p, sampling_modified);
  300. obs_property_list_add_string(p, T_SAMPLING_POINT, S_SAMPLING_POINT);
  301. obs_property_list_add_string(p, T_SAMPLING_BILINEAR, S_SAMPLING_BILINEAR);
  302. obs_property_list_add_string(p, T_SAMPLING_BICUBIC, S_SAMPLING_BICUBIC);
  303. obs_property_list_add_string(p, T_SAMPLING_LANCZOS, S_SAMPLING_LANCZOS);
  304. obs_property_list_add_string(p, T_SAMPLING_AREA, S_SAMPLING_AREA);
  305. /* ----------------- */
  306. p = obs_properties_add_list(props, S_RESOLUTION, T_RESOLUTION,
  307. OBS_COMBO_TYPE_EDITABLE, OBS_COMBO_FORMAT_STRING);
  308. obs_property_list_add_string(p, T_NONE, T_NONE);
  309. obs_property_list_add_string(p, T_BASE, T_BASE);
  310. for (size_t i = 0; i < NUM_ASPECTS; i++)
  311. obs_property_list_add_string(p, aspects[i], aspects[i]);
  312. for (size_t i = 0; i < NUM_DOWNSCALES; i++) {
  313. char str[32];
  314. snprintf(str, 32, "%dx%d", downscales[i].cx, downscales[i].cy);
  315. obs_property_list_add_string(p, str, str);
  316. }
  317. obs_properties_add_bool(props, S_UNDISTORT, T_UNDISTORT);
  318. /* ----------------- */
  319. UNUSED_PARAMETER(data);
  320. return props;
  321. }
  322. static void scale_filter_defaults(obs_data_t *settings)
  323. {
  324. obs_data_set_default_string(settings, S_SAMPLING, S_SAMPLING_BICUBIC);
  325. obs_data_set_default_string(settings, S_RESOLUTION, T_NONE);
  326. obs_data_set_default_bool(settings, S_UNDISTORT, 0);
  327. }
  328. static uint32_t scale_filter_width(void *data)
  329. {
  330. struct scale_filter_data *filter = data;
  331. return (uint32_t)filter->cx_out;
  332. }
  333. static uint32_t scale_filter_height(void *data)
  334. {
  335. struct scale_filter_data *filter = data;
  336. return (uint32_t)filter->cy_out;
  337. }
  338. struct obs_source_info scale_filter = {
  339. .id = "scale_filter",
  340. .type = OBS_SOURCE_TYPE_FILTER,
  341. .output_flags = OBS_SOURCE_VIDEO,
  342. .get_name = scale_filter_name,
  343. .create = scale_filter_create,
  344. .destroy = scale_filter_destroy,
  345. .video_tick = scale_filter_tick,
  346. .video_render = scale_filter_render,
  347. .update = scale_filter_update,
  348. .get_properties = scale_filter_properties,
  349. .get_defaults = scale_filter_defaults,
  350. .get_width = scale_filter_width,
  351. .get_height = scale_filter_height
  352. };