Log in

View Full Version : C++ API for VapourSynth


Stephen R. Savage
6th June 2016, 03:10
Do you miss the AviSynth plugin API? Can't remember the signatures for all those VSAPI plugin callbacks? Dealing with optional filter arguments got you down? Memory leaks driving you crazy? Try VapourSynth++.

Consider the SDK invert example with VS++:


#include <stdexcept>
#include "vsxx_pluginmain.h"

using namespace vsxx;

class InvertFilter : public FilterBase {
FilterNode m_node;
VSVideoInfo m_vi;
bool m_enabled;
public:
InvertFilter(void * = nullptr) : m_vi(), m_enabled{} {}

const char *get_name(int) noexcept override
{
return "Invert";
}

std::pair<VSFilterMode, int> init(const ConstPropertyMap &in, const PropertyMap &out, const VapourCore &core)
{
m_node = in.get_prop<FilterNode>("clip");
m_vi = m_node.video_info();
m_enabled = in.get_prop<bool>("enabled", map::default_val(true));

if (!m_vi.format || m_vi.format->sampleType != stInteger || m_vi.format->bitsPerSample != 8)
throw std::runtime_error{ "clip must be 8-bit integer" };

return{ fmParallel, 0 };
}

std::pair<const VSVideoInfo *, size_t> get_video_info() noexcept override
{
return{ &m_vi, 1 };
}

void get_frame_initial(int n, const VapourCore &, VSFrameContext *frame_ctx) override
{
m_node.request_frame_filter(n, frame_ctx);
}

ConstVideoFrame get_frame(int n, const VapourCore &core, VSFrameContext *frame_ctx) override
{
ConstVideoFrame src = m_node.get_frame_filter(n, frame_ctx);

if (!m_enabled)
return src;

const VSFormat &format = src.format();
VideoFrame dst = core.new_video_frame(format, src.width(0), src.height(0));

for (int p = 0; p < format.numPlanes; ++p) {
const uint8_t *src_p = src.read_ptr(p);
uint8_t *dst_p = dst.write_ptr(p);

int src_stride = src.stride(p);
int dst_stride = dst.stride(p);
int w = src.width(p);
int h = src.height(p);

for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
dst_p[j] = ~src_p[j];
}
src_p += src_stride;
dst_p += dst_stride;
}
}
return dst;
}
};

const PluginInfo g_plugin_info = {
"com.example.invert", "invert", "VapourSynth Invert Example", {
{ &FilterBase::filter_create<InvertFilter>, "Invert", "clip:clip;enabled:int:opt;" }
}
};


VS++ eliminates boilerplate and lets you focus on your algorithm.

Download: CatHub (https://github.com/sekrit-twc/vsxx)

feisty2
6th June 2016, 10:28
New fancy toy, gr8

Mystery Keeper
6th June 2016, 13:41
Bad idea. There are many compilers with different ABI. Plugins written in C++ with such an API may or may not be compatible with VapourSynth, depending on which compiler they were compiled with.

splinter98
6th June 2016, 14:51
Bad idea. There are many compilers with different ABI. Plugins written in C++ with such an API may or may not be compatible with VapourSynth, depending on which compiler they were compiled with.

Surely that doesn't apply here as the C++ header here is actually a wrapper around the C interface, so the C++ is self contained to the module.

This is different to the AviSynth C++ API where it doesn't wrap around a C interface and as such has ABI issues due to C++ class mangling making linking between different compilers impossible.

Mystery Keeper
6th June 2016, 19:56
Surely that doesn't apply here as the C++ header here is actually a wrapper around the C interface, so the C++ is self contained to the module.

This is different to the AviSynth C++ API where it doesn't wrap around a C interface and as such has ABI issues due to C++ class mangling making linking between different compilers impossible.

Ah, okay. But that's all up to coders. Anyone who feels the need can write their wrapper.

splinter98
7th June 2016, 08:48
Ah, okay. But that's all up to coders. Anyone who feels the need can write their wrapper.



Yes, or use one that someone else has written. This is no different to using the c++ interface for other APIs like OpenCL or JNI. They're designed to provide a better interface that's more aligned to C++ coding style and should take advantage of the features that c++ uses that C doesn't.



I haven't spent much time reading this wrapper, but for a first pass it looks reasonably sane.

feisty2
6th June 2020, 18:01
has the name "vs++" been offically taken by this wrapper? the only name reference in its repository seems to be "vsxx"

I will take the name "vs++" for my wrapper if it is still available