forked from stefsietz/reframe360resolve
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Reframe360.cpp
1301 lines (1056 loc) · 43.5 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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Reframe360.h"
#include "ofxsImageEffect.h"
#include "ofxsMultiThread.h"
#include "ofxsProcessing.h"
#define kPluginName "Reframe360 XL"
#define kPluginGrouping "Reframe360 XL"
#define kPluginDescription "Lets you animate a virtual camera in your 360 footage."
#define kPluginIdentifier "net.lerondpoint.Reframe360XLPlugin"
#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>
////////////////////////////////////////////////////////////////////////////////
class ImageScaler : public OFX::ImageProcessor
{
public:
explicit ImageScaler(OFX::ImageEffect& p_Instance);
#ifndef __APPLE__
virtual void processImagesCUDA();
#endif
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;
};
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];
}
// There is no CUDA on MacOS
#ifndef __APPLE__
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);
}
#endif
//diasble Metal since there is not yet a metal kernel
#if defined(__APPLE__)
extern void RunMetalKernel(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::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(_pMetalCmdQ, width, height, _fov, _tinyplanet, _rectilinear, input, output, _rotMat, _samples, _bilinear);
}
#endif
//#if defined(__OPENCL__)
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);
}
//#endif
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 = vec2((float)x / width, (float)y / height);
vec3 dir = vec3(0, 0, 0);
dir.x = (uv.x * 2) - 1;
dir.y = (uv.y * 2) - 1;
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);
iuv.x *= (width - 1);
iuv.y *= (height - 1);
int x_new = p_ProcWindow.x1 + (int)iuv.x;
int y_new = p_ProcWindow.y1 + (int)iuv.y;
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 = vec4(srcPix[0], srcPix[1], srcPix[2], srcPix[3]);
}
}
else
{
interpCol = vec4(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() const;
void setActiveParams();
std::string paramIdForCam(std::string baseName, int cam);
void setHiddenParam(std::string name, int cam, double value);
template<class TParam>
float BlendParam(TParam* param, const OFX::RenderArguments& p_Args, float offset) const;
float ApplyBlendCurve(float blend, const OFX::RenderArguments& p_Args, float offset) const;
float BlendFromAlpha(float alpha, const OFX::RenderArguments& p_Args, float offset) const;
/* 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::BooleanParam* m_ShowActiveCamera;
OFX::PushButtonParam* m_CopyButton;
OFX::PushButtonParam* m_PasteButton;
OFX::IntParam* m_CopyValue;
OFX::IntParam* m_CameraSequence;
OFX::ChoiceParam* m_BlendCurve;
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;
template <class TParam>
static double getNextKeyframeTime(TParam* param, double time);
template <class TParam>
static double getPreviousKeyframeTime(TParam* param, double time);
template <class TParam>
static float getPreviousKeyframeValue(TParam* param, double time);
template <class TParam>
static float getNextKeyframeValue(TParam* param, double time);
template <class TParam>
static float getRelativeKeyframeAlpha(TParam* param, double time, double offset);
template<class TParam>
static bool needsInterPolation(TParam* param, double time);
template<class TParam>
static bool isFirstKeyFrameTimeOrEarlier(TParam* param, double time);
template<class TParam>
static bool isLastKeyFrameTimeOrLater(TParam* param, double time);
template<class TParam>
static bool isExactlyOnKeyFrame(TParam* param, double time);
//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_CameraSequence = fetchIntParam("cam1");
m_BlendCurve = fetchChoiceParam("blend_curve");
m_Accel = fetchDoubleParam("accel");
m_ActiveCamera = fetchIntParam("active_cam");
m_ShowActiveCamera = fetchBooleanParam("show_active_cam");
m_CopyButton = fetchPushButtonParam("copy_button");
m_PasteButton = fetchPushButtonParam("paste_button");
m_CopyValue = fetchIntParam("copy_value");
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);
}
else if (p_ParamName.find("copy_button") == 0)
{
m_CopyValue->setValue(m_ActiveCamera->getValue());
}
else if (p_ParamName.find("paste_button") == 0)
{
int activeCam = m_ActiveCamera->getValue();
activeCam--;
int copyCam = m_CopyValue->getValue();
copyCam--;
m_Pitch1->setValue(m_Pitch2[copyCam]->getValue());
m_Yaw1->setValue(m_Yaw2[copyCam]->getValue());
m_Roll1->setValue(m_Roll2[copyCam]->getValue());
m_Fov1->setValue(m_Fov2[copyCam]->getValue());
m_Tinyplanet1->setValue(m_Tinyplanet2[copyCam]->getValue());
m_Recti1->setValue(m_Recti2[copyCam]->getValue());
}
}
void Reframe360::changedClip(const OFX::InstanceChangedArgs& p_Args, const std::string& p_ClipName)
{
if (p_ClipName == kOfxImageEffectSimpleSourceClipName)
{
setEnabledness();
}
}
void Reframe360::setEnabledness() const
{
// 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_BlendCurve->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 (float)param->getValueAtTime(p_Args.time);
}
else if (offset < 0)
{
offset = -offset;
float floor = std::floor(offset);
float frac = offset - floor;
return (float)param->getValueAtTime(p_Args.time - (floor + 1)) * frac + (float)param->
getValueAtTime(p_Args.time - floor) * (1 - frac);
}
else
{
float floor = std::floor(offset);
float frac = offset - floor;
return (float)param->getValueAtTime(p_Args.time + (floor + 1)) * frac + (float)param->
getValueAtTime(p_Args.time + floor) * (1 - frac);
}
}
static int interpParam(OFX::ChoiceParam* param, const OFX::RenderArguments& p_Args, float offset)
{
float floor = std::floor(offset);
int value;
param->getValueAtTime(p_Args.time + floor, value);
return value;
}
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);
}
float pitch = 0.0f, roll = 0.0f, yaw = 0.0f, fov = 1.0f,
pitch1 = 1.0f, yaw1 = 1.0f, roll1 = 0.0f, fov1 = 1.0f, tinyplanet1 = 1.0f, recti1 = 0.0f,
pitch2 = 1.0f, yaw2 = 1.0f, roll2 = 0.0f, fov2 = 1.0f, tinyplanet2 = 1.0f, recti2 = 0.0f,
blend = 0.0f;
int mb_samples = (int)m_Samples->getValueAtTime(p_Args.time);
float mb_shutter = (float)m_Shutter->getValueAtTime(p_Args.time);
int bilinear = m_Bilinear->getValueAtTime(p_Args.time);
int activeCam = m_ActiveCamera->getValueAtTime(p_Args.time) - 1;
int cam1 = static_cast<int>(getPreviousKeyframeValue(m_CameraSequence, p_Args.time)) - 1;
int cam2 = static_cast<int>(getNextKeyframeValue(m_CameraSequence, 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);
// Yaw needs to be inverted in multi threaded rendering... not sure why
bool invertYaw = !p_Args.isEnabledCudaRender && !p_Args.isEnabledOpenCLRender && !p_Args.isEnabledMetalRender;
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 / 360;
pitch = BlendParam(m_Pitch, p_Args, offset);
yaw = BlendParam(m_Yaw, p_Args, offset);
roll = BlendParam(m_Roll, p_Args, offset);
fov = BlendParam(m_Fov, 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);
float camBlend = 0;
if (cam1 != cam2)
camBlend = getRelativeKeyframeAlpha(m_CameraSequence, p_Args.time, offset);
if (showActiveCam)
{
blend = 0;
}
else
{
blend = BlendFromAlpha(camBlend, p_Args, offset);
}
pitch = pitch1 + (pitch2 - pitch1) * blend + pitch;
yaw = yaw1 + (yaw2 - yaw1) * blend + yaw;
roll = roll1 + (roll2 - roll1) * blend + roll;
fov = (fov1 + (fov2 - fov1) * blend) * fov;
tinyplanet1 = tinyplanet1 + (tinyplanet2 - tinyplanet1) * blend;
recti1 = recti1 + (recti2 - recti1) * blend;
if (invertYaw)
{
yaw = -yaw;
}
float* pitchMat = (float*)calloc(9, sizeof(float));
pitchMatrix(-pitch / 180 * (float)M_PI, &pitchMat);
float* yawMat = (float*)calloc(9, sizeof(float));
yawMatrix(yaw / 180 * (float)M_PI, &yawMat);
float* rollMat = (float*)calloc(9, sizeof(float));
rollMatrix(-roll / 180 * (float)M_PI, &rollMat);
float* pitchRollMat = (float*)calloc(9, sizeof(float));
float* rotMat = (float*)calloc(9, sizeof(float));
matMul(pitchMat, rollMat, &pitchRollMat);
matMul(yawMat, pitchRollMat, &rotMat);
free(pitchMat);
free(yawMat);
free(rollMat);
memcpy(&(rotmats[i * 9]), rotMat, sizeof(float) * 9);
free(rotMat);
fovs[i] = static_cast<float>(fov);
tinyplanets[i] = static_cast<float>(tinyplanet1);
rectilinears[i] = static_cast<float>(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);
#ifndef __APPLE__
p_Desc.setSupportsCudaRender(true);
#endif
#ifdef __APPLE__
p_Desc.setSupportsMetalRender(true);
#endif
}
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 DoubleParamDescriptor* defineAngleParam(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(eDoubleTypeAngle);
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 ChoiceParamDescriptor* defineChoiceParam(OFX::ImageEffectDescriptor& p_Desc, const std::string& p_Name,
const std::string& p_Label,
const std::string& p_Hint, GroupParamDescriptor* p_Parent, int default_value,
std::string p_ChoiceLabels[], int choiceCount)
{
ChoiceParamDescriptor* param = p_Desc.defineChoiceParam(p_Name);
param->setLabels(p_Label, p_Label, p_Label);
param->setScriptName(p_Name);
param->setHint(p_Hint);
param->setDefault(default_value);
for (int i = 0; i < choiceCount; ++i)
{
auto choice = p_ChoiceLabels[i];
param->appendOption(choice, choice);
}
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;
}
static PushButtonParamDescriptor* defineButtonParam(OFX::ImageEffectDescriptor& p_Desc, const std::string& p_Name,
const std::string& p_Label,
const std::string& p_Hint, GroupParamDescriptor* p_Parent)
{
PushButtonParamDescriptor* param = p_Desc.definePushButtonParam(p_Name);
param->setLabels(p_Label, p_Label, p_Label);
param->setScriptName(p_Name);
param->setHint(p_Hint);
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 = defineAngleParam(p_Desc, "main_pitch", "Pitch", "Up/down camera rotation. Displayed value might not be in sync with actual camera depending on animation curve parameters.",
camera1ParamsGroup, -90, 90, 0);
page->addChild(*param);
param = defineAngleParam(p_Desc, "main_yaw", "Yaw", "Left/right camera rotation. Displayed value might not be in sync with actual camera depending on animation curve parameters.", camera1ParamsGroup, -180, 180, 0);
page->addChild(*param);
param = defineAngleParam(p_Desc, "main_roll", "Roll", "Camera Roll. Displayed value might not be in sync with actual camera depending on animation curve parameters.", camera1ParamsGroup, -180, 180, 0);
page->addChild(*param);
param = defineParam(p_Desc, "main_fov", "Field of View", "Camera field of view. Displayed value might not be in sync with actual camera depending on animation curve parameters.", camera1ParamsGroup, 0.15f, 5, 1);
page->addChild(*param);
GroupParamDescriptor* animGroup = p_Desc.defineGroupParam("animParams");
animGroup->setHint("Camera Animation Parameters");
animGroup->setLabels("Camera Animation Parameters", "Camera Animation Parameters", "Camera Animation Parameters");
IntParamDescriptor* intParam = defineIntParam(p_Desc, "cam1", "Camera Sequence", "Camera Sequence", animGroup, 1,
20, 1);
page->addChild(*intParam);
std::string choices[] = {"Power", "Sine", "Exponential", "Circular"};
ChoiceParamDescriptor* choiceParam = defineChoiceParam(p_Desc, "blend_curve", "Blend Curve", "Blend Curve",
animGroup, 0, choices, 4);
page->addChild(*choiceParam);
param = defineParam(p_Desc, "accel", "Power Acceleration", "Power Acceleration (only used for Power curve mode)", animGroup, 1, 20, 3, 0.5, 20);
page->addChild(*param);
GroupParamDescriptor* cameraSelectionParamsGroup = p_Desc.defineGroupParam("cameraSelectionParams");
cameraSelectionParamsGroup->setHint("Camera Selection Parameters");
cameraSelectionParamsGroup->setLabels("Camera Selection Parameters", "Camera Selection Parameters",
"Camera Selection Parameters");
intParam = defineIntParam(p_Desc, "active_cam", "Edit Camera", "Edit Camera", cameraSelectionParamsGroup, 1,
MAX_CAM_NUM, 1, 1, 20);
page->addChild(*intParam);
BooleanParamDescriptor* boolParam = defineBooleanParam(p_Desc, "show_active_cam", "Show Edit Camera",
"Show Edit Camera", cameraSelectionParamsGroup, 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");
PushButtonParamDescriptor* buttonParam = defineButtonParam(p_Desc, "copy_button", "Copy Camera", "Copy Camera",
camera2ParamsGroup);
page->addChild(*buttonParam);
buttonParam = defineButtonParam(p_Desc, "paste_button", "Paste Camera", "Paste Camera", camera2ParamsGroup);
page->addChild(*buttonParam);
intParam = defineIntParam(p_Desc, "copy_value", "", "", camera2ParamsGroup, 1, 20, 1);
intParam->setIsSecret(true);
page->addChild(*intParam);
// Make the camera 2 params
param = defineAngleParam(p_Desc, "aux_pitch", "Pitch", "Up/down camera rotation", camera2ParamsGroup, -90, 90, 0);
page->addChild(*param);
param = defineAngleParam(p_Desc, "aux_yaw", "Yaw", "Left/right camera rotation", camera2ParamsGroup, -180, 180, 0);
page->addChild(*param);
param = defineAngleParam(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.15f, 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++)
{