scale-filter.c 11 KB

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