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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "inputsMap.hpp"
#include "common/container.hpp"
#include "core/geoTransform.hpp"
#include "gpu/buffer.hpp"
#include "gpu/memcpy.hpp"
#include "gpu/stream.hpp"
#include "gpu/core1/strip.hpp"
#include "gpu/core1/transform.hpp"
#include "mask/mergerMask.hpp"
#include "util/polylineEncodingUtils.hpp"
#include "libvideostitch/stereoRigDef.hpp"
#include "libvideostitch/geometryDef.hpp"
#include "libvideostitch/logging.hpp"
#include "libvideostitch/inputDef.hpp"
#include <fstream>
#include <sstream>
#include <algorithm>
//#define READBACKSETUPIMAGE
//#define INPUTMAPS_PRECOMPUTED
#if defined(READBACKSETUPIMAGE) || defined(INPUTMAPS_PRECOMPUTED)
#ifdef _MSC_VER
static const std::string DEBUG_FOLDER = "";
#else
static const std::string DEBUG_FOLDER = "/tmp/inputs/";
#endif
#include "cuda/error.hpp"
#include "util/pngutil.hpp"
#include "util/pnm.hpp"
#include "util/debugUtils.hpp"
#include "image/unpack.hpp"
#endif
static const double OVERLAP = 0.25;
namespace VideoStitch {
namespace Core {
Potential<InputsMap> InputsMap::create(const PanoDefinition& pano) {
std::unique_ptr<InputsMap> inputsMap;
inputsMap.reset(new InputsMap(pano));
Status status = inputsMap->allocateBuffers();
if (status.ok()) {
return Potential<InputsMap>(inputsMap.release());
} else {
return Potential<InputsMap>(status);
}
}
InputsMap::InputsMap(const PanoDefinition& pano)
: _boundedFrames(std::make_pair(std::numeric_limits<frameid_t>::max(), std::numeric_limits<frameid_t>::min())),
_width(pano.getWidth()),
_height(pano.getHeight()) {}
InputsMap::~InputsMap() {}
Status InputsMap::allocateBuffers() { return setupBuffer.alloc(_width * _height, "Setup Buffer"); }
Status InputsMap::compute(const std::map<readerid_t, Input::VideoReader*>& readers, const PanoDefinition& pano,
const bool loadingEnabled) {
return compute(readers, pano, nullptr, LeftEye, loadingEnabled);
}
void computeStrip(float& min, float& max, double baseline, double rigradius, size_t countCameras) {
double theta = 2.0 * M_PI / (double)countCameras;
/*Compute disparity angle*/
double disparityAngle = asin(0.5 * baseline / rigradius);
double angularStripWidth = theta / 2.0;
max = (float)(disparityAngle + (1.0 + OVERLAP) * angularStripWidth);
min = (float)(disparityAngle - (1.0 + OVERLAP) * angularStripWidth);
}
std::pair<frameid_t, frameid_t> InputsMap::getBoundedFrameIds() const { return _boundedFrames; }
#ifndef VS_OPENCL
Status InputsMap::loadPrecomputedMap(const frameid_t frameId, const PanoDefinition& pano,
const std::map<readerid_t, Input::VideoReader*>& readers,
std::unique_ptr<MaskInterpolation::InputMaskInterpolation>& inputMaskInterpolation,
bool& loaded) {
loaded = false;
if (!pano.getBlendingMaskEnabled()) {
return Status::OK();
}
std::pair<frameid_t, frameid_t> boundedFrames = pano.getBlendingMaskBoundedFrameIds(frameId);
std::vector<std::pair<frameid_t, std::map<videoreaderid_t, std::string>>> inputIndexPixelData =
pano.getInputIndexPixelDataIfValid(frameId);
if (_boundedFrames.first == boundedFrames.first && _boundedFrames.second == boundedFrames.second &&
frameId <= boundedFrames.second && frameId >= boundedFrames.first &&
boundedFrames.first != boundedFrames.second) {
// The bounded maps have been loaded, perform interpolation if needed
loaded = true;
if (pano.getBlendingMaskInterpolationEnabled() && inputMaskInterpolation.get() && inputIndexPixelData.size()) {
FAIL_RETURN(inputMaskInterpolation->getInputsMap(pano, frameId, setupBuffer.borrow()));
}
return Status::OK();
}
if (inputIndexPixelData.size()) {
if (inputMaskInterpolation.get()) {
inputMaskInterpolation->deactivate();
}
if (!pano.getBlendingMaskInterpolationEnabled() || inputIndexPixelData.size() == 1 ||
!inputMaskInterpolation.get()) {
FAIL_RETURN(MergerMask::MergerMask::transformMasksFromEncodedInputToOutputSpace(
pano, readers, inputIndexPixelData[0].second, setupBuffer.borrow()));
Logger::get(Logger::Info) << "Precomputed map is loaded" << std::endl;
} else {
_boundedFrames = std::make_pair((frameid_t)boundedFrames.first, (frameid_t)boundedFrames.second);
std::map<videoreaderid_t, std::vector<cv::Point>> point0s, point1s;
Util::PolylineEncoding::polylineDecodePolygons(inputIndexPixelData[0].second, point0s);
Util::PolylineEncoding::polylineDecodePolygons(inputIndexPixelData[1].second, point1s);
Logger::get(Logger::Info) << "Bounded maps are loaded" << std::endl;
FAIL_RETURN(
inputMaskInterpolation->setupKeyframes(pano, _boundedFrames.first, point0s, _boundedFrames.second, point1s));
FAIL_RETURN(inputMaskInterpolation->getInputsMap(pano, frameId, setupBuffer.borrow()));
}
#ifdef INPUTMAPS_PRECOMPUTED
{
std::stringstream ss;
ss.str("");
ss << "precomputed-result " << frameId << ".png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), setupBuffer.borrow_const().as_const(), _cropped_width,
_cropped_height);
}
#endif
loaded = true;
}
return Status::OK();
}
#endif
Status InputsMap::compute(const std::map<readerid_t, Input::VideoReader*>& readers, const PanoDefinition& pano,
const StereoRigDefinition* rigDef, Eye eye, const bool loadingEnabled) {
/* The blending mask is computed in the output but stored in the input space.
* There are two major advantages when storing it in the input space:
* - The interpolation between frames will become easier in the input space
* - The potential to call remap directly in the transformation stacks
* However, storing the blending mask in the input space might result in a slightly different
* from the mask when it was computed.
*/
// Load the precomputed map if it is valid
if (loadingEnabled) {
bool loaded = false;
#ifndef VS_OPENCL
std::unique_ptr<MaskInterpolation::InputMaskInterpolation> inputMaskInterpolation(nullptr);
FAIL_RETURN(loadPrecomputedMap(0, pano, readers, inputMaskInterpolation, loaded));
#endif
if (loaded) {
return Status::OK();
}
}
// Reset the boundedFrames to infinity, nothing was loaded
_boundedFrames = std::make_pair(std::numeric_limits<frameid_t>::max(), std::numeric_limits<frameid_t>::min());
// If no map was precomputed, now generate it on the fly
FAIL_RETURN(GPU::memsetToZeroBlocking(setupBuffer.borrow(), _width * _height * 4));
for (auto reader : readers) {
const InputDefinition& inputDef = pano.getInput(reader.second->id);
const GeometryDefinition& geometry = inputDef.getGeometries().at(0);
const size_t bufferSize = (size_t)(inputDef.getWidth() * inputDef.getHeight());
/*Create mask buffer*/
GPU::UniqueBuffer<unsigned char> maskDevBuffer;
FAIL_RETURN(maskDevBuffer.alloc(bufferSize, "MaskSetup"));
/* Retrieve input mask and send it to the gpu */
const unsigned char* data = inputDef.getMaskPixelDataIfValid();
if (data && inputDef.deletesMaskedPixels()) {
FAIL_RETURN(GPU::memcpyBlocking(maskDevBuffer.borrow(), data, bufferSize));
} else {
FAIL_RETURN(GPU::memsetToZeroBlocking(maskDevBuffer.borrow(), bufferSize));
}
/* Get an horizontal strip for stereoscopic videos */
if (rigDef && rigDef->getGeometry() == StereoRigDefinition::Circular) {
std::vector<int> inputs = (eye == LeftEye ? rigDef->getLeftInputs() : rigDef->getRightInputs());
if (std::find(inputs.begin(), inputs.end(), reader.second->id) != inputs.end()) {
TransformStack::GeoTransform* geoParams = TransformStack::GeoTransform::create(pano, inputDef);
TransformGeoParams params(inputDef, geometry, pano);
StereoRigDefinition::Orientation orientation = rigDef->getOrientation();
float min = 0.0;
float max = 1.0;
const auto countCamera = inputs.size();
double baseline = rigDef->getIPD();
double radius = rigDef->getDiameter() / 2.0;
computeStrip(min, max, baseline, radius, countCamera);
// flip ?
if ((eye == RightEye) !=
(orientation == StereoRigDefinition::Landscape || orientation == StereoRigDefinition::Portrait)) {
float tmp = min;
min = -max;
max = -tmp;
}
float2 inputScale = {(float)geometry.getHorizontalFocal(), (float)geometry.getVerticalFocal()};
switch (orientation) {
case StereoRigDefinition::Portrait:
case StereoRigDefinition::Portrait_flipped:
hStrip(maskDevBuffer.borrow(), inputDef.getWidth(), inputDef.getHeight(), min, max, inputDef.getFormat(),
(float)inputDef.getCenterX(geometry), (float)inputDef.getCenterY(geometry), params, inputScale,
GPU::Stream::getDefault());
break;
case StereoRigDefinition::Landscape:
case StereoRigDefinition::Landscape_flipped:
vStrip(maskDevBuffer.borrow(), inputDef.getWidth(), inputDef.getHeight(), min, max, inputDef.getFormat(),
(float)inputDef.getCenterX(geometry), (float)inputDef.getCenterY(geometry), params, inputScale,
GPU::Stream::getDefault());
}
delete geoParams;
}
}
/* Update assigned pixels */
Transform* t = Transform::create(inputDef);
if (!t) {
return {Origin::Stitcher, ErrType::SetupFailure,
"Cannot create v1 transformation for input " + std::to_string(reader.second->id)};
}
FAIL_RETURN(t->computeZone(setupBuffer.borrow(), pano, inputDef, reader.second->id, maskDevBuffer.borrow(),
GPU::Stream::getDefault()));
FAIL_RETURN(GPU::Stream::getDefault().synchronize());
delete t;
#ifdef READBACKSETUPIMAGE
{
{
const int64_t width = pano.getWidth();
const int64_t height = pano.getHeight();
GPU::Stream::getDefault().synchronize();
{
std::stringstream ss;
ss << DEBUG_FOLDER << "setup-" << reader.second->id;
if (eye == LeftEye) {
ss << "left";
} else {
ss << "right";
}
ss << "-.png";
Debug::dumpRGBAIndexDeviceBuffer(ss.str().c_str(), setupBuffer.borrow_const(), width, height);
}
}
}
#endif
}
return Status::OK();
}
} // namespace Core
} // namespace VideoStitch