├── .gitignore ├── data ├── locale │ └── en-US.ini └── rgb_levels.effect ├── README.md ├── external └── FindLibObs.cmake ├── CMakeLists.txt ├── src ├── rgb-levels-filter.c └── color-key-filter.c └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | *~ 3 | -------------------------------------------------------------------------------- /data/locale/en-US.ini: -------------------------------------------------------------------------------- 1 | Vintage="Vintage" 2 | Type="Type" 3 | BlackWhite="Black and white" 4 | Sepia="Sepia" 5 | -------------------------------------------------------------------------------- /data/rgb_levels.effect: -------------------------------------------------------------------------------- 1 | uniform float4x4 ViewProj; 2 | uniform texture2d image; 3 | 4 | uniform float3 rgb_min; 5 | uniform float3 rgb_scale; 6 | 7 | sampler_state textureSampler { 8 | Filter = Linear; 9 | AddressU = Clamp; 10 | AddressV = Clamp; 11 | }; 12 | 13 | struct VertData { 14 | float4 pos : POSITION; 15 | float2 uv : TEXCOORD0; 16 | }; 17 | 18 | VertData VSDefault(VertData v_in) 19 | { 20 | VertData vert_out; 21 | vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj); 22 | vert_out.uv = v_in.uv; 23 | return vert_out; 24 | } 25 | 26 | float4 DoLevel(VertData v_in) : TARGET 27 | { 28 | float4 rgba = image.Sample(textureSampler, v_in.uv); 29 | 30 | float3 leveled = saturate((rgba.rgb - rgb_min) * rgb_scale); 31 | return float4(leveled, rgba.a); 32 | } 33 | 34 | technique Draw 35 | { 36 | pass 37 | { 38 | vertex_shader = VSDefault(v_in); 39 | pixel_shader = DoLevel(v_in); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # obs-rgb-levels-filter 2 | A simple OBS Studio filter to adjust RGB levels. 3 | 4 | I made this in order to fix a poor quality webcam image, for which the 5 | built-in OBS color-correction filter was inadequate. 6 | 7 | Tested with OBS 25.0.8 running on Ubuntu 18.04.04 (x86_64). 8 | 9 | ## Compiling on Linux 10 | You will have installed the obs-studio source files. 11 | 12 | Set OBS_SRC_DIR to obs-studio source directory (e.g. /usr/local/src/obs-studio) 13 | and LIBOBS_LIB to full path to libobs.so (e.g. /usr/lib/libobs.so) 14 | 15 | ``` 16 | git clone https:????? 17 | cd obs_rgb_levels_filter 18 | mkdir build && cd build 19 | cmake -DOBS_SRC_DIR="...path to obs-studio source directory..." \ 20 | -DLIBOBS_LIB="...path to libobs.so file..." -DCMAKE_INSTALL_PREFIX=/usr .. 21 | make -j4 22 | sudo make install 23 | ``` 24 | 25 | ## Other platforms 26 | 27 | I don't have access to a suitable machine to test this, but it should compile on 28 | Windows/Mac (you may have to edit some paths in CMakeLists.txt for Windows) 29 | 30 | -------------------------------------------------------------------------------- /external/FindLibObs.cmake: -------------------------------------------------------------------------------- 1 | # This module can be copied and used by external plugins for OBS 2 | # 3 | # Once done these will be defined: 4 | # 5 | # LIBOBS_FOUND 6 | # LIBOBS_INCLUDE_DIRS 7 | # LIBOBS_LIBRARIES 8 | 9 | find_package(PkgConfig QUIET) 10 | if (PKG_CONFIG_FOUND) 11 | pkg_check_modules(_OBS QUIET obs libobs) 12 | endif() 13 | 14 | if(CMAKE_SIZEOF_VOID_P EQUAL 8) 15 | set(_lib_suffix 64) 16 | else() 17 | set(_lib_suffix 32) 18 | endif() 19 | 20 | if(DEFINED CMAKE_BUILD_TYPE) 21 | if(CMAKE_BUILD_TYPE STREQUAL "Debug") 22 | set(_build_type_base "debug") 23 | else() 24 | set(_build_type_base "release") 25 | endif() 26 | endif() 27 | 28 | find_path(LIBOBS_INCLUDE_DIR 29 | NAMES obs.h 30 | HINTS 31 | ENV obsPath${_lib_suffix} 32 | ENV obsPath 33 | ${obsPath} 34 | PATHS 35 | /usr/include /usr/local/include /opt/local/include /sw/include 36 | PATH_SUFFIXES 37 | libobs 38 | ) 39 | 40 | function(find_obs_lib base_name repo_build_path lib_name) 41 | string(TOUPPER "${base_name}" base_name_u) 42 | 43 | if(DEFINED _build_type_base) 44 | set(_build_type_${repo_build_path} "${_build_type_base}/${repo_build_path}") 45 | set(_build_type_${repo_build_path}${_lib_suffix} "${_build_type_base}${_lib_suffix}/${repo_build_path}") 46 | endif() 47 | 48 | find_library(${base_name_u}_LIB 49 | NAMES ${_${base_name_u}_LIBRARIES} ${lib_name} lib${lib_name} 50 | HINTS 51 | ENV obsPath${_lib_suffix} 52 | ENV obsPath 53 | ${obsPath} 54 | ${_${base_name_u}_LIBRARY_DIRS} 55 | PATHS 56 | /usr/lib /usr/local/lib /opt/local/lib /sw/lib 57 | PATH_SUFFIXES 58 | lib${_lib_suffix} lib 59 | libs${_lib_suffix} libs 60 | bin${_lib_suffix} bin 61 | ../lib${_lib_suffix} ../lib 62 | ../libs${_lib_suffix} ../libs 63 | ../bin${_lib_suffix} ../bin 64 | # base repo non-msvc-specific search paths 65 | ${_build_type_${repo_build_path}} 66 | ${_build_type_${repo_build_path}${_lib_suffix}} 67 | build/${repo_build_path} 68 | build${_lib_suffix}/${repo_build_path} 69 | # base repo msvc-specific search paths on windows 70 | build${_lib_suffix}/${repo_build_path}/Debug 71 | build${_lib_suffix}/${repo_build_path}/RelWithDebInfo 72 | build/${repo_build_path}/Debug 73 | build/${repo_build_path}/RelWithDebInfo 74 | ) 75 | endfunction() 76 | 77 | find_obs_lib(LIBOBS libobs obs) 78 | 79 | if(MSVC) 80 | find_obs_lib(W32_PTHREADS deps/w32-pthreads w32-pthreads) 81 | endif() 82 | 83 | include(FindPackageHandleStandardArgs) 84 | find_package_handle_standard_args(Libobs DEFAULT_MSG LIBOBS_LIB LIBOBS_INCLUDE_DIR) 85 | mark_as_advanced(LIBOBS_INCLUDE_DIR LIBOBS_LIB) 86 | 87 | if(LIBOBS_FOUND) 88 | if(MSVC) 89 | if (NOT DEFINED W32_PTHREADS_LIB) 90 | message(FATAL_ERROR "Could not find the w32-pthreads library" ) 91 | endif() 92 | 93 | set(W32_PTHREADS_INCLUDE_DIR ${LIBOBS_INCLUDE_DIR}/../deps/w32-pthreads) 94 | endif() 95 | 96 | set(LIBOBS_INCLUDE_DIRS ${LIBOBS_INCLUDE_DIR} ${W32_PTHREADS_INCLUDE_DIR}) 97 | set(LIBOBS_LIBRARIES ${LIBOBS_LIB} ${W32_PTHREADS_LIB}) 98 | include(${LIBOBS_INCLUDE_DIR}/../cmake/external/ObsPluginHelpers.cmake) 99 | 100 | # allows external plugins to easily use/share common dependencies that are often included with libobs (such as FFmpeg) 101 | if(NOT DEFINED INCLUDED_LIBOBS_CMAKE_MODULES) 102 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${LIBOBS_INCLUDE_DIR}/../cmake/Modules/") 103 | set(INCLUDED_LIBOBS_CMAKE_MODULES true) 104 | endif() 105 | else() 106 | message(FATAL_ERROR "Could not find the libobs library" ) 107 | endif() 108 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.2) 2 | 3 | project(obs-rgb-levels-filter) 4 | 5 | if(CMAKE_SIZEOF_VOID_P EQUAL 8) 6 | set(ARCH_NAME "64bit") 7 | set(OBS_BUILDDIR_ARCH "build64") 8 | set(_lib_suffix 64) 9 | else() 10 | set(ARCH_NAME "32bit") 11 | set(OBS_BUILDDIR_ARCH "build32") 12 | set(_lib_suffix 32) 13 | endif() 14 | 15 | ########################################## 16 | # find libobs # 17 | ########################################## 18 | set (LIBOBS_INCLUDE_DIR "${OBS_SRC_DIR}/libobs") 19 | include(external/FindLibObs.cmake) 20 | find_package(LibObs REQUIRED) 21 | 22 | set(obs-rgb-levels-filter_SOURCES 23 | src/rgb-levels-filter.c 24 | ) 25 | 26 | include_directories( 27 | ${LIBOBS_INCLUDE_DIR} 28 | ) 29 | 30 | add_library(obs-rgb-levels-filter MODULE 31 | ${obs-rgb-levels-filter_SOURCES} 32 | ) 33 | 34 | target_link_libraries(obs-rgb-levels-filter 35 | ${LIBOBS_LIB} 36 | ) 37 | 38 | # --- Windows-specific build settings and tasks --- 39 | if(WIN32) 40 | # --- Release package helper --- 41 | # The "release" folder has a structure similar OBS' one on Windows 42 | set(RELEASE_DIR "${PROJECT_SOURCE_DIR}/release") 43 | 44 | add_custom_command(TARGET obs-rgb-levels-filter POST_BUILD 45 | # If config is Release, package release files 46 | COMMAND if $==1 ( 47 | "${CMAKE_COMMAND}" -E make_directory 48 | "${RELEASE_DIR}/data/obs-plugins/obs-rgb-levels-filter" 49 | "${RELEASE_DIR}/obs-plugins/${ARCH_NAME}") 50 | 51 | # If config is RelWithDebInfo, package release files 52 | COMMAND if $==1 ( 53 | "${CMAKE_COMMAND}" -E make_directory 54 | "${RELEASE_DIR}/data/obs-plugins/obs-rgb-levels-filter" 55 | "${RELEASE_DIR}/obs-plugins/${ARCH_NAME}") 56 | 57 | COMMAND if $==1 ("${CMAKE_COMMAND}" -E copy_directory 58 | "${PROJECT_SOURCE_DIR}/data" 59 | "${RELEASE_DIR}/data/obs-plugins/obs-rgb-levels-filter") 60 | 61 | COMMAND if $==1 ("${CMAKE_COMMAND}" -E copy_directory 62 | "${PROJECT_SOURCE_DIR}/data" 63 | "${RELEASE_DIR}/data/obs-plugins/obs-rgb-levels-filter") 64 | 65 | COMMAND if $==1 ("${CMAKE_COMMAND}" -E copy 66 | "$" 67 | "${RELEASE_DIR}/obs-plugins/${ARCH_NAME}") 68 | 69 | COMMAND ( 70 | "${CMAKE_COMMAND}" -E copy 71 | "$" 72 | "${RELEASE_DIR}/obs-plugins/${ARCH_NAME}") 73 | 74 | # Copy to obs-studio environment for immediate testing 75 | COMMAND ( 76 | "${CMAKE_COMMAND}" -E copy 77 | "$" 78 | "${LIBOBS_INCLUDE_DIR}/../${OBS_BUILDDIR_ARCH}/rundir/$/obs-plugins/${ARCH_NAME}") 79 | 80 | COMMAND if $==1 ( 81 | "${CMAKE_COMMAND}" -E copy 82 | "$" 83 | "${LIBOBS_INCLUDE_DIR}/../${OBS_BUILDDIR_ARCH}/rundir/$/obs-plugins/${ARCH_NAME}") 84 | 85 | COMMAND ( 86 | "${CMAKE_COMMAND}" -E make_directory 87 | "${LIBOBS_INCLUDE_DIR}/../${OBS_BUILDDIR_ARCH}/rundir/$/data/obs-plugins/obs-rgb-levels-filter") 88 | 89 | COMMAND ( 90 | "${CMAKE_COMMAND}" -E copy_directory 91 | "${PROJECT_SOURCE_DIR}/data" 92 | "${LIBOBS_INCLUDE_DIR}/../${OBS_BUILDDIR_ARCH}/rundir/$/data/obs-plugins/obs-rgb-levels-filter") 93 | ) 94 | endif() 95 | 96 | if(UNIX AND NOT APPLE) 97 | set_target_properties(obs-rgb-levels-filter PROPERTIES PREFIX "") 98 | 99 | file(GLOB effect_files data/*.effect) 100 | file(GLOB locale_files data/locale/*.ini) 101 | 102 | install(TARGETS obs-rgb-levels-filter LIBRARY DESTINATION 103 | "${CMAKE_INSTALL_PREFIX}/lib/obs-plugins") 104 | install(FILES ${effect_files} DESTINATION 105 | "${CMAKE_INSTALL_PREFIX}/share/obs/obs-plugins/obs-rgb-levels-filter/") 106 | install(FILES ${locale_files} DESTINATION 107 | "${CMAKE_INSTALL_PREFIX}/share/obs/obs-plugins/obs-rgb-levels-filter/locale") 108 | endif() 109 | -------------------------------------------------------------------------------- /src/rgb-levels-filter.c: -------------------------------------------------------------------------------- 1 | /* modelled on color-key-filter.c in obs distribution */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define SETTINGS_RED_MIN "red_min" 8 | #define SETTINGS_RED_MAX "red_max" 9 | #define SETTINGS_GREEN_MIN "green_min" 10 | #define SETTINGS_GREEN_MAX "green_max" 11 | #define SETTINGS_BLUE_MIN "blue_min" 12 | #define SETTINGS_BLUE_MAX "blue_max" 13 | 14 | #define MAX(a,b) ({ __typeof__ (a) _a = (a); \ 15 | __typeof__ (b) _b = (b); \ 16 | _a > _b ? _a : _b; }) 17 | 18 | struct rgb_levels_filter_data { 19 | obs_source_t *context; 20 | gs_effect_t *effect; 21 | 22 | gs_eparam_t *min_param; 23 | gs_eparam_t *scale_param; 24 | 25 | struct vec3 rgb_min; 26 | struct vec3 rgb_scale; 27 | }; 28 | 29 | static const char *rgb_levels_name(void *unused) 30 | { 31 | UNUSED_PARAMETER(unused); 32 | return obs_module_text("RGB levels"); 33 | } 34 | 35 | static inline void rgb_levels_update(void *data, 36 | obs_data_t *settings) 37 | { 38 | struct rgb_levels_filter_data *filter = data; 39 | 40 | int r_min = obs_data_get_int(settings, SETTINGS_RED_MIN); 41 | int g_min = obs_data_get_int(settings, SETTINGS_GREEN_MIN); 42 | int b_min = obs_data_get_int(settings, SETTINGS_BLUE_MIN); 43 | int r_max = MAX(r_min + 1, obs_data_get_int(settings, SETTINGS_RED_MAX)); 44 | int g_max = MAX(g_min + 1, obs_data_get_int(settings, SETTINGS_GREEN_MAX)); 45 | int b_max = MAX(b_min + 1, obs_data_get_int(settings, SETTINGS_BLUE_MAX)); 46 | 47 | vec3_set( &filter->rgb_min, r_min / 255.0f, g_min / 255.0f, b_min / 255.0f); 48 | vec3_set( &filter->rgb_scale, 255.0f / (r_max - r_min), 49 | 255.0f / (g_max - g_min), 255.0f / (b_max - b_min) ); 50 | } 51 | 52 | static void rgb_levels_destroy(void *data) 53 | { 54 | struct rgb_levels_filter_data *filter = data; 55 | 56 | if (filter->effect) { 57 | obs_enter_graphics(); 58 | gs_effect_destroy(filter->effect); 59 | obs_leave_graphics(); 60 | } 61 | bfree(data); 62 | } 63 | 64 | static void *rgb_levels_create(obs_data_t *settings, obs_source_t *context) 65 | { 66 | struct rgb_levels_filter_data *filter = bzalloc(sizeof(struct rgb_levels_filter_data)); 67 | char *effect_path = obs_module_file("rgb_levels.effect"); 68 | filter->context = context; 69 | 70 | obs_enter_graphics(); 71 | filter->effect = gs_effect_create_from_file(effect_path, NULL); 72 | if (filter->effect) { 73 | filter->min_param = gs_effect_get_param_by_name( 74 | filter->effect, "rgb_min"); 75 | filter->scale_param = gs_effect_get_param_by_name( 76 | filter->effect, "rgb_scale"); 77 | } 78 | obs_leave_graphics(); 79 | 80 | bfree(effect_path); 81 | if (!filter->effect) { 82 | rgb_levels_destroy(filter); 83 | return NULL; 84 | } 85 | rgb_levels_update(filter, settings); 86 | return filter; 87 | } 88 | 89 | static void rgb_levels_render(void *data, gs_effect_t *effect) 90 | { 91 | struct rgb_levels_filter_data *filter = data; 92 | 93 | if (!obs_source_process_filter_begin(filter->context, GS_RGBA, 94 | OBS_ALLOW_DIRECT_RENDERING)) 95 | return; 96 | 97 | gs_effect_set_vec3(filter->min_param, &filter->rgb_min); 98 | gs_effect_set_vec3(filter->scale_param, &filter->rgb_scale); 99 | 100 | obs_source_process_filter_end(filter->context, filter->effect, 0, 0); 101 | 102 | UNUSED_PARAMETER(effect); 103 | } 104 | 105 | static obs_properties_t *rgb_levels_properties(void *data) 106 | { 107 | obs_properties_t *props = obs_properties_create(); 108 | 109 | obs_properties_add_int_slider(props, "red_min", "Red min", 0, 254, 1); 110 | obs_properties_add_int_slider(props, "red_max", "Red max", 1, 255, 1); 111 | obs_properties_add_int_slider(props, "green_min", "Green min", 0, 254, 1); 112 | obs_properties_add_int_slider(props, "green_max", "Green max", 1, 255, 1); 113 | obs_properties_add_int_slider(props, "blue_min", "Blue min", 0, 254, 1); 114 | obs_properties_add_int_slider(props, "blue_max", "Blue max", 1, 255, 1); 115 | 116 | UNUSED_PARAMETER(data); 117 | return props; 118 | } 119 | 120 | static void rgb_levels_defaults(obs_data_t *settings) 121 | { 122 | obs_data_set_default_int(settings, SETTINGS_RED_MIN, 0); 123 | obs_data_set_default_int(settings, SETTINGS_GREEN_MIN, 0); 124 | obs_data_set_default_int(settings, SETTINGS_BLUE_MIN, 0); 125 | obs_data_set_default_int(settings, SETTINGS_RED_MAX, 255); 126 | obs_data_set_default_int(settings, SETTINGS_GREEN_MAX, 255); 127 | obs_data_set_default_int(settings, SETTINGS_BLUE_MAX, 255); 128 | } 129 | 130 | struct obs_source_info rgb_levels_filter = { 131 | .id = "rgb_levels_filter", 132 | .type = OBS_SOURCE_TYPE_FILTER, 133 | .output_flags = OBS_SOURCE_VIDEO, 134 | .get_name = rgb_levels_name, 135 | .create = rgb_levels_create, 136 | .destroy = rgb_levels_destroy, 137 | .video_render = rgb_levels_render, 138 | .update = rgb_levels_update, 139 | .get_properties = rgb_levels_properties, 140 | .get_defaults = rgb_levels_defaults 141 | }; 142 | 143 | OBS_DECLARE_MODULE() 144 | OBS_MODULE_USE_DEFAULT_LOCALE("obs-rgb-levels-filter", "en-US") 145 | 146 | bool obs_module_load(void) 147 | { 148 | obs_register_source(&rgb_levels_filter); 149 | 150 | return true; 151 | } 152 | 153 | void obs_module_unload(void) 154 | { 155 | } 156 | 157 | -------------------------------------------------------------------------------- /src/color-key-filter.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* clang-format off */ 7 | 8 | #define SETTING_OPACITY "opacity" 9 | #define SETTING_CONTRAST "contrast" 10 | #define SETTING_BRIGHTNESS "brightness" 11 | #define SETTING_GAMMA "gamma" 12 | #define SETTING_COLOR_TYPE "key_color_type" 13 | #define SETTING_KEY_COLOR "key_color" 14 | #define SETTING_SIMILARITY "similarity" 15 | #define SETTING_SMOOTHNESS "smoothness" 16 | 17 | #define TEXT_OPACITY obs_module_text("Opacity") 18 | #define TEXT_CONTRAST obs_module_text("Contrast") 19 | #define TEXT_BRIGHTNESS obs_module_text("Brightness") 20 | #define TEXT_GAMMA obs_module_text("Gamma") 21 | #define TEXT_COLOR_TYPE obs_module_text("KeyColorType") 22 | #define TEXT_KEY_COLOR obs_module_text("KeyColor") 23 | #define TEXT_SIMILARITY obs_module_text("Similarity") 24 | #define TEXT_SMOOTHNESS obs_module_text("Smoothness") 25 | 26 | /* clang-format on */ 27 | 28 | struct color_key_filter_data { 29 | obs_source_t *context; 30 | 31 | gs_effect_t *effect; 32 | 33 | gs_eparam_t *color_param; 34 | gs_eparam_t *contrast_param; 35 | gs_eparam_t *brightness_param; 36 | gs_eparam_t *gamma_param; 37 | 38 | gs_eparam_t *key_color_param; 39 | gs_eparam_t *similarity_param; 40 | gs_eparam_t *smoothness_param; 41 | 42 | struct vec4 color; 43 | float contrast; 44 | float brightness; 45 | float gamma; 46 | 47 | struct vec4 key_color; 48 | float similarity; 49 | float smoothness; 50 | }; 51 | 52 | static const char *color_key_name(void *unused) 53 | { 54 | UNUSED_PARAMETER(unused); 55 | return obs_module_text("ColorKeyFilter"); 56 | } 57 | 58 | static inline void color_settings_update(struct color_key_filter_data *filter, 59 | obs_data_t *settings) 60 | { 61 | uint32_t opacity = 62 | (uint32_t)obs_data_get_int(settings, SETTING_OPACITY); 63 | uint32_t color = 0xFFFFFF | (((opacity * 255) / 100) << 24); 64 | double contrast = obs_data_get_double(settings, SETTING_CONTRAST); 65 | double brightness = obs_data_get_double(settings, SETTING_BRIGHTNESS); 66 | double gamma = obs_data_get_double(settings, SETTING_GAMMA); 67 | 68 | contrast = (contrast < 0.0) ? (1.0 / (-contrast + 1.0)) 69 | : (contrast + 1.0); 70 | 71 | brightness *= 0.5; 72 | 73 | gamma = (gamma < 0.0) ? (-gamma + 1.0) : (1.0 / (gamma + 1.0)); 74 | 75 | filter->contrast = (float)contrast; 76 | filter->brightness = (float)brightness; 77 | filter->gamma = (float)gamma; 78 | 79 | vec4_from_rgba(&filter->color, color); 80 | } 81 | 82 | static inline void key_settings_update(struct color_key_filter_data *filter, 83 | obs_data_t *settings) 84 | { 85 | int64_t similarity = obs_data_get_int(settings, SETTING_SIMILARITY); 86 | int64_t smoothness = obs_data_get_int(settings, SETTING_SMOOTHNESS); 87 | uint32_t key_color = 88 | (uint32_t)obs_data_get_int(settings, SETTING_KEY_COLOR); 89 | const char *key_type = 90 | obs_data_get_string(settings, SETTING_COLOR_TYPE); 91 | 92 | if (strcmp(key_type, "green") == 0) 93 | key_color = 0x00FF00; 94 | else if (strcmp(key_type, "blue") == 0) 95 | key_color = 0xFF0000; 96 | else if (strcmp(key_type, "red") == 0) 97 | key_color = 0x0000FF; 98 | else if (strcmp(key_type, "magenta") == 0) 99 | key_color = 0xFF00FF; 100 | 101 | vec4_from_rgba(&filter->key_color, key_color | 0xFF000000); 102 | 103 | filter->similarity = (float)similarity / 1000.0f; 104 | filter->smoothness = (float)smoothness / 1000.0f; 105 | } 106 | 107 | static void color_key_update(void *data, obs_data_t *settings) 108 | { 109 | struct color_key_filter_data *filter = data; 110 | 111 | color_settings_update(filter, settings); 112 | key_settings_update(filter, settings); 113 | } 114 | 115 | static void color_key_destroy(void *data) 116 | { 117 | struct color_key_filter_data *filter = data; 118 | 119 | if (filter->effect) { 120 | obs_enter_graphics(); 121 | gs_effect_destroy(filter->effect); 122 | obs_leave_graphics(); 123 | } 124 | 125 | bfree(data); 126 | } 127 | 128 | static void *color_key_create(obs_data_t *settings, obs_source_t *context) 129 | { 130 | struct color_key_filter_data *filter = 131 | bzalloc(sizeof(struct color_key_filter_data)); 132 | char *effect_path = obs_module_file("color_key_filter.effect"); 133 | 134 | filter->context = context; 135 | 136 | obs_enter_graphics(); 137 | 138 | filter->effect = gs_effect_create_from_file(effect_path, NULL); 139 | if (filter->effect) { 140 | filter->color_param = 141 | gs_effect_get_param_by_name(filter->effect, "color"); 142 | filter->contrast_param = 143 | gs_effect_get_param_by_name(filter->effect, "contrast"); 144 | filter->brightness_param = gs_effect_get_param_by_name( 145 | filter->effect, "brightness"); 146 | filter->gamma_param = 147 | gs_effect_get_param_by_name(filter->effect, "gamma"); 148 | filter->key_color_param = gs_effect_get_param_by_name( 149 | filter->effect, "key_color"); 150 | filter->similarity_param = gs_effect_get_param_by_name( 151 | filter->effect, "similarity"); 152 | filter->smoothness_param = gs_effect_get_param_by_name( 153 | filter->effect, "smoothness"); 154 | } 155 | 156 | obs_leave_graphics(); 157 | 158 | bfree(effect_path); 159 | 160 | if (!filter->effect) { 161 | color_key_destroy(filter); 162 | return NULL; 163 | } 164 | 165 | color_key_update(filter, settings); 166 | return filter; 167 | } 168 | 169 | static void color_key_render(void *data, gs_effect_t *effect) 170 | { 171 | struct color_key_filter_data *filter = data; 172 | 173 | if (!obs_source_process_filter_begin(filter->context, GS_RGBA, 174 | OBS_ALLOW_DIRECT_RENDERING)) 175 | return; 176 | 177 | gs_effect_set_vec4(filter->color_param, &filter->color); 178 | gs_effect_set_float(filter->contrast_param, filter->contrast); 179 | gs_effect_set_float(filter->brightness_param, filter->brightness); 180 | gs_effect_set_float(filter->gamma_param, filter->gamma); 181 | gs_effect_set_vec4(filter->key_color_param, &filter->key_color); 182 | gs_effect_set_float(filter->similarity_param, filter->similarity); 183 | gs_effect_set_float(filter->smoothness_param, filter->smoothness); 184 | 185 | obs_source_process_filter_end(filter->context, filter->effect, 0, 0); 186 | 187 | UNUSED_PARAMETER(effect); 188 | } 189 | 190 | static bool key_type_changed(obs_properties_t *props, obs_property_t *p, 191 | obs_data_t *settings) 192 | { 193 | const char *type = obs_data_get_string(settings, SETTING_COLOR_TYPE); 194 | bool custom = strcmp(type, "custom") == 0; 195 | 196 | obs_property_set_visible(obs_properties_get(props, SETTING_KEY_COLOR), 197 | custom); 198 | 199 | UNUSED_PARAMETER(p); 200 | return true; 201 | } 202 | 203 | static obs_properties_t *color_key_properties(void *data) 204 | { 205 | obs_properties_t *props = obs_properties_create(); 206 | 207 | obs_property_t *p = obs_properties_add_list(props, SETTING_COLOR_TYPE, 208 | TEXT_COLOR_TYPE, 209 | OBS_COMBO_TYPE_LIST, 210 | OBS_COMBO_FORMAT_STRING); 211 | obs_property_list_add_string(p, obs_module_text("Green"), "green"); 212 | obs_property_list_add_string(p, obs_module_text("Blue"), "blue"); 213 | obs_property_list_add_string(p, obs_module_text("Red"), "red"); 214 | obs_property_list_add_string(p, obs_module_text("Magenta"), "magenta"); 215 | obs_property_list_add_string(p, obs_module_text("CustomColor"), 216 | "custom"); 217 | 218 | obs_property_set_modified_callback(p, key_type_changed); 219 | 220 | obs_properties_add_color(props, SETTING_KEY_COLOR, TEXT_KEY_COLOR); 221 | obs_properties_add_int_slider(props, SETTING_SIMILARITY, 222 | TEXT_SIMILARITY, 1, 1000, 1); 223 | obs_properties_add_int_slider(props, SETTING_SMOOTHNESS, 224 | TEXT_SMOOTHNESS, 1, 1000, 1); 225 | 226 | obs_properties_add_int_slider(props, SETTING_OPACITY, TEXT_OPACITY, 0, 227 | 100, 1); 228 | obs_properties_add_float_slider(props, SETTING_CONTRAST, TEXT_CONTRAST, 229 | -1.0, 1.0, 0.01); 230 | obs_properties_add_float_slider(props, SETTING_BRIGHTNESS, 231 | TEXT_BRIGHTNESS, -1.0, 1.0, 0.01); 232 | obs_properties_add_float_slider(props, SETTING_GAMMA, TEXT_GAMMA, -1.0, 233 | 1.0, 0.01); 234 | 235 | UNUSED_PARAMETER(data); 236 | return props; 237 | } 238 | 239 | static void color_key_defaults(obs_data_t *settings) 240 | { 241 | obs_data_set_default_int(settings, SETTING_OPACITY, 100); 242 | obs_data_set_default_double(settings, SETTING_CONTRAST, 0.0); 243 | obs_data_set_default_double(settings, SETTING_BRIGHTNESS, 0.0); 244 | obs_data_set_default_double(settings, SETTING_GAMMA, 0.0); 245 | obs_data_set_default_int(settings, SETTING_KEY_COLOR, 0x00FF00); 246 | obs_data_set_default_string(settings, SETTING_COLOR_TYPE, "green"); 247 | obs_data_set_default_int(settings, SETTING_SIMILARITY, 80); 248 | obs_data_set_default_int(settings, SETTING_SMOOTHNESS, 50); 249 | } 250 | 251 | struct obs_source_info color_key_filter = { 252 | .id = "color_key_filter", 253 | .type = OBS_SOURCE_TYPE_FILTER, 254 | .output_flags = OBS_SOURCE_VIDEO, 255 | .get_name = color_key_name, 256 | .create = color_key_create, 257 | .destroy = color_key_destroy, 258 | .video_render = color_key_render, 259 | .update = color_key_update, 260 | .get_properties = color_key_properties, 261 | .get_defaults = color_key_defaults, 262 | }; 263 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | --------------------------------------------------------------------------------