-
Notifications
You must be signed in to change notification settings - Fork 4
/
RemapFrames.cpp
231 lines (206 loc) · 8.63 KB
/
RemapFrames.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "Common.h"
struct RemapData {
VSNodeRef *node1;
VSNodeRef *node2;
VSVideoInfo vi;
std::vector<unsigned int> frameMap;
};
static void VS_CC remapInit(VSMap *in, VSMap *out, void **instanceData, VSNode *node, VSCore *core, const VSAPI *vsapi) {
RemapData *d{ static_cast<RemapData*>(*instanceData) };
vsapi->setVideoInfo(&d->vi, 1, node);
}
static const VSFrameRef *VS_CC remapGetFrame(int n, int activationReason, void **instanceData, void **frameData, VSFrameContext *frameCtx, VSCore *core, const VSAPI *vsapi) {
RemapData *d{ static_cast<RemapData*>(*instanceData) };
if (activationReason == arInitial) {
if (d->frameMap[n] == UINT_MAX)
vsapi->requestFrameFilter(n, d->node1, frameCtx);
else
vsapi->requestFrameFilter(d->frameMap[n], d->node2, frameCtx);
}
else if (activationReason == arAllFramesReady) {
if (d->frameMap[n] == UINT_MAX)
return vsapi->getFrameFilter(n, d->node1, frameCtx);
return vsapi->getFrameFilter(d->frameMap[n], d->node2, frameCtx);
}
return nullptr;
}
static void VS_CC remapFree(void *instanceData, VSCore *core, const VSAPI *vsapi) {
RemapData *d{ static_cast<RemapData*>(instanceData) };
vsapi->freeNode(d->node1);
if (d->node1 != d->node2)
vsapi->freeNode(d->node2);
delete d;
}
//Map one frame to another. For the case: x y
static void matchIntToInt(const std::string &str, int &col, std::vector<unsigned int> &frameMap, const int &line, const bool &file, int maxFrames) {
int initialFrame{ getInt(str, col, line, file, maxFrames, Filter::REMAP_FRAMES) }; //Original frame number
skipWhitespace(str, col);
int replaceFrame{ getInt(str, col, line, file, maxFrames, Filter::REMAP_FRAMES) }; //Frame to be remapped to
frameMap[initialFrame] = replaceFrame;
}
//Maps a frame range to a single frame. For the case: [x y] z
static void matchRangeToInt(const std::string &str, int &col, std::vector<unsigned int> &frameMap, Range range, const int &line, const bool &file, int maxFrames) {
int replaceFrame{ getInt(str, col, line, file, maxFrames, Filter::REMAP_FRAMES) };
for (int i = range.start; i <= range.end; i++) {
frameMap[i] = replaceFrame;
}
}
//Maps one frame range to another. For the case: [x y] [z a]
//If the input and output ranges are not of equal size, the output
//range will be interpolated across the input range.
static void matchRangeToRange(const std::string &str, int &col, std::vector<unsigned int> &frameMap, Range rangeIn, const int &line, const bool &file, int maxFrames) {
Range rangeOut;
fillRange(str, col, rangeOut, line, file, maxFrames, Filter::REMAP_FRAMES);
int rangeInSize = rangeIn.end - rangeIn.start + 1;
double rangeOutSize = rangeOut.end - rangeOut.start;
rangeOutSize += (rangeOutSize < 0) ? -1 : 1;
rangeOutSize /= rangeInSize;
for (int i = 0; i < rangeInSize; i++)
{
frameMap[rangeIn.start + i] = int(rangeOut.start + rangeOutSize * i);
}
}
//The backbone of the filter. bool file indicates whether we are reading an input file or not.
//If we aren't reading an input file, then we are reading the mappings string.
//This checks for the following conditions:
//x y
//[x y] z
//[x y] [z a]
//
//If it isn't able to parse the string as any of the above three patterns, it throws a runtime error (Parse Error).
//Runtime errors (Index out of Bounds, Overflow, or Parse errors) can also be thrown from inside getInt and fillRange.
static void parse(std::string name, std::vector<unsigned int> &frameMap, void *stream, bool file, int maxFrames, const VSAPI *vsapi) {
int line{ 0 }; //Current line. This is simply for throwing detailed errors and has no other use.
int col{ 0 }; //Current column. This is used to track the current column position in the string we are at.
if (!file) {
skipWhitespace(name, col);
//If mappings string is empty.
if (col == name.size())
return;
}
std::string temp;
while (file ? std::getline(*static_cast<std::ifstream*>(stream), temp) : std::getline(*static_cast<std::stringstream*>(stream), temp)) {
col = 0;
while (col < temp.size()) {
skipWhitespace(temp, col);
char ch{ getChar(temp, col) };
if (ch != 0) {
if (ch == '#')
continue;
else if (std::isdigit(ch) || ch == '-')
matchIntToInt(temp, col, frameMap, line, file, maxFrames);
else if (ch == '[') {
++col;
Range rangeIn;
fillRange(temp, col, rangeIn, line, file, maxFrames, Filter::REMAP_FRAMES);
if (rangeIn.start > rangeIn.end) {
std::string location{ file ? " text file " : " mappings " };
std::string error{ "RemapFrames: Index out of bounds in" + location + "at line " + std::to_string(line + 1) + ", column " + std::to_string(col + 1) };
throw std::runtime_error(error);
}
skipWhitespace(temp, col);
ch = getChar(temp, col);
if (ch != 0) {
if (std::isdigit(ch) || ch == '-') {
matchRangeToInt(temp, col, frameMap, rangeIn, line, file, maxFrames);
}
else if (ch == '[') {
++col;
matchRangeToRange(temp, col, frameMap, rangeIn, line, file, maxFrames);
}
}
}
else {
std::string location{ file ? " text file " : " mappings " };
std::string error{ "RemapFrames: Parse Error in" + location + "at line " + std::to_string(line + 1) + ", column " + std::to_string(col + 1) };
throw std::runtime_error(error);
}
}
}
line++;
}
}
void VS_CC remapCreate(const VSMap *in, VSMap *out, void *userData, VSCore *core, const VSAPI *vsapi) {
RemapData d;
d.node1 = vsapi->propGetNode(in, "baseclip", 0, 0);
d.vi = *vsapi->getVideoInfo(d.node1);
int err;
//We use a const char* to store the value from propGetData as std::string crashes or has undefined behaviour when fed NULL.
std::string filename;
const char* fn{ vsapi->propGetData(in, "filename", 0, &err) };
if (err)
filename = "";
else
filename = fn;
std::string mappings;
const char* mp{ vsapi->propGetData(in, "mappings", 0, &err) };
if (err)
mappings = "";
else
mappings = mp;
//If sourceclip is not provided, we set sourceclip equal to baseclip.
d.node2 = vsapi->propGetNode(in, "sourceclip", 0, &err);
if (err)
d.node2 = d.node1;
bool mismatch{ !!vsapi->propGetInt(in, "mismatch", 0, &err) };
if (err)
mismatch = false;
//We do not accept variable clip lengths regardless of mismatch's value.
MismatchCauses mismatchCause = findCommonVi(&d.vi, d.node2, vsapi);
if (mismatchCause == MismatchCauses::DIFFERENT_LENGTHS) {
vsapi->setError(out, "RemapFrames: Clip lengths don't match");
//Free baseclip.
vsapi->freeNode(d.node1);
//If sourceclip and baseclip aren't the same, then free sourceclip.
//freeNode(node) doesn't change the value of node so the comparison is safe.
if (d.node1 != d.node2)
vsapi->freeNode(d.node2);
return;
}
if (static_cast<bool>(mismatchCause) && (!mismatch)) {
if (mismatchCause == MismatchCauses::DIFFERENT_DIMENSIONS)
vsapi->setError(out, "RemapFrames: Clip dimensions don't match");
else if (mismatchCause == MismatchCauses::DIFFERENT_FORMATS)
vsapi->setError(out, "RemapFrames: Clip formats don't match");
else if (mismatchCause == MismatchCauses::DIFFERENT_FRAMERATES)
vsapi->setError(out, "RemapFrames: Clip frame rates don't match");
vsapi->freeNode(d.node1);
if (d.node1 != d.node2)
vsapi->freeNode(d.node2);
return;
}
//We use a vector to store frame mappings. Each index represents a frame number,
//and each value at that index represents which frame it going to be replaced with.
//A value of UINT_MAX indicates that the frame doesn't need to be replaced.
d.frameMap.assign(d.vi.numFrames, UINT_MAX);
//Enclosed in a try catch block to catch any runtime errors.
//Frame mappings in the mappings string have higher precedence than
//the ones in the text file (they can override frame mappings in
//the text file).
try {
if (!filename.empty()) {
std::ifstream file(filename);
if (!file) {
vsapi->setError(out, "RemapFrames: Failed to open the timecodes file.");
vsapi->freeNode(d.node1);
if (d.node1 != d.node2)
vsapi->freeNode(d.node2);
return;
}
parse(filename, d.frameMap, &file, true, d.vi.numFrames, vsapi);
}
if (!mappings.empty()) {
std::stringstream stream(mappings);
parse(mappings, d.frameMap, &stream, false, d.vi.numFrames, vsapi);
}
}
catch (const std::exception &ex) {
vsapi->setError(out, ex.what());
vsapi->freeNode(d.node1);
if (d.node1 != d.node2)
vsapi->freeNode(d.node2);
return;
}
RemapData *data = new RemapData{ d };
vsapi->createFilter(in, out, "Remap", remapInit, remapGetFrame, remapFree, fmParallel, 0, data, core);
}