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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "panoStitcherBase.hpp"
#include "gpu/buffer.hpp"
#include "gpu/memcpy.hpp"
#include "common/angles.hpp"
#include "common/container.hpp"
#include "image/unpack.hpp"
#include "photoTransform.hpp"
#include "stitchOutput/stitchOutput.hpp"
#include "stitchOutput/stereoOutput.hpp"
#include "libvideostitch/logging.hpp"
#include "libvideostitch/output.hpp"
#include "libvideostitch/postprocessor.hpp"
#include "libvideostitch/profile.hpp"
#include "libvideostitch/gpu_device.hpp"
#include <cassert>
#include <fstream>
#include <memory>
#include <sstream>
namespace VideoStitch {
namespace Core {
template <typename Output>
PanoStitcherImplBase<Output>::PanoStitcherImplBase(const std::string& nameValue, const PanoDefinition& panoValue,
Eye eyeValue)
: name(nameValue), pano(&panoValue), eye(eyeValue) {}
template <typename Output>
PanoStitcherImplBase<Output>::~PanoStitcherImplBase() {
deleteAllValues(photoTransforms);
for (auto stream : streams) {
stream.second.destroy();
}
streams.clear();
}
template <typename Output>
const DevicePhotoTransform& PanoStitcherImplBase<Output>::getPhotoTransform(readerid_t inputId) const {
assert(0 <= inputId && inputId < (readerid_t)photoTransforms.size());
return *photoTransforms.at(inputId);
}
template <typename Output>
PanoSurface& acquireFrame(Eye, Output* output, mtime_t date);
template <>
PanoSurface& acquireFrame(Eye, StitchOutput* output, mtime_t date) {
return output->pimpl->acquireFrame(date);
}
template <>
PanoSurface& acquireFrame(Eye eye, StereoOutput* output, mtime_t date) {
if (eye == LeftEye) {
return output->pimpl->acquireLeftFrame(date);
} else {
return output->pimpl->acquireRightFrame(date);
}
}
template <typename Output>
Status pushVideo(Eye, Output* output, mtime_t date);
template <>
Status pushVideo(Eye, StitchOutput* output, mtime_t date) {
return output->pimpl->pushVideo(date);
}
template <>
Status pushVideo(Eye eye, StereoOutput* output, mtime_t date) {
return output->pimpl->pushVideo(date, eye);
}
/**
*
* - do not load concurrently (avoid concurrent accesses to disk)
* - do not write while loading
* - loading, transmitting and mapping are independant for two different images
* - merging requires the previous and current images to be mapped.
* - keep frames independant for the moment, i.e. do not start images for next frames during current frame.
*
* Image1 | load | map | merge | | load |
* Image2 | | load | map | merge |
* Image3 | | load | map |XXXXX| merge |
* Image4 | | load | map |XXXXXXXXX| merge |
* Image5 | | load | map |XXX| merge |
*
* | readback | write |
*
* The main thread orchestrates the loading, delegating asynchronous logic to cuda streams.
*/
template <typename Output>
Status PanoStitcherImplBase<Output>::stitch(mtime_t date, frameid_t frame, PostProcessor* postprocessor,
std::map<readerid_t, Input::PotentialFrame> inputBuffers,
std::map<readerid_t, Input::VideoReader*> readers,
std::map<readerid_t, PreProcessor*> preprocessors, Output* output) {
auto stitchProcess = [&]() -> Status {
FAIL_RETURN(GPU::useDefaultBackendDevice());
PanoSurface& surf = acquireFrame(eye, output, date);
Status status = merge(frame, inputBuffers, readers, preprocessors, surf);
if (status.ok()) {
if (postprocessor) {
status = postprocessor->process(surf.pimpl->buffer, *pano, frame, surf.pimpl->stream);
}
}
if (!status.ok()) {
GPU::memsetToZeroAsync(surf.pimpl->buffer, (size_t)(pano->getWidth() * pano->getHeight() * 4),
surf.pimpl->stream);
// still push the output, to release the panorama frame buffer
pushVideo(eye, output, date);
} else {
status = pushVideo(eye, output, date);
}
surf.pimpl->stream.flush();
return status;
};
const Status success = stitchProcess();
if (!success.ok()) {
// Signal an error to the output so that it does not wait forever for the next frame.
Logger::get(Logger::Warning) << "Failed to stitch. Skipping output for frame " << frame << "." << std::endl;
}
return success;
}
template <typename Output>
Status PanoStitcherImplBase<Output>::createTransforms(const std::map<readerid_t, Input::VideoReader*>& readers) {
deleteAllValues(photoTransforms);
for (auto reader : readers) {
const InputDefinition& inputDef = pano->getInput(reader.second->id);
DevicePhotoTransform* photoTransform = DevicePhotoTransform::create(inputDef);
if (!photoTransform) {
deleteAllValues(photoTransforms);
return {Origin::Stitcher, ErrType::SetupFailure,
"Cannot create transformation for input " + std::to_string(reader.second->id)};
}
photoTransforms[reader.second->id] = photoTransform;
}
return Status::OK();
}
template <typename Output>
Status PanoStitcherImplBase<Output>::setup(const ImageMergerFactory& mergerFactory,
const ImageWarperFactory& warperFactory, const ImageFlowFactory& flowFactory,
const std::map<readerid_t, Input::VideoReader*>& readers,
const StereoRigDefinition* rig) {
FAIL_RETURN(GPU::useDefaultBackendDevice());
if (streams.size() > 0) {
return {Origin::Stitcher, ErrType::ImplementationError, "Stitcher has already been setup"};
}
// Create streams for async operations.
for (auto r : readers) {
auto potStream = GPU::Stream::create();
if (!potStream.ok()) {
streams.clear();
return potStream.status();
}
streams[r.second->id] = potStream.value();
}
PROPAGATE_FAILURE_STATUS(createTransforms(readers));
// Setup the implementation.
return setupImpl(mergerFactory, warperFactory, flowFactory, readers, rig);
}
template <typename Output>
Status PanoStitcherImplBase<Output>::redoSetup(const PanoDefinition& newPano, const ImageMergerFactory& mergerFactory,
const ImageWarperFactory& warperFactory,
const ImageFlowFactory& flowFactory,
std::map<readerid_t, Input::VideoReader*> readers,
const StereoRigDefinition* rig) {
pano = &newPano;
FAIL_RETURN(GPU::useDefaultBackendDevice());
PROPAGATE_FAILURE_STATUS(createTransforms(readers));
// redoSetup the implementation.
return redoSetupImpl(mergerFactory, warperFactory, flowFactory, readers, rig);
}
template <typename Output>
ChangeCompatibility PanoStitcherImplBase<Output>::worstCompatibility(ChangeCompatibility a, ChangeCompatibility b) {
switch (a) {
case IncompatibleChanges:
return IncompatibleChanges;
case SetupIncompatibleChanges:
return b == IncompatibleChanges ? IncompatibleChanges : SetupIncompatibleChanges;
case SetupCompatibleChanges:
return b;
}
return IncompatibleChanges;
}
template <typename Output>
void PanoStitcherImplBase<Output>::applyRotation(double yaw, double pitch, double roll) {
interactivePersp *= Matrix33<double>::fromEulerZXY(degToRad(yaw), degToRad(pitch), degToRad(roll));
}
template <typename Output>
void PanoStitcherImplBase<Output>::resetRotation() {
interactivePersp = Matrix33<double>();
}
template <typename Output>
Quaternion<double> PanoStitcherImplBase<Output>::getRotation() const {
double yaw, pitch, roll;
interactivePersp.toEuler(yaw, pitch, roll);
return Quaternion<double>::fromEulerZXY(yaw, pitch, roll);
}
// explicit instantiations
template class PanoStitcherImplBase<StitchOutput>;
template class PanoStitcherImplBase<StereoOutput>;
} // namespace Core
} // namespace VideoStitch