Skip to content

Commit

Permalink
StripeMask: added fullRange parameter, made gamma linear conversion m…
Browse files Browse the repository at this point in the history
…ore accurate, and fixed a bug
  • Loading branch information
mysteryx93 committed Jun 21, 2021
1 parent fc398e2 commit f0db5de
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 62 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 2.0 (2021-06-20)
- Ported to VapourSynth
- Fixed a bug in StripeMask
- StripeMask linear gamma correction is now more accurate
- StripeMask now accounts for PC vs TV range
- StripeMask: added fullRange parameter, set to True if input is in full range

Version 1.3 (2019-06-01)
- Greatly enhanced quality in artifact areas! Ghost effect eliminated.
- Added ConvertFpsLimit to blend with reduced blending ratios. Ratio=0 is frame copy and Ratio=100 is full blending. Ratio=50 reduces blending by half.
Expand Down
2 changes: 1 addition & 1 deletion Src/Avisynth/InitAvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit3(IScri
{
AVS_linkage = vectors;
env->AddFunction("ConditionalFilterMT", "cccsss[show]b", ConditionalFilter::Create, 0);
env->AddFunction("StripeMask", "c[blksize]i[blksizev]i[overlap]i[overlapv]i[thr]i[Comp]i[CompV]i[str]i[strf]i[lines]b", StripeMaskAvs::Create, 0);
env->AddFunction("StripeMask", "c[blkSize]i[blkSizeV]i[overlap]i[overlapV]i[thr]i[comp]i[compV]i[str]i[strf]i[fullRange]b[lines]b", StripeMaskAvs::Create, 0);
env->AddFunction("ContinuousMask", "c[radius]i", ContinuousMaskAvs::Create, 0);
env->AddFunction("ConvertFpsLimit", "ci[]i[ratio]i", ConvertFpsLimitAvs::Create, 0);
env->AddFunction("ConvertFpsLimit", "cf[ratio]i", ConvertFpsLimitAvs::CreateFloat, 0);
Expand Down
39 changes: 32 additions & 7 deletions Src/Avisynth/StripeMaskAvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,46 @@ AVSValue __cdecl StripeMaskAvs::Create(AVSValue args, void* user_data, IScriptEn
int BlkSizeV = args[2].AsInt(BlkSize > 0 ? BlkSize : 16);
int Overlap = args[3].AsInt(BlkSize / 4);
int OverlapV = args[4].AsInt(BlkSizeV / 4);
int Thr = args[5].AsInt(26);
int Thr = args[5].AsInt(28);
int Comp = args[6].AsInt(BlkSize <= 16 ? 2 : 3);
int CompV = args[7].AsInt(Comp);
int Str = args[8].AsInt(255);
int Strf = args[9].AsInt(0);
bool Lines = args[10].AsBool(false);
int FullRange = args[10].AsBool(false);
bool Lines = args[11].AsBool(false);

int SrcBit = input->GetVideoInfo().BitsPerComponent();

// Convert to 16-bit.
{
AVSValue sargs[2] = { input, 16 };
const char* nargs[2] = { 0, 0 };
input = env->Invoke("ConvertBits", AVSValue(sargs, 2), nargs).AsClip();
}

// Convert to Y
{
AVSValue sargs[1] = { input };
const char* nargs[1] = { 0 };
input = env->Invoke("ConvertToY", AVSValue(sargs, 1), nargs).AsClip();
}

// Convert to Full levels.
if (!FullRange)
{
AVSValue sargs[2] = { input, "TV->PC" };
const char* nargs[2] = { 0, "levels" };
input = env->Invoke("ColorYUV", AVSValue(sargs, 2), nargs).AsClip();
}

// Convert input to linear (gamma 2.2)
AVSValue sargs[2] = { input, ((1.0 / 2.2) - 1.0) * 256.0 };
const char* nargs[2] = { 0, "gamma_y" };
input = env->Invoke("ColorYUV", AVSValue(sargs, 2), nargs).AsClip();
{
AVSValue sargs[6] = { input, 0, 1.0/2.2, 65535, 0, 65535 };
const char* nargs[6] = { 0, 0, 0, 0, 0, 0 };
input = env->Invoke("Levels", AVSValue(sargs, 6), nargs).AsClip();
}

// Convert input to 8-bit; nothing to gain in processing at higher bit-depth.
int SrcBit = input->GetVideoInfo().BitsPerComponent();
if (SrcBit > 8)
{
AVSValue sargs[2] = { input, 8 };
const char* nargs[2] = { 0, 0 };
Expand Down
73 changes: 35 additions & 38 deletions Src/Common/StripeMaskBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void StripeMaskBase::CalcBandAvg(const BYTE* src, int pitch, int size, BYTE* lin
Sum = 0;
for (int j = 0; j < blk; j++)
{
Sum += src[pitch*j];
Sum += src[pitch * j];
}
lineAvg[i] = Sum / blk;
src++;
Expand Down Expand Up @@ -182,49 +182,46 @@ void StripeMaskBase::CalcBand(BYTE* dst, int dstPitch, int size, BYTE* lineAvg,
history[hLength++] = PatternStep(i, i - AltPos);
AltPos = i;

if (!Alt)
// Look through history to find patterns.
if (!Alt && PatternLength == 0)
{
// Look through history to find patterns.
if (PatternLength == 0)
// 2 last alt match 2 previous ones, or 4 last alt match 4 previous ones.
if (hLength >= 8)
{
// 2 last alt match 2 previous ones, or 4 last alt match 4 previous ones.
if (hLength >= 8)
{
PatternLength = CompareHistory(history, hLength, 4, blk) ? 4 : 0;
}
if (PatternLength == 0 && hLength >= 4)
{
PatternLength = CompareHistory(history, hLength, 2, blk) ? 2 : 0;
}
if (PatternLength > 0)
{
PatternStart = history[max(0, hLength - PatternLength * 2 - 2)].Pos;
}
PatternLength = CompareHistory(history, hLength, 4, blk) ? 4 : 0;
}
else if (PatternLength > 0)
if (PatternLength == 0 && hLength >= 4)
{
// Once pattern is detected, detect when pattern breaks.
if (CompareHistory(history, hLength, PatternLength, blk))
{
PatternCont += 2;
}
else
{
PatternEnd = history[hLength - 3].Pos;
PatternLength = 0;
PatternCont = 0;
if (hLength > 6) // if hLength=6, 0-1 is blank, 2-3 is 'pattern start' and 4-5 is pattern break.
{
MarkArea(dst, dstPitch, PatternStart, PatternEnd, strength, blk, vertical);
}
}
PatternLength = CompareHistory(history, hLength, 2, blk) ? 2 : 0;
}
// If 2 full blocks are continuous white, mark it.
if (history[hLength - 1].Val > blk * 2)
if (PatternLength > 0)
{
MarkArea(dst, dstPitch, history[hLength - 2].Pos, i, strength, blk, vertical);
PatternStart = history[max(0, hLength - PatternLength * 2 - 2)].Pos;
}
}
else if (PatternLength > 0)
{
// Once pattern is detected, detect when pattern breaks.
if (CompareHistory(history, hLength, PatternLength, blk))
{
PatternCont += 2;
}
else
{
PatternEnd = history[hLength - 3].Pos;
PatternLength = 0;
PatternCont = 0;
if (hLength > 6) // if hLength=6, 0-1 is blank, 2-3 is 'pattern start' and 4-5 is pattern break.
{
MarkArea(dst, dstPitch, PatternStart, PatternEnd, strength, blk, vertical);
}
}
}
// If 2 full blocks are continuous white, mark it.
if (!Alt && history[hLength - 1].Length > blk * 2)
{
MarkArea(dst, dstPitch, history[hLength - 2].Pos, i, strength, blk, vertical);
}
}
}
}
Expand Down Expand Up @@ -284,11 +281,11 @@ bool StripeMaskBase::CompareHistory(PatternStep* history, int hLength, int lengt
for (int i = 0; i < length; i++)
{
// Maximum pattern step is blksize*2
if (Item1->Val > blk * 2 || Item2->Val > blk * 2)
if (Item1->Length > blk * 2 || Item2->Length > blk * 2)
{
return false;
}
else if (abs(Item1->Val - Item2->Val) > (Item2->Val <= 4 ? 1 : Item2->Val <= 8 ? 2 : Item2->Val <= 16 ? 3 : 4))
else if (abs(Item1->Length - Item2->Length) > (Item2->Length <= 4 ? 1 : Item2->Length <= 8 ? 2 : Item2->Length <= 16 ? 3 : 4))
{
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions Src/Common/StripeMaskBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

struct PatternStep {
PatternStep() {};
PatternStep(int _pos, int _val) : Pos(_pos), Val(_val) {};
PatternStep(int _pos, int _val) : Pos(_pos), Length(_val) {};
int Pos = 0;
int Val = 0;
int Length = 0;
};

enum MaskMode {
Expand Down
12 changes: 1 addition & 11 deletions Src/FrameRateConverter.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31321.278
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FrcAvisynth", "FrcAvisynth.vcxproj", "{65E5B18E-B47F-43CB-B398-02E6E82E632E}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FrcVapourSynth", "FrcVapourSynth.vcxproj", "{3826CF45-A338-4BF5-A1F5-CFC79C1B1F60}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FrameRateConverter", "FrameRateConverter.vcxproj", "{65E5B18E-B47F-43CB-B398-02E6E82E632E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -23,14 +21,6 @@ Global
{65E5B18E-B47F-43CB-B398-02E6E82E632E}.Release|x64.Build.0 = Release|x64
{65E5B18E-B47F-43CB-B398-02E6E82E632E}.Release|x86.ActiveCfg = Release|Win32
{65E5B18E-B47F-43CB-B398-02E6E82E632E}.Release|x86.Build.0 = Release|Win32
{3826CF45-A338-4BF5-A1F5-CFC79C1B1F60}.Debug|x64.ActiveCfg = Debug|x64
{3826CF45-A338-4BF5-A1F5-CFC79C1B1F60}.Debug|x64.Build.0 = Debug|x64
{3826CF45-A338-4BF5-A1F5-CFC79C1B1F60}.Debug|x86.ActiveCfg = Debug|Win32
{3826CF45-A338-4BF5-A1F5-CFC79C1B1F60}.Debug|x86.Build.0 = Debug|Win32
{3826CF45-A338-4BF5-A1F5-CFC79C1B1F60}.Release|x64.ActiveCfg = Release|x64
{3826CF45-A338-4BF5-A1F5-CFC79C1B1F60}.Release|x64.Build.0 = Release|x64
{3826CF45-A338-4BF5-A1F5-CFC79C1B1F60}.Release|x86.ActiveCfg = Release|Win32
{3826CF45-A338-4BF5-A1F5-CFC79C1B1F60}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion Src/FrcAvisynth.vcxproj → Src/FrameRateConverter.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<Keyword>Win32Proj</Keyword>
<RootNamespace>ConditionalFilterMT</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>FrcAvisynth</ProjectName>
<ProjectName>FrameRateConverter</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions Src/VapourSynth/InitVpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ VS_EXTERNAL_API(void) VapourSynthPluginInit(VSConfigPlugin configFunc, VSRegiste
"compV:int:opt;"
"str:int:opt;"
"strf:int:opt;"
"fullRange:int:opt;"
"lines:int:opt;",
StripeMaskVpy::Create, 0, plugin);
registerFunc("ConvertFpsLimit",
Expand Down
6 changes: 4 additions & 2 deletions Src/VapourSynth/StripeMaskVpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ void VS_CC StripeMaskVpy::Create(const VSMap* in, VSMap* out, void* userData, VS
int BlkSizeV = prop.GetInt("blkSizeV", BlkSize > 0 ? BlkSize : 16);
int Overlap = prop.GetInt("overlap", BlkSize / 4);
int OverlapV = prop.GetInt("overlapV", BlkSizeV / 4);
int Thr = prop.GetInt("thr", 26);
int Thr = prop.GetInt("thr", 28);
int Comp = prop.GetInt("comp", BlkSize <= 16 ? 2 : 3);
int CompV = prop.GetInt("compV", Comp);
int Str = prop.GetInt("str", 255);
int Strf = prop.GetInt("strf", 0);
bool FullRangeDef = prop.GetInt("_ColorRange", 0) == 1;
bool FullRange = prop.GetInt("fullRange", FullRangeDef);
bool Lines = prop.GetInt("lines", false);

auto Input = InClip;
Expand All @@ -32,7 +34,7 @@ void VS_CC StripeMaskVpy::Create(const VSMap* in, VSMap* out, void* userData, VS
api->propSetNode(Args, "clip", Input, paReplace);
api->propSetData(Args, "transs", "709", -1, paReplace);
api->propSetData(Args, "transd", "linear", -1, paReplace);
// api->propSetInt(Args, "fulls", 0, paReplace);
api->propSetInt(Args, "fulls", FullRange ? 1 : 0, paReplace);
Input = Env.InvokeClip("fmtc", "transfer", Args);
if (Input)
{
Expand Down

0 comments on commit f0db5de

Please sign in to comment.