-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxToEquirectPlugin.cpp
More file actions
341 lines (275 loc) · 11.5 KB
/
MaxToEquirectPlugin.cpp
File metadata and controls
341 lines (275 loc) · 11.5 KB
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
/*
* Copyright (c) 2021 Ronan LE MEILLAT
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "MaxToEquirectPlugin.h"
#include <stdio.h>
#include "ofxsImageEffect.h"
#include "ofxsMultiThread.h"
#include "ofxsProcessing.h"
#include "ofxsLog.h"
#define kPluginName "GoPro Max to Equirectangular"
#define kPluginGrouping "GoPro Max"
#define kPluginDescription "Convert GoPro Max pseudo EAC to Equirectangular"
#define kPluginIdentifier "free.gopromax.maxToEquiRect"
#define kPluginVersionMajor 1
#define kPluginVersionMinor 0
#define kSupportsTiles false
#define kSupportsMultiResolution false
#define kSupportsMultipleClipPARs false
////////////////////////////////////////////////////////////////////////////////
class ImageScaler : public OFX::ImageProcessor
{
public:
explicit ImageScaler(OFX::ImageEffect& p_Instance);
virtual void processImagesCUDA();
virtual void processImagesOpenCL();
virtual void processImagesMetal();
virtual void multiThreadProcessImages(OfxRectI p_ProcWindow);
void setSrcImg(OFX::Image* p_SrcImg);
private:
OFX::Image* _srcImg;
};
ImageScaler::ImageScaler(OFX::ImageEffect& p_Instance)
: OFX::ImageProcessor(p_Instance)
{
}
#ifndef __APPLE__
extern void RunCudaKernel(int p_in_Width, int p_in_Height, int p_out_Width, int p_out_Height, const float* gopromax_stack, float* dst);
#endif
void ImageScaler::processImagesCUDA()
{
#ifndef __APPLE__
const OfxRectI& in_bounds = _srcImg->getBounds();
const int in_width = in_bounds.x2 - in_bounds.x1;
const int in_height = in_bounds.y2 - in_bounds.y1;
const OfxRectI& out_bounds = _dstImg->getBounds();
const int out_width = out_bounds.x2 - out_bounds.x1;
const int out_height = out_bounds.y2 - out_bounds.y1;
float* input = static_cast<float*>(_srcImg->getPixelData());
float* output = static_cast<float*>(_dstImg->getPixelData());
RunCudaKernel(in_width, in_height, out_width, out_height, input, output);
#endif
}
#ifdef __APPLE__
extern void RunMetalKernel(void* p_CmdQ, int p_in_Width, int p_in_Height, int p_out_Width, int p_out_Height, const float* p_Input, float* p_Output);
#endif
void ImageScaler::processImagesMetal()
{
#ifdef __APPLE__
const OfxRectI& in_bounds = _srcImg->getBounds();
const int in_width = in_bounds.x2 - in_bounds.x1;
const int in_height = in_bounds.y2 - in_bounds.y1;
const OfxRectI& out_bounds = _dstImg->getBounds();
const int out_width = out_bounds.x2 - out_bounds.x1;
const int out_height = out_bounds.y2 - out_bounds.y1;
float* input = static_cast<float*>(_srcImg->getPixelData());
float* output = static_cast<float*>(_dstImg->getPixelData());
RunMetalKernel(_pMetalCmdQ, in_width, in_height, out_width, out_height, input, output);
#endif
}
extern void RunOpenCLKernel(void* p_CmdQ, int p_in_Width, int p_in_Height, int p_out_Width, int p_out_Height, const float* p_Input, float* p_Output);
void ImageScaler::processImagesOpenCL()
{
const OfxRectI& in_bounds = _srcImg->getBounds();
const int in_width = in_bounds.x2 - in_bounds.x1;
const int in_height = in_bounds.y2 - in_bounds.y1;
const OfxRectI& out_bounds = _dstImg->getBounds();
const int out_width = out_bounds.x2 - out_bounds.x1;
const int out_height = out_bounds.y2 - out_bounds.y1;
float* input = static_cast<float*>(_srcImg->getPixelData());
float* output = static_cast<float*>(_dstImg->getPixelData());
RunOpenCLKernel(_pOpenCLCmdQ, in_width, in_height, out_width, out_height, input, output);
}
void ImageScaler::multiThreadProcessImages(OfxRectI p_ProcWindow)
{
for (int y = p_ProcWindow.y1; y < p_ProcWindow.y2; ++y)
{
if (_effect.abort()) break;
float* dstPix = static_cast<float*>(_dstImg->getPixelAddress(p_ProcWindow.x1, y));
for (int x = p_ProcWindow.x1; x < p_ProcWindow.x2; ++x)
{
float* srcPix = static_cast<float*>(_srcImg ? _srcImg->getPixelAddress(x, y) : 0);
// do we have a source image to scale up
if (srcPix)
{
for(int c = 0; c < 4; ++c)
{
dstPix[c] = srcPix[c] ; //TODO only OpenCL and Metal implemented so copy
}
}
else
{
// no src pixel here, be black and transparent
for (int c = 0; c < 4; ++c)
{
dstPix[c] = 0;
}
}
// increment the dst pixel
dstPix += 4;
}
}
}
void ImageScaler::setSrcImg(OFX::Image* p_SrcImg)
{
_srcImg = p_SrcImg;
}
////////////////////////////////////////////////////////////////////////////////
/** @brief The plugin that does our work */
class MaxToEquirectPlugin : public OFX::ImageEffect
{
public:
explicit MaxToEquirectPlugin(OfxImageEffectHandle p_Handle);
/* Override the render */
virtual void render(const OFX::RenderArguments& p_Args);
/* Override is identity */
virtual bool isIdentity(const OFX::IsIdentityArguments& p_Args, OFX::Clip*& p_IdentityClip, double& p_IdentityTime);
/* Override changedParam */
virtual void changedParam(const OFX::InstanceChangedArgs& p_Args, const std::string& p_ParamName);
/* Override changed clip */
virtual void changedClip(const OFX::InstanceChangedArgs& p_Args, const std::string& p_ClipName);
/* Set up and run a processor */
void setupAndProcess(ImageScaler &p_ImageScaler, const OFX::RenderArguments& p_Args);
private:
// Does not own the following pointers
OFX::Clip* m_DstClip;
OFX::Clip* m_SrcClip;
};
MaxToEquirectPlugin::MaxToEquirectPlugin(OfxImageEffectHandle p_Handle)
: ImageEffect(p_Handle)
{
m_DstClip = fetchClip(kOfxImageEffectOutputClipName);
m_SrcClip = fetchClip(kOfxImageEffectSimpleSourceClipName);
}
void MaxToEquirectPlugin::render(const OFX::RenderArguments& p_Args)
{
if ((m_DstClip->getPixelDepth() == OFX::eBitDepthFloat) && (m_DstClip->getPixelComponents() == OFX::ePixelComponentRGBA))
{
ImageScaler imageScaler(*this);
setupAndProcess(imageScaler, p_Args);
}
else
{
OFX::throwSuiteStatusException(kOfxStatErrUnsupported);
}
}
bool MaxToEquirectPlugin::isIdentity(const OFX::IsIdentityArguments& p_Args, OFX::Clip*& p_IdentityClip, double& p_IdentityTime)
{
return false;
}
void MaxToEquirectPlugin::changedParam(const OFX::InstanceChangedArgs& p_Args, const std::string& p_ParamName)
{
// nothing to do (yet)
}
void MaxToEquirectPlugin::changedClip(const OFX::InstanceChangedArgs& p_Args, const std::string& p_ClipName)
{
// nothing to do (yet)
}
void MaxToEquirectPlugin::setupAndProcess(ImageScaler& p_ImageScaler, const OFX::RenderArguments& p_Args)
{
// Get the dst image
std::auto_ptr<OFX::Image> dst(m_DstClip->fetchImage(p_Args.time));
OFX::BitDepthEnum dstBitDepth = dst->getPixelDepth();
OFX::PixelComponentEnum dstComponents = dst->getPixelComponents();
// Get the src image
std::auto_ptr<OFX::Image> src(m_SrcClip->fetchImage(p_Args.time));
OFX::BitDepthEnum srcBitDepth = src->getPixelDepth();
OFX::PixelComponentEnum srcComponents = src->getPixelComponents();
// Check to see if the bit depth and number of components are the same
if ((srcBitDepth != dstBitDepth) || (srcComponents != dstComponents))
{
OFX::throwSuiteStatusException(kOfxStatErrValue);
}
// Set the images
p_ImageScaler.setDstImg(dst.get());
p_ImageScaler.setSrcImg(src.get());
// Setup OpenCL and CUDA Render arguments
p_ImageScaler.setGPURenderArgs(p_Args);
// Set the render window
p_ImageScaler.setRenderWindow(p_Args.renderWindow);
// Call the base class process member, this will call the derived templated process code
p_ImageScaler.process();
}
////////////////////////////////////////////////////////////////////////////////
using namespace OFX;
MaxToEquirectPluginFactory::MaxToEquirectPluginFactory()
: OFX::PluginFactoryHelper<MaxToEquirectPluginFactory>(kPluginIdentifier, kPluginVersionMajor, kPluginVersionMinor)
{
}
void MaxToEquirectPluginFactory::describe(OFX::ImageEffectDescriptor& p_Desc)
{
// Basic labels
p_Desc.setLabels(kPluginName, kPluginName, kPluginName);
p_Desc.setPluginGrouping(kPluginGrouping);
p_Desc.setPluginDescription(kPluginDescription);
// Add the supported contexts, only filter at the moment
p_Desc.addSupportedContext(eContextFilter);
p_Desc.addSupportedContext(eContextGeneral);
// Add supported pixel depths
p_Desc.addSupportedBitDepth(eBitDepthFloat);
// Set a few flags
p_Desc.setSingleInstance(false);
p_Desc.setHostFrameThreading(false);
p_Desc.setSupportsMultiResolution(kSupportsMultiResolution);
p_Desc.setSupportsTiles(kSupportsTiles);
p_Desc.setTemporalClipAccess(false);
p_Desc.setRenderTwiceAlways(false);
p_Desc.setSupportsMultipleClipPARs(kSupportsMultipleClipPARs);
// Setup OpenCL render capability flags
p_Desc.setSupportsOpenCLRender(true);
// Setup CUDA render capability flags on non-Apple system
#ifndef __APPLE__
p_Desc.setSupportsCudaRender(true);
p_Desc.setSupportsCudaStream(true);
#endif
// Setup Metal render capability flags only on Apple system
#ifdef __APPLE__
p_Desc.setSupportsMetalRender(true);
#endif
// Indicates that the plugin output does not depend on location or neighbours of a given pixel.
// Therefore, this plugin could be executed during LUT generation.
p_Desc.setNoSpatialAwareness(true);
}
void MaxToEquirectPluginFactory::describeInContext(OFX::ImageEffectDescriptor& p_Desc, OFX::ContextEnum /*p_Context*/)
{
// Source clip only in the filter context
// Create the mandated source clip
ClipDescriptor* srcClip = p_Desc.defineClip(kOfxImageEffectSimpleSourceClipName);
srcClip->addSupportedComponent(ePixelComponentRGBA);
srcClip->setTemporalClipAccess(false);
srcClip->setSupportsTiles(kSupportsTiles);
srcClip->setIsMask(false);
// Create the mandated output clip
ClipDescriptor* dstClip = p_Desc.defineClip(kOfxImageEffectOutputClipName);
dstClip->addSupportedComponent(ePixelComponentRGBA);
dstClip->addSupportedComponent(ePixelComponentAlpha);
dstClip->setSupportsTiles(kSupportsTiles);
// Make some pages and to things in
PageParamDescriptor* page = p_Desc.definePageParam("Controls");
}
ImageEffect* MaxToEquirectPluginFactory::createInstance(OfxImageEffectHandle p_Handle, ContextEnum /*p_Context*/)
{
return new MaxToEquirectPlugin(p_Handle);
}
void OFX::Plugin::getPluginIDs(PluginFactoryArray& p_FactoryArray)
{
static MaxToEquirectPluginFactory gainPlugin;
p_FactoryArray.push_back(&gainPlugin);
}