Skip to content

Instantly share code, notes, and snippets.

@stuxcrystal
Created August 5, 2019 21:45
Show Gist options
  • Save stuxcrystal/a9867f17a44663330c1ae686c4a08c8d to your computer and use it in GitHub Desktop.
Save stuxcrystal/a9867f17a44663330c1ae686c4a08c8d to your computer and use it in GitHub Desktop.
Using suspendFrame
//////////////////////////////////////////
// This file contains a simple filter
// skeleton you can use to get started.
// With no changes it simply passes
// frames through.
#include <thread>
#include <chrono>
#include "VapourSynth.h"
#include "VSHelper.h"
using namespace std::literals;
typedef struct {
VSNodeRef *node;
const VSVideoInfo *vi;
} FilterData;
static void VS_CC filterInit(VSMap *in, VSMap *out, void **instanceData, VSNode *node, VSCore *core, const VSAPI *vsapi) {
FilterData *d = (FilterData *) * instanceData;
vsapi->setVideoInfo(d->vi, 1, node);
}
static const VSFrameRef *VS_CC filterGetFrame(int n, int activationReason, void **instanceData, void **frameData, VSFrameContext *frameCtx, VSCore *core, const VSAPI *vsapi) {
FilterData *d = (FilterData *) * instanceData;
VSSuspensionContext* suspension = nullptr;
if (activationReason == arInitial) {
suspension = vsapi->suspendFrame(core, frameCtx);
try {
/*
* Do whatever you want here.
*
* This is just a simple example by creating a thread that just sleeps for
* 10 seconds before resuming.
*/
std::thread([suspension, vsapi](){
std::this_thread::sleep_for(10s);
vsapi->resumeFrame(suspension);
}).detach();
} catch(...) {
vsapi->setFilterError("Failed to start thread", frameCtx);
// Cancel the suspension as an error occured while passing the suspension-context
// to another thread.
vsapi->cancelSuspend(suspension, frameCtx);
return 0;
}
} else if (activationReason == arResume) {
vsapi->requestFrameFilter(n, d->node, frameCtx);
} else if (activationReason == arAllFramesReady) {
const VSFrameRef *frame = vsapi->getFrameFilter(n, d->node, frameCtx);
return frame;
}
return 0;
}
static void VS_CC filterFree(void *instanceData, VSCore *core, const VSAPI *vsapi) {
FilterData *d = (FilterData *)instanceData;
vsapi->freeNode(d->node);
free(d);
}
static void VS_CC filterCreate(const VSMap *in, VSMap *out, void *userData, VSCore *core, const VSAPI *vsapi) {
FilterData d;
FilterData *data;
d.node = vsapi->propGetNode(in, "clip", 0, 0);
d.vi = vsapi->getVideoInfo(d.node);
data = (FilterData*)malloc(sizeof(d));
*data = d;
vsapi->createFilter(in, out, "Filter", filterInit, filterGetFrame, filterFree, fmParallel, 0, data, core);
}
//////////////////////////////////////////
// Init
VS_EXTERNAL_API(void) VapourSynthPluginInit(VSConfigPlugin configFunc, VSRegisterFunction registerFunc, VSPlugin *plugin) {
configFunc("moe.encode.delay", "delay", "Simple Suspend Frame example", VAPOURSYNTH_API_VERSION, 1, plugin);
registerFunc("Delay", "clip:clip;", filterCreate, 0, plugin);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment