2019-04-27 14:10:16 +00:00
|
|
|
/*
|
|
|
|
* Modern effects for a modern Streamer
|
|
|
|
* Copyright (C) 2019 Michael Fabian Dirks
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "filter-dynamic-mask.hpp"
|
|
|
|
#include <sstream>
|
|
|
|
#include "strings.hpp"
|
|
|
|
|
|
|
|
// Filter to allow dynamic masking
|
|
|
|
// Allow any channel to affect any other channel
|
|
|
|
//
|
|
|
|
// Red/Green/Blue/Alpha Mask Input
|
|
|
|
// - Red Mask Output
|
|
|
|
// - Blue Mask Output
|
|
|
|
// - Green Mask Output
|
|
|
|
// - Alpha Mask Output
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
#define ST "Filter.DynamicMask"
|
2019-04-27 14:10:16 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
#define ST_INPUT "Filter.DynamicMask.Input"
|
|
|
|
#define ST_CHANNEL "Filter.DynamicMask.Channel"
|
|
|
|
#define ST_CHANNEL_VALUE "Filter.DynamicMask.Channel.Value"
|
|
|
|
#define ST_CHANNEL_MULTIPLIER "Filter.DynamicMask.Channel.Multiplier"
|
|
|
|
#define ST_CHANNEL_INPUT "Filter.DynamicMask.Channel.Input"
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
static std::pair<filter::dynamic_mask::channel, const char*> channel_translations[] = {
|
|
|
|
{filter::dynamic_mask::channel::Red, S_CHANNEL_RED},
|
|
|
|
{filter::dynamic_mask::channel::Green, S_CHANNEL_GREEN},
|
|
|
|
{filter::dynamic_mask::channel::Blue, S_CHANNEL_BLUE},
|
|
|
|
{filter::dynamic_mask::channel::Alpha, S_CHANNEL_ALPHA},
|
|
|
|
};
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
P_INITIALIZER(FilterDynamicMaskInit)
|
2019-04-27 14:10:16 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
initializer_functions.push_back([] { filter::dynamic_mask::dynamic_mask_factory::initialize(); });
|
|
|
|
finalizer_functions.push_back([] { filter::dynamic_mask::dynamic_mask_factory::finalize(); });
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::shared_ptr<filter::dynamic_mask::dynamic_mask_factory> factory_instance = nullptr;
|
|
|
|
|
|
|
|
void filter::dynamic_mask::dynamic_mask_factory::initialize()
|
|
|
|
{
|
|
|
|
factory_instance = std::make_shared<filter::dynamic_mask::dynamic_mask_factory>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void filter::dynamic_mask::dynamic_mask_factory::finalize()
|
|
|
|
{
|
|
|
|
factory_instance.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<filter::dynamic_mask::dynamic_mask_factory> filter::dynamic_mask::dynamic_mask_factory::get()
|
|
|
|
{
|
|
|
|
return factory_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
filter::dynamic_mask::dynamic_mask_factory::dynamic_mask_factory()
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
memset(&_source_info, 0, sizeof(obs_source_info));
|
|
|
|
_source_info.id = "obs-stream-effects-filter-dynamic-mask";
|
|
|
|
_source_info.type = OBS_SOURCE_TYPE_FILTER;
|
|
|
|
_source_info.output_flags = OBS_SOURCE_VIDEO;
|
2019-04-27 14:10:16 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
_source_info.get_name = [](void*) { return D_TRANSLATE(ST); };
|
2019-04-27 14:10:16 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
_source_info.create = [](obs_data_t* settings, obs_source_t* source) {
|
2019-04-27 14:10:16 +00:00
|
|
|
return reinterpret_cast<void*>(new filter::dynamic_mask::dynamic_mask_instance(settings, source));
|
|
|
|
};
|
2019-08-04 14:20:26 +00:00
|
|
|
_source_info.destroy = [](void* _ptr) {
|
2019-04-27 14:10:16 +00:00
|
|
|
delete reinterpret_cast<filter::dynamic_mask::dynamic_mask_instance*>(_ptr);
|
|
|
|
};
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
_source_info.get_defaults2 = [](void*, obs_data_t* settings) {
|
|
|
|
obs_data_set_default_int(settings, ST_CHANNEL, static_cast<int64_t>(channel::Red));
|
2019-04-27 14:10:16 +00:00
|
|
|
for (auto kv : channel_translations) {
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_data_set_default_double(settings, (std::string(ST_CHANNEL_VALUE) + "." + kv.second).c_str(), 1.0);
|
|
|
|
obs_data_set_default_double(settings, (std::string(ST_CHANNEL_MULTIPLIER) + "." + kv.second).c_str(), 1.0);
|
2019-04-27 14:10:16 +00:00
|
|
|
for (auto kv2 : channel_translations) {
|
|
|
|
obs_data_set_default_double(
|
2019-08-04 14:20:26 +00:00
|
|
|
settings, (std::string(ST_CHANNEL_INPUT) + "." + kv.second + "." + kv2.second).c_str(), 0.0);
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-08-04 14:20:26 +00:00
|
|
|
_source_info.get_properties2 = [](void* _ptr, void* _type_data_ptr) {
|
2019-04-27 14:10:16 +00:00
|
|
|
obs_properties_t* props = obs_properties_create_param(_type_data_ptr, nullptr);
|
|
|
|
reinterpret_cast<filter::dynamic_mask::dynamic_mask_instance*>(_ptr)->get_properties(props);
|
|
|
|
return props;
|
|
|
|
};
|
2019-08-04 14:20:26 +00:00
|
|
|
_source_info.update = [](void* _ptr, obs_data_t* settings) {
|
2019-04-27 14:10:16 +00:00
|
|
|
reinterpret_cast<filter::dynamic_mask::dynamic_mask_instance*>(_ptr)->update(settings);
|
|
|
|
};
|
2019-08-04 14:20:26 +00:00
|
|
|
_source_info.load = [](void* _ptr, obs_data_t* settings) {
|
2019-04-27 14:10:16 +00:00
|
|
|
reinterpret_cast<filter::dynamic_mask::dynamic_mask_instance*>(_ptr)->load(settings);
|
|
|
|
};
|
2019-08-04 14:20:26 +00:00
|
|
|
_source_info.save = [](void* _ptr, obs_data_t* settings) {
|
2019-04-27 14:10:16 +00:00
|
|
|
reinterpret_cast<filter::dynamic_mask::dynamic_mask_instance*>(_ptr)->save(settings);
|
|
|
|
};
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
_source_info.video_tick = [](void* _ptr, float_t _seconds) {
|
2019-04-27 14:10:16 +00:00
|
|
|
reinterpret_cast<filter::dynamic_mask::dynamic_mask_instance*>(_ptr)->video_tick(_seconds);
|
|
|
|
};
|
2019-08-04 14:20:26 +00:00
|
|
|
_source_info.video_render = [](void* _ptr, gs_effect_t* _effect) {
|
2019-04-27 14:10:16 +00:00
|
|
|
reinterpret_cast<filter::dynamic_mask::dynamic_mask_instance*>(_ptr)->video_render(_effect);
|
|
|
|
};
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_register_source(&_source_info);
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filter::dynamic_mask::dynamic_mask_factory::~dynamic_mask_factory() {}
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
filter::dynamic_mask::dynamic_mask_instance::dynamic_mask_instance(obs_data_t* data, obs_source_t* self) : _self(self)
|
2019-04-27 14:10:16 +00:00
|
|
|
{
|
|
|
|
this->update(data);
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
this->_filter_rt = std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
|
|
|
|
this->_final_rt = std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
char* file = obs_module_file("effects/channel-mask._effect");
|
2019-04-27 14:10:16 +00:00
|
|
|
try {
|
2019-08-04 14:20:26 +00:00
|
|
|
this->_effect = std::make_shared<gs::effect>(file);
|
2019-07-27 17:57:17 +00:00
|
|
|
} catch (std::exception& ex) {
|
2019-08-04 14:20:26 +00:00
|
|
|
P_LOG_ERROR("Loading channel mask _effect failed with error(s):\n%s", ex.what());
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
2019-08-04 14:20:26 +00:00
|
|
|
assert(this->_effect != nullptr);
|
2019-04-27 14:10:16 +00:00
|
|
|
bfree(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filter::dynamic_mask::dynamic_mask_instance::~dynamic_mask_instance() {}
|
|
|
|
|
|
|
|
uint32_t filter::dynamic_mask::dynamic_mask_instance::get_width()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t filter::dynamic_mask::dynamic_mask_instance::get_height()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void filter::dynamic_mask::dynamic_mask_instance::get_properties(obs_properties_t* properties)
|
|
|
|
{
|
|
|
|
obs_property_t* p;
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
this->_translation_map.clear();
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
p = obs_properties_add_list(properties, ST_INPUT, D_TRANSLATE(ST_INPUT), OBS_COMBO_TYPE_LIST,
|
2019-04-27 14:10:16 +00:00
|
|
|
OBS_COMBO_FORMAT_STRING);
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_INPUT)));
|
2019-04-27 14:10:16 +00:00
|
|
|
obs_property_list_add_string(p, "", "");
|
|
|
|
obs::source_tracker::get()->enumerate(
|
|
|
|
[&p](std::string name, obs_source_t*) {
|
|
|
|
std::stringstream sstr;
|
2019-08-04 14:20:26 +00:00
|
|
|
sstr << name << " (" << D_TRANSLATE(S_SOURCETYPE_SOURCE) << ")";
|
2019-04-27 14:10:16 +00:00
|
|
|
obs_property_list_add_string(p, sstr.str().c_str(), name.c_str());
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
obs::source_tracker::filter_video_sources);
|
|
|
|
obs::source_tracker::get()->enumerate(
|
|
|
|
[&p](std::string name, obs_source_t*) {
|
|
|
|
std::stringstream sstr;
|
2019-08-04 14:20:26 +00:00
|
|
|
sstr << name << " (" << D_TRANSLATE(S_SOURCETYPE_SCENE) << ")";
|
2019-04-27 14:10:16 +00:00
|
|
|
obs_property_list_add_string(p, sstr.str().c_str(), name.c_str());
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
obs::source_tracker::filter_scenes);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
p = obs_properties_add_list(properties, ST_CHANNEL, D_TRANSLATE(ST_CHANNEL), OBS_COMBO_TYPE_LIST,
|
2019-04-27 14:10:16 +00:00
|
|
|
OBS_COMBO_FORMAT_INT);
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_CHANNEL)));
|
2019-04-27 14:10:16 +00:00
|
|
|
for (auto kv : channel_translations) {
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_property_list_add_int(p, D_TRANSLATE(kv.second), static_cast<int64_t>(kv.first));
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
obs_property_set_modified_callback2(p, modified, this);
|
|
|
|
|
|
|
|
for (auto kv : channel_translations) {
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string color = D_TRANSLATE(kv.second);
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string _chv = D_TRANSLATE(ST_CHANNEL_VALUE);
|
2019-04-27 14:10:16 +00:00
|
|
|
std::vector<char> _chv_data(_chv.size() * 2 + color.size() * 2, '\0');
|
|
|
|
sprintf_s(_chv_data.data(), _chv_data.size(), _chv.c_str(), color.c_str());
|
2019-08-04 14:20:26 +00:00
|
|
|
auto _chv_key = std::tuple{kv.first, channel::Invalid, std::string(ST_CHANNEL_VALUE)};
|
|
|
|
_translation_map.insert({_chv_key, std::string(_chv_data.begin(), _chv_data.end())});
|
|
|
|
auto chv = _translation_map.find(_chv_key);
|
|
|
|
std::string chv_key = std::string(ST_CHANNEL_VALUE) + "." + kv.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
p = obs_properties_add_float_slider(properties, chv_key.c_str(), chv->second.c_str(), -100.0, 100.0,
|
|
|
|
0.01);
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_CHANNEL_VALUE)));
|
2019-04-27 14:10:16 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string _chm = D_TRANSLATE(ST_CHANNEL_MULTIPLIER);
|
2019-04-27 14:10:16 +00:00
|
|
|
std::vector<char> _chm_data(_chm.size() * 2 + color.size() * 2, '\0');
|
|
|
|
sprintf_s(_chm_data.data(), _chm_data.size(), _chm.c_str(), color.c_str());
|
2019-08-04 14:20:26 +00:00
|
|
|
auto _chm_key = std::tuple{kv.first, channel::Invalid, std::string(ST_CHANNEL_MULTIPLIER)};
|
|
|
|
_translation_map.insert({_chm_key, std::string(_chm_data.begin(), _chm_data.end())});
|
|
|
|
auto chm = _translation_map.find(_chm_key);
|
|
|
|
std::string chm_key = std::string(ST_CHANNEL_MULTIPLIER) + "." + kv.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
p = obs_properties_add_float_slider(properties, chm_key.c_str(), chm->second.c_str(), -100.0, 100.0,
|
|
|
|
0.01);
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_CHANNEL_MULTIPLIER)));
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
for (auto kv1 : channel_translations) {
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string color1 = D_TRANSLATE(kv1.second);
|
2019-04-27 14:10:16 +00:00
|
|
|
for (auto kv2 : channel_translations) {
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string color2 = D_TRANSLATE(kv2.second);
|
2019-04-27 14:10:16 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string _chm = D_TRANSLATE(ST_CHANNEL_INPUT);
|
2019-04-27 14:10:16 +00:00
|
|
|
std::vector<char> _chm_data(_chm.size() * 2 + color1.size() * 2 + color2.size() * 2, '\0');
|
|
|
|
sprintf_s(_chm_data.data(), _chm_data.size(), _chm.c_str(), color1.c_str(), color2.c_str());
|
2019-08-04 14:20:26 +00:00
|
|
|
auto _chm_key = std::tuple{kv1.first, kv2.first, std::string(ST_CHANNEL_INPUT)};
|
|
|
|
_translation_map.insert({_chm_key, std::string(_chm_data.begin(), _chm_data.end())});
|
|
|
|
auto chm = _translation_map.find(_chm_key);
|
|
|
|
std::string chm_key = std::string(ST_CHANNEL_INPUT) + "." + kv1.second + "." + kv2.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
p = obs_properties_add_float_slider(properties, chm_key.c_str(), chm->second.c_str(), -100.0, 100.0,
|
|
|
|
0.01);
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_CHANNEL_INPUT)));
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void filter::dynamic_mask::dynamic_mask_instance::update(obs_data_t* settings)
|
|
|
|
{
|
|
|
|
// Update source.
|
|
|
|
try {
|
2019-08-04 14:20:26 +00:00
|
|
|
this->_input = std::make_shared<obs::source>(obs_data_get_string(settings, ST_INPUT));
|
|
|
|
this->_input_capture = std::make_shared<gfx::source_texture>(this->_input, _self);
|
|
|
|
this->_input->events.rename += std::bind(&filter::dynamic_mask::dynamic_mask_instance::input_renamed, this,
|
2019-04-27 14:10:16 +00:00
|
|
|
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
|
|
|
|
} catch (...) {
|
2019-08-04 14:20:26 +00:00
|
|
|
this->_input.reset();
|
|
|
|
this->_input_capture.reset();
|
|
|
|
this->_input_texture.reset();
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update data store
|
|
|
|
for (auto kv1 : channel_translations) {
|
2019-08-04 14:20:26 +00:00
|
|
|
auto found = this->_channels.find(kv1.first);
|
|
|
|
if (found == this->_channels.end()) {
|
|
|
|
this->_channels.insert({kv1.first, channel_data()});
|
|
|
|
found = this->_channels.find(kv1.first);
|
|
|
|
if (found == this->_channels.end()) {
|
|
|
|
assert(found != this->_channels.end());
|
|
|
|
throw std::exception("Unable to insert element into data _store.");
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string chv_key = std::string(ST_CHANNEL_VALUE) + "." + kv1.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
found->second.value = static_cast<float_t>(obs_data_get_double(settings, chv_key.c_str()));
|
2019-08-04 14:20:26 +00:00
|
|
|
this->_precalc.base.ptr[static_cast<size_t>(kv1.first)] = found->second.value;
|
2019-04-27 14:10:16 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string chm_key = std::string(ST_CHANNEL_MULTIPLIER) + "." + kv1.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
found->second.scale = static_cast<float_t>(obs_data_get_double(settings, chm_key.c_str()));
|
2019-08-04 14:20:26 +00:00
|
|
|
this->_precalc.scale.ptr[static_cast<size_t>(kv1.first)] = found->second.scale;
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
vec4* ch = nullptr;
|
|
|
|
switch (kv1.first) {
|
|
|
|
case channel::Red:
|
2019-08-04 14:20:26 +00:00
|
|
|
ch = &this->_precalc.matrix.x;
|
2019-04-27 14:10:16 +00:00
|
|
|
break;
|
|
|
|
case channel::Green:
|
2019-08-04 14:20:26 +00:00
|
|
|
ch = &this->_precalc.matrix.y;
|
2019-04-27 14:10:16 +00:00
|
|
|
break;
|
|
|
|
case channel::Blue:
|
2019-08-04 14:20:26 +00:00
|
|
|
ch = &this->_precalc.matrix.z;
|
2019-04-27 14:10:16 +00:00
|
|
|
break;
|
|
|
|
case channel::Alpha:
|
2019-08-04 14:20:26 +00:00
|
|
|
ch = &this->_precalc.matrix.t;
|
2019-04-27 14:10:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto kv2 : channel_translations) {
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string ab_key = std::string(ST_CHANNEL_INPUT) + "." + kv1.second + "." + kv2.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
found->second.values.ptr[static_cast<size_t>(kv2.first)] =
|
|
|
|
static_cast<float_t>(obs_data_get_double(settings, ab_key.c_str()));
|
|
|
|
ch->ptr[static_cast<size_t>(kv2.first)] = found->second.values.ptr[static_cast<size_t>(kv2.first)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void filter::dynamic_mask::dynamic_mask_instance::load(obs_data_t* settings)
|
|
|
|
{
|
|
|
|
update(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void filter::dynamic_mask::dynamic_mask_instance::save(obs_data_t* settings)
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
if (this->_input) {
|
|
|
|
obs_data_set_string(settings, ST_INPUT, obs_source_get_name(this->_input->get()));
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto kv1 : channel_translations) {
|
2019-08-04 14:20:26 +00:00
|
|
|
auto found = this->_channels.find(kv1.first);
|
|
|
|
if (found == this->_channels.end()) {
|
|
|
|
this->_channels.insert({kv1.first, channel_data()});
|
|
|
|
found = this->_channels.find(kv1.first);
|
|
|
|
if (found == this->_channels.end()) {
|
|
|
|
assert(found != this->_channels.end());
|
|
|
|
throw std::exception("Unable to insert element into data _store.");
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string chv_key = std::string(ST_CHANNEL_VALUE) + "." + kv1.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
obs_data_set_double(settings, chv_key.c_str(), static_cast<double_t>(found->second.value));
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string chm_key = std::string(ST_CHANNEL_MULTIPLIER) + "." + kv1.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
obs_data_set_double(settings, chm_key.c_str(), static_cast<double_t>(found->second.scale));
|
|
|
|
|
|
|
|
for (auto kv2 : channel_translations) {
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string ab_key = std::string(ST_CHANNEL_INPUT) + "." + kv1.second + "." + kv2.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
obs_data_set_double(settings, ab_key.c_str(),
|
|
|
|
static_cast<double_t>(found->second.values.ptr[static_cast<size_t>(kv2.first)]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void filter::dynamic_mask::dynamic_mask_instance::input_renamed(obs::source*, std::string old_name,
|
|
|
|
std::string new_name)
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_data_t* settings = obs_source_get_settings(_self);
|
|
|
|
obs_data_set_string(settings, ST_INPUT, new_name.c_str());
|
|
|
|
obs_source_update(_self, settings);
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool filter::dynamic_mask::dynamic_mask_instance::modified(void*, obs_properties_t* properties, obs_property_t*,
|
|
|
|
obs_data_t* settings)
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
channel mask = static_cast<channel>(obs_data_get_int(settings, ST_CHANNEL));
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
for (auto kv1 : channel_translations) {
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string chv_key = std::string(ST_CHANNEL_VALUE) + "." + kv1.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
obs_property_set_visible(obs_properties_get(properties, chv_key.c_str()), (mask == kv1.first));
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string chm_key = std::string(ST_CHANNEL_MULTIPLIER) + "." + kv1.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
obs_property_set_visible(obs_properties_get(properties, chm_key.c_str()), (mask == kv1.first));
|
|
|
|
|
|
|
|
for (auto kv2 : channel_translations) {
|
2019-08-04 14:20:26 +00:00
|
|
|
std::string io_key = std::string(ST_CHANNEL_INPUT) + "." + kv1.second + "." + kv2.second;
|
2019-04-27 14:10:16 +00:00
|
|
|
obs_property_set_visible(obs_properties_get(properties, io_key.c_str()), (mask == kv1.first));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void filter::dynamic_mask::dynamic_mask_instance::video_tick(float)
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
_have_input_texture = false;
|
|
|
|
_have_filter_texture = false;
|
|
|
|
_have_final_texture = false;
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void filter::dynamic_mask::dynamic_mask_instance::video_render(gs_effect_t* in_effect)
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_source_t* parent = obs_filter_get_parent(this->_self);
|
|
|
|
obs_source_t* target = obs_filter_get_target(this->_self);
|
2019-04-27 14:10:16 +00:00
|
|
|
uint32_t width = obs_source_get_base_width(target);
|
|
|
|
uint32_t height = obs_source_get_base_height(target);
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
if (!_self || !parent || !target || !width || !height || !_input || !_input_capture || !_effect) {
|
|
|
|
obs_source_skip_video_filter(_self);
|
2019-04-27 14:10:16 +00:00
|
|
|
return;
|
2019-08-04 14:20:26 +00:00
|
|
|
} else if (!_input->width() || !_input->height()) {
|
|
|
|
obs_source_skip_video_filter(_self);
|
2019-04-27 14:10:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gs_effect_t* default_effect = obs_get_base_effect(obs_base_effect::OBS_EFFECT_DEFAULT);
|
|
|
|
|
|
|
|
try { // Capture filter and input
|
2019-08-04 14:20:26 +00:00
|
|
|
if (!_have_filter_texture) {
|
|
|
|
if (obs_source_process_filter_begin(this->_self, GS_RGBA, OBS_ALLOW_DIRECT_RENDERING)) {
|
|
|
|
auto op = this->_filter_rt->render(width, height);
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
gs_blend_state_push();
|
|
|
|
gs_reset_blend_state();
|
|
|
|
gs_enable_blending(false);
|
|
|
|
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
|
|
|
|
|
|
|
|
gs_set_cull_mode(GS_NEITHER);
|
|
|
|
gs_enable_color(true, true, true, true);
|
|
|
|
|
|
|
|
gs_enable_depth_test(false);
|
|
|
|
gs_depth_function(GS_ALWAYS);
|
|
|
|
|
|
|
|
gs_enable_stencil_test(false);
|
|
|
|
gs_enable_stencil_write(false);
|
|
|
|
gs_stencil_function(GS_STENCIL_BOTH, GS_ALWAYS);
|
|
|
|
gs_stencil_op(GS_STENCIL_BOTH, GS_KEEP, GS_KEEP, GS_KEEP);
|
|
|
|
gs_ortho(0, (float)width, 0, (float)height, -1., 1.);
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_source_process_filter_end(this->_self, default_effect, width, height);
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
gs_blend_state_pop();
|
|
|
|
} else {
|
|
|
|
throw std::exception("Failed to render filter.");
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
this->_filter_texture = this->_filter_rt->get_texture();
|
|
|
|
this->_have_filter_texture = true;
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
if (!_have_input_texture) {
|
|
|
|
this->_input_texture = this->_input_capture->render(_input->width(), _input->height());
|
|
|
|
this->_have_input_texture = true;
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw source
|
2019-08-04 14:20:26 +00:00
|
|
|
if (!this->_have_final_texture) {
|
2019-04-27 14:10:16 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
auto op = this->_final_rt->render(width, height);
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
gs_blend_state_push();
|
|
|
|
gs_reset_blend_state();
|
|
|
|
gs_enable_blending(false);
|
|
|
|
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
|
|
|
|
|
|
|
|
gs_set_cull_mode(GS_NEITHER);
|
|
|
|
gs_enable_color(true, true, true, true);
|
|
|
|
|
|
|
|
gs_enable_depth_test(false);
|
|
|
|
gs_depth_function(GS_ALWAYS);
|
|
|
|
|
|
|
|
gs_enable_stencil_test(false);
|
|
|
|
gs_enable_stencil_write(false);
|
|
|
|
gs_stencil_function(GS_STENCIL_BOTH, GS_ALWAYS);
|
|
|
|
gs_stencil_op(GS_STENCIL_BOTH, GS_KEEP, GS_KEEP, GS_KEEP);
|
|
|
|
gs_ortho(0, (float)width, 0, (float)height, -1., 1.);
|
|
|
|
|
2019-08-04 21:12:30 +00:00
|
|
|
this->_effect->get_parameter("pMaskInputA")->set_texture(this->_filter_texture);
|
|
|
|
this->_effect->get_parameter("pMaskInputB")->set_texture(this->_input_texture);
|
2019-04-27 14:10:16 +00:00
|
|
|
|
2019-08-04 21:12:30 +00:00
|
|
|
this->_effect->get_parameter("pMaskBase")->set_float4(this->_precalc.base);
|
|
|
|
this->_effect->get_parameter("pMaskMatrix")->set_matrix(this->_precalc.matrix);
|
|
|
|
this->_effect->get_parameter("pMaskMultiplier")->set_float4(this->_precalc.scale);
|
2019-04-27 14:10:16 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
while (gs_effect_loop(this->_effect->get_object(), "Mask")) {
|
2019-04-27 14:10:16 +00:00
|
|
|
gs_draw_sprite(0, 0, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
gs_blend_state_pop();
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
this->_final_texture = this->_final_rt->get_texture();
|
|
|
|
this->_have_final_texture = true;
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
} catch (...) {
|
2019-08-04 14:20:26 +00:00
|
|
|
obs_source_skip_video_filter(this->_self);
|
2019-04-27 14:10:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
if (!_have_filter_texture || !_have_input_texture || !_have_final_texture) {
|
|
|
|
obs_source_skip_video_filter(this->_self);
|
2019-04-27 14:10:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-04 14:20:26 +00:00
|
|
|
if (!this->_filter_texture->get_object() || !this->_input_texture->get_object() || !this->_final_texture->get_object()) {
|
|
|
|
obs_source_skip_video_filter(this->_self);
|
2019-05-28 00:51:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-04-27 14:10:16 +00:00
|
|
|
|
|
|
|
// Draw source
|
|
|
|
{
|
|
|
|
// It is important that we do not modify the blend state here, as it is set correctly by OBS
|
|
|
|
gs_set_cull_mode(GS_NEITHER);
|
|
|
|
gs_enable_color(true, true, true, true);
|
|
|
|
gs_enable_depth_test(false);
|
|
|
|
gs_depth_function(GS_ALWAYS);
|
|
|
|
gs_enable_stencil_test(false);
|
|
|
|
gs_enable_stencil_write(false);
|
|
|
|
gs_stencil_function(GS_STENCIL_BOTH, GS_ALWAYS);
|
|
|
|
gs_stencil_op(GS_STENCIL_BOTH, GS_ZERO, GS_ZERO, GS_ZERO);
|
|
|
|
|
|
|
|
gs_effect_t* final_effect = in_effect ? in_effect : default_effect;
|
|
|
|
gs_eparam_t* param = gs_effect_get_param_by_name(final_effect, "image");
|
|
|
|
if (!param) {
|
2019-08-04 14:20:26 +00:00
|
|
|
P_LOG_ERROR("<filter-dynamic-mask:%s> Failed to set image param.", obs_source_get_name(this->_self));
|
|
|
|
obs_source_skip_video_filter(this->_self);
|
2019-04-27 14:10:16 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2019-08-04 14:20:26 +00:00
|
|
|
gs_effect_set_texture(param, this->_final_texture->get_object());
|
2019-04-27 14:10:16 +00:00
|
|
|
}
|
|
|
|
while (gs_effect_loop(final_effect, "Draw")) {
|
|
|
|
gs_draw_sprite(0, 0, width, height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|