forked from stefsietz/reframe360resolve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reframe360.cpp
912 lines (717 loc) · 29.2 KB
/
Reframe360.cpp
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
#include "Reframe360.h"
#include <stdio.h>
#include <memory.h>
#include "ofxsImageEffect.h"
#include "ofxsMultiThread.h"
#include "ofxsProcessing.h"
#include "ofxsLog.h"
#define kPluginName "Reframe360"
#define kPluginGrouping "Reframe360"
#define kPluginDescription "Lets you animate a virtual camera in your 360 footage."
#define kPluginIdentifier "at.stefansietzen.Reframe360Plugin"
#define kPluginVersionMajor 1
#define kPluginVersionMinor 0
#define kSupportsTiles false
#define kSupportsMultiResolution false
#define kSupportsMultipleClipPARs false
#define MAX_CAM_NUM 20
#define M_PI 3.14159265358979323846
///GLM Imports
#include "MathUtil.h"
#include <ctime>
////////////////////////////////////////////////////////////////////////////////
typedef struct CameraParams {
float pitch = 0;
float yaw = 0;
float roll = 0;
float fov = 1.0;
float tinyplanet = 1;
float rectilinear = 0;
}CameraParams;
typedef struct ReframeParamSet {
CameraParams mainCamParams;
int activeCamera = 1;
int camera1 = 1;
int camera2 = 2;
float blend = 0;
float acceleration = 3.0f;
bool forceActiveAuxCamera = false;
CameraParams auxCamParams[20];
}ReframeParamSet;
class ImageScaler : public OFX::ImageProcessor
{
public:
explicit ImageScaler(OFX::ImageEffect& p_Instance);
virtual void processImagesCUDA();
virtual void processImagesOpenCL();
#if defined(__APPLE__)
virtual void processImagesMetal();
#endif
virtual void multiThreadProcessImages(OfxRectI p_ProcWindow);
void setSrcImg(OFX::Image* p_SrcImg);
void setParams(float* p_RotMat, float* p_Fov, float* p_Tinyplanet, float* p_Rectilinear, int p_Samples, bool p_Bilinear);
private:
OFX::Image* _srcImg;
float* _rotMat;
float* _fov;
float* _tinyplanet;
float* _rectilinear;
int _samples;
bool _bilinear;
CameraParams mainCamParams;
std::map<int, CameraParams> auxCameraParams();
};
ImageScaler::ImageScaler(OFX::ImageEffect& p_Instance)
: OFX::ImageProcessor(p_Instance)
{
}
void pitchMatrix(float pitch, float** out) {
(*out)[0] = 1.0;
(*out)[1] = 0;
(*out)[2] = 0;
(*out)[3] = 0;
(*out)[4] = cos(pitch);
(*out)[5] = -sin(pitch);
(*out)[6] = 0;
(*out)[7] = sin(pitch);
(*out)[8] = cos(pitch);
}
void yawMatrix(float yaw, float** out) {
(*out)[0] = cos(yaw);
(*out)[1] = 0;
(*out)[2] = sin(yaw);
(*out)[3] = 0;
(*out)[4] = 1.0;
(*out)[5] = 0;
(*out)[6] = -sin(yaw);
(*out)[7] = 0;
(*out)[8] = cos(yaw);
}
void rollMatrix(float roll, float** out) {
(*out)[0] = cos(roll);
(*out)[1] = -sin(roll);
(*out)[2] = 0;
(*out)[3] = sin(roll);
(*out)[4] = cos(roll);
(*out)[5] = 0;
(*out)[6] = 0;
(*out)[7] = 0;
(*out)[8] = 1.0;
}
void matMul(const float* y, const float* p, float** outmat){
(*outmat)[0] = p[0] * y[0] + p[3] * y[1] + p[6] * y[2];
(*outmat)[1] = p[1] * y[0] + p[4] * y[1] + p[7] * y[2];
(*outmat)[2] = p[2] * y[0] + p[5] * y[1] + p[8] * y[2];
(*outmat)[3] = p[0] * y[3] + p[3] * y[4] + p[6] * y[5];
(*outmat)[4] = p[1] * y[3] + p[4] * y[4] + p[7] * y[5];
(*outmat)[5] = p[2] * y[3] + p[5] * y[4] + p[8] * y[5];
(*outmat)[6] = p[0] * y[6] + p[3] * y[7] + p[6] * y[8];
(*outmat)[7] = p[1] * y[6] + p[4] * y[7] + p[7] * y[8];
(*outmat)[8] = p[2] * y[6] + p[5] * y[7] + p[8] * y[8];
}
extern void RunCudaKernel(int p_Width, int p_Height, float* p_Fov, float* p_Tinyplanet, float* p_Rectilinear, const float* p_Input, float* p_Output, const float* p_RotMat, int p_Samples, bool p_Bilinear);
void ImageScaler::processImagesCUDA()
{
const OfxRectI& bounds = _srcImg->getBounds();
const int width = bounds.x2 - bounds.x1;
const int height = bounds.y2 - bounds.y1;
float* input = static_cast<float*>(_srcImg->getPixelData());
float* output = static_cast<float*>(_dstImg->getPixelData());
RunCudaKernel(width, height, _fov, _tinyplanet, _rectilinear, input, output, _rotMat, _samples, _bilinear);
}
#if defined(__APPLE__)
extern void RunMetalKernel(int p_Width, int p_Height, float* p_Gain, const float* p_Input, float* p_Output);
void ImageScaler::processImagesMetal()
{
const OfxRectI& bounds = _srcImg->getBounds();
const int width = bounds.x2 - bounds.x1;
const int height = bounds.y2 - bounds.y1;
float* input = static_cast<float*>(_srcImg->getPixelData());
float* output = static_cast<float*>(_dstImg->getPixelData());
RunMetalKernel(width, height, _params, input, output);
}
#endif
extern void RunOpenCLKernel(void* p_CmdQ, int p_Width, int p_Height, float* p_Fov, float* p_Tinyplanet, float* p_Rectilinear, const float* p_Input, float* p_Output, float* p_RotMat, int p_Samples, bool p_Bilinear);
void ImageScaler::processImagesOpenCL()
{
const OfxRectI& bounds = _srcImg->getBounds();
const int width = bounds.x2 - bounds.x1;
const int height = bounds.y2 - bounds.y1;
float* input = static_cast<float*>(_srcImg->getPixelData());
float* output = static_cast<float*>(_dstImg->getPixelData());
RunOpenCLKernel(_pOpenCLCmdQ, width, height, _fov, _tinyplanet, _rectilinear, input, output, _rotMat, _samples, _bilinear);
}
void ImageScaler::multiThreadProcessImages(OfxRectI p_ProcWindow)
{
int width = p_ProcWindow.x2 - p_ProcWindow.x1;
int height = p_ProcWindow.y2 - p_ProcWindow.y1;
for (int i = 0; i < _samples; i++)
{
mat3 rotMat;
rotMat[0][0] = _rotMat[i * 9 + 0];
rotMat[0][1] = _rotMat[i * 9 + 1];
rotMat[0][2] = _rotMat[i * 9 + 2];
rotMat[1][0] = _rotMat[i * 9 + 3];
rotMat[1][1] = _rotMat[i * 9 + 4];
rotMat[1][2] = _rotMat[i * 9 + 5];
rotMat[2][0] = _rotMat[i * 9 + 6];
rotMat[2][1] = _rotMat[i * 9 + 7];
rotMat[2][2] = _rotMat[i * 9 + 8];
float fov = _fov[i];
float aspect = (float)width / (float)height;
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)
{
vec2 uv = { (float)x / width, (float)y / height };
vec3 dir = { 0, 0, 0 };
dir.x = (uv.x - 0.5)*2.0;
dir.y = (uv.y - 0.5)*2.0;
dir.y /= aspect;
dir.z = fov;
vec3 tinyplanet = tinyPlanetSph(dir);
tinyplanet = normalize(tinyplanet);
tinyplanet = rotMat * tinyplanet;
vec3 rectdir = rotMat * dir;
rectdir = normalize(rectdir);
dir = mix(fisheyeDir(dir, rotMat), tinyplanet, _tinyplanet[i]);
dir = mix(dir, rectdir, _rectilinear[i]);
vec2 iuv = polarCoord(dir);
iuv = repairUv(iuv);
int x_new = p_ProcWindow.x1 + iuv.x * (width - 1);
int y_new = p_ProcWindow.y1 + iuv.y * (height - 1);
iuv.x *= (width - 1);
iuv.y *= (height - 1);
if ((x_new < width) && (y_new < height))
{
float* srcPix = static_cast<float*>(_srcImg ? _srcImg->getPixelAddress(x_new, y_new) : 0);
vec4 interpCol;
// do we have a source image to scale up
if (srcPix)
{
if (_bilinear){
interpCol = linInterpCol(iuv, _srcImg, p_ProcWindow, width, height);
}
else {
interpCol = { srcPix[0], srcPix[1], srcPix[2], srcPix[3] };
}
}
else
{
interpCol = { 0, 0, 0, 1.0 };
}
if (i == 0) {
dstPix[0] = 0;
dstPix[1] = 0;
dstPix[2] = 0;
dstPix[3] = 0;
}
dstPix[0] += interpCol.x / _samples;
dstPix[1] += interpCol.y / _samples;
dstPix[2] += interpCol.z / _samples;
dstPix[3] += interpCol.w / _samples;
}
// increment the dst pixel
dstPix += 4;
continue;
}
}
}
}
void ImageScaler::setSrcImg(OFX::Image* p_SrcImg)
{
_srcImg = p_SrcImg;
}
void ImageScaler::setParams(float* p_RotMat, float* p_Fov, float* p_Tinyplanet, float* p_Rectilinear, int p_Samples, bool p_Bilinear)
{
_rotMat = p_RotMat;
_fov = p_Fov;
_tinyplanet = p_Tinyplanet;
_rectilinear = p_Rectilinear;
_samples = p_Samples;
_bilinear = p_Bilinear;
}
////////////////////////////////////////////////////////////////////////////////
/** @brief The plugin that does our work */
class Reframe360 : public OFX::ImageEffect
{
public:
explicit Reframe360(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 the enabledness of the component scale params depending on the type of input image and the state of the scaleComponents param */
void setEnabledness();
void setActiveParams();
std::string paramIdForCam(std::string baseName, int cam);
void setHiddenParam(std::string name, int cam, double value);
/* 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;
OFX::DoubleParam* m_Pitch;
OFX::DoubleParam* m_Yaw;
OFX::DoubleParam* m_Roll;
OFX::DoubleParam* m_Fov;
OFX::IntParam* m_ActiveCamera;
OFX::IntParam* m_Camera1;
OFX::IntParam* m_Camera2;
OFX::BooleanParam* m_ShowActiveCamera;
OFX::DoubleParam* m_Blend;
OFX::DoubleParam* m_Accel;
OFX::DoubleParam* m_Pitch1;
OFX::DoubleParam* m_Yaw1;
OFX::DoubleParam* m_Roll1;
OFX::DoubleParam* m_Fov1;
OFX::DoubleParam* m_Tinyplanet1;
OFX::DoubleParam* m_Recti1;
OFX::DoubleParam* m_Pitch2[MAX_CAM_NUM];
OFX::DoubleParam* m_Yaw2[MAX_CAM_NUM];
OFX::DoubleParam* m_Roll2[MAX_CAM_NUM];
OFX::DoubleParam* m_Fov2[MAX_CAM_NUM];
OFX::DoubleParam* m_Tinyplanet2[MAX_CAM_NUM];
OFX::DoubleParam* m_Recti2[MAX_CAM_NUM];
OFX::DoubleParam* m_Shutter;
OFX::IntParam* m_Samples;
OFX::BooleanParam* m_Bilinear;
//ReframeParamSet m_ParamStruct;
};
Reframe360::Reframe360(OfxImageEffectHandle p_Handle)
: ImageEffect(p_Handle)
{
m_DstClip = fetchClip(kOfxImageEffectOutputClipName);
m_SrcClip = fetchClip(kOfxImageEffectSimpleSourceClipName);
m_Pitch = fetchDoubleParam("main_pitch");
m_Yaw = fetchDoubleParam("main_yaw");
m_Roll = fetchDoubleParam("main_roll");
m_Fov = fetchDoubleParam("main_fov");
m_ActiveCamera = fetchIntParam("active_cam");
m_Camera1 = fetchIntParam("cam1");
m_Camera2 = fetchIntParam("cam2");
m_ShowActiveCamera = fetchBooleanParam("show_active_cam");
m_Blend = fetchDoubleParam("blend");
m_Accel = fetchDoubleParam("accel");
m_Shutter = fetchDoubleParam("shutter");
m_Samples = fetchIntParam("samples");
m_Bilinear = fetchBooleanParam("bilinear");
m_Pitch1 = fetchDoubleParam("aux_pitch");
m_Yaw1 = fetchDoubleParam("aux_yaw");
m_Roll1 = fetchDoubleParam("aux_roll");
m_Fov1 = fetchDoubleParam("aux_fov");
m_Tinyplanet1 = fetchDoubleParam("aux_tiny");
m_Recti1 = fetchDoubleParam("aux_recti");
for (int i = 0; i < MAX_CAM_NUM; i++){
m_Pitch2[i] = fetchDoubleParam(paramIdForCam("aux_pitch", i));
m_Yaw2[i] = fetchDoubleParam(paramIdForCam("aux_yaw", i));
m_Roll2[i] = fetchDoubleParam(paramIdForCam("aux_roll", i));
m_Fov2[i] = fetchDoubleParam(paramIdForCam("aux_fov", i));
m_Tinyplanet2[i] = fetchDoubleParam(paramIdForCam("aux_tiny", i));
m_Recti2[i] = fetchDoubleParam(paramIdForCam("aux_recti", i));
}
// Set the enabledness of our RGBA sliders
setEnabledness();
}
void Reframe360::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 Reframe360::isIdentity(const OFX::IsIdentityArguments& p_Args, OFX::Clip*& p_IdentityClip, double& p_IdentityTime)
{
return false;
}
void Reframe360::changedParam(const OFX::InstanceChangedArgs& p_Args, const std::string& p_ParamName)
{
if (p_ParamName.find("active_cam") == 0){
int activeCam = m_ActiveCamera->getValue();
activeCam--;
m_Pitch1->setValue(m_Pitch2[activeCam]->getValue());
m_Yaw1->setValue(m_Yaw2[activeCam]->getValue());
m_Roll1->setValue(m_Roll2[activeCam]->getValue());
m_Fov1->setValue(m_Fov2[activeCam]->getValue());
m_Tinyplanet1->setValue(m_Tinyplanet2[activeCam]->getValue());
m_Recti1->setValue(m_Recti2[activeCam]->getValue());
}
else if(p_ParamName.find("hidden") == std::string::npos && p_ParamName.find("aux")==0){
double value = fetchDoubleParam(p_ParamName)->getValue();
int activeCam = fetchIntParam("active_cam")->getValue();
std::stringstream ss;
ss << p_ParamName << "_hidden_" << activeCam;
std::string name = ss.str();
setHiddenParam(name, activeCam, value);
}
}
void Reframe360::changedClip(const OFX::InstanceChangedArgs& p_Args, const std::string& p_ClipName)
{
if (p_ClipName == kOfxImageEffectSimpleSourceClipName)
{
setEnabledness();
}
}
void Reframe360::setEnabledness()
{
// the component enabledness depends on the clip being RGBA and the param being true
const bool enable = ((m_SrcClip->getPixelComponents() == OFX::ePixelComponentRGBA));
/*
m_Pitch->setEnabled(enable);
m_Yaw->setEnabled(enable);
m_Fov->setEnabled(enable);
m_Recti->setEnabled(enable);
m_Blend->setEnabled(enable);
m_Accel->setEnabled(enable);
m_Pitch2->setEnabled(enable);
m_Yaw2->setEnabled(enable);
m_Fov2->setEnabled(enable);
m_Fisheye2->setEnabled(enable);
*/
}
static float interpParam(OFX::DoubleParam* param, const OFX::RenderArguments& p_Args, float offset){
if (offset == 0){
return param->getValueAtTime(p_Args.time);
}
else if (offset < 0) {
offset = -offset;
float floor = std::floor(offset);
float frac = offset - floor;
return param->getValueAtTime(p_Args.time - (floor + 1))*frac + param->getValueAtTime(p_Args.time - floor)*(1 - frac);
}
else {
float floor = std::floor(offset);
float frac = offset - floor;
return param->getValueAtTime(p_Args.time + (floor + 1))*frac + param->getValueAtTime(p_Args.time + floor)*(1 - frac);
}
}
void Reframe360::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);
}
double pitch = 0.0, roll = 0.0, yaw = 0.0, fov = 1.0,
pitch1 = 1.0, yaw1 = 1.0, roll1 = 0.0, fov1 = 1.0, tinyplanet1 = 1.0, recti1 = 0.0,
pitch2 = 1.0, yaw2 = 1.0, roll2 = 0.0, fov2 = 1.0, tinyplanet2 = 1.0, recti2 = 0.0,
blend = 0.0, accel = 0.5;
int mb_samples = (int)m_Samples->getValueAtTime(p_Args.time);
float mb_shutter = m_Shutter->getValueAtTime(p_Args.time) * 0.5;
int bilinear = m_Bilinear->getValueAtTime(p_Args.time);
int activeCam = m_ActiveCamera->getValueAtTime(p_Args.time)-1;
int cam1 = m_Camera1->getValueAtTime(p_Args.time)-1;
int cam2 = m_Camera2->getValueAtTime(p_Args.time)-1;
bool showActiveCam = m_ShowActiveCamera->getValueAtTime(p_Args.time);
if (showActiveCam) {
cam1 = activeCam;
}
float* fovs = (float*)malloc(sizeof(float)*mb_samples);
float* tinyplanets = (float*)malloc(sizeof(float)*mb_samples);
float* rectilinears = (float*)malloc(sizeof(float)*mb_samples);
float* rotmats = (float*)malloc(sizeof(float)*mb_samples*9);
for (int i = 0; i < mb_samples; i++){
float offset = 0;
if (mb_samples > 1){
offset = fitRange((float)i, 0, mb_samples - 1.0f, -1.0f, 1.0f);
}
offset *= mb_shutter;
pitch = interpParam(m_Pitch, p_Args, offset);
yaw = interpParam(m_Yaw, p_Args, offset);
roll = interpParam(m_Roll, p_Args, offset);
fov = interpParam(m_Fov, p_Args, offset);
blend = interpParam(m_Blend, p_Args, offset);
accel = interpParam(m_Accel, p_Args, offset);
pitch1 = interpParam(m_Pitch2[cam1], p_Args, offset);
yaw1 = interpParam(m_Yaw2[cam1], p_Args, offset);
roll1 = interpParam(m_Roll2[cam1], p_Args, offset);
fov1 = interpParam(m_Fov2[cam1], p_Args, offset);
tinyplanet1 = interpParam(m_Tinyplanet2[cam1], p_Args, offset);
recti1 = interpParam(m_Recti2[cam1], p_Args, offset);
pitch2 = interpParam(m_Pitch2[cam2], p_Args, offset);
yaw2 = interpParam(m_Yaw2[cam2], p_Args, offset);
roll2 = interpParam(m_Roll2[cam2], p_Args, offset);
fov2 = interpParam(m_Fov2[cam2], p_Args, offset);
tinyplanet2 = interpParam(m_Tinyplanet2[cam2], p_Args, offset);
recti2 = interpParam(m_Recti2[cam2], p_Args, offset);
if (blend < 0.5){
blend = fitRange(blend, 0, 0.5, 0, 1);
blend = std::pow(blend, accel);
blend = fitRange(blend, 0, 1, 0, 0.5);
}
else{
blend = fitRange(blend, 0.5, 1.0, 0, 1);
blend = 1.0 - blend;
blend = std::pow(blend, accel);
blend = 1.0 - blend;
blend = fitRange(blend, 0, 1, 0.5, 1.0);
}
if (showActiveCam) {
blend = 0;
}
pitch = (pitch1 * (1.0 - blend) + pitch2 * blend) + pitch;
yaw = (yaw1 * (1.0 - blend) + yaw2 * blend) + yaw;
roll = (roll1 * (1.0 - blend) + roll2 * blend) + roll;
fov = (fov1 * (1.0 - blend) + fov2 * blend) * fov;
tinyplanet1 = (tinyplanet1 * (1.0 - blend) + tinyplanet2 * blend);
recti1 = (recti1 * (1.0 - blend) + recti2 * blend);
float* pitchMat = (float*)calloc(9, sizeof(float));
pitchMatrix(-pitch / 180 * M_PI, &pitchMat);
float* yawMat = (float*)calloc(9, sizeof(float));
yawMatrix(yaw / 180 * M_PI, &yawMat);
float* rollMat = (float*)calloc(9, sizeof(float));
rollMatrix(-roll / 180 * M_PI, &rollMat);
float* pitchYawMat = (float*)calloc(9, sizeof(float));
float* rotMat = (float*)calloc(9, sizeof(float));
matMul(pitchMat, rollMat, &pitchYawMat);
matMul(yawMat, pitchYawMat, &rotMat);
free(pitchMat);
free(yawMat);
free(rollMat);
memcpy(&(rotmats[i * 9]), rotMat, sizeof(float) * 9);
free(rotMat);
fovs[i] = fov;
tinyplanets[i] = tinyplanet1;
rectilinears[i] = recti1;
}
// 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);
// Set the scales
p_ImageScaler.setParams(rotmats, fovs, tinyplanets, rectilinears, mb_samples, bilinear);
// Call the base class process member, this will call the derived templated process code
p_ImageScaler.process();
}
////////////////////////////////////////////////////////////////////////////////
using namespace OFX;
Reframe360Factory::Reframe360Factory()
: OFX::PluginFactoryHelper<Reframe360Factory>(kPluginIdentifier, kPluginVersionMajor, kPluginVersionMinor)
{
}
void Reframe360Factory::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 and CUDA render capability flags
p_Desc.setSupportsOpenCLRender(true);
p_Desc.setSupportsCudaRender(true);
p_Desc.setSupportsMetalRender(false);
}
static DoubleParamDescriptor* defineParam(OFX::ImageEffectDescriptor& p_Desc, const std::string& p_Name, const std::string& p_Label,
const std::string& p_Hint, GroupParamDescriptor* p_Parent, float min, float max, float default_value, float hardmin = INT_MIN, float hardmax = INT_MAX)
{
DoubleParamDescriptor* param = p_Desc.defineDoubleParam(p_Name);
param->setLabels(p_Label, p_Label, p_Label);
param->setScriptName(p_Name);
param->setHint(p_Hint);
param->setDefault(default_value);
param->setRange(hardmin, hardmax);
param->setIncrement(0.1);
param->setDisplayRange(min, max);
param->setDoubleType(eDoubleTypePlain);
if (p_Parent)
{
param->setParent(*p_Parent);
}
return param;
}
static IntParamDescriptor* defineIntParam(OFX::ImageEffectDescriptor& p_Desc, const std::string& p_Name, const std::string& p_Label,
const std::string& p_Hint, GroupParamDescriptor* p_Parent, int min, int max, int default_value, int hardmin = INT_MIN, int hardmax = INT_MAX)
{
IntParamDescriptor* param = p_Desc.defineIntParam(p_Name);
param->setLabels(p_Label, p_Label, p_Label);
param->setScriptName(p_Name);
param->setHint(p_Hint);
param->setDefault(default_value);
param->setRange(hardmin, hardmax);
param->setDisplayRange(min, max);
if (p_Parent)
{
param->setParent(*p_Parent);
}
return param;
}
static BooleanParamDescriptor* defineBooleanParam(OFX::ImageEffectDescriptor& p_Desc, const std::string& p_Name, const std::string& p_Label,
const std::string& p_Hint, GroupParamDescriptor* p_Parent, bool default_value)
{
BooleanParamDescriptor* param = p_Desc.defineBooleanParam(p_Name);
param->setLabels(p_Label, p_Label, p_Label);
param->setScriptName(p_Name);
param->setHint(p_Hint);
param->setDefault(default_value);
if (p_Parent)
{
param->setParent(*p_Parent);
}
return param;
}
void Reframe360Factory::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");
GroupParamDescriptor* camera1ParamsGroup = p_Desc.defineGroupParam("mainCameraParams");
camera1ParamsGroup->setHint("Main Camera Parameters");
camera1ParamsGroup->setLabels("Main Camera Parameters", "Main Camera Parameters", "Main Camera Parameters");
// Make the camera 1 params
DoubleParamDescriptor* param = defineParam(p_Desc, "main_pitch", "Pitch", "Up/down camera rotation", camera1ParamsGroup, -90, 90, 0);
page->addChild(*param);
param = defineParam(p_Desc, "main_yaw", "Yaw", "Left/right camera rotation", camera1ParamsGroup, -180, 180, 0);
page->addChild(*param);
param = defineParam(p_Desc, "main_roll", "Roll", "Camera Roll", camera1ParamsGroup, -180, 180, 0);
page->addChild(*param);
param = defineParam(p_Desc, "main_fov", "Field of View", "Camera field of view", camera1ParamsGroup, 0.15, 5, 1);
page->addChild(*param);
GroupParamDescriptor* blendGroup = p_Desc.defineGroupParam("blendParams");
blendGroup->setHint("Blend Camera Parameters");
blendGroup->setLabels("Blend Camera Parameters", "Blend Camera Parameters", "Blend Camera Parameters");
IntParamDescriptor* intParam = defineIntParam(p_Desc, "cam1", "Camera 1", "Camera 1", blendGroup, 1, MAX_CAM_NUM, 1, 1, 20);
page->addChild(*intParam);
intParam = defineIntParam(p_Desc, "cam2", "Camera 2", "Camera 2", blendGroup, 1, MAX_CAM_NUM, 2, 1, 20);
page->addChild(*intParam);
param = defineParam(p_Desc, "blend", "Blend", "Blend Cameras", blendGroup, 0, 1, 0);
page->addChild(*param);
param = defineParam(p_Desc, "accel", "Blend Acceleration", "Blend Acceleration", blendGroup, 1, 20, 3, 0.5, 20);
page->addChild(*param);
GroupParamDescriptor* cameraSelctionParamsGroup = p_Desc.defineGroupParam("cameraSelectionParams");
cameraSelctionParamsGroup->setHint("Camera Selection Parameters");
cameraSelctionParamsGroup->setLabels("Camera Selection Parameters", "Camera Selection Parameters", "Camera Selection Parameters");
intParam = defineIntParam(p_Desc, "active_cam", "Edit Camera", "Edit Camera", cameraSelctionParamsGroup, 1, MAX_CAM_NUM, 1, 1, 20);
page->addChild(*intParam);
BooleanParamDescriptor* boolParam = defineBooleanParam(p_Desc, "show_active_cam", "Show Edit Camera", "Show Edit Camera", cameraSelctionParamsGroup, false);
page->addChild(*boolParam);
GroupParamDescriptor* camera2ParamsGroup = p_Desc.defineGroupParam("auxCameraParams");
camera2ParamsGroup->setHint("Aux Camera Parameters");
camera2ParamsGroup->setLabels("Aux Camera Parameters", "Aux Camera Parameters", "Aux Camera Parameters");
// Make the camera 2 params
param = defineParam(p_Desc, "aux_pitch", "Pitch", "Up/down camera rotation", camera2ParamsGroup, -90, 90, 0);
page->addChild(*param);
param = defineParam(p_Desc, "aux_yaw", "Yaw", "Left/right camera rotation", camera2ParamsGroup, -180, 180, 0);
page->addChild(*param);
param = defineParam(p_Desc, "aux_roll", "Roll", "Camera Roll", camera2ParamsGroup, -180, 180, 0);
page->addChild(*param);
param = defineParam(p_Desc, "aux_fov", "Field of View", "Camera field of view", camera2ParamsGroup, 0.15, 5, 1);
page->addChild(*param);
param = defineParam(p_Desc, "aux_recti", "Rectilinear Projection", "Rectilinear Projection", camera2ParamsGroup, 0, 1, 0, 0, 1);
page->addChild(*param);
param = defineParam(p_Desc, "aux_tiny", "Tiny Planet", "Blend between standard Fisheye projection and Tiny Planet Projection", camera2ParamsGroup, 0, 1, 0, 0, 1);
page->addChild(*param);
//hidden params
GroupParamDescriptor* hiddenCameraParamsGroup = p_Desc.defineGroupParam("hiddenCameraParams");
hiddenCameraParamsGroup->setHint("Camera Parameters");
hiddenCameraParamsGroup->setLabels("Camera Parameters", "Camera Parameters", "Camera Parameters");
hiddenCameraParamsGroup->setIsSecret(true);
for (int i = 0; i < MAX_CAM_NUM; i++){
param = defineParam(p_Desc, paramIdForCam("aux_pitch", i), "Pitch", "Up/down camera rotation", hiddenCameraParamsGroup, -90, 90, 0);
page->addChild(*param);
param = defineParam(p_Desc, paramIdForCam("aux_yaw", i), "Yaw", "Left/right camera rotation", hiddenCameraParamsGroup, -180, 180, 0);
page->addChild(*param);
param = defineParam(p_Desc, paramIdForCam("aux_roll", i), "Roll", "Camera Roll", hiddenCameraParamsGroup, -180, 180, 0);
page->addChild(*param);
param = defineParam(p_Desc, paramIdForCam("aux_fov", i), "Field of View", "Camera field of view", hiddenCameraParamsGroup, 0.15, 5, 1);
page->addChild(*param);
param = defineParam(p_Desc, paramIdForCam("aux_tiny", i), "Tiny Planet", "Blend between standard Fisheye projection and Tiny Planet Projection", hiddenCameraParamsGroup, 0, 1, 0, 0, 1);
page->addChild(*param);
param = defineParam(p_Desc, paramIdForCam("aux_recti", i), "Rectify Projection", "Rectify Projection", hiddenCameraParamsGroup, 0, 1, 0, 0, 1);
page->addChild(*param);
}
GroupParamDescriptor* motionblurGroup = p_Desc.defineGroupParam("motionblurParams");
motionblurGroup->setHint("Motion Camera Parameters");
motionblurGroup->setLabels("Motion Blur Parameters", "Motion Blur Parameters", "Motion Blur Parameters");
param = defineParam(p_Desc, "shutter", "Shutter Angle", "Shutter Angle", motionblurGroup, 0, 1, 0, 0, 3);
page->addChild(*param);
intParam = defineIntParam(p_Desc, "samples", "Samples", "Samples", motionblurGroup, 1, 20, 1, 1, 256);
page->addChild(*intParam);
BooleanParamDescriptor* bilinearParam = defineBooleanParam(p_Desc, "bilinear", "Bilinear Filtering", "Bilinear Filtering", motionblurGroup, true);
page->addChild(*bilinearParam);
}
ImageEffect* Reframe360Factory::createInstance(OfxImageEffectHandle p_Handle, ContextEnum /*p_Context*/)
{
return new Reframe360(p_Handle);
}
void OFX::Plugin::getPluginIDs(PluginFactoryArray& p_FactoryArray)
{
static Reframe360Factory reframe360;
p_FactoryArray.push_back(&reframe360);
}
std::string Reframe360Factory::paramIdForCam(std::string baseName, int cam){
std::stringstream ss;
ss << baseName << "_hidden_" << cam;
return ss.str();
}
std::string Reframe360::paramIdForCam(std::string baseName, int cam){
std::stringstream ss;
ss << baseName << "_hidden_" << cam;
return ss.str();
}
void Reframe360::setHiddenParam(std::string name, int cam, double value){
cam--;
if (name.find("pitch") != std::string::npos){
m_Pitch2[cam]->setValue(value);
}
else if (name.find("yaw") != std::string::npos){
m_Yaw2[cam]->setValue(value);
}
else if (name.find("roll") != std::string::npos){
m_Roll2[cam]->setValue(value);
}
else if (name.find("fov") != std::string::npos){
m_Fov2[cam]->setValue(value);
}
else if (name.find("tiny") != std::string::npos){
m_Tinyplanet2[cam]->setValue(value);
}
else if (name.find("recti") != std::string::npos){
m_Recti2[cam]->setValue(value);
}
}