-
Notifications
You must be signed in to change notification settings - Fork 3
/
SCSelect.cpp
147 lines (126 loc) · 5.08 KB
/
SCSelect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "shared.h"
typedef struct {
VSNodeRef *input;
VSNodeRef *sceneBegin;
VSNodeRef *sceneEnd;
VSNodeRef *globalMotion;
const VSVideoInfo *vi;
int32_t dfactor;
int32_t hblocks;
int32_t incpitch;
uint32_t lastdiff;
uint32_t lnr;
double dirmult;
} SCSelectData;
static void VS_CC SCSelectFree(void *instanceData, VSCore *core, const VSAPI *vsapi)
{
SCSelectData *d = (SCSelectData *)instanceData;
vsapi->freeNode(d->input);
vsapi->freeNode(d->sceneBegin);
vsapi->freeNode(d->sceneEnd);
vsapi->freeNode(d->globalMotion);
free(d);
}
static const VSFrameRef *VS_CC SCSelectGetFrame(int32_t n, int32_t activationReason, void **instanceData, void **frameData, VSFrameContext *frameCtx, VSCore *core, const VSAPI *vsapi)
{
SCSelectData *d = (SCSelectData *) *instanceData;
if (activationReason == arInitial) {
if (n == 0) {
vsapi->requestFrameFilter(n, d->sceneBegin, frameCtx);
} else if (n >= d->vi->numFrames) {
vsapi->requestFrameFilter(n, d->sceneEnd, frameCtx);
} else if (n > 0) {
vsapi->requestFrameFilter(n, d->input, frameCtx);
vsapi->requestFrameFilter(n - 1, d->input, frameCtx);
if (n < d->vi->numFrames) {
vsapi->requestFrameFilter(n + 1, d->input, frameCtx);
}
vsapi->requestFrameFilter(n, d->sceneBegin, frameCtx);
vsapi->requestFrameFilter(n, d->sceneEnd, frameCtx);
vsapi->requestFrameFilter(n, d->globalMotion, frameCtx);
}
} else if (activationReason == arAllFramesReady) {
VSNodeRef *selected;
if (n == 0) {
set_begin:
selected = d->sceneBegin;
} else if (n >= d->vi->numFrames) {
set_end:
selected = d->sceneEnd;
} else {
const VSFrameRef *src_frame = vsapi->getFrameFilter(n, d->input, frameCtx);
if (d->lnr != n - 1) {
const VSFrameRef *prev_frame = vsapi->getFrameFilter(n - 1, d->input, frameCtx);
d->lastdiff = gdiff(vsapi->getReadPtr(src_frame, 0), vsapi->getStride(src_frame, 0),
vsapi->getReadPtr(prev_frame, 0), vsapi->getStride(prev_frame, 0),
d->hblocks, d->incpitch, d->vi->height);
vsapi->freeFrame(prev_frame);
}
int32_t olddiff = d->lastdiff;
const VSFrameRef *next_frame = vsapi->getFrameFilter(n + 1, d->input, frameCtx);
d->lastdiff = gdiff(vsapi->getReadPtr(src_frame, 0), vsapi->getStride(src_frame, 0),
vsapi->getReadPtr(next_frame, 0), vsapi->getStride(next_frame, 0),
d->hblocks, d->incpitch, d->vi->height);
d->lnr = n;
vsapi->freeFrame(src_frame);
vsapi->freeFrame(next_frame);
if(d->dirmult * olddiff < d->lastdiff ) {
goto set_end;
}
if(d->dirmult * d->lastdiff < olddiff ) {
goto set_begin;
}
selected = d->globalMotion;
}
return vsapi->getFrameFilter(n, selected, frameCtx);
}
return 0;
}
static void VS_CC SCSelectInit(VSMap *in, VSMap *out, void **instanceData, VSNode *node, VSCore *core, const VSAPI *vsapi)
{
SCSelectData *d = (SCSelectData *) *instanceData;
vsapi->setVideoInfo(d->vi, 1, node);
}
void VS_CC SCSelectCreate(const VSMap *in, VSMap *out, void *userData, VSCore *core, const VSAPI *vsapi)
{
SCSelectData d = { 0 };
d.input = vsapi->propGetNode(in, "input", 0, 0);
d.vi = vsapi->getVideoInfo(d.input);
if (!isConstantFormat(d.vi)) {
vsapi->freeNode(d.input);
vsapi->setError(out, "SCSelect: Only constant format input supported");
return;
}
if (d.vi->format->id != pfYUV420P8 && d.vi->format->id != pfYUV422P8) {
vsapi->freeNode(d.input);
vsapi->setError(out, "SCSelect: Only planar YV12 and YUY2 colorspaces are supported");
return;
}
d.sceneBegin = vsapi->propGetNode(in, "sceneBegin", 0, 0);
d.sceneEnd = vsapi->propGetNode(in, "sceneEnd", 0, 0);
d.globalMotion = vsapi->propGetNode(in, "globalMotion", 0, 0);
if (!isSameFormat(d.vi, vsapi->getVideoInfo(d.sceneBegin)) ||
!isSameFormat(d.vi, vsapi->getVideoInfo(d.sceneEnd)) ||
!isSameFormat(d.vi, vsapi->getVideoInfo(d.globalMotion))) {
vsapi->freeNode(d.input);
vsapi->freeNode(d.sceneBegin);
vsapi->freeNode(d.sceneEnd);
vsapi->freeNode(d.globalMotion);
vsapi->setError(out, "SCSelect: Clips are not of equal type");
return;
}
int32_t err;
double dFactor = vsapi->propGetFloat(in, "dfactor", 0, &err);
if (err) {
dFactor = 4.0;
}
d.hblocks = d.vi->width / (2 * 16);
d.incpitch = d.hblocks * (-2 * 16);
SCSelectData *data = (SCSelectData *)malloc(sizeof(d));
if (!data) {
vsapi->setError(out, "Could not allocate SCSelectData");
return;
}
*data = d;
vsapi->createFilter(in, out, "SCSelect", SCSelectInit, SCSelectGetFrame, SCSelectFree, fmParallel, 0, data, core);
};