Skip to content

Commit

Permalink
thsad2
Browse files Browse the repository at this point in the history
  • Loading branch information
IFeelBloated committed May 15, 2020
1 parent e5db0f3 commit e9a8f10
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
18 changes: 15 additions & 3 deletions src/MVCompensate.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,21 @@ static void VS_CC mvcompensateCreate(const VSMap* in, VSMap* out, void* userData
auto clip = Clip{};
auto vectors = Clip{};
auto cclip = Clip{};
auto thsad = 10000.;
auto thsad2 = thsad;
clip = args["clip"];
vectors = args["vectors"];
if (args["cclip"].Exists())
cclip = args["cclip"];
else
cclip = clip;
auto Eval = [&](auto&& vec) {
if (args["thsad"].Exists())
thsad = args["thsad"];
if (args["thsad2"].Exists())
thsad2 = args["thsad2"];
else
thsad2 = thsad;
auto Eval = [&](auto&& vec, auto sad) {
auto CreateArgumentMap = [&]() {
auto Map = vsapi->createMap();
for (auto x : Range{ vsapi->propNumKeys(in) }) {
Expand All @@ -571,8 +579,11 @@ static void VS_CC mvcompensateCreate(const VSMap* in, VSMap* out, void* userData
}
}
auto v = WritableItem{ Map, "vectors" };
auto sad_param = WritableItem{ Map, "thsad" };
v.Erase();
v = vec;
sad_param.Erase();
sad_param = sad;
return Map;
};
auto argMap = CreateArgumentMap();
Expand Down Expand Up @@ -603,14 +614,14 @@ static void VS_CC mvcompensateCreate(const VSMap* in, VSMap* out, void* userData
auto comps = std::vector<Clip>{};
comps.reserve(2 * radius + 1);
for (auto x : Range{ radius }) {
auto comp = Eval(Core["std"]["SelectEvery"]("clip", vectors, "cycle", 2 * radius, "offsets", x));
auto comp = Eval(Core["std"]["SelectEvery"]("clip", vectors, "cycle", 2 * radius, "offsets", x), CosineAnnealing(thsad, thsad2, radius - x, radius));
if (comp.ContainsVideoReference() == false)
return;
comps.push_back(std::move(comp));
}
comps.push_back(cclip);
for (auto x : Range{ radius, 2 * radius })
comps.push_back(Eval(Core["std"]["SelectEvery"]("clip", vectors, "cycle", 2 * radius, "offsets", x)));
comps.push_back(Eval(Core["std"]["SelectEvery"]("clip", vectors, "cycle", 2 * radius, "offsets", x), CosineAnnealing(thsad, thsad2, x - radius + 1, radius)));
auto compmulti = Core["std"]["Interleave"]("clips", comps);
VaporGlobals::API->propSetNode(out, "clip", compmulti.VideoNode, VSPropAppendMode::paAppend);
}
Expand All @@ -624,6 +635,7 @@ void mvcompensateRegister(VSRegisterFunction registerFunc, VSPlugin *plugin) {
"cclip:clip:opt;"
"scbehavior:int:opt;"
"thsad:float:opt;"
"thsad2:float:opt;"
"fields:int:opt;"
"time:float:opt;"
"thscd1:float:opt;"
Expand Down
40 changes: 30 additions & 10 deletions src/MVDegrain.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct MVDegrainData {
self(super, Clip{});
self(vectors, std::vector<Clip>{});
self(radius, 0);
double thSAD[3];
self(thSAD, std::vector<std::array<double, 3>>{});
int32_t YUVplanes;
double nLimit[3];
double nSCD1;
Expand Down Expand Up @@ -203,7 +203,6 @@ static const VSFrameRef* VS_CC mvdegrainGetFrame(int32_t n, int32_t activationRe
const int32_t* nBlkSizeY = d->nBlkSizeY;
const int32_t* nWidth_B = d->nWidth_B;
const int32_t* nHeight_B = d->nHeight_B;
const double* thSAD = d->thSAD;
const double* nLimit = d->nLimit;
auto pRefGOF = d->CreateArray<MVGroupOfFrames*>();
for (int32_t r = 0; r < d->radius * 2; r++)
Expand Down Expand Up @@ -246,7 +245,7 @@ static const VSFrameRef* VS_CC mvdegrainGetFrame(int32_t n, int32_t activationRe
double WSrc;
auto WRefs = d->CreateArray<double>();
for (int32_t r = 0; r < d->radius * 2; r++)
useBlock(pointers[r], strides[r], WRefs[r], isUsable[r], balls[r][0], i, pPlanes[plane][r], pSrcCur, xx, nSrcPitches, nLogPel, plane, xSubUV, ySubUV, thSAD);
useBlock(pointers[r], strides[r], WRefs[r], isUsable[r], balls[r][0], i, pPlanes[plane][r], pSrcCur, xx, nSrcPitches, nLogPel, plane, xSubUV, ySubUV, d->thSAD[r].data());
normalizeWeights(d->radius, WSrc, WRefs.data());
d->DEGRAIN[plane](d->radius, pDstCur[plane] + xx, nDstPitches[plane], pSrcCur[plane] + xx, nSrcPitches[plane],
pointers.data(), strides.data(),
Expand Down Expand Up @@ -280,7 +279,7 @@ static const VSFrameRef* VS_CC mvdegrainGetFrame(int32_t n, int32_t activationRe
double WSrc;
auto WRefs = d->CreateArray<double>();
for (int32_t r = 0; r < d->radius * 2; r++)
useBlock(pointers[r], strides[r], WRefs[r], isUsable[r], balls[r][0], i, pPlanes[plane][r], pSrcCur, xx, nSrcPitches, nLogPel, plane, xSubUV, ySubUV, thSAD);
useBlock(pointers[r], strides[r], WRefs[r], isUsable[r], balls[r][0], i, pPlanes[plane][r], pSrcCur, xx, nSrcPitches, nLogPel, plane, xSubUV, ySubUV, d->thSAD[r].data());
normalizeWeights(d->radius, WSrc, WRefs.data());
d->DEGRAIN[plane](d->radius, tmpBlock, tmpBlockPitch, pSrcCur[plane] + xx, nSrcPitches[plane],
pointers.data(), strides.data(),
Expand Down Expand Up @@ -414,29 +413,32 @@ static void VS_CC mvdegrainCreate(const VSMap* in, VSMap* out, void* userData, V
auto args = ArgumentList{ in };
auto Core = VaporCore{ core };
auto filter = "Degrain"s;

MVDegrainData d;
MVDegrainData* data;
auto mvmulti = Clip{};
d.node = args["clip"];
mvmulti = args["mvmulti"];
auto radius = mvmulti.FrameCount / d.node.FrameCount / 2;
auto thsad = std::array<double, 3>{};
auto thsad2 = thsad;

d.radius = radius;
d.vectors.resize(2 * radius);
d.mvClips.resize(2 * radius);
d.thSAD.resize(2 * radius);

int err;
const int m = vsapi->propNumElements(in, "thsad");
const int n = vsapi->propNumElements(in, "limit");
for (int i = 0; i < 3; ++i) {
if (m == -1)
d.thSAD[0] = d.thSAD[1] = d.thSAD[2] = 400.;
thsad[0] = thsad[1] = thsad[2] = 400.;
else
if (i < m)
d.thSAD[i] = vsapi->propGetFloat(in, "thsad", i, nullptr);
thsad[i] = vsapi->propGetFloat(in, "thsad", i, nullptr);
else
d.thSAD[i] = d.thSAD[i - 1];
thsad[i] = thsad[i - 1];
if (n == -1)
d.nLimit[0] = d.nLimit[1] = d.nLimit[2] = 1.;
else
Expand All @@ -450,6 +452,19 @@ static void VS_CC mvdegrainCreate(const VSMap* in, VSMap* out, void* userData, V
else
d.nLimit[i] = d.nLimit[i - 1];
}

if (auto sad2Count = vsapi->propNumElements(in, "thsad2"); sad2Count == -1) {
thsad2[0] = thsad[0];
thsad2[1] = thsad[1];
thsad2[2] = thsad[2];
}
else
for (auto c : Range{ 3 })
if (c < sad2Count)
thsad2[c] = vsapi->propGetFloat(in, "thsad2", c, nullptr);
else
thsad2[c] = thsad2[c - 1];

int32_t plane = int64ToIntS(vsapi->propGetInt(in, "plane", 0, &err));
if (err)
plane = 4;
Expand Down Expand Up @@ -499,6 +514,8 @@ static void VS_CC mvdegrainCreate(const VSMap* in, VSMap* out, void* userData, V
for (auto r : Range{ radius }) {
d.vectors[2 * r] = bvn(r + 1);
d.vectors[2 * r + 1] = fvn(r + 1);
for (auto c : Range{ 3 })
d.thSAD[2 * r + 1][c] = d.thSAD[2 * r][c] = CosineAnnealing(thsad[c], thsad2[c], r + 1, radius);
}

for (int32_t r = 0; r < radius * 2; r++) {
Expand Down Expand Up @@ -527,8 +544,10 @@ static void VS_CC mvdegrainCreate(const VSMap* in, VSMap* out, void* userData, V
return;
}

d.thSAD[0] = d.thSAD[0] * d.mvClips[0].GetThSCD1() / d.nSCD1;
d.thSAD[1] = d.thSAD[2] = d.thSAD[1] * d.mvClips[0].GetThSCD1() / d.nSCD1;
for (auto& SADArray : d.thSAD) {
SADArray[0] = SADArray[0] * d.mvClips[0].GetThSCD1() / d.nSCD1;
SADArray[1] = SADArray[2] = SADArray[1] * d.mvClips[0].GetThSCD1() / d.nSCD1;
}

auto& supervi = d.super.ExposeVideoInfo();
int32_t nSuperWidth = supervi.width;
Expand Down Expand Up @@ -583,6 +602,7 @@ void mvdegrainRegister(VSRegisterFunction registerFunc, VSPlugin* plugin) {
"super:clip;"
"mvmulti:clip;"
"thsad:float[]:opt;"
"thsad2:float[]:opt;"
"plane:int:opt;"
"limit:float[]:opt;"
"thscd1:float:opt;"
Expand Down
6 changes: 6 additions & 0 deletions src/Overlap.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,9 @@ void ToPixels(uint8_t *pDst8, int32_t nDstPitch, const uint8_t *pSrc8, int32_t n
pSrc8 += nSrcPitch;
}
}

auto CosineAnnealing(auto StartPoint, auto EndPoint, auto Position, auto Radius) {
auto x = (Position - 1) * MV_PI / (Radius - 1);
auto Ratio = (1. - cos(x)) * 0.5;
return StartPoint + Ratio * (EndPoint - StartPoint);
}

0 comments on commit e9a8f10

Please sign in to comment.