Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function CSmod(clip filtered, clip "source", clip "pclip", int "strength", float "divisor", float "index", float "ss_w", float "ss_h",
- \ bool "chroma", int "preblur", bool "prec", bool "secure", bool "limit", bool "limit2", bool "Slimit", bool "SlimitY", bool "Tlimit",
- \ bool "ssrep", int "preR", int "Smethod", int "Smode", int "Szrp", int "Spwr", int "SdmpLo", int "SdmpHi", int "RGmode", int "RGmodeU",
- \ int "Repmode", int "RepmodeU", int "Sovershoot", int "Sundershoot", int "Tovershoot", int "Tundershoot", int "Soft", int "Soothe",
- \ string "filter_ss", int "blksize", int "overlap", int "thSAD", int "thSCD1", int "thSCD2", bool "truemotion", bool "MVglobal",
- \ int "pel", int "pelsearch", int "search", int "searchparam", int "MVsharp", int "DCT", bool "chrepsh")
- {
- sw = filtered.width()
- sh = filtered.height()
- HD = (sw > 1024 || sh > 576) ? true : false
- chroma = Default(chroma, false )
- ss_w = Default(ss_w, 1.25 )
- ss_h = Default(ss_h, 1.25 )
- ssrep = Default(ssrep, Defined(filter_ss))
- preblur = Default(preblur, 1 )
- prec = Default(prec, preblur>=1)
- preR = Default(preR, preblur>=2?0:ss_w*ss_h<2.25?1:2)
- limit = Default(limit, true )
- limit2 = Default(limit2, limit )
- RGmode = Default(RGmode, 11 )
- RGmodeU = Default(RGmodeU, RGmode )
- Repmode = Default(Repmode, 1 )
- RepmodeU = Default(RepmodeU,Repmode)
- limit = Defined(source) || Defined(filter_ss) ? limit : false
- limit2 = Defined(source) || Defined(filter_ss) ? limit2 : false
- Smethod = Default(Smethod, 3 )
- secure = Default(secure, true )
- Smode = Default(Smode, 3 )
- divisor = Default(divisor, 1.5 )
- index = Default(index, 0.8 )
- strength = Default(strength,100 )
- Szrp = Default(Szrp, 16 )
- Spwr = Default(Spwr, 4 )
- SdmpLo = Default(SdmpLo, 4 )
- SdmpHi = Default(SdmpHi, 48 )
- Slimit = Default(Slimit, true )
- SlimitY = Default(SlimitY, true )
- Sovershoot = Default(Sovershoot, limit ? 0 : strength/100)
- Sundershoot = Default(Sundershoot, Sovershoot )
- chrepsh = Default(chrepsh, false )
- Tlimit = Default(Tlimit, false )
- Tovershoot = Default(Tovershoot, strength/25 )
- Tundershoot = Default(Tundershoot, Tovershoot )
- Soft = Default(Soft, -2 )
- Soothe = Default(Soothe, 24 )
- blksize = Default(blksize, HD ? 16 : 8 )
- overlap = Default(overlap, HD ? 8 : 4 )
- thSAD = Default(thSAD, 300 )
- thSCD1 = Default(thSCD1, 300 )
- thSCD2 = Default(thSCD2, 100 )
- truemotion = Default(truemotion, false )
- MVglobal = Default(MVglobal, true )
- pel = Default(pel, 2 )
- pelsearch = Default(pelsearch, 2 )
- search = Default(search, 2 )
- searchparam = Default(searchparam, 2 )
- MVsharp = Default(MVsharp, 2 )
- DCT = Default(DCT, 0 )
- ss_w = ss_w < 1.0 ? 1.0 : ss_w
- ss_h = ss_h < 1.0 ? 1.0 : ss_h
- wss = Round(sw*ss_w/8)*8
- hss = Round(sh*ss_h/8)*8
- ch21 = chroma ? 2 : 1
- ch31 = chroma ? 3 : 1
- ch32 = chroma ? 3 : 2
- strength = Smode==1 ? strength/50.0 : Smode==2 ? strength/20.0 : strength/100.0
- Slimit = limit ? false : Slimit
- Soft = limit ? 100
- \ : Soft<=-2 ? int( (1.0+(2.0/(ss_w+ss_h))) * sqrt(strength*100) )
- \ : Soft==-1 ? sqrt( (((ss_w+ss_h)/2.0-1.0)*100.0) ) * 10
- \ : Soft<=100 ? Soft : 100
- Soothe = Tlimit ? -1 : Soothe
- preR = preR <= 0 ? 0 : preR >= 3 ? 3 : preR
- Sovershoot = Sovershoot < 0 ? 0 : Sovershoot
- Sundershoot = Sundershoot < 0 ? 0 : Sundershoot
- Tovershoot = Tovershoot < 0 ? 0 : Tovershoot
- Tundershoot = Tundershoot < 0 ? 0 : Tundershoot
- #super sampling with filtering
- source = Defined(source) ? source : filtered
- filtered_os = filtered
- filtered = (ss_w > 1.0 || ss_h > 1.0) ? filtered.Spline64Resize(wss, hss).Repair(filtered.GaussResize(wss, hss, p=100), 1, chroma ? 1 : -1) : filtered
- filtered = (ss_w > 1.0 || ss_h > 1.0)&&Defined(filter_ss) ? Eval("filtered."+filter_ss) : filtered
- filtered_ds = (ss_w > 1.0 || ss_h > 1.0)&&Defined(filter_ss) ? chroma ? filtered.Spline36Resize(sw, sh) : filtered.Spline36Resize(sw, sh).MergeChroma(filtered_os) : filtered_os
- Assert( (isFrameBased(filtered) && isFrameBased(source)), """CSmod: input clip must be Frame Based!""" )
- Assert( (source.width()==sw && source.height()==sh), """CSmod: resolution of "filtered" and "source" must match!""" )
- #pre filter before sharpening
- spatial = preblur>=1 ? filtered.MinBlur(preR, chroma ? (prec ? 3 : 2) : 1) : NOP()
- temporal = preblur>=2 ? spatial .FluxsmoothT(7).Repair(spatial, 1, chroma ? 1 : -1) : NOP()
- mixed = preblur>=2 ? temporal.merge(spatial,0.251) : NOP()
- pre = Defined(pclip) ? pclip
- \ : preblur==1 ? spatial
- \ : preblur>=2 ? mixed
- \ : filtered
- pre_ds = (pre.width()==sw&&pre.height()==sh) ? pre : pre.Spline36Resize(sw, sh)
- pre = (ss_w > 1.0 || ss_h > 1.0) ? (pre.width()==wss&&pre.height()==hss) ? pre : pre.Spline64Resize(wss, hss).Repair(filtered.GaussResize(wss, hss, p=100), 1, chroma ? 1 : -1) : pre_ds
- #unsharp
- dark_limit = pre.mt_inpand(U=ch31, V=ch31)
- bright_limit = pre.mt_expand(U=ch31, V=ch31)
- minmaxavg = Smethod==1 ? NOP() : mt_average(dark_limit, bright_limit, U=ch31, V=ch31)
- method = Smethod==1 ? pre .removegrain(RGmode, chroma ? RGmodeU : -1)
- \ : Smethod==2 ? minmaxavg
- \ : minmaxavg.removegrain(RGmode, chroma ? RGmodeU : -1)
- method = secure ? method.mt_lutxy(pre,"x y < x 1 + x y > x 1 - x ? ?", U=ch31, V=ch31) : method
- #making difference clip of sharpening
- sharpdiff = mt_makediff(pre, method, U=ch31,V=ch31)
- #sharpening diff generate mode
- sharpdiff = Smode==1 ? sharpdiff.mt_lut(expr="x 128 - "+string(strength)+" * 128 +", U=ch31, V=ch31)
- \ : Smode==2 ? sharpdiff.mt_lut("x 128 == x x 128 - "+String(divisor)+" / Abs "+String(index)+" ^ "+String(strength)+" * x 128 > 1 -1 ? * 128 + ?", U=ch31, V=ch31)
- \ : Smode==3 ? sharpdiff.mt_lut("x 128 == x x 128 - abs "+string(Szrp)+" / 1 "+string(Spwr)+" / ^ "+string(Szrp)+" * "+string(strength)+" * x 128 - x 128 - abs / * x 128 - 2 ^ "+string(Szrp)+" 2 ^ "+string(SdmpLo)+" + * x 128 - 2 ^ "+string(SdmpLo)+" + "+string(Szrp)+" 2 ^ * / * 1 "+string(SdmpHi)+" 0 == 0 "+string(Szrp)+" "+string(SdmpHi)+" / 4 ^ ? + 1 "+string(SdmpHi)+" 0 == 0 x 128 - abs "+string(SdmpHi)+" / 4 ^ ? + / * 128 + ?", U=ch31, V=ch31)
- \ : sharpdiff
- #spatial limit
- sclp = Slimit ? mt_adddiff(filtered, sharpdiff, U=ch31, V=ch31) : NOP()
- sclp = Slimit ? sclp.mt_clamp(bright_limit, dark_limit, Sovershoot, Sundershoot, Y=SlimitY?3:2, U=ch31, V=ch31) : sclp
- #Soft
- sharpdiff = Slimit ? mt_makediff(sclp, filtered, U=ch31, V=ch31) : sharpdiff
- sharpdiff = Soft==100 ? sharpdiff : mt_lutxy(sharpdiff, sharpdiff.removegrain(19, chroma ? 19 : -1), "x 128 - abs y 128 - abs > y "+string(Soft)+" * x "+string(100-Soft)+" * + 100 / x ?", U=ch31, V=ch31)
- #Soothe
- sharpdiff2 = (Soothe>=0&&Soothe<=100) ? sharpdiff.temporalsoften(1,255,chroma ? 255 : 0,32,2) : NOP()
- sharpdiff = (Soothe>=0&&Soothe<=100) ? mt_lutxy(sharpdiff, sharpdiff2, "x 128 - y 128 - * 0 < x 128 - 100 / "+string(Soothe)+" * 128 + x 128 - abs y 128 - abs > x "+string(Soothe)+" * y 100 "+string(Soothe)+" - * + 100 / x ? ?", U=ch31, V=ch31) : sharpdiff
- #the difference achieved by filtering
- allD = limit ? ssrep&&(ss_w > 1.0 || ss_h > 1.0) ? mt_makediff(source.Spline64Resize(wss, hss).Repair(source.GaussResize(wss, hss, p=100), 1, chroma ? 1 : -1), filtered, U=ch31, V=ch31)
- \ : mt_makediff(source, filtered_ds, U=ch31, V=ch31) : NOP()
- #limiting sharpening to source clip
- sharpdiff = !ssrep&&(ss_w > 1.0 || ss_h > 1.0) ? sharpdiff.Spline36Resize(sw, sh) : sharpdiff
- ssDD = limit ? sharpdiff.repair(allD, mode=Repmode, modeU=chroma ? RepmodeU : -1) : sharpdiff
- ssDD = limit2 ? ssDD.mt_lutxy(sharpdiff, "x 128 - abs y 128 - abs < x y ?", U=ch31, V=ch31) : ssDD
- #sharpening more than source
- Dovershoot = 128 + Sovershoot
- Dundershoot= 128 - Sundershoot
- ssDDdiff = limit&&(Dovershoot>=129||Dundershoot<=127) ? mt_makediff(sharpdiff, ssDD, U=chrepsh? 3 : 1, V=chrepsh? 3 : 1) : NOP()
- ssDDdiff = limit&&(Dovershoot>=129||Dundershoot<=127) ? ssDDdiff.mt_lut("x "+string(Dovershoot)+" > "+string(Dovershoot)+" x "+string(Dundershoot)+" >= x "+string(Dundershoot)+" ? ?", U=chrepsh? 3 : 1, V=chrepsh? 3 : 1) : NOP()
- ssDD = limit&&(Dovershoot>=129||Dundershoot<=127) ? mt_adddiff(ssDD, ssDDdiff, U=chrepsh? ch31 : 2, V=chrepsh? ch31 : 2) : ssDD
- ssDD = ssrep&&(ss_w > 1.0 || ss_h > 1.0) ? ssDD.Spline36Resize(sw, sh) : ssDD
- #add difference clip to clip "filtered" of original resolution
- sclp = mt_adddiff(filtered_ds, ssDD, U=ch32, V=ch32)
- #temporal limit
- pMVS = Tlimit ? pre_ds.MSuper(hpad=0, vpad=0, pel=pel, sharp=MVsharp, chroma=false) : NOP()
- f1v = Tlimit ? MAnalyse(pMVS, isb=false, delta=1, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=false) : NOP()
- b1v = Tlimit ? MAnalyse(pMVS, isb=true, delta=1, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=false) : NOP()
- f1c = Tlimit ? MCompensate(source, pMVS, f1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : NOP()
- b1c = Tlimit ? MCompensate(source, pMVS, b1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : NOP()
- Tmax = Tlimit ? source.mt_logic(f1c, "max", U=1, V=1).mt_logic(b1c, "max", U=1, V=1) : NOP()
- Tmin = Tlimit ? source.mt_logic(f1c, "min", U=1, V=1).mt_logic(b1c, "min", U=1, V=1) : NOP()
- sclp = Tlimit ? sclp.mt_clamp(Tmax, Tmin, Tovershoot, Tundershoot, U=2, V=2) : sclp
- return sclp
- }
- Function MinBlur(clip clp, int "r", int "uv"){
- r = default(r,1)
- uv = default(uv,3)
- uv2 = (uv==2) ? 1 : uv
- rg4 = (uv==3) ? 4 : -1
- rg11 = (uv==3) ? 11 : -1
- rg20 = (uv==3) ? 20 : -1
- medf = (uv==3) ? 1 : -200
- RG11D = (r==0) ? mt_makediff(clp,clp.sbr(uv=uv2),U=uv2,V=uv2)
- \ : (r==1) ? mt_makediff(clp,clp.removegrain(11,rg11),U=uv2,V=uv2)
- \ : (r==2) ? mt_makediff(clp,clp.removegrain(11,rg11).removegrain(20,rg20),U=uv2,V=uv2)
- \ : mt_makediff(clp,clp.removegrain(11,rg11).removegrain(20,rg20).removegrain(20,rg20),U=uv2,V=uv2)
- RG4D = (r<=1) ? mt_makediff(clp,clp.removegrain(4,rg4),U=uv2,V=uv2)
- \ : (r<=2) ? mt_makediff(clp,clp.medianblur(2,2*medf,2*medf),U=uv2,V=uv2)
- \ : mt_makediff(clp,clp.medianblur(3,3*medf,3*medf),U=uv2,V=uv2)
- DD = mt_lutxy(RG11D,RG4D,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?",U=uv2,V=uv2)
- clp.mt_makediff(DD,U=uv,V=uv)
- return(last)
- }
- Function sbr(clip c, int "uv") {
- uv = Default(uv, 1)
- uv2 = (uv==2) ? 1 : uv
- rg11 = (uv==3) ? 11 : -1
- rg11D = mt_makediff(c,c.removegrain(11, rg11), U=uv2, V=uv2)
- rg11DD = mt_lutxy(rg11D, rg11D.removegrain(11, rg11), "x y - x 128 - * 0 < 128 x y - abs x 128 - abs < x y - 128 + x ? ?", U=uv2, V=uv2)
- c.mt_makediff(rg11DD, U=uv, V=uv)
- return(last)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement