Advertisement
mawen1250

Contra-Sharpen mod 16 1.0

Sep 3rd, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 20.97 KB | None | 0 0
  1. ###### Contra-Sharpen mod 16 1.0 ###### by mawen1250 ###### 2012.09.03 ######
  2.  
  3. Function CSmod16(clip filtered, clip "source", clip "pclip", float "strength", float "divisor", float "index", float "ss_w", float "ss_h",
  4. \ bool "chroma", int "preblur", bool "prec", bool "limit", bool "choversh", bool "Slimit", bool "SlimitY", bool "Tlimit", bool "ss_hq",
  5. \ bool "ssrep", int "Smethod", int "Smode", float "Szrp", float "Spwr", float "SdmpLo", float "SdmpHi", int "RGmode", int "RGmodeU",
  6. \ int "Repmode", int "RepmodeU", float "Sovershoot", float "Sundershoot", float "Tovershoot", float "Tundershoot", float "Soft",
  7. \ string "filter_ss", int "blksize", int "overlap", int "thSAD", int "thSCD1", int "thSCD2", bool "truemotion", bool "MVglobal",
  8. \ int "pel", int "pelsearch", int "search", int "searchparam", int "MVsharp", int "DCT", bool "usepasf", string "kernel",
  9. \ string "ssmethod", float "thr", bool "ssout", bool "ssoutc", float "secure", bool "lsb_in", bool "lsb", int "dither")
  10. {
  11.  
  12. lsb_in   = Default(lsb_in,  true   )
  13. lsb      = Default(lsb,     true   )
  14. dither   = Default(dither,  6      )
  15.  
  16. defsrc   = Defined(source)
  17. defpclp  = Defined(pclip )
  18. defRGm   = Defined(RGmode)
  19. deffss   = Defined(filter_ss)
  20. filtered8=           !lsb_in ? filtered : filtered.DitherPost(mode=dither)
  21. source8  = defsrc  ? !lsb_in ? source   : source  .DitherPost(mode=dither)  : NOP()
  22. pclip8   = defpclp ? !lsb_in ? pclip    : pclip   .DitherPost(mode=dither)  : NOP()
  23. filtered =            lsb_in ? filtered : filtered.Dither_convert_8_to_16()
  24. source   = defsrc  ?  lsb_in ? source   : source  .Dither_convert_8_to_16() : NOP()
  25. pclip    = defpclp ?  lsb_in ? pclip    : pclip   .Dither_convert_8_to_16() : NOP()
  26. sw       = filtered.width()
  27. sh       = filtered.height()/2
  28. HD       = (sw > 1024 || sh > 576) ? true : false
  29.  
  30. chroma   = Default(chroma,  false  )
  31. ss_w     = Default(ss_w,    1.50   )   #Super sampling multiplier of width.
  32. ss_h     = Default(ss_h,    1.50   )   #Super sampling multiplier of height.
  33. ss_w     = max    (ss_w,    1.00   )
  34. ss_h     = max    (ss_h,    1.00   )
  35. ss_hq    = Default(ss_hq,   false  )   #True using nnedi3_rpow2 in super sampling, false using non-ringing Spline64Resize.
  36. ssout    = Default(ssout,   false  )   #Output in super sampling resolution.
  37. ssoutc   = Default(ssoutc,  true   )   #Whether to output correct chroma when (chroma==false && ssout==true).
  38. ssrep    = Default(ssrep,   false  )   #When limiting sharpening to source clip, whether to repair in super sampling resolution.
  39. preblur  = Default(preblur, 1      )   #Pre-filtering, 1 uses Minblur16(mode=1)(RG11|RG4), 2 uses Minblur16(mode=2)(SBR|RG4), 3 uses Minblur16(mode=3)(RG20|RG4).
  40. prec     = Default(prec,    preblur>=1)   #Whether to use preblur in chroma plane.
  41. usepasf  = Default(usepasf, false  )   #Whether to use pre-filtered clip as input(filtered) clip, which will reduce noise/halo/ring from input clip.
  42. limit    = Default(limit,   true   )   #Whether to limit sharpening to source clip, only takes effect when (Defined(source) || Defined(filter_ss) || usepasf) == true.
  43. thr      = Default(thr,     0      )   #When limiting sharpening to source clip, allow sharpening more than the source in this threshold(on an 8-bit scale).
  44. RGmode   = Default(RGmode,  HD?20:11)
  45. RGmodeU  = Default(RGmodeU, defRGm?RGmode:11)
  46. Repmode  = Default(Repmode, 1      )
  47. RepmodeU = Default(RepmodeU,Repmode)
  48. ssout    = (ss_w > 1.0 || ss_h > 1.0) ? ssout : false
  49. ssrep    = (ss_w > 1.0 || ss_h > 1.0) ? ssrep : false
  50. ssoutc   = (!chroma && ssout) ? ssoutc : false
  51. ssrep    = ssout ? true : ssrep
  52. limit    = defsrc || deffss || usepasf ? limit : false
  53.  
  54. Smethod  = Default(Smethod, limit?1:3)   #Sharpen Method - 1: 3x3 kernel, 2: Min/Max, 3: Min/Max + 3x3 kernel.
  55. secure   = Default(secure,  0.50   )   #Threshold(on an 8-bit scale) to avoid banding & oil painting (or face wax) effect of sharpening, set to 0 to disable.(from LSFmod)
  56. Smode    = Default(Smode,   3      )   #Sharpen Mode - 1: Linear, 2: Non-linear 1, 3:Non-linear 2(from LSFmod Smode=5).
  57. divisor  = Default(divisor, 1.5    )
  58. index    = Default(index,   0.8    )
  59. strength = Default(strength,100.0  )
  60. Szrp     = Default(Szrp,    16.0   )
  61. Spwr     = Default(Spwr,    4.0    )
  62. SdmpLo   = Default(SdmpLo,  4.0    )
  63. SdmpHi   = Default(SdmpHi,  48.0   )
  64. miSpwr   = 1.0/Spwr
  65. Szrp16   = Szrp*256
  66. divisor16= divisor*256
  67.  
  68. Slimit      = Default(Slimit,      !limit      )   #Spatial limit with overshoot and undershoot. Disabled when (limit==true && thr==0).
  69. Sovershoot  = Default(Sovershoot,  strength/100)   #(on an 8-bit scale)
  70. Sundershoot = Default(Sundershoot, Sovershoot  )   #(on an 8-bit scale)
  71. choversh    = Default(choversh,    true        )   #When (chroma==true && limit==true && thr>0 && choversh==false), luma will sharpen more than source, chroma will not.
  72. Tlimit      = Default(Tlimit,      false       )   #Use MC Temporal limit at the end of sharpening(only process luma).(from MCTD)
  73. Tovershoot  = Default(Tovershoot,  strength/50 )   #(on an 8-bit scale)
  74. Tundershoot = Default(Tundershoot, Tovershoot  )   #(on an 8-bit scale)
  75. SlimitY     = Default(SlimitY,     !Tlimit     )   #It will automatically set Slimit=true, SlimitY=false to use Spatial limit on chroma when (chroma==true && Tlimit=true).
  76. Soft        = Default(Soft,        limit?0:-2  )   #Soft the sharpening effect (-1 = old autocalculate, -2 = new autocalculate, 0 = disable, (0, 100] = enable). Disabled when (limit==true && thr==0).(from LSFmod)
  77.  
  78. blksize     = Default(blksize,     HD ? 16 : 8 )
  79. overlap     = Default(overlap,     HD ? 8  : 4 )
  80. thSAD       = Default(thSAD,       300         )
  81. thSCD1      = Default(thSCD1,      300         )
  82. thSCD2      = Default(thSCD2,      100         )
  83. truemotion  = Default(truemotion,  false       )
  84. MVglobal    = Default(MVglobal,    true        )
  85. pel         = Default(pel,         2           )
  86. pelsearch   = Default(pelsearch,   2           )
  87. search      = Default(search,      2           )
  88. searchparam = Default(searchparam, 2           )
  89. MVsharp     = Default(MVsharp,     2           )
  90. DCT         = Default(DCT,         0           )
  91.  
  92.  
  93. wss      = Round(sw*ss_w/8)*8
  94. hss      = Round(sh*ss_h/8)*8
  95. ssmethod = Defined(ssmethod) ? ssmethod
  96. \        :             ss_hq ? "nnedi3_rpow2(rfactor=2, qual=2).Dither_convert_8_to_16().CSmod16_nrSpline64Resize16(wss, hss, -0.5, -0.5, chroma=chroma||ssoutc)"
  97. \        :                     "CSmod16_nrSpline64Resize16(wss, hss, chroma=chroma||ssoutc)"
  98. #You can define your own super sampling method with ssmethod.
  99.  
  100. ch21     = chroma ? 2 : 1
  101. ch31     = chroma ? 3 : 1
  102. ch41     = chroma ? 4 : 1
  103. Slimit   = limit && thr==0 ? false : Slimit
  104. Soft     = limit && thr==0 ? 0
  105. \        : Soft <= -2  ? (1.0+(2.0/(ss_w+ss_h))) * sqrt(strength)
  106. \        : Soft == -1  ? sqrt( (((ss_w+ss_h)/2.0-1.0)*100.0) ) * 10
  107. \        : Soft <= 100 ? Soft : 100
  108. strength = Smode == 1 ? strength/50.0 : Smode == 2 ? strength*256/20.0 : strength*256/100.0
  109.  
  110. Sovershoot  = Sovershoot  < 0 ? 0 : Sovershoot
  111. Sundershoot = Sundershoot < 0 ? 0 : Sundershoot
  112. Tovershoot  = Tovershoot  < 0 ? 0 : Tovershoot
  113. Tundershoot = Tundershoot < 0 ? 0 : Tundershoot
  114.  
  115.  
  116. #super sampling with filtering
  117. source      = defsrc ? source  : filtered
  118. source8     = defsrc ? source8 : source.DitherPost(mode=dither)
  119. filtered_os = filtered
  120. filtered_ss = (ss_w > 1.0 || ss_h > 1.0) ? Eval((ss_hq ? "filtered8." : "filtered.")+ssmethod) : filtered
  121. filtered    = (ss_w > 1.0 || ss_h > 1.0) ? filtered_ss : filtered
  122. source_ss   = (ss_w > 1.0 || ss_h > 1.0) ? defsrc ? Eval((ss_hq ? "source8." : "source.")+ssmethod) : filtered : source
  123. filtered    = (ss_w > 1.0 || ss_h > 1.0) && deffss ? Eval("filtered."+filter_ss) : filtered
  124. filtered_ds = (ss_w > 1.0 || ss_h > 1.0) && deffss ? filtered.CSmod16_nrSpline64Resize16(sw, sh, chroma=chroma) : filtered_os
  125.  
  126. Assert( (isFrameBased(filtered_os)),                              """CSmod16: input clip "filtered" must be Frame Based!""" )
  127. Assert( (isFrameBased(source)),                                     """CSmod16: input clip "source" must be Frame Based!""" )
  128. Assert( (source.width() == sw && source.height()/2 == sh), """CSmod16: resolution of "filtered" and "source" must match!""" )
  129.  
  130.  
  131. #pre-filtering before sharpening
  132. pre         = defpclp    ? pclip
  133. \           : preblur>=1 ? filtered.MinBlur16(preblur, chroma ? (prec ? 3 : 2) : 1)
  134. \           :              filtered
  135. #You can define your own pre-filter clip with pclip.
  136.  
  137. pre_ds      = (pre.width() == sw && pre.height()/2 == sh) ? pre : pre.CSmod16_nrSpline64Resize16(sw, sh, chroma=chroma)
  138. pre         = (ss_w > 1.0 || ss_h > 1.0) ? (pre.width() == wss && pre.height()/2 == hss) ? pre : Eval((ss_hq ? "pclip8." : "pclip.")+ssmethod) : pre_ds
  139.  
  140. #whether to use pre-filtered clip as main clip
  141. filtered    = usepasf ? pre    : filtered
  142. filtered_ds = usepasf ? pre_ds : filtered_ds
  143.  
  144.  
  145. #unsharp
  146. dark_limit   = pre.CSmod16_inpand16(U=ch31, V=ch31)
  147. bright_limit = pre.CSmod16_expand16(U=ch31, V=ch31)
  148. minmaxavg    = Smethod == 1 ? NOP() : CSmod16_average16(dark_limit, bright_limit, U=ch31, V=ch31)
  149.  
  150. method     = Smethod == 1  ? Defined(kernel) ? Eval("pre.      "+kernel) : pre      .Dither_removegrain16(RGmode, chroma ? RGmodeU : -1)
  151. \          : Smethod == 2  ?                                               minmaxavg
  152. \          :                 Defined(kernel) ? Eval("minmaxavg."+kernel) : minmaxavg.Dither_removegrain16(RGmode, chroma ? RGmodeU : -1)
  153.  
  154. method     = secure>0     ? pre.Dither_limit_dif16(method, thr=secure, elast=3.0, U=ch31, V=ch31) : method
  155.  
  156.  
  157. #making difference clip of sharpening
  158. sharpdiff  = Dither_sub16(pre, method, U=ch31, V=ch31, dif=true)
  159.  
  160.  
  161. #sharpening diff generate mode
  162. sharpdiff  = Smode == 1 ? sharpdiff.Dither_lut16("x 32768 - "+string(strength)+" * 32768 +", U=ch31, V=ch31)
  163. \          : Smode == 2 ? sharpdiff.Dither_lut16("x 32768 == x x 32768 - "+String(divisor16)+" / Abs "+String(index)+" ^ "+String(strength)+" * x 32768 > 1 -1 ? * 32768 + ?", U=ch31, V=ch31)
  164. \          : Smode == 3 ? sharpdiff.Dither_lut16("x 32768 == x x 32768 - abs "+string(Szrp16)+" / "+string(miSpwr)+" ^ "+string(Szrp)+" * "+string(strength)+" * x 32768 > 1 -1 ? * x 32768 - 256 / 2 ^ "+string(Szrp)+" 2 ^ "+string(SdmpLo)+" + * x 32768 - 256 / 2 ^ "+string(SdmpLo)+" + "+string(Szrp)+" 2 ^ * / * 1 "+string(SdmpHi)+" 0 == 0 "+string(Szrp)+" "+string(SdmpHi)+" / 4 ^ ? + 1 "+string(SdmpHi)+" 0 == 0 x 32768 - 256 / abs "+string(SdmpHi)+" / 4 ^ ? + / * 32768 + ?", U=ch31, V=ch31)
  165. \          : sharpdiff
  166. # x==32768 ? x : (abs(x-32768)/Szrp16)^(miSpwr)*Szrp*strength*(x>32768 ? 1 : -1) * (((x-32768)/256)^2*(Szrp^2+SdmpLo) / (((x-32768)/256)^2+SdmpLo)*Szrp^2) * ((1+ SdmpHi==0 ? 0 : (Szrp/SdmpHi)^4) / (1+ SdmpHi==0 ? 0 : (abs((x-32768)/256)/SdmpHi)^4)) + 32768
  167.  
  168.  
  169. #spatial limit
  170. sclp       = Slimit&&(chroma||SlimitY) ? Dither_add16(filtered, sharpdiff, U=ch31, V=ch31, dif=true) : NOP()
  171. sclp       = Slimit&&(chroma||SlimitY) ? sclp.CSmod16_clamp16(bright_limit, dark_limit, Sovershoot, Sundershoot, Y=SlimitY?3:2, U=ch31, V=ch31) : NOP()
  172.  
  173.  
  174. #Soft
  175. sharpdiff  = Slimit&&(chroma||SlimitY) ? Dither_sub16(sclp, filtered, U=ch31, V=ch31, dif=true) : sharpdiff
  176. sharpdiffS = sharpdiff.Dither_removegrain16(19, chroma ? 19 : -1)
  177. wmask      = sharpdiff.Dither_lut16(Y=Round(-655.35*Soft), U=chroma?Round(-655.35*Soft):1, V=chroma?Round(-655.35*Soft):1)
  178. sharpdiffW = Soft == 100 ? sharpdiffS : Dither_merge16(sharpdiff, sharpdiffS, wmask, luma=false, U=ch31, V=ch31)
  179. sharpdiff  = Soft ==   0 ? sharpdiff  : CSmod16_limitdiff16(sharpdiff, sharpdiffW, U=ch31, V=ch31)
  180.  
  181.  
  182. #the difference achieved by filtering
  183. allD       = limit  ? ssrep && (ss_w > 1.0 || ss_h > 1.0) ? Dither_sub16(source_ss, filtered, U=ch31, V=ch31, dif=true)
  184. \          : Dither_sub16(source, filtered_ds, U=ch31, V=ch31, dif=true) : NOP()
  185.  
  186. #limiting sharpening to source clip
  187. sharpdiff  = !ssrep && (ss_w > 1.0 || ss_h > 1.0) ? sharpdiff.CSmod16_nrSpline64Resize16(sw, sh, chroma=chroma) : sharpdiff
  188. ssDD       = limit ? sharpdiff.Dither_repair16(allD, mode=Repmode, modeU=chroma ? RepmodeU : -1) : sharpdiff
  189. ssDD       = limit ? ssDD.CSmod16_limitdiff16(sharpdiff, U=ch31, V=ch31) : ssDD
  190. ssDD       = (limit && thr > 0) ? sharpdiff.Dither_limit_dif16(ssDD, thr=thr, elast=3.0, U=choversh?ch31:ch41, V=choversh?ch31:ch41) : ssDD
  191. ssDD       = ssrep && (ss_w > 1.0 || ss_h > 1.0) && !ssout ? ssDD.CSmod16_nrSpline64Resize16(sw, sh, chroma=chroma) : ssDD
  192.  
  193. #add difference clip to clip "filtered" of ss/original resolution
  194. sclp       = ssout ? Dither_add16(filtered, ssDD, U=ch31, V=ch31, dif=true) : Dither_add16(filtered_ds, ssDD, U=ch31, V=ch31, dif=true)
  195.  
  196.  
  197. #temporal limit
  198. pre_ds8    = (ssout ? pre            : pre_ds).DitherPost(mode=-1, U=1, V=1)
  199. source8    = (ssout ? source_ss.DitherPost(mode=dither, U=1, V=1) : source8)
  200. source     = (ssout ? source_ss                                   : source )
  201.  
  202. pMVS       = Tlimit ? pre_ds8.MSuper(hpad=0, vpad=0, pel=pel, sharp=MVsharp, chroma=false) : NOP()
  203. 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()
  204. 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()
  205. f1c        = Tlimit ? MCompensate(source8, pMVS, f1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : NOP()
  206. b1c        = Tlimit ? MCompensate(source8, pMVS, b1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : NOP()
  207. Tmax       = Tlimit ? source8.mt_logic(f1c, "max", U=1, V=1).mt_logic(b1c, "max", U=1, V=1).Dither_convert_8_to_16() : NOP()
  208. Tmin       = Tlimit ? source8.mt_logic(f1c, "min", U=1, V=1).mt_logic(b1c, "min", U=1, V=1).Dither_convert_8_to_16() : NOP()
  209. Tmax       = Tlimit ? lsb_in ? source.Dither_limit_dif16(Tmax, thr=0.50, elast=3.0, U=1, V=1) : Tmax : NOP()
  210. Tmin       = Tlimit ? lsb_in ? source.Dither_limit_dif16(Tmin, thr=0.50, elast=3.0, U=1, V=1) : Tmin : NOP()
  211. sclp       = Tlimit ? sclp.CSmod16_clamp16(Tmax, Tmin, Tovershoot, Tundershoot, U=2, V=2) : sclp
  212.  
  213.  
  214. end        = chroma ? sclp
  215. \                   : ssout ? ssoutc ? sclp.MergeChroma(filtered_ss)
  216. \                                    : sclp
  217. \                           : sclp.MergeChroma(filtered_os)
  218.  
  219. return lsb ? end : end.DitherPost(mode=dither)
  220. }
  221.  
  222.  
  223. Function MinBlur16(clip clp, int "mode", int "uv"){
  224.  
  225. mode  = Default(mode, 1)
  226. uv    = Default(uv,   3)
  227.  
  228. uv2   = (uv==2) ? 1  : uv
  229. rg4   = (uv==3) ? 4  : -1
  230. rg11  = (uv==3) ? 11 : -1
  231. rg20  = (uv==3) ? 20 : -1
  232.  
  233. RG11D = (mode==1) ? Dither_sub16(clp, clp.Dither_removegrain16(11, rg11),U=uv2, V=uv2, dif=true)
  234. \     : (mode==2) ? Dither_sub16(clp, clp.sbr16(uv=uv2), U=uv2, V=uv2, dif=true)
  235. \     :             Dither_sub16(clp, clp.Dither_removegrain16(20, rg20),U=uv2, V=uv2, dif=true)
  236. RG4D  =             Dither_sub16(clp, clp.Dither_removegrain16(4,  rg4 ),U=uv2, V=uv2, dif=true)
  237. DD    = CSmod16_limitdiff16(RG11D, RG4D, U=uv2, V=uv2)
  238.  
  239. return clp.Dither_sub16(DD, U=uv, V=uv, dif=true)
  240. }
  241.  
  242.  
  243. Function sbr16(clip clp, int "uv") {
  244. uv       = Default(uv, 1)
  245. uv2      = (uv==2) ? 1  : uv
  246. rg11     = (uv==3) ? 11 : -1
  247.  
  248. rg11D    = Dither_sub16(clp, clp.Dither_removegrain16(11, rg11), U=uv2, V=uv2, dif=true)
  249. rg11Dr   = rg11D.Dither_removegrain16(11, rg11)
  250.  
  251. abrg11D  = rg11D.Dither_lut16("x 32768 - abs", U=uv2, V=uv2)
  252. Ddiff    = Dither_sub16(rg11D, rg11Dr, U=uv2, V=uv2, dif=true)
  253. abDdiff  = Ddiff.Dither_lut16("x 32768 - abs", U=uv2, V=uv2)
  254. abDDD    = Dither_sub16(abDdiff, abrg11D, U=uv2, V=uv2, dif=true)
  255.  
  256. Dmask1   = abDDD.Dither_lut16("x 32768 < 65535 0 ?", U=uv2, V=uv2)
  257. Ddiffg   = Ddiff.Dither_lut16("x 32768 == x x 32768 < 0 65535 ? ?", U=uv2, V=uv2).Crop(0, 0, 0, -Ddiff.height()/2)
  258. rg11Dg   = rg11D.Dither_lut16("x 32768 == x x 32768 < 0 65535 ? ?", U=uv2, V=uv2).Crop(0, 0, 0, -rg11D.height()/2)
  259. Dmask2   = mt_lutxy(Ddiffg, rg11Dg, "x 128 - y 128 - * 0 < 0 255 ?", U=uv2, V=uv2)
  260.  
  261. DD1      = Dither_merge16(rg11D, Ddiff, Dmask1, luma=false, U=uv2, V=uv2)
  262. DD2      = Dither_merge16_8(DD1.CSmod16_gen_null_diff(), DD1, Dmask2, luma=false, U=uv2, V=uv2)
  263.  
  264. return clp.Dither_sub16(DD2, U=uv, V=uv, dif=true)
  265. }
  266.  
  267.  
  268. Function CSmod16_nrSpline64Resize16(clip input, int "target_width", int "target_height", float "src_left", float "src_top", float "src_width", float "src_height", bool "chroma", val "nr") {
  269.  
  270.   w             = input.width()
  271.   h             = input.height()/2
  272.  
  273.   target_width  = Default(target_width,      w)
  274.   target_height = Default(target_height,     h)
  275.   src_left      = Default(src_left,          0)
  276.   src_top       = Default(src_top,           0)
  277.   src_width     = Default(src_width,         w)
  278.   src_height    = Default(src_height,        h)
  279.   chroma        = Default(chroma,         true)
  280.   nr            = Default(nr,             true)
  281.  
  282.   Assert( IsFloat(nr) || IsBool(nr), """CSmod16_nrSpline64Resize16: "nr" should be either bool or float!""" )
  283.  
  284.   res_mul   = Float( target_width * target_height ) / Float( w * h )
  285.   res_mul   = min( max( res_mul, 1 ), 2.25 )
  286.   nr_weight = IsFloat(nr)       ? nr
  287.   \         : nr /* == True  */ ? Spline( res_mul, 1, 0, 2.25, 1, 3.5, 0, true )
  288.   \         :    /* == False */   0
  289.   nr_weight = min( max( nr_weight, 0 ), 1 )
  290.   wmaskv    = Round(-65535*nr_weight)
  291.  
  292.   resize = input.Dither_resize16(target_width, target_height, src_left, src_top, src_width, src_height, kernel="Spline64", Y=3, U=chroma?3:1, V=chroma?3:1)
  293.   nrres  = input.Dither_resize16(target_width, target_height, src_left, src_top, src_width, src_height, kernel="Gaussian", a1=100, Y=3, U=chroma?3:1, V=chroma?3:1)
  294.   wmask  = resize.Dither_lut16(Y=wmaskv, U=chroma?wmaskv:1, V=chroma?wmaskv:1)
  295.  
  296.   resize = nr_weight == 0 ? resize
  297.   \      : nr_weight == 1 ? resize.Dither_repair16(nrres, 1, chroma ? 1 : -1)
  298.   \      :                  Dither_merge16(resize, resize.Dither_repair16(nrres, 1, chroma ? 1 : -1), wmask, luma=false, Y=3, U=chroma?3:1, V=chroma?3:1)
  299.  
  300.   return resize
  301. }
  302.  
  303.  
  304. Function CSmod16_inpand16(clip input, int "Y", int "U", int "V")
  305. {
  306. Y          = Default(Y,        3)
  307. U          = Default(U,        1)
  308. V          = Default(V,        1)
  309.  
  310. input_msb  = input.Crop(0, 0, 0, -input.height()/2)
  311. inpand_msb = input_msb.mt_inpand(Y=Y, U=U, V=V)
  312. inpand_16  = inpand_msb.Dither_convert_8_to_16()
  313.  
  314. return inpand_16.Dither_repair16(input, Y==3?1:Y==2?0:-1, U==3?1:U==2?0:-1, V==3?1:V==2?0:-1)
  315. }
  316.  
  317.  
  318. Function CSmod16_expand16(clip input, int "Y", int "U", int "V")
  319. {
  320. Y          = Default(Y,        3)
  321. U          = Default(U,        1)
  322. V          = Default(V,        1)
  323.  
  324. input_msb  = input.Crop(0, 0, 0, -input.height()/2)
  325. expand_msb = input_msb.mt_lut("x 1 +", Y=Y, U=U, V=V).mt_expand(Y=Y, U=U, V=V)
  326. expand_16  = expand_msb.Dither_convert_8_to_16()
  327.  
  328. return expand_16.Dither_repair16(input, Y==3?1:Y==2?0:-1, U==3?1:U==2?0:-1, V==3?1:V==2?0:-1)
  329. }
  330.  
  331.  
  332. Function CSmod16_average16(clip input1, clip input2, int "Y", int "U", int "V")
  333. {
  334. Y          = Default(Y,        3)
  335. U          = Default(U,        3)
  336. V          = Default(V,        3)
  337.  
  338. hmask = input1.CSmod16_gen_null_diff()
  339.  
  340. return Dither_merge16(input1, input2, hmask, luma=false, Y=Y, U=U, V=V)
  341. }
  342.  
  343. Function CSmod16_limitdiff16(clip diff1, clip diff2, int "Y", int "U", int "V")
  344. {
  345. Y          = Default(Y,        3)
  346. U          = Default(U,        3)
  347. V          = Default(V,        3)
  348.  
  349. abdiff1    = Dither_lut16(diff1, "x 32768 - abs", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  350. abdiff2    = Dither_lut16(diff2, "x 32768 - abs", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  351. abdiffdiff = Dither_sub16(abdiff1, abdiff2, Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1, dif=true)
  352. bin        = Dither_lut16(abdiffdiff, "x 32768 <= 0 65535 ?", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  353.  
  354. return Dither_merge16(diff1, diff2, bin, luma=false, Y=Y, U=U, V=V)
  355. }
  356.  
  357.  
  358. Function CSmod16_clamp16(clip c, clip bright_limit, clip dark_limit, float "overshoot", float "undershoot", int "Y", int "U", int "V")
  359. {
  360. Y          = Default(Y,          3)
  361. U          = Default(U,          1)
  362. V          = Default(V,          1)
  363. overshoot  = Default(overshoot,  0)
  364. undershoot = Default(undershoot, 0)
  365. os16       = string(overshoot *256)
  366. us16       = string(undershoot*256)
  367.  
  368. brightdiff = Dither_sub16(c, bright_limit, Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1, dif=true)
  369. darkdiff   = Dither_sub16(c,   dark_limit, Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1, dif=true)
  370.  
  371. brightDdec = brightdiff.Dither_lut16("x " + os16 + " - 32768 > x " + os16 + " - 32768 ?", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  372. darkDdec   = darkdiff  .Dither_lut16("x " + us16 + " + 32768 < x " + us16 + " + 32768 ?", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  373.  
  374. return c.Dither_sub16(brightDdec, Y=Y, U=U, V=V, dif=true).Dither_sub16(darkDdec, Y=Y, U=U, V=V, dif=true)
  375. }
  376.  
  377.  
  378. Function CSmod16_gen_null_diff(clip input, bool "lsb_in")
  379. {
  380.     lsb_in = Default(lsb_in, true)
  381.    
  382.     vers   = VersionNumber ()
  383.     p_t    = (vers < 2.60) ? "YV12" : Dither_undef ()
  384.    
  385.     input    = lsb_in ? input.Crop(0, 0, 0, -input.height/2) : input
  386.    
  387.     StackVertical(BlankClip(input, pixel_type=p_t, color_yuv=8421504), BlankClip(input, pixel_type=p_t, color_yuv=0))
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement