forked from xaxaxa/libytpmv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
videorenderer.C
421 lines (362 loc) · 13.2 KB
/
videorenderer.C
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include <ytpmv/videorenderer.H>
#include <ytpmv/framerenderer2.H>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <assert.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
using namespace std;
namespace ytpmv {
struct NoteEventV {
int t;
int segmentIndex;
bool off;
};
static bool operator<(const NoteEventV& a, const NoteEventV& b) {
return a.t < b.t;
}
// comparator for segment id that will sort by zIndex of the video segments
struct SegmentCompare {
const vector<VideoSegment>& segments;
SegmentCompare(const vector<VideoSegment>& segments): segments(segments) {
}
bool operator()(int a, int b) {
if(this->segments.at(a).zIndex < this->segments.at(b).zIndex) return true;
if(this->segments.at(a).zIndex > this->segments.at(b).zIndex) return false;
return a<b;
}
};
// returns a fast key based on the addresses of the shader strings
uint64_t shaderKey(const string* shader, const string* vertexShader, const string* fragmentShader) {
return ((uint64_t)shader) ^ ((uint64_t)vertexShader) ^ ((uint64_t)fragmentShader);
}
uint64_t shaderKey(const VideoSegment& seg) {
return shaderKey(seg.shader, seg.vertexShader, seg.fragmentShader);
}
// returns a hashed key based on the shader text body
size_t shaderHashKey(const VideoSegment& seg) {
size_t ret = 0;
hash<string> hashFn;
if(seg.shader != nullptr) ret ^= hashFn(*seg.shader);
if(seg.vertexShader != nullptr) ret ^= hashFn(*seg.vertexShader);
if(seg.fragmentShader != nullptr) ret ^= hashFn(*seg.fragmentShader);
return ret;
}
class ShaderProgramCache {
public:
vector<string> shaders;
unordered_map<size_t, int> shaderIDs;
unordered_map<uint64_t, int> shaderIDs2;
void buildCache(const vector<VideoSegment>& segments) {
// find all used shader code strings
int nextIndex = 0;
int i=-1;
for(const VideoSegment& seg: segments) {
i++;
// we identify a segment's shader program by the tuple (shader, vertexShader, fragmentShader);
// if several segments have the same tuple then the shader program can be reused.
uint64_t key = shaderKey(seg);
if(shaderIDs2.find(key) != shaderIDs2.end()) continue;
size_t hashKey = shaderHashKey(seg);
if(shaderIDs.find(hashKey) != shaderIDs.end()) {
shaderIDs2[key] = shaderIDs[hashKey];
continue;
}
shaderIDs[hashKey] = nextIndex;
shaderIDs2[key] = nextIndex;
if(seg.vertexShader == nullptr)
shaders.push_back(defaultVertexShader);
else shaders.push_back(*seg.vertexShader);
if(seg.fragmentShader != nullptr)
shaders.push_back(*seg.fragmentShader);
else if(seg.shader != nullptr)
shaders.push_back(FrameRenderer2_generateCode(*seg.shader, MAXUSERPARAMS));
else shaders.push_back(defaultFragmentShader);
nextIndex++;
}
PRNT(0, "%d unique shader keys\n", (int)shaderIDs2.size());
PRNT(0, "%d shader programs\n", nextIndex);
}
int getShaderProgramIndex(const VideoSegment& seg) {
assert(shaderIDs2.find(shaderKey(seg)) != shaderIDs2.end());
return shaderIDs2[shaderKey(seg)];
}
};
void renderVideo(const vector<VideoSegment>& segments, double fps, int w, int h, function<void(uint8_t* data)> writeFrame) {
FrameRenderer fr(w,h);
ShaderProgramCache shaderCache;
shaderCache.buildCache(segments);
fr.setRenderers(shaderCache.shaders);
// convert note list into note event list
vector<NoteEventV> events;
for(int i=0;i<(int)segments.size();i++) {
if(segments[i].startSeconds >= segments[i].endSeconds) continue;
NoteEventV evt;
evt.t = (int)round(segments[i].startSeconds*fps);
evt.off = false;
evt.segmentIndex = i;
events.push_back(evt);
evt.t = (int)round(segments[i].endSeconds*fps);
evt.off = true;
events.push_back(evt);
}
// sort based on event time
sort(events.begin(), events.end());
basic_string<float> buf;
int evts = (int)events.size();
// go through all note events and render the regions between events
// map from note index to start time in frames
map<int, int, SegmentCompare> notesActive(SegmentCompare{segments});
for(int i=0;i<evts-1;i++) {
NoteEventV evt = events[i];
int curTimeFrames = events[i].t;
int nextTimeFrames = events[i+1].t;
if(!evt.off) { // note on
notesActive[evt.segmentIndex] = curTimeFrames;
const VideoSegment& s = segments.at(evt.segmentIndex);
PRNT(0, "videoclip on: %5d: dur %3.2fs\n", evt.segmentIndex, s.durationSeconds());
} else {
notesActive.erase(evt.segmentIndex);
PRNT(0, "videoclip off:%5d\n", evt.segmentIndex);
}
if(curTimeFrames >= nextTimeFrames) continue;
int durationFrames = nextTimeFrames-curTimeFrames;
// collect renderers and parameters for this region
vector<int> enabledRenderers;
vector<int> instanceCount;
vector<vector<float> > params;
for(auto it = notesActive.begin(); it!=notesActive.end(); it++) {
const VideoSegment& s = segments.at((*it).first);
enabledRenderers.push_back(shaderCache.getShaderProgramIndex(s));
params.push_back(s.shaderParams);
instanceCount.push_back(s.instances);
}
fr.setEnabledRenderers(enabledRenderers);
fr.setInstanceCount(instanceCount);
fr.setUserParams(params);
int j=0;
for(auto it = notesActive.begin(); it!=notesActive.end(); it++) {
const VideoSegment& s = segments.at((*it).first);
fr.setVertexes(j, s.vertexes, s.vertexVarSizes);
j++;
}
// render frames in this region
vector<float> relTimeSeconds(notesActive.size());
vector<uint32_t> textures(notesActive.size());
for(int j=0; j<durationFrames; j++) {
int k=0;
double timeSeconds = double(curTimeFrames+j)/fps;
for(auto it = notesActive.begin(); it!=notesActive.end(); it++) {
const VideoSegment& seg = segments.at((*it).first);
double t = timeSeconds-seg.startSeconds;
textures[k] = seg.source->getFrame(t*seg.speed + seg.offsetSeconds);
fr.setImage(k, textures[k]);
relTimeSeconds[k] = float(t);
// find current keyframe
for(auto& kf: seg.keyframes) {
if(kf.relTimeSeconds <= t)
params.at(k) = kf.shaderParams;
else break;
}
k++;
}
fr.setTime(float(timeSeconds), relTimeSeconds);
fr.setUserParams(params);
string imgData = fr.render();
// release textures
k=0;
for(auto it = notesActive.begin(); it!=notesActive.end(); it++) {
const VideoSegment& seg = segments.at((*it).first);
seg.source->releaseFrame(textures[k]);
k++;
}
writeFrame((uint8_t*)imgData.data());
}
}
}
class VideoRendererState {
public:
FrameRenderer fr;
const vector<VideoSegment>& segments;
double fps, systemFPS;
int lastEventIndex;
int curFrame;
vector<NoteEventV> events;
ShaderProgramCache shaderCache;
map<int, int, SegmentCompare> notesActive; // map from note index to start time in frames
vector<vector<float> > curUserParams;
VideoRendererState(const vector<VideoSegment>& segments, int w, int h, double fps, double systemFPS):
fr(w,h), segments(segments), fps(fps), systemFPS(systemFPS),
notesActive(SegmentCompare(segments)) {
lastEventIndex = 0;
curFrame = INT_MIN;
shaderCache.buildCache(segments);
fr.setRenderers(shaderCache.shaders);
// prepare all sources
unordered_set<VideoSource*> sources;
for(const VideoSegment& seg: segments)
sources.insert(seg.source);
for(VideoSource* source: sources)
source->prepare();
// convert note list into note event list
for(int i=0;i<(int)segments.size();i++) {
if(segments[i].startSeconds >= segments[i].endSeconds) continue;
NoteEventV evt;
evt.t = (int)round(segments[i].startSeconds*fps);
evt.off = false;
evt.segmentIndex = i;
events.push_back(evt);
evt.t = (int)round(segments[i].endSeconds*fps);
evt.off = true;
events.push_back(evt);
}
// sort based on event time
sort(events.begin(), events.end());
// draw to screen
fr.setRenderToScreen();
}
void interpolateKeyframes(vector<float>& out, const vector<float>& kf1, const vector<float>& kf2,
double t1, double t2, double t) {
int sz = (int)out.size();
double a = (t1==t2) ? 0. : clamp((t-t1)/(t2-t1),0.,1.);
double b = 1. - a;
for(int i=0; i<sz; i++)
out[i] = b*kf1.at(i) + a*kf2.at(i);
}
string drawFrame(bool render = false) {
string ret;
int k=0;
double timeSeconds = curFrame/fps;
vector<float> relTimeSeconds(notesActive.size());
vector<uint32_t> textures(notesActive.size());
for(auto it = notesActive.begin(); it!=notesActive.end(); it++) {
const VideoSegment& seg = segments.at((*it).first);
// find source frame
double relTime = timeSeconds-seg.startSeconds;
textures[k] = seg.source->getFrame(relTime*seg.speed + seg.offsetSeconds);
fr.setImage(k, textures[k]);
// set parameters
relTimeSeconds[k] = float(timeSeconds-seg.startSeconds);
// find current keyframe
int kfIndex = -1;
double kfTime = 0.;
const vector<float>* kfLeft = &seg.shaderParams;
for(int i=0; i<(int)seg.keyframes.size(); i++) {
if(seg.keyframes[i].relTimeSeconds <= relTime)
kfIndex = i;
else break;
}
if(kfIndex >= 0) {
kfLeft = &seg.keyframes[kfIndex].shaderParams;
kfTime = seg.keyframes[kfIndex].relTimeSeconds;
}
// find next keyframe
const vector<float>* kfRight = kfLeft;
double kfRightTime = kfTime;
if((kfIndex+1) < (int)seg.keyframes.size()) {
kfRight = &seg.keyframes[kfIndex+1].shaderParams;
kfRightTime = seg.keyframes[kfIndex+1].relTimeSeconds;
}
if(seg.interpolateKeyframes != nullptr)
seg.interpolateKeyframes(curUserParams.at(k), *kfLeft, *kfRight, kfTime, kfRightTime, relTime);
else interpolateKeyframes(curUserParams.at(k), *kfLeft, *kfRight, kfTime, kfRightTime, relTime);
k++;
}
fr.setTime(float(timeSeconds), relTimeSeconds);
fr.setUserParams(curUserParams);
if(render) ret = fr.render();
else fr.draw();
// release textures
k=0;
for(auto it = notesActive.begin(); it!=notesActive.end(); it++) {
const VideoSegment& seg = segments.at((*it).first);
seg.source->releaseFrame(textures[k]);
k++;
}
return ret;
}
// returns true if we are still within bounds of the video
bool advanceTo(int frame) {
if(frame <= curFrame) return true;
bool encounteredEvent = false;
//PRNT(0, "%d events\n", (int)events.size());
// go through events until one beyond the requested frame is encountered
while(lastEventIndex < (int)events.size()) {
NoteEventV& evt = events[lastEventIndex];
if(evt.t > frame) break;
// if the event's start time is after curFrame then we haven't processed it yet
if(evt.t > curFrame) {
PRNT(1, "event %d: ", lastEventIndex);
if(!evt.off) { // note on
notesActive[evt.segmentIndex] = evt.t;
const VideoSegment& s = segments.at(evt.segmentIndex);
PRNT(1, "videoclip on: %5d: dur %3.2fs\n", evt.segmentIndex, s.durationSeconds());
} else {
notesActive.erase(evt.segmentIndex);
PRNT(1, "videoclip off:%5d\n", evt.segmentIndex);
}
encounteredEvent = true;
}
lastEventIndex++;
}
if(lastEventIndex > 0)
lastEventIndex--;
// if the current event is the last event, we are past the end of the video
if(lastEventIndex >= (int(events.size())-1)) return false;
curFrame = frame;
if(encounteredEvent) {
// collect renderers and parameters for this region
vector<int> enabledRenderers;
vector<int> instanceCount;
int lastZIndex = -(1<<29);
curUserParams.clear();
for(auto it = notesActive.begin(); it!=notesActive.end(); it++) {
const VideoSegment& s = segments.at((*it).first);
enabledRenderers.push_back(shaderCache.getShaderProgramIndex(s));
curUserParams.push_back(s.shaderParams);
instanceCount.push_back(s.instances);
assert(s.zIndex >= lastZIndex);
lastZIndex = s.zIndex;
}
fr.setEnabledRenderers(enabledRenderers);
fr.setInstanceCount(instanceCount);
fr.setUserParams(curUserParams);
int i=0;
for(auto it = notesActive.begin(); it!=notesActive.end(); it++) {
const VideoSegment& s = segments.at((*it).first);
fr.setVertexes(i, s.vertexes, s.vertexVarSizes);
i++;
}
}
return true;
}
};
void renderVideo2(const vector<VideoSegment>& segments, double fps, double startSeconds, int w, int h, function<void(uint8_t* data)> writeFrame) {
VideoRendererState r(segments, w, h, fps, fps);
int frame = (int)round(startSeconds*fps);
r.fr.setRenderToInternal();
while(r.advanceTo(frame)) {
string tmp = r.drawFrame(true);
writeFrame((uint8_t*)tmp.data());
frame++;
}
}
VideoRendererTimeDriven::VideoRendererTimeDriven(const vector<VideoSegment>& segments, int w, int h, double fps, double systemFPS) {
st = new VideoRendererState(segments,w,h,fps,systemFPS);
}
VideoRendererTimeDriven::~VideoRendererTimeDriven() {
delete st;
}
void VideoRendererTimeDriven::drawFrame() {
st->drawFrame();
}
bool VideoRendererTimeDriven::advanceTo(int frame) {
return st->advanceTo(frame);
}
int VideoRendererTimeDriven::concurrentSegments() {
return (int)st->notesActive.size();
}
}