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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "cmdUtils.hpp"
#include <libvideostitch/logging.hpp>
#include <libvideostitch/gpu_device.hpp>
#include <libvideostitch/inputController.hpp>
#include <libvideostitch/panoDef.hpp>
#include <libvideostitch/parse.hpp>
#include <libvideostitch/status.hpp>
#include "libgpudiscovery/backendLibHelper.hpp"
// System-dependant filesystem stuff.
#ifdef _MSC_VER
#include <direct.h>
#define chdir _chdir
#define snprintf _snprintf
#define getcwd _getcwd
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#else
#include <unistd.h>
#endif
#ifdef _MSC_VER
#include <libgpudiscovery/delayLoad.hpp>
#ifdef DELAY_LOAD_ENABLED
SET_DELAY_LOAD_HOOK
#endif // DELAY_LOAD_ENABLED
FARPROC WINAPI delayFailureHook(unsigned dliNotify, PDelayLoadInfo pdli) {
switch (dliNotify) {
case dliFailLoadLib:
throw std::runtime_error(std::string(pdli->szDll) + " could not be loaded");
case dliFailGetProc:
throw std::runtime_error("Could not find procedure in module " + std::string(pdli->szDll));
default:
assert(false);
throw std::runtime_error("Unknown error code");
};
}
#endif // _MSC_VER
namespace VideoStitch {
namespace Cmd {
namespace {
/**
* Give a pointer on the file component of a full input filename.
* @param input The input full filename.
* @return the filename, belonging to the same char* as @input.
*/
const char* extractFilename(const char* input) {
const char* file = input;
#ifdef _MSC_VER
// separator is '\' or '/'
for (const char* p = input; *p != 0; ++p) {
if (*p == '/' || *p == '\\') {
file = p + 1;
}
}
#else
// separator is '/'
for (const char* p = input; *p != 0; ++p) {
if (*p == '/') {
file = p + 1;
}
}
#endif
return file;
}
} // namespace
bool loadGPUBackend(const int deviceId, int& returnCode) {
// select backend
VideoStitch::Discovery::Framework selectedFramework = VideoStitch::Discovery::Framework::Unknown;
Discovery::DeviceProperties prop;
prop.supportedFramework = VideoStitch::Discovery::Framework::Unknown;
// if a device is selected, select its backend
if (deviceId != -1) {
if (Discovery::getDeviceProperties(deviceId, prop) &&
BackendLibHelper::isBackendAvailable(prop.supportedFramework)) {
selectedFramework = prop.supportedFramework;
std::cout << "Selected device " << deviceId << std::endl;
}
}
// if no device is selected, try select best available backend
if (selectedFramework == VideoStitch::Discovery::Framework::Unknown) {
std::cout << "No device selected, selecting default device" << std::endl;
selectedFramework = BackendLibHelper::getBestFrameworkAndBackend();
}
bool needToRestart = false;
if (BackendLibHelper::selectBackend(selectedFramework, &needToRestart)) {
std::cout << "[videostitch-cmd] " << VideoStitch::Discovery::getFrameworkName(selectedFramework)
<< " backend selected" << std::endl;
} else {
std::cerr << "No CUDA nor OpenCL capable GPU detected on your system. "
<< "A Nvidia card with CUDA capability and CUDA drivers or "
<< "a graphics card with OpenCL capability and drivers installed are mandatory to run the software. "
<< std::endl;
returnCode = 1;
return false;
}
if (needToRestart) {
std::cout << "Closing application to finalize setup " << std::endl;
#ifdef __APPLE__
BackendLibHelper::forceUpdateSymlink();
#endif
returnCode = 0;
return false;
}
#ifdef DELAY_LOAD_ENABLED
// failure hook, to prevent crash on delay load failures
PfnDliHook oldFailureHook = __pfnDliFailureHook2;
__pfnDliFailureHook2 = &delayFailureHook;
try {
// call any lib function
Status::OK();
} catch (std::exception& e) {
std::cerr << "Error using backend library: " << e.what() << std::endl;
}
__pfnDliFailureHook2 = oldFailureHook;
#endif
return true;
}
const char* changeWorkingPathToPtvFolder(char* ptvPath) {
// Change directory to the project directory, so that all paths are relative.
const char* ptvFile = extractFilename(ptvPath);
if (ptvFile != ptvPath) {
// if ptvPath is "/"
if (ptvFile - ptvPath == 1) {
if (chdir("/")) {
Logger::get(Logger::Error) << "Incorrect input file: " << ptvFile << std::endl;
return nullptr;
}
} else {
ptvPath[ptvFile - ptvPath - 1] = 0; // trim the path
if (chdir(ptvPath)) {
Logger::get(Logger::Error) << "Incorrect input file: " << ptvFile << std::endl;
return nullptr;
}
}
}
return ptvFile;
}
bool parseInputPath(int argc, char** argv, int index, char** ptvPath) {
if (index >= argc - 1 || (argv[index + 1][0] == '-' && argv[index + 1][1] != '\0')) {
Logger::get(Logger::Error) << "The -i option takes a parameter." << std::endl;
return false;
}
if (*ptvPath) {
Logger::get(Logger::Error) << "Several input files: \"" << *ptvPath << "\" and \"" << argv[index + 1] << "\""
<< std::endl;
return false;
}
*ptvPath = argv[index + 1];
return true;
}
bool parseOutputPath(int argc, char** argv, int index, char** ptvPath) {
if (index >= argc - 1 || (argv[index + 1][0] == '-' && argv[index + 1][1] != '\0')) {
Logger::get(Logger::Error) << "The -o option takes a parameter." << std::endl;
return false;
}
if (*ptvPath) {
Logger::get(Logger::Error) << "Several output files: \"" << *ptvPath << "\" and \"" << argv[index + 1] << "\""
<< std::endl;
return false;
}
*ptvPath = argv[index + 1];
return true;
}
bool parseFirstFrame(int argc, char** argv, int index, int* firstFrame) {
if (index >= argc - 1 || (argv[index + 1][0] == '-' && argv[index + 1][1] != '\0')) {
Logger::get(Logger::Error) << "The -f option takes a parameter." << std::endl;
return false;
}
*firstFrame = atoi(argv[index + 1]);
return true;
}
bool parseLastFrame(int argc, char** argv, int index, int* lastFrame) {
if (index >= argc - 1 || (argv[index + 1][0] == '-' && argv[index + 1][1] != '\0')) {
Logger::get(Logger::Error) << "The -l option takes a parameter." << std::endl;
return false;
}
*lastFrame = atoi(argv[index + 1]);
return true;
}
Status checkDevice(int vsDeviceID) {
FAIL_RETURN(GPU::setDefaultBackendDeviceVS(vsDeviceID));
return GPU::checkDefaultBackendDeviceInitialization();
}
bool parseDeviceId(int argc, char** argv, int& deviceId, int& returnCode) {
deviceId = -1;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] != '\0' && argv[i][1] != '\0' && argv[i][0] == '-') {
switch (argv[i][1]) {
case 'd':
if (i >= argc - 1 || argv[i + 1][0] == '-') {
std::cout << "-d option used without parameter, list compatible GPU devices:" << std::endl;
int numDevices = Discovery::getNumberOfDevices();
if (numDevices == 0) {
std::cerr << "Could not find any compatible GPU device on this computer." << std::endl;
returnCode = 1;
return false;
}
for (int i = 0; i < numDevices; ++i) {
Discovery::DeviceProperties prop;
if (Discovery::getDeviceProperties(i, prop)) {
std::cout << "Device " << i << ": " << prop << std::endl;
} else {
std::cerr << "Could not query device properties for device " << i << std::endl;
}
}
returnCode = 0;
return false;
}
// else
std::stringstream ss(argv[++i]);
if (!(ss >> deviceId)) {
std::cerr << "Malformed device id: '" << argv[i] << "'" << std::endl;
returnCode = 1;
return false;
}
break;
}
}
}
return true;
}
/**
* Detects GPU devices and returns true on success.
* No check is done for GPU usability
*/
bool selectGPUDevice(int argc, char** argv, int& deviceId, int& returnCode) {
if (!parseDeviceId(argc, argv, deviceId, returnCode)) {
return false;
}
int defaultGpu = 0;
int devCount = Discovery::getNumberOfDevices();
if (devCount == 0) {
std::cerr << "Error: No GPU found!" << std::endl;
returnCode = 1;
return false;
} else if (devCount == 1) {
if (deviceId != -1) {
std::cout << "Warning: Only one GPU found, ignoring -d." << std::endl;
}
deviceId = defaultGpu;
} else {
if (deviceId == -1) {
std::cerr << "Error: More than 1 device and no '-d' option found. Falling back to the default device ('-d 0')."
<< std::endl;
deviceId = defaultGpu;
} else {
if (deviceId >= devCount) {
std::cerr << "Error: No such device " << deviceId << ". Only " << devCount << " GPUs found." << std::endl;
returnCode = 1;
return false;
}
}
}
return true;
}
/**
* @brief Prints properties of a given GPU device.
* @param os Output stream to print to.
* @param vsGPUDeviceID GPU device (libvideostitch internal ID)
*/
void printDeviceProperties(ThreadSafeOstream& os, int vsGPUDeviceID) {
Discovery::DeviceProperties prop;
if (VideoStitch::Discovery::getDeviceProperties(vsGPUDeviceID, prop)) {
os << prop << std::endl;
} else {
os << "Error when trying to access device properties of GPU device #" << vsGPUDeviceID << std::endl;
}
}
/**
* Checks the usable GPU devices and returns true on success.
*/
bool checkGPUDevice(Core::PanoDeviceDefinition& dev) {
auto& errLog = Logger::get(Logger::Error);
if (!checkDevice(dev.device).ok()) {
errLog << "Cannot use device " << dev.device << std::endl;
return false;
}
printDeviceProperties(Logger::get(Logger::Verbose), dev.device);
return true;
}
std::unique_ptr<Core::PanoDefinition> parsePanoDef(const Ptv::Value& ptvRoot, const char* ptvFile) {
if (!ptvRoot.has("pano")) {
Logger::get(Logger::Error) << "Error: No 'pano' entry in file: " << ptvFile << std::endl;
return nullptr;
}
// Create a runtime panorama from the parsed project.
Core::PanoDefinition* panoDef = Core::PanoDefinition::create(*ptvRoot.has("pano"));
if (!panoDef) {
Logger::get(Logger::Error) << "Error: Invalid panorama definition in " << ptvFile << std::endl;
return nullptr;
}
return std::unique_ptr<Core::PanoDefinition>{panoDef};
}
bool parsePtvFile(Ptv::Parser& parser, const char* ptvFile) {
// Load the project and parse it.
if (!parser.parse(ptvFile)) {
Logger::get(Logger::Error) << "Error: Cannot parse PTV file: " << ptvFile << std::endl;
Logger::get(Logger::Error) << parser.getErrorMessage() << std::endl;
return false;
}
return true;
}
std::unique_ptr<Core::PanoDefinition> parsePanoDef(Ptv::Parser& parser, const char* ptvFile) {
if (parsePtvFile(parser, ptvFile)) {
return parsePanoDef(parser.getRoot(), ptvFile);
}
return nullptr;
}
bool normalizeFrameBoundaries(const Core::InputController& controller, const frameid_t firstFrame,
frameid_t& lastFrame) {
lastFrame =
lastFrame < 0 ? controller.getLastStitchableFrame() : std::min(lastFrame, controller.getLastStitchableFrame());
if (lastFrame == NO_LAST_FRAME) {
Logger::get(Logger::Error) << "Last frame auto detection was enabled, but all the readers are unbounded (Are you "
"using only procedural readers ?)."
<< std::endl;
lastFrame = firstFrame;
}
if (lastFrame < firstFrame) {
Logger::get(Logger::Error) << "Nothing to stitch: last_frame = " << lastFrame << " < first_frame = " << firstFrame
<< "." << std::endl;
return false;
}
Logger::get(Logger::Info) << "Will stitch " << lastFrame - firstFrame + 1 << " images." << std::endl;
return true;
}
} // namespace Cmd
} // namespace VideoStitch