## HybridFuPP 0.92b by FuPP

Function HybridFuPP(clip input, int "width", int "height", bool "Fast", bool "Dering", bool "Deblock", int "MP_Mode", 
      \             int "EM_Mode", int "DB_Q", int "DB_Off_a" , int "DB_Off_b", int "M_Thr", int "M_SCD", int "E_Thr",
      \             int "D_Thr", int "B_Thr", int "Resizer", int "S_Str", int "T_Str", int "S_Radius", float "S_Dist",
      \             int "C_Str", int "M_Str", int "E_Str_X", int "E_Str_Y", int"E_Str_B", int "LP_Str", int "DR_Str", 
      \             int "DR_Radius", string "show", string "N1", string "N2", string "M", string "E1", string "E2", 
      \             string "LP", string "DR", string "Preset")
{
width      = Default(width , width(input))
height     = Default(height, height(input))
Preset     = Default(Preset, "high")
Show       = Default(Show  , "")

preset_num = -1

preset_num = Preset == "medium"    ? 0 : preset_num
preset_num = Preset == "high"      ? 1 : preset_num
preset_num = Preset == "very high" ? 2 : preset_num
preset_num = Preset == "low"       ? 3 : preset_num
preset_num = Preset == "very low"  ? 4 : preset_num
preset_num = Preset == "anime1"    ? 5 : preset_num
preset_num = Preset == "anime2"    ? 6 : preset_num
preset_num = Preset == "HybridQ"   ? 7 : preset_num

Assert(preset_num >=0 ? true : false, chr(10) + "This preset does not exist !" + chr(10))

_Resizer    = Select(preset_num, 3    , 4    , 5    , 2    , 0    , 1    , 1    , 0    )
_Fast       = Select(preset_num, true , true , true , true , true , true , true , true )
_S_Str      = Select(preset_num, 10   , 5    , 7    , 12   , 12   , 20   , 20   , 5    )
_S_Radius   = Select(preset_num, 1    , 1    , 1    , 1    , 1    , 1    , 2    , 1    ) 
_S_Dist     = Select(preset_num, 0.1  , 0.1  , 0.1  , 0.1  , 0.1  , 0.1  , 0.1  , 0.1  )
_T_Str      = Select(preset_num, 3    , 3    , 2    , 5    , 5    , 15   , 15   , 3    )
_C_Str      = Select(preset_num, 20   , 20   , 20   , 20   , 20   , 20   , 20   , 20   )
_MP_Mode    = Select(preset_num, 2    , 2    , 2    , 2    , 2    , 0    , 0    , 2    )
_M_Thr      = Select(preset_num, 10   , 10   , 10   , 10   , 10   , 10   , 10   , 10   )
_M_SCD      = Select(preset_num, 260  , 260  , 260  , 260  , 260  , 260  , 260  , 260  ) 
_M_Str      = Select(preset_num, 120  , 120  , 80   , 120  , 120  , 0    , 0    , 120  ) 
_EM_Mode    = Select(preset_num, 1    , 1    , 1    , 1    , 1    , 2    , 2    , 1    )
_E_Thr      = Select(preset_num, 14   , 12   , 12   , 14   , 14   , 7    , 7    , 10   )
_E_Str_X    = Select(preset_num, 0    , 0    , 0    , 0    , 0    , 30   , 40   , 30   )
_E_Str_Y    = Select(preset_num, 0    , 0    , 0    , 0    , 0    , 30   , 40   , 30   )
_E_Str_B    = Select(preset_num, 0    , 0    , 0    , 0    , 0    , -3   , -3   , 0    )
_Dering     = Select(preset_num, false, false, false, false, false, false, false, false)
_DR_Radius  = Select(preset_num, 2    , 2    , 2    , 2    , 2    , 4    , 4    , 2    )
_DR_Str     = Select(preset_num, 30   , 30   , 30   , 30   , 30   , 30   , 30   , 30   )
_D_Thr      = Select(preset_num, 35   , 32   , 31   , 37   , 39   , 30   , 30   , 32   )
_B_Thr      = Select(preset_num, 0    , 0    , 0    , 0    , 0    , 0    , 0    , 0    )
_LP_Str     = Select(preset_num, 30   , 30   , 30   , 30   , 30   , 40   , 40   , 30   )
_Deblock    = Select(preset_num, false, false, false, false, false, false, false, false)
_DB_Q       = Select(preset_num, 25   , 25   , 25   , 25   , 25   , 25   , 25   , 25   )
_DB_Off_a   = Select(preset_num, 0    , 0    , 0    , 0    , 0    , 0    , 0    , 0    )
_DB_Off_b   = Select(preset_num, 0    , 0    , 0    , 0    , 0    , 0    , 0    , 0    )

Resizer    = Default(Resizer, _Resizer)
Fast       = Default(Fast, _Fast)

S_Str      = Default(S_Str, _S_Str)
S_Radius   = Default(S_Radius, _S_Radius)
S_Dist     = Default(S_Dist, _S_Dist)
T_Str      = Default(T_Str, _T_Str)
C_Str      = Default(C_Str, _C_Str)

MP_Mode    = Default(MP_Mode, _MP_Mode)
M_Thr      = Default(M_Thr, _M_Thr)
M_SCD      = Default(M_SCD, _M_SCD)
M_Str      = Default(M_Str, _M_Str)

D_Thr      = Default(D_Thr, _D_Thr)
B_Thr      = Default(B_Thr, _B_Thr)
LP_Str     = Default(LP_Str, _LP_Str)

Deblock    = Default(Deblock, _Deblock)
DB_Q       = Default(DB_Q, _DB_Q)
DB_Off_a   = Default(DB_Off_a, _DB_Off_a)
DB_Off_b   = Default(DB_Off_b, _DB_Off_b)

EM_Mode    = Default(EM_Mode, _EM_Mode)
E_Thr      = Default(E_Thr, _E_Thr)
E_Str_X    = Default(E_Str_X, _E_Str_X)
E_Str_Y    = Default(E_Str_Y, _E_Str_Y)
E_Str_B    = Default(E_Str_B, _E_Str_B)

Dering     = Default(Dering, _Dering)
DR_Str     = Default(DR_Str, _DR_Str)
DR_Radius  = Default(DR_Radius, _DR_Radius)

mod_preset = False

current  =  string(fast)       +
      \     string(dering)     +
      \     string(MP_Mode)    +
      \     string(EM_Mode)    +
      \     string(M_Thr)      +
      \     string(M_Scd)      +
      \     string(D_Thr)      +
      \     string(B_Thr)      +
      \     string(E_Thr)      +
      \     string(Resizer)    +
      \     string(S_Str)      +
      \     string(T_Str)      +
      \     string(S_Radius)   +
      \     string(S_Dist)     +
      \     string(M_Str)      +
      \     string(LP_Str)     +
      \     string(DR_Str)     +
      \     string(DR_Radius)  +
      \     string(E_Str_X)    +
      \     string(E_Str_Y)    +
      \     string(E_Str_B)    +
      \     string(C_Str)      +
      \     string(Deblock)    +
      \     string(DB_Q)       +
      \     string(DB_Off_a)   +
      \     string(DB_Off_b)   

_defaults = string(_fast)      +
      \     string(_dering)    +
      \     string(_EM_Mode)   +
      \     string(_MP_Mode)   +
      \     string(_M_Thr)     +
      \     string(_M_Scd)     +
      \     string(_D_Thr)     +
      \     string(_B_Thr)     +
      \     string(_E_Thr)     +
      \     string(_Resizer)   +
      \     string(_S_Str)     +
      \     string(_T_Str)     +
      \     string(_S_Radius)  +
      \     string(_S_Dist)    +
      \     string(_M_Str)     +
      \     string(_LP_Str)    +
      \     string(_DR_Str)    +
      \     string(_DR_Radius) +
      \     string(_E_Str_X)   +
      \     string(_E_Str_Y)   +
      \     string(_E_Str_B)   +
      \     string(_C_Str)     +
      \     string(_Deblock)   +
      \     string(_DB_Q)      +
      \     string(_DB_Off_a)  +
      \     string(_DB_Off_b)  

mod_preset = current != _defaults ? True : mod_preset

Def_N1     = ""
Def_N2     = """Deen("a3d", S_Radius, S_Str, 3*C_Str, T_Str, C_Str, S_Dist, 9)"""
Def_E1     =   "UnFilter(E_Str_X, E_Str_Y)"
Def_E2     =   "ColorYUV(off_Y = E_Str_B)"
Def_M      = """Deen("a2d", 1, M_Str   , 0, 0, 0, 0, 0)"""
Def_LP     = """Deen("a2d", 2, LP_Str  , 0, 0, 0, 0, 0)"""
Def_DR     = """Deen("a2d", 2, DR_Str  , 0, 0, 0, 0, 0)"""

N1         = Default(N1, Def_N1)
N2         = Default(N2, Def_N2)
E1         = Default(E1, Def_E1)
E2         = Default(E2, Def_E2)
M          = Default(M , Def_M )
LP         = Default(LP, Def_LP)
DR         = Default(DR, Def_DR)

D_process  = (LP == "" || LP_Str == 0 || D_Thr == 0) ? false : true
B_process  = (LP == "" || LP_Str == 0 || B_Thr == 0) ? false : true
E_process  = (E1 == "" && E2 == "") || (E_Str_X == 0 && E_Str_Y == 0 && E_Str_B == 0) ? false : true
M_process  = (M  == "" || M_Str == 0 || M_Thr == 0) ? false : true

Assert(isyv12(input) == true, chr(10) + "This is not an YV12 clip ! Please convert color space to YV12 before using"
      \                               + " HybridFuPP" + chr(10))

Assert((Width(input)%4 != 0 || Height(input)%4 != 0)  == false, chr(10) + "Width and height (source) must be dividable by 4"
      \                                                                 + " if you want to use HybridFuPP ! Please Crop if"
      \                                                                 + " needed !" + chr(10))

Bl_Size = 0
Bl_Size = (Width%8  == 0 && Height%8  == 0) ? 8  : Bl_Size
Bl_Size = (Width%16 == 0 && Height%16 == 0) ? 16 : Bl_Size
Bl_Size = M_Process ? Bl_Size : 1

Assert(Bl_Size != 0, chr(10) + "Width and height (destination) must be dividable by 8 if you want to use motion processing"
      \                      + chr(10))

_input     = Deblock ? input.Deblock(DB_Q, DB_Off_A, DB_Off_B) : input
Static     = Eval("_input." + (N1 !="" ? N1 + "." : N1) + "Resizer(width, height, Resizer)" + (N2 !="" ? "." + N2 : N2))
clip       = Fast ? Static : _input.Resizer(width, height, Resizer)

D          = (show != "" || D_process) ? DM(clip, D_Thr) : NOP()
B          = (show != "" || B_process) ? BM(clip, B_Thr) : NOP()
LM         = NOP()
LM         = (D_process && B_process == false) ? D : LM
LM         = (B_process && D_process == false) ? L : LM
LM         = (B_process && D_process) ? Logic(D, B, "OR") : LM
EM         = (show != "" || E_process || Dering) ? EM(_input, EM_Mode, E_thr, width, height, Resizer) : NOP()
MM         = (show != "" || (M_process && MP_Mode == 2)) ? MM(clip, M_Thr, M_SCD, Bl_Size) : NOP()
RM         = (show != "" || Dering) ? RM(EM, DR_Radius) : NOP()

Luma       = (B_process || D_process)    ? Eval("Clip." + LP) : NOP()
Edges      = E_process                   ? Eval("Clip." + (E1 !="" ? E1 + "." : E1) + "Resizer(width, height, Resizer)" 
      \                                                 + (E2 !="" ? "." + E2 : E2)) : NOP()
DeRinging  = Dering                      ? Eval("Clip." + DR) : NOP()
Motion     = (M_process && MP_Mode == 2) ? Eval("Clip." + M ) : NOP()

Final      = (B_process || D_process)    ? MaskedMerge(Static , Luma,      LM)    : Static
Final      = Dering                      ? MaskedMerge(Final  , DeRinging, RM)    : Final
Final      = E_process                   ? MaskedMerge(Final  , Edges,     EM)    : Final
Final      = (M_process && MP_Mode == 2) ? MaskedMerge(Final  , Motion,    MM)    : 
      \      (M_process && MP_Mode == 1) ? MDenoise(Final, M_Thr, M_SCD, Bl_Size) : Final

return (show == "") ? Final : Show(Final, EM, MM, RM, D, B, show, width, height, Fast, Dering, Deblock, MP_Mode, EM_Mode,
      \                            DB_Q, DB_Off_a, DB_Off_b, M_Thr, M_SCD, E_Thr, D_Thr, B_Thr, Resizer, S_Str, T_Str,
      \                            S_Radius, S_Dist, M_Str, E_Str_X, E_Str_Y, E_Str_B, LP_Str, DR_Str, DR_Radius, C_Str,
      \                            Preset, mod_preset)
}

Function EM(clip i, int mode, int e_th, int w, int h, int r)
{
e_mask = i.Msharpen(threshold=e_th,mask=true)
e_mask = mode == 2 ? e_mask.deflate().deflate() : e_mask

return e_mask.Resizer(w, h, r)
}


Function RM(clip i, dr_rad)
{
return logic(i,i.DR_Radius(DR_rad,1),"xor").binarize(upper=false)
}


Function DR_Radius(clip i, int dr_rad, int count)
{
return count > dr_rad ? i : DR_Radius(i.expand(), dr_rad, count+1)
}
 

Function MM(clip i, int m_th, int scd, int bl_sz)
{
return Binarize(i.MVMask(MVAnalyse(i, isb=false, lambda=1000, blksize=bl_sz), ml=m_th, Gamma=50, ThSCD1=scd, thSCD2=135), 
      \         Upper=False, Threshold=30)
}


Function DM(clip i, int d_th)
{
return Binarize(i, threshold = d_th).deflate()
}


Function BM(clip i, int b_th)
{
return Binarize(i, threshold = b_th, upper = false).inpand()
}

Function MDenoise(clip i, int m_th, int scd, int bl_sz)
{
b1 = i.MVAnalyse(isb = true,  blksize = bl_sz, lambda = 1000, delta = 1)
f1 = i.MVAnalyse(isb = false, blksize = bl_sz, lambda = 1000, delta = 1)

return i.MVDenoise(b1, f1, tht = m_th, thsad = scd)

}

Function Resizer(clip i, int w, int h, int t)
{
return  (t == 0) ? BilinearResize(i,w, h) : 
      \ (t == 1) ? BicubicResize(i, w, h, 0.333, 0.333) : 
      \ (t == 2) ? BicubicResize(i, w, h, 0.2  , 0.4  ) : 
      \ (t == 3) ? BicubicResize(i, w, h, 0    , 0.5  ) : 
      \ (t == 4) ? BicubicResize(i, w, h, 0    , 0.6  ) : 
      \ (t == 5) ? LanczosResize(i, w, h) : Nop()
}

Function Show(clip i, clip em, clip mm, clip rm, clip dm, clip bm, string sh, int w, int h, bool Fast, bool Dering, 
      \        bool deblock, int MP_Mode, int EM_Mode, int DB_Q, int DB_Off_a, int DB_Off_b,int M_Thr, int M_SCD, int E_Thr,
      \        int D_Thr, int B_Thr, int Resizer, int S_Str, int T_Str, int S_Radius, float S_Dist, int M_Str, int E_Str_X,
      \        int E_Str_Y, int E_Str_B, int LP_Str, int DR_Str, int DR_Radius, int C_Str, string Preset, bool mod_preset)
{
MM_Blank = BlankClip(1, w, h, color=$FFFF0F, pixel_type="yv12")
DM_Blank = BlankClip(1, w, h, color=$FFF0,   pixel_type="yv12")
BM_Blank = BlankClip(1, w, h, color=$FF0A,   pixel_type="yv12")
EM_Blank = BlankClip(1, w, h, color=$FF0808, pixel_type="yv12")
RM_Blank = BlankClip(1, w, h, color=$FFBF0A, pixel_type="yv12")

EShow = FindStr(UCase(sh),"E")
MShow = FindStr(UCase(sh),"M")
RShow = FindStr(UCase(sh),"R")
DShow = FindStr(UCase(sh),"D")
BShow = FindStr(UCase(sh),"B")
PShow = FindStr(UCase(sh),"P")

mod = mod_preset ? "(mod)" : ""

i = EShow != 0 ? Overlay(i, EM_Blank, Mask=em, mode="chroma", opacity=0.6) : i
i = MShow != 0 ? Overlay(i, MM_Blank, Mask=mm, mode="blend" , opacity=0.3) : i
i = RShow != 0 ? Overlay(i, RM_Blank, Mask=rm, mode="blend" , opacity=0.7) : i
i = DShow != 0 ? Overlay(i, DM_Blank, Mask=Logic(dm, dm.Inpand(), "Xor"), Mode="blend", Opacity=1) : i
i = BShow != 0 ? Overlay(i, BM_Blank, Mask=Logic(bm, bm.Inpand(), "Xor"), Mode="blend", Opacity=1) : i

i = EShow != 0 ? i.SubTitle("Edges" ,       text_color=$FF0808, size=14, x=1, y=h-52) : i
i = MShow != 0 ? i.SubTitle("Motion",       text_color=$FFFF0F, size=14, x=1, y=h-40) : i
i = RShow != 0 ? i.SubTitle("DeRinging",    text_color=$FFBF0A, size=14, x=1, y=h-28) : i
i = DShow != 0 ? i.SubTitle("Dark areas",   text_color=$FFF0,   size=14, x=1, y=h-16) : i
i = BShow != 0 ? i.SubTitle("Bright areas", text_color=$FF0A,   size=14, x=1, y=h-4 ) : i

i = PShow != 0 ? i.SubTitle("General Parameters" ,             text_color=$FFFFFF,size=14,x=1,y=12  ) : i
i = PShow != 0 ? i.SubTitle("Preset   = " + string(Preset)+mod,text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=24   ) : i
i = PShow != 0 ? i.SubTitle("Resizer  = " + string(Resizer),   text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=36   ) : i
i = PShow != 0 ? i.SubTitle("Fast     = " + string(fast),      text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=48   ) : i

i = PShow != 0 ? i.SubTitle("Denoising N2 (no motion)" ,       text_color=$FFFFFF,size=14,x=1,y=64  ) : i
i = PShow != 0 ? i.SubTitle("S_Str    = " + string(S_Str),     text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=76   ) : i
i = PShow != 0 ? i.SubTitle("T_Str    = " + string(T_Str),     text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=88   ) : i
i = PShow != 0 ? i.SubTitle("S_Radius = " + string(S_Radius),  text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=100  ) : i
i = PShow != 0 ? i.SubTitle("S_Dist   = " + string(S_Dist),    text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=112  ) : i
i = PShow != 0 ? i.SubTitle("C_Str    = " + string(C_Str),     text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=124  ) : i

i = PShow != 0 ? i.SubTitle("Denoising M (motion)" ,           text_color=$FFFFFF,size=14,x=1,y=140 ) : i
i = PShow != 0 ? i.SubTitle("MP_Mode  = " + string(MP_Mode),   text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=152  ) : i
i = PShow != 0 ? i.SubTitle("M_Thr    = " + string(M_Thr),     text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=164  ) : i
i = PShow != 0 ? i.SubTitle("M_SCD    = " + string(M_SCD),     text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=176  ) : i
i = PShow != 0 ? i.SubTitle("M_Str    = " + string(M_Str),     text_color=$FFFFFF,font="COURIER NEW",size=14,x=1,y=188  ) : i

i = PShow != 0 ? i.SubTitle("Luma processing LP" ,             text_color=$FFFFFF,size=14,x=172,y=12) : i
i = PShow != 0 ? i.SubTitle("D_Thr     = " + string(D_Thr),    text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=24 ) : i
i = PShow != 0 ? i.SubTitle("B_Thr     = " + string(B_Thr),    text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=36 ) : i
i = PShow != 0 ? i.SubTitle("LP_Str    = " + string(LP_Str),   text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=48 ) : i

i = PShow != 0 ? i.SubTitle("Deblocking" ,                     text_color=$FFFFFF,size=14,x=172,y=64) : i
i = PShow != 0 ? i.SubTitle("Deblock   = " + string(Deblock),  text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=76 ) : i
i = PShow != 0 ? i.SubTitle("DB_Q      = " + string(DB_Q),     text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=88 ) : i
i = PShow != 0 ? i.SubTitle("DB_Off_a  = " + string(DB_Off_a), text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=100) : i
i = PShow != 0 ? i.SubTitle("DB_Off_b  = " + string(DB_Off_b), text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=112) : i

i = PShow != 0 ? i.SubTitle("Edges E1+E2 / Dering DR" ,        text_color=$FFFFFF,size=14,x=172,y=128) : i
i = PShow != 0 ? i.SubTitle("EM_Mode   = " + string(EM_Mode),  text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=140) : i
i = PShow != 0 ? i.SubTitle("E_Thr     = " + string(E_Thr),    text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=152) : i
i = PShow != 0 ? i.SubTitle("E_Str_X   = " + string(E_Str_X),  text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=164) : i
i = PShow != 0 ? i.SubTitle("E_Str_Y   = " + string(E_Str_Y),  text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=176) : i
i = PShow != 0 ? i.SubTitle("E_Str_B   = " + string(E_Str_B),  text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=188) : i
i = PShow != 0 ? i.SubTitle("Dering    = " + string(Dering),   text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=204) : i
i = PShow != 0 ? i.SubTitle("DR_Str    = " + string(DR_Str),   text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=216) : i
i = PShow != 0 ? i.SubTitle("DR_Radius = " + string(DR_Radius),text_color=$FFFFFF,font="COURIER NEW",size=14,x=172,y=228) : i

return i
}