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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "allocator.hpp"
#include "core/transformGeoParams.hpp"
#include "core1/panoRemapper.hpp"
#include "core1/imageMapping.hpp"
#include "core1/imageMerger.hpp"
#include "gpu/image/imageOps.hpp"
#include "libvideostitch/panoDef.hpp"
#include "libvideostitch/overlay.hpp"
//#define DUMP_FINAL
//#define DUMP_ORIGIN
#if defined(DUMP_FINAL) || defined DUMP_ORIGIN
#include "util/debugUtils.hpp"
#endif
namespace VideoStitch {
namespace Core {
SourceSurface::Pimpl::Pimpl(GPU::Surface* s, GPU::Stream stream) : surface(s), stream(stream) {}
SourceSurface::Pimpl::~Pimpl() {
stream.destroy();
delete surface;
}
PanoSurface::Pimpl::Pimpl(GPU::Stream stream, GPU::Buffer<uint32_t> buffer, size_t w, size_t h)
: buffer(buffer), stream(stream), width(w), height(h) {}
PanoPimpl::PanoPimpl(GPU::Stream stream, GPU::Buffer<uint32_t> buffer, GPU::Surface* remap, size_t w, size_t h)
: PanoSurface::Pimpl(stream, buffer, w, h), remapBuffer(remap) {}
CubemapPimpl::CubemapPimpl(bool equiangular, GPU::Stream stream, GPU::Buffer<uint32_t>* bufs,
GPU::Buffer<uint32_t> buffer, GPU::CubemapSurface* cubemap, GPU::Buffer<uint32_t> t,
size_t w)
: CubemapSurface::Pimpl(stream, buffer, w), equiangular(equiangular), remapBuffer(cubemap), tmp(t) {
for (int i = 0; i < 6; i++) {
buffers[i] = bufs[i];
}
}
PanoSurface::Pimpl::~Pimpl() {
stream.destroy();
if (!externalAlloc) {
buffer.release();
}
}
PanoPimpl::~PanoPimpl() { delete remapBuffer; }
CubemapPimpl::~CubemapPimpl() {
if (!externalAlloc) {
for (int i = 0; i < 6; i++) {
buffers[i].release();
}
}
delete remapBuffer;
tmp.release();
}
Potential<SourceSurface::Pimpl> SourceSurface::Pimpl::create(GPU::Surface* surface) {
PotentialValue<GPU::Stream> stream = GPU::Stream::create();
if (stream.ok()) {
return Potential<Pimpl>(new Pimpl(surface, stream.value()));
} else {
return Potential<Pimpl>(stream.status());
}
}
void SourceSurface::acquire() { pimpl->acquireReader(); }
void SourceSurface::release() { pimpl->releaseReader(); }
void SourceSurface::Pimpl::acquireWriter() {
std::unique_lock<std::mutex> lk(mutex);
cv.wait(lk, [this] { return renderers == 0; });
stitcher = true;
}
void SourceSurface::Pimpl::releaseWriter() {
{
std::lock_guard<std::mutex> lk(mutex);
stitcher = false;
}
cv.notify_all();
}
void SourceSurface::Pimpl::acquireReader() {
std::unique_lock<std::mutex> lk(mutex);
cv.wait(lk, [this] { return !stitcher; });
renderers++;
}
void SourceSurface::Pimpl::releaseReader() {
{
std::lock_guard<std::mutex> lk(mutex);
renderers--;
}
cv.notify_one();
}
size_t SourceSurface::getWidth() const { return pimpl->getWidth(); }
size_t SourceSurface::getHeight() const { return pimpl->getHeight(); }
size_t SourceSurface::Pimpl::getWidth() const { return surface->width(); }
size_t SourceSurface::Pimpl::getHeight() const { return surface->height(); }
Potential<PanoPimpl> PanoPimpl::create(GPU::Buffer<uint32_t> buffer, GPU::Surface* surface, size_t w, size_t h) {
PotentialValue<GPU::Stream> stream = GPU::Stream::create();
if (stream.ok()) {
return Potential<PanoPimpl>(new PanoPimpl(stream.value(), buffer, surface, w, h));
} else {
return Potential<PanoPimpl>(stream.status());
}
}
void PanoSurface::acquire() { pimpl->acquireReader(); }
void PanoSurface::release() { pimpl->releaseReader(); }
void PanoSurface::Pimpl::acquireWriter() {
std::unique_lock<std::mutex> lk(mutex);
cv.wait(lk, [this] { return renderers == 0; });
stitcher = true;
}
void PanoSurface::Pimpl::releaseWriter() {
{
std::lock_guard<std::mutex> lk(mutex);
stitcher = false;
}
cv.notify_all();
}
void PanoSurface::Pimpl::acquireReader() {
std::unique_lock<std::mutex> lk(mutex);
cv.wait(lk, [this] { return !stitcher; });
renderers++;
}
void PanoSurface::Pimpl::releaseReader() {
{
std::lock_guard<std::mutex> lk(mutex);
renderers--;
}
cv.notify_one();
}
size_t PanoSurface::getWidth() const { return pimpl->getWidth(); }
size_t PanoSurface::getHeight() const { return pimpl->getHeight(); }
SourceSurface::SourceSurface(Pimpl* pimpl) : pimpl(pimpl) {}
SourceSurface::~SourceSurface() { delete pimpl; }
PanoSurface::PanoSurface(Pimpl* pimpl) : pimpl(pimpl) {}
PanoSurface::~PanoSurface() { delete pimpl; }
CubemapSurface::CubemapSurface(Pimpl* pimpl) : PanoSurface(pimpl) {}
CubemapSurface::~CubemapSurface() {}
size_t CubemapSurface::getLength() const { return dynamic_cast<CubemapPimpl*>(pimpl)->getLength(); }
Status PanoPimpl::reset(const Core::ImageMerger* merger) {
if (merger && (merger->warpMergeType() == Core::ImageMerger::Format::Gradient)) {
return memsetToZeroAsync(*remapBuffer, stream);
} else {
return memsetToZeroAsync(buffer, stream);
}
}
Status PanoPimpl::reproject(const Core::PanoDefinition& pano, const Matrix33<double>& perspective,
const Core::ImageMerger* merger) {
#if defined(DUMP_ORIGIN)
if (merger && (merger->warpMergeType() == Core::ImageMerger::Format::Gradient)) {
memcpyAsync(buffer, *remapBuffer, stream);
}
stream.synchronize();
Debug::dumpRGBADeviceBuffer("origin.png", buffer, (unsigned)pano.getWidth(), (unsigned)pano.getHeight());
#endif
if (width != (unsigned)pano.getWidth() || height != (unsigned)pano.getHeight()) {
return {Origin::Surface, ErrType::InvalidConfiguration, "Surface unadapted to the current panorama configuration"};
}
if (!(merger && (merger->warpMergeType() == Core::ImageMerger::Format::Gradient))) {
memcpyAsync(*remapBuffer, buffer.as_const(), stream);
}
float2 srcScale = {
Core::TransformGeoParams::computePanoScale(Core::PanoProjection::Equirectangular, pano.getWidth(), 360.f),
2 * Core::TransformGeoParams::computePanoScale(Core::PanoProjection::Equirectangular, pano.getHeight(), 360.f)};
float2 dstScale = {
Core::TransformGeoParams::computePanoScale(pano.getProjection(), pano.getWidth(), (float)pano.getHFOV()),
Core::TransformGeoParams::computePanoScale(pano.getProjection(), pano.getWidth(), (float)pano.getHFOV())};
switch (pano.getProjection()) {
case Core::PanoProjection::Rectilinear:
return Core::reprojectRectilinear(buffer, dstScale, *remapBuffer, srcScale, (unsigned)pano.getWidth(),
(unsigned)pano.getHeight(), perspective, stream);
case Core::PanoProjection::Cylindrical:
assert(false);
return Status::OK();
case Core::PanoProjection::Equirectangular:
return Core::reprojectEquirectangular(buffer, dstScale, *remapBuffer, srcScale, (unsigned)pano.getWidth(),
(unsigned)pano.getHeight(), perspective, stream);
case Core::PanoProjection::FullFrameFisheye:
return Core::reprojectFullFrameFisheye(buffer, dstScale, *remapBuffer, srcScale, (unsigned)pano.getWidth(),
(unsigned)pano.getHeight(), perspective, stream);
case Core::PanoProjection::CircularFisheye:
return Core::reprojectCircularFisheye(buffer, dstScale, *remapBuffer, srcScale, (unsigned)pano.getWidth(),
(unsigned)pano.getHeight(), perspective, stream);
case Core::PanoProjection::Stereographic:
return Core::reprojectStereographic(buffer, dstScale, *remapBuffer, srcScale, (unsigned)pano.getWidth(),
(unsigned)pano.getHeight(), perspective, stream);
case Core::PanoProjection::Cubemap:
case Core::PanoProjection::EquiangularCubemap:
assert(false);
return Status::OK();
}
return Status::OK();
}
Status CubemapPimpl::reproject(const Core::PanoDefinition& pano, const Matrix33<double>& perspective,
const Core::ImageMerger*) {
#if defined(DUMP_ORIGIN)
stream.synchronize();
Debug::dumpRGBADeviceBuffer("origin_face_+x.png", buffers[0], (unsigned)pano.getLength(), (unsigned)pano.getLength());
Debug::dumpRGBADeviceBuffer("origin_face_-x.png", buffers[1], (unsigned)pano.getLength(), (unsigned)pano.getLength());
Debug::dumpRGBADeviceBuffer("origin_face_+y.png", buffers[2], (unsigned)pano.getLength(), (unsigned)pano.getLength());
Debug::dumpRGBADeviceBuffer("origin_face_-y.png", buffers[3], (unsigned)pano.getLength(), (unsigned)pano.getLength());
Debug::dumpRGBADeviceBuffer("origin_face_+z.png", buffers[4], (unsigned)pano.getLength(), (unsigned)pano.getLength());
Debug::dumpRGBADeviceBuffer("origin_face_-z.png", buffers[5], (unsigned)pano.getLength(), (unsigned)pano.getLength());
#endif
memcpyCubemapAsync(*remapBuffer, buffers[0], buffers[1], buffers[2], buffers[3], buffers[4], buffers[5],
pano.getLength(), stream);
stream.synchronize();
rotateCubemap(pano, *remapBuffer, buffers[0], buffers[1], buffers[2], buffers[3], buffers[4], buffers[5], perspective,
equiangular, stream);
#if defined(DUMP_FINAL)
stream.synchronize();
Debug::dumpRGBADeviceBuffer("face_+x.png", buffers[0], (unsigned)pano.getLength(), (unsigned)pano.getLength());
Debug::dumpRGBADeviceBuffer("face_-x.png", buffers[1], (unsigned)pano.getLength(), (unsigned)pano.getLength());
Debug::dumpRGBADeviceBuffer("face_+y.png", buffers[2], (unsigned)pano.getLength(), (unsigned)pano.getLength());
Debug::dumpRGBADeviceBuffer("face_-y.png", buffers[3], (unsigned)pano.getLength(), (unsigned)pano.getLength());
Debug::dumpRGBADeviceBuffer("face_+z.png", buffers[4], (unsigned)pano.getLength(), (unsigned)pano.getLength());
Debug::dumpRGBADeviceBuffer("face_-z.png", buffers[5], (unsigned)pano.getLength(), (unsigned)pano.getLength());
#endif
return Status::OK();
}
Status PanoPimpl::warp(Core::ImageMapping* mapping, frameid_t frame, const Core::PanoDefinition& pano,
GPU::Stream& stream) {
// warp the image to the destination space
if (mapping->getMerger().isMultiScale()) {
FAIL_RETURN(mapping->warp(frame, pano, progressivePbo.borrow(), *remapBuffer, stream));
} else {
FAIL_RETURN(mapping->warp(frame, pano, buffer, *remapBuffer, stream));
}
// analyze the image content if needed (eg. compute a multi-band pyramid)
return mapping->getMerger().prepareMergeAsync(Core::EQUIRECTANGULAR, *mapping, stream);
}
Status CubemapPimpl::warp(Core::ImageMapping* mapping, frameid_t frame, const Core::PanoDefinition& pano,
GPU::Stream& stream) {
FAIL_RETURN(mapping->warpCubemap(frame, pano, equiangular, stream));
// analyze the image content if needed (eg. compute a multi-band pyramid)
FAIL_RETURN(mapping->getMerger().prepareMergeAsync(Core::CUBE_MAP_POSITIVE_X, *mapping, stream));
FAIL_RETURN(mapping->getMerger().prepareMergeAsync(Core::CUBE_MAP_NEGATIVE_X, *mapping, stream));
FAIL_RETURN(mapping->getMerger().prepareMergeAsync(Core::CUBE_MAP_POSITIVE_Y, *mapping, stream));
FAIL_RETURN(mapping->getMerger().prepareMergeAsync(Core::CUBE_MAP_NEGATIVE_Y, *mapping, stream));
FAIL_RETURN(mapping->getMerger().prepareMergeAsync(Core::CUBE_MAP_POSITIVE_Z, *mapping, stream));
return mapping->getMerger().prepareMergeAsync(Core::CUBE_MAP_NEGATIVE_Z, *mapping, stream);
}
Status PanoPimpl::blend(const Core::PanoDefinition& pano, const Core::ImageMapping& mapping, bool firstMerger,
GPU::Stream& stream) {
return mapping.getMerger().mergeAsync(Core::EQUIRECTANGULAR, pano, buffer, progressivePbo, mapping, firstMerger,
stream);
}
Status CubemapPimpl::blend(const Core::PanoDefinition& pano, const Core::ImageMapping& mapping, bool firstMerger,
GPU::Stream& stream) {
GPU::UniqueBuffer<uint32_t> dummy;
FAIL_RETURN(
mapping.getMerger().mergeAsync(Core::CUBE_MAP_POSITIVE_X, pano, buffers[0], dummy, mapping, firstMerger, stream));
FAIL_RETURN(
mapping.getMerger().mergeAsync(Core::CUBE_MAP_NEGATIVE_X, pano, buffers[1], dummy, mapping, firstMerger, stream));
FAIL_RETURN(
mapping.getMerger().mergeAsync(Core::CUBE_MAP_POSITIVE_Y, pano, buffers[2], dummy, mapping, firstMerger, stream));
FAIL_RETURN(
mapping.getMerger().mergeAsync(Core::CUBE_MAP_NEGATIVE_Y, pano, buffers[3], dummy, mapping, firstMerger, stream));
FAIL_RETURN(
mapping.getMerger().mergeAsync(Core::CUBE_MAP_POSITIVE_Z, pano, buffers[4], dummy, mapping, firstMerger, stream));
return mapping.getMerger().mergeAsync(Core::CUBE_MAP_NEGATIVE_Z, pano, buffers[5], dummy, mapping, firstMerger,
stream);
}
Status PanoPimpl::flatten() { return Status::OK(); }
Status CubemapPimpl::flatten() {
if (layout == YOUTUBE) {
FAIL_RETURN(memcpy2DAsync(buffer, buffers[0], 0, 0, 0, 0, length, length, length, width, stream));
FAIL_RETURN(memcpy2DAsync(buffer, buffers[1], 0, 0, length, 0, length, length, length, width, stream));
FAIL_RETURN(memcpy2DAsync(buffer, buffers[2], 0, 0, 2 * length, 0, length, length, length, width, stream));
FAIL_RETURN(memcpy2DAsync(buffer, buffers[3], 0, 0, 0, length, length, length, length, width, stream));
FAIL_RETURN(memcpy2DAsync(buffer, buffers[4], 0, 0, length, length, length, length, length, width, stream));
return memcpy2DAsync(buffer, buffers[5], 0, 0, 2 * length, length, length, length, length, width, stream);
} else if (layout == ROT) {
FAIL_RETURN(Image::rotate(tmp, buffers[1], length, stream));
FAIL_RETURN(memcpy2DAsync(buffer, tmp, 0, 0, 0, 0, length, length, length, width, stream));
FAIL_RETURN(Image::rotate(tmp, buffers[5], length, stream));
FAIL_RETURN(memcpy2DAsync(buffer, tmp, 0, 0, length, 0, length, length, length, width, stream));
FAIL_RETURN(Image::rotate(tmp, buffers[0], length, stream));
FAIL_RETURN(memcpy2DAsync(buffer, tmp, 0, 0, 2 * length, 0, length, length, length, width, stream));
FAIL_RETURN(Image::rotateLeft(tmp, buffers[2], length, stream));
FAIL_RETURN(memcpy2DAsync(buffer, tmp, 0, 0, 0, length, length, length, length, width, stream));
FAIL_RETURN(Image::rotateLeft(tmp, buffers[4], length, stream));
FAIL_RETURN(memcpy2DAsync(buffer, tmp, 0, 0, length, length, length, length, length, width, stream));
FAIL_RETURN(Image::rotateLeft(tmp, buffers[3], length, stream));
return memcpy2DAsync(buffer, tmp, 0, 0, 2 * length, length, length, length, length, width, stream);
}
assert(false);
return Status::OK();
}
Status PanoPimpl::reconstruct(const Core::PanoDefinition& pano, const Core::ImageMapping& mapping, GPU::Stream& stream,
bool final) {
if (mapping.getMerger().isMultiScale()) {
return mapping.reconstruct(Core::EQUIRECTANGULAR, pano, progressivePbo.borrow(), final, stream);
} else {
return mapping.reconstruct(Core::EQUIRECTANGULAR, pano, buffer, final, stream);
}
}
Status CubemapPimpl::reconstruct(const Core::PanoDefinition& pano, const Core::ImageMapping& mapping, GPU::Stream&,
bool final) {
GPU::Buffer<uint32_t> dummy;
FAIL_RETURN(mapping.reconstruct(Core::CUBE_MAP_POSITIVE_X, pano, dummy, final, stream));
FAIL_RETURN(mapping.reconstruct(Core::CUBE_MAP_NEGATIVE_X, pano, dummy, final, stream));
FAIL_RETURN(mapping.reconstruct(Core::CUBE_MAP_POSITIVE_Y, pano, dummy, final, stream));
FAIL_RETURN(mapping.reconstruct(Core::CUBE_MAP_NEGATIVE_Y, pano, dummy, final, stream));
FAIL_RETURN(mapping.reconstruct(Core::CUBE_MAP_POSITIVE_Z, pano, dummy, final, stream));
return mapping.reconstruct(Core::CUBE_MAP_NEGATIVE_Z, pano, dummy, final, stream);
}
void SourceSurface::accept(std::shared_ptr<SourceRenderer>, mtime_t) {}
void SourceOpenGLSurface::accept(std::shared_ptr<SourceRenderer> renderer, mtime_t date) {
renderer->render(std::dynamic_pointer_cast<SourceOpenGLSurface>(shared_from_this()), date);
}
void PanoSurface::accept(std::shared_ptr<PanoRenderer>, mtime_t) {}
void PanoSurface::accept(const std::shared_ptr<GPU::Overlayer>&, std::shared_ptr<PanoOpenGLSurface>, mtime_t) {}
void PanoOpenGLSurface::accept(std::shared_ptr<PanoRenderer> renderer, mtime_t date) {
renderer->render(std::dynamic_pointer_cast<PanoOpenGLSurface>(shared_from_this()), date);
}
void PanoOpenGLSurface::accept(const std::shared_ptr<GPU::Overlayer>& compositor,
std::shared_ptr<PanoOpenGLSurface> oglSurf, mtime_t date) {
compositor->computeOverlay(std::dynamic_pointer_cast<PanoOpenGLSurface>(shared_from_this()), oglSurf, date);
}
void CubemapSurface::accept(std::shared_ptr<PanoRenderer>, mtime_t) {}
void CubemapSurface::accept(const std::shared_ptr<GPU::Overlayer>&, std::shared_ptr<PanoOpenGLSurface>, mtime_t) {}
void CubemapOpenGLSurface::accept(std::shared_ptr<PanoRenderer> renderer, mtime_t date) {
if ((static_cast<CubemapPimpl*>(pimpl))->equiangular) {
renderer->renderEquiangularCubemap(std::dynamic_pointer_cast<CubemapOpenGLSurface>(shared_from_this()), date);
} else {
renderer->renderCubemap(std::dynamic_pointer_cast<CubemapOpenGLSurface>(shared_from_this()), date);
}
}
void CubemapOpenGLSurface::accept(const std::shared_ptr<GPU::Overlayer>&, std::shared_ptr<PanoOpenGLSurface>, mtime_t) {
}
} // namespace Core
} // namespace VideoStitch