Advertisement
mawen1250

GrainStabilizeMC v0.2

Nov 5th, 2012
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 15.92 KB | None | 0 0
  1. ###### GrainStabilizeMC v0.2 ######   by mawen1250   ######  2012.11.05  ######
  2. ###### Requirements: masktools v2.0a48, mvtools v2.6.0.5, dither v1.21.0 ######
  3. ###### RemoveGrain v1.0pre                                               ######
  4.  
  5. Function GSMC(clip input, clip "p", clip "Lmaskl", string "Preset", int "nrmode", int "radius", int "adapt", int "rep",
  6. \ int "Y", int "U", int "V", bool "lsb_in", bool "lsb", bool "lsb_out", int "dither",
  7. \ bool "chromamv", int "blksize", int "overlap", int "thSAD", int "thSADC", int "thSCD1", int "thSCD2", int "limit", int "limitc",
  8. \ bool "truemotion", bool "MVglobal", int "pel", int "pelsearch", int "search", int "searchparam", int "MVsharp", int "DCT")
  9. {
  10.  
  11.   nrmode   = Default(nrmode, 1      )
  12.   # Mode to get grain/noise from input clip:
  13.   # 1: 3x3 Average Blur on luma, 3x3 Gaussian Blur on chroma,
  14.   # 2: 3x3 Min Blur,  3: 5x5 Min Blur,  4: 7x7 Min Blur.
  15.   # or define your own denoise clip "p".
  16.  
  17.   radius   = Default(radius, 1      )
  18.   # Temporal radius of MDegrain for grain stabilize.(1-3)
  19.  
  20.   adapt    = Default(adapt,  64     )
  21.   # Threshold for luma-adaptative mask
  22.   # -1: off,  0: source,  255: invert.
  23.   # or define your own luma mask clip "Lmaskl".
  24.  
  25.   rep      = Default(rep,    13     )
  26.   # Mode of repair to avoid artifacts, set 0 to turn off this operation.
  27.  
  28.   Y        = Default(Y,      3      )
  29.   U        = Default(U,      2      )
  30.   V        = Default(V,      2      )
  31.  
  32.   Y        = min(Y, 3)
  33.   U        = min(U, 3)
  34.   V        = min(V, 3)
  35.   Yt       = Y == 3
  36.   Ut       = U == 3
  37.   Vt       = V == 3
  38.   Y31      = Yt ? 3 : 1
  39.   U31      = Ut ? 3 : 1
  40.   V31      = Vt ? 3 : 1
  41.  
  42.   Assert( nrmode>=1 && nrmode<=4,  "GrainStabilizeMC: invalid value for radius(1~3)!"   )
  43.   Assert( radius>=1 && radius<=3,  "GrainStabilizeMC: invalid value for radius(1~3)!"   )
  44.   Assert( adapt>=-1 && adapt<=255, "GrainStabilizeMC: invalid value for adapt(-1~255)!" )
  45.  
  46.   lsb_in   = Default(lsb_in, false  )   # input      clip is 16-bit stacked or not
  47.   lsb      = Default(lsb,    false  )   # processing clip is 16-bit stacked or not
  48.   lsb_out  = Default(lsb_out,lsb    )   # output     clip is 16-bit stacked or not
  49.   dither   = Default(dither, 6      )   # dither mode for 16-bit to 8-bit conversion
  50.  
  51.   sw       = input.width
  52.   sh       = input.height
  53.   sh       = lsb_in ? sh/2 : sh
  54.   HD       = (sw > 1024 || sh > 576) ? true : false
  55.  
  56.   Preset   = Default(Preset, "Medium")
  57.  
  58.   pnum     = preset == "Very Fast" ?  0
  59.   \        : preset == "Faster"    ?  1
  60.   \        : preset == "Fast"      ?  2
  61.   \        : preset == "Medium"    ?  3
  62.   \        : preset == "Slow"      ?  4
  63.   \        : preset == "Slower"    ?  5
  64.   \        : preset == "Very Slow" ?  6
  65.   \        :                          7
  66.   Assert( pnum<=6, """GrainStabilizeMC: Unknown Preset!
  67.  Allowed Preset: "Very Fast","Faster","Fast","Medium","Slow","Slower","Very Slow".""" )
  68.  
  69.   # Preset groups:                                 Very Fast     Fast          Slow          Very Slow
  70.   # Preset groups:                                        Faster        Medium        Slower
  71.  
  72.   bs          = HD ? 16 :  8
  73.   bs2         = HD ? 32 : 16
  74.   chromamv    = Default(chromamv,    Ut || Vt    )
  75.   blksize     = Default(blksize,     Select(pnum,  bs2,   bs2,   bs2,   bs,    bs,    bs,    bs    ))
  76.   ol          = blksize/2
  77.   ol2         = blksize/4
  78.   overlap     = Default(overlap,     Select(pnum,  ol2,   ol2,   ol2,   ol,    ol,    ol,    ol    ))
  79.   thSAD       = Default(thSAD,       300         )
  80.   thSADC      = Default(thSADC,      thSAD/2     )
  81.   thSCD1      = Default(thSCD1,      300         )
  82.   thSCD2      = Default(thSCD2,      100         )
  83.   truemotion  = Default(truemotion,  false       )
  84.   MVglobal    = Default(MVglobal,    false       )
  85.   pel         = Default(pel,         Select(pnum,  1,     1,     1,     2,     2,     2,     2     ))
  86.   pelsearch   = Default(pelsearch,   Select(pnum,  1,     1,     1,     1,     2,     2,     2     ))
  87.   search      = Default(search,      Select(pnum,  0,     1,     2,     2,     2,     4,     5     ))
  88.   searchparam = Default(searchparam, Select(pnum,  1,     1,     1,     2,     2,     2,     2     ))
  89.   MVsharp     = Default(MVsharp,     2           )
  90.   DCT         = Default(DCT,         0           )
  91.  
  92.   Umv      = chromamv ? 3 : U31
  93.   Vmv      = chromamv ? 3 : V31
  94.   plane    = !Yt ? !Ut ? 2 : !Vt ? 1 : 3 : !Ut&&!Vt ? 0 : 4
  95.  
  96.  
  97.  
  98.   input8   = !lsb_in ? input : input.DitherPost(Y=3, U=U, V=V, mode=dither)
  99.   input16  =  lsb_in ? input : input.Dither_convert_8_to_16
  100.  
  101.   plsb_in  = Defined(p) ? input.height == p.height ? lsb_in : !lsb_in : lsb_in
  102.   iresc    = Defined(p) ? plsb_in == lsb_in ? input.height == p.height ? true : false
  103.   \                                         : lsb_in == false ? input.height*2 == p.height ? true : false
  104.   \                                                           : input.height == p.height*2 ? true : false
  105.   \                     : true
  106.   Assert( iresc == true, """GrainStabilizeMC: clip "input" and clip "p" resolution not match!
  107.  (GSMC have already considered different condition of lsb)""" )
  108.  
  109.   p8       = Defined(p) ? !plsb_in ? p : p.DitherPost(Y=3, U=Umv, V=Vmv, mode=0) : NOP()
  110.   p16      = Defined(p) ?  plsb_in ? p : p.Dither_convert_8_to_16 : NOP()
  111.  
  112.   pre_nr8  = !lsb ? Defined(p) ? p8  
  113.   \                            : nrmode == 1 ? input8 .RemoveGrain(Yt?20:0, Ut?11:0, Vt?11:0)
  114.   \                                          : input8 .GSMC_MinBlur(nrmode-1, Ut||Vt?3:2)
  115.   \               : NOP()
  116.   dif_nr8  = !lsb ? mt_makediff(input8, pre_nr8, Y=Y31, U=U31, V=V31) : NOP()
  117.  
  118.   pre_nr16 =  lsb ? Defined(p) ? p16
  119.   \                            : nrmode == 1 ? input16.Dither_removegrain16(Yt?20:0, Ut?11:0, Vt?11:0)
  120.   \                                          : input16.GSMC_MinBlur16(nrmode-1, Ut||Vt?3:2)
  121.   \               : NOP()
  122.   dif_nr16 =  lsb ? Dither_sub16(input16, pre_nr16, Y=Y31, U=U31, V=V31, dif=true) : NOP()
  123.  
  124.   pre_nr   = Defined(p) ? p8 : lsb ? pre_nr16.DitherPost(Y=3  , U=Umv, V=Vmv, mode=0) : pre_nr8
  125.   dif_nr   =  lsb ? dif_nr16.DitherPost(Y=Y31, U=U31, V=V31, mode=dither) : dif_nr8
  126.  
  127.   psuper   = pre_nr.MSuper(pel=pel, levels=0, sharp=MVsharp, chroma=chromamv)
  128.   difsuper = dif_nr.MSuper(pel=pel, levels=1, sharp=MVsharp, chroma=Ut || Vt)
  129.  
  130.   fv3      = radius>=3 ? psuper.MAnalyse(isb=false, delta=3, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chromamv) : NOP()
  131.   fv2      = radius>=2 ? psuper.MAnalyse(isb=false, delta=2, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chromamv) : NOP()
  132.   fv1      = radius>=1 ? psuper.MAnalyse(isb=false, delta=1, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chromamv) : NOP()
  133.   bv1      = radius>=1 ? psuper.MAnalyse(isb=true,  delta=1, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chromamv) : NOP()
  134.   bv2      = radius>=2 ? psuper.MAnalyse(isb=true,  delta=2, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chromamv) : NOP()
  135.   bv3      = radius>=3 ? psuper.MAnalyse(isb=true,  delta=3, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chromamv) : NOP()
  136.  
  137.   dif_sb   = radius==1 ? dif_nr.MDegrain1(difsuper, bv1, fv1                    , thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, lsb=lsb)
  138.   \        : radius==2 ? dif_nr.MDegrain2(difsuper, bv1, fv1, bv2, fv2          , thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, lsb=lsb)
  139.   \        :             dif_nr.MDegrain3(difsuper, bv1, fv1, bv2, fv2, bv3, fv3, thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, lsb=lsb)
  140.  
  141.   difdiff  =  lsb ? Dither_sub16(dif_nr.Dither_convert_8_to_16, dif_sb, Y=Y31, U=U31, V=V31, dif=true) : NOP()
  142.   diffmask =  lsb ? difdiff.Dither_lut16("x 32768 = 0 65535 ?", Y=Y31, U=U31, V=V31) : NOP()
  143.   dif_sb   =  lsb ? Dither_merge16(dif_nr16, dif_sb, diffmask, luma=false, Y=Y31, U=U31, V=V31) : dif_sb
  144.  
  145.   Lmaskl   = Defined(Lmaskl) ? Lmaskl
  146.   \        : adapt == 0      ? input8.RemoveGrain(19, -1)
  147.   \        : adapt == 255    ? input8.mt_invert(U=1, V=1).RemoveGrain(19, -1)
  148.   \        :                   input8.mt_lut("x "+string(adapt)+" - abs 255 * "+string(adapt)+" 128 - abs 128 + /", U=1, V=1).RemoveGrain(19, -1)
  149.   Lmask    = Ut || Vt  ? Lmaskl.GSMC_YtoYUV : Lmaskl
  150.  
  151.   stable8  = !lsb ? mt_adddiff(pre_nr8, dif_sb, Y=Y31, U=U31, V=V31) : NOP()
  152.   stable8  = !lsb ? rep   ==  0 ? stable8  : stable8.Repair(input8, Yt?rep:-1, Ut?rep:-1, Vt?rep:-1) : NOP()
  153.   mL8      = !lsb ? adapt == -1 ? stable8  : mt_merge(input8, stable8, Lmask, luma=false, Y=Y, U=U, V=V) : NOP()
  154.  
  155.   stable16 =  lsb ? Dither_add16(pre_nr16, dif_sb, Y=Y31, U=U31, V=V31, dif=true) : NOP()
  156.   stable16 =  lsb ? rep   ==  0 ? stable16 : stable16.Dither_repair16(input16, Yt?rep:-1, Ut?rep:-1, Vt?rep:-1) : NOP()
  157.   mL16     =  lsb ? adapt == -1 ? stable16 : Dither_merge16_8(input16, stable16, Lmask, luma=false, Y=Y, U=U, V=V) : NOP()
  158.  
  159.   end      = lsb_out ? lsb ? mL16
  160.   \                        : mL8.Dither_convert_8_to_16
  161.   \                  : lsb ? mL16.DitherPost(Y=Y, U=U, V=V, mode=dither)
  162.   \                        : mL8
  163.  
  164.   return end
  165.  
  166. }
  167.  
  168.  
  169. Function GSMC_YtoYUV(clip inputl, string "colorspace")
  170. {
  171.  sw          = inputl.width
  172.  sh          = inputl.height
  173.  
  174.  icolorspace = inputl.YtoYUV_GetCSP
  175.  ocolorspace = Defined(colorspace) ? colorspace : icolorspace
  176.  
  177.  try {
  178.    inputp = inputl.ConvertToY8
  179.    inputc = ocolorspace == "YV24" ? inputp
  180.    \      : ocolorspace == "YV16" ? inputp.Spline36Resize(sw/2, sh  , -0.50)
  181.    \      :                         inputp.Spline36Resize(sw/2, sh/2, -0.50)  
  182.  } catch ( error_msg ) {
  183.    inputc = ocolorspace == "YV24" ? inputl
  184.    \      : ocolorspace == "YV16" ? inputl.Spline36Resize(sw/2, sh  , -0.50)
  185.    \      :                         inputl.Spline36Resize(sw/2, sh/2, -0.50)  
  186.  }
  187.  
  188.  output      = YtoUV(inputc, inputc, inputl)
  189.  
  190.  return output
  191.  
  192.  Function YtoYUV_GetCSP(clip c) {
  193.    return c.IsPlanar ? c.IsYV12 ? "YV12" :
  194.    \                   c.IsYV16 ? "YV16" :
  195.    \                   c.IsYV24 ? "YV24" : c.GetCSP_Y8_YV411 :
  196.    \      c.IsYUY2   ? "YUY2"   :
  197.    \      c.IsRGB32  ? "RGB32"  :
  198.    \      c.IsRGB24  ? "RGB24"  : "Unknown"
  199.  
  200.    Function GetCSP_Y8_YV411(clip c) {
  201.      try {
  202.        c.UtoY
  203.        csp = "YV411"
  204.      } catch ( error_msg ) {
  205.       csp = "Y8"
  206.      }
  207.      return csp
  208.    }
  209.  }
  210. }
  211.  
  212.  
  213. Function GSMC_MinBlur(clip clp, int "r", int "uv"){
  214.  
  215. r     = Default(r,  1)
  216. uv    = Default(uv, 3)
  217.  
  218. uv2   = (uv==2) ? 1  : uv
  219. rg4   = (uv==3) ? 4  : -1
  220. rg11  = (uv==3) ? 11 : -1
  221. rg20  = (uv==3) ? 20 : -1
  222. medf  = (uv==3) ? 1  : -200
  223. uvm2  = (r==2)  ? (uv==3?3:uv==2?0:-1) : nop()
  224. uvm3  = (r==3)  ? (uv==3?3:uv==2?0:-1) : nop()
  225.  
  226. RG11D = (r<=0) ? mt_makediff(clp, clp.MinBlur_sbr(uv=uv2  ), U=uv2, V=uv2)
  227. \     : (r==1) ? mt_makediff(clp, clp.RemoveGrain(11, rg11), U=uv2, V=uv2)
  228. \     : (r==2) ? mt_makediff(clp, clp.RemoveGrain(11, rg11).RemoveGrain(20, rg20), U=uv2, V=uv2)
  229. \     :          mt_makediff(clp, clp.RemoveGrain(11, rg11).RemoveGrain(20, rg20).RemoveGrain(20, rg20), U=uv2, V=uv2)
  230. RG4D  = (r<=1) ? mt_makediff(clp, clp.RemoveGrain(4,  rg4 ), U=uv2, V=uv2)
  231. \     : (r==2) ? mt_makediff(clp, clp.Quantile(radius_y=2, radius_u=uvm2, radius_v=uvm2), U=uv2, V=uv2)
  232. \     :          mt_makediff(clp, clp.Quantile(radius_y=3, radius_u=uvm3, radius_v=uvm3), U=uv2, V=uv2)
  233.  
  234. DD    = mt_lutxy(RG11D, RG4D, "x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?", U=uv2, V=uv2)
  235.  
  236. return clp.mt_makediff(DD, U=uv, V=uv)
  237.  
  238.  
  239.   Function MinBlur_sbr(clip c, int "uv") {
  240.   uv     = Default(uv, 1)
  241.   uv2    = (uv==2) ? 1  : uv
  242.   rg11   = (uv==3) ? 11 : -1
  243.   rg11D  = mt_makediff(c, c.RemoveGrain(11, rg11), U=uv2, V=uv2)
  244.   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)
  245.  
  246.   return c.mt_makediff(rg11DD, U=uv, V=uv)
  247.   }
  248. }
  249.  
  250.  
  251. Function GSMC_MinBlur16(clip clp, int "mode", int "uv"){
  252.  
  253. mode  = Default(mode, 1)
  254. uv    = Default(uv,   3)
  255.  
  256. uv2   = (uv==2) ? 1  : uv
  257. rg4   = (uv==3) ? 4  : -1
  258. rg11  = (uv==3) ? 11 : -1
  259. rg20  = (uv==3) ? 20 : -1
  260.  
  261. RG11D = (mode<=1) ? Dither_sub16(clp, clp.Dither_removegrain16(11, rg11), U=uv2, V=uv2, dif=true)
  262. \     : (mode==2) ? Dither_sub16(clp, clp.Dither_removegrain16(20, rg20), U=uv2, V=uv2, dif=true)
  263. \     : (mode==3) ? Dither_sub16(clp, clp.MinBlur16_sbr16     (uv=uv2  ), U=uv2, V=uv2, dif=true)
  264. \     : (mode==4) ? Dither_sub16(clp, clp.Dither_removegrain16(11, rg11).Dither_removegrain16(20, rg20), U=uv2, V=uv2, dif=true)
  265. \     :             Dither_sub16(clp, clp.Dither_removegrain16(11, rg11).Dither_removegrain16(20, rg20)
  266. \                                        .Dither_removegrain16(20, rg20), U=uv2, V=uv2, dif=true)
  267. RG4D  = (mode<=3) ? Dither_sub16(clp, clp.Dither_removegrain16(4,  rg4 ), U=uv2, V=uv2, dif=true)
  268. \     : (mode==4) ? Dither_sub16(clp, clp.Dither_median16(2, 2, 0, U=uv2, V=uv2), U=uv2, V=uv2, dif=true)
  269. \     :             Dither_sub16(clp, clp.Dither_median16(3, 3, 0, U=uv2, V=uv2), U=uv2, V=uv2, dif=true)
  270.  
  271. DD    = MinBlur16_limitdiff16(RG11D, RG4D, U=uv2, V=uv2)
  272.  
  273. return clp.Dither_sub16(DD, U=uv, V=uv, dif=true)
  274.  
  275.  
  276.   Function MinBlur16_sbr16(clip clp, int "uv") {
  277.   uv       = Default(uv, 1)
  278.   uv2      = (uv==2) ? 1  : uv
  279.   rg11     = (uv==3) ? 11 : -1
  280.  
  281.   rg11D    = Dither_sub16(clp, clp.Dither_removegrain16(11, rg11), U=uv2, V=uv2, dif=true)
  282.   rg11Dr   = rg11D.Dither_removegrain16(11, rg11)
  283.  
  284.   abrg11D  = rg11D.Dither_lut16("x 32768 - abs", U=uv2, V=uv2)
  285.   Ddiff    = Dither_sub16(rg11D, rg11Dr, U=uv2, V=uv2, dif=true)
  286.   abDdiff  = Ddiff.Dither_lut16("x 32768 - abs", U=uv2, V=uv2)
  287.   abDDD    = Dither_sub16(abDdiff, abrg11D, U=uv2, V=uv2, dif=true)
  288.  
  289.   Dmask1   = abDDD.Dither_lut16("x 32768 < 65535 0 ?", U=uv2, V=uv2)
  290.   Ddiffg   = Ddiff.Dither_lut16("x 32768 == x x 32768 < 0 65535 ? ?", U=uv2, V=uv2).Crop(0, 0, 0, -Ddiff.height()/2)
  291.   rg11Dg   = rg11D.Dither_lut16("x 32768 == x x 32768 < 0 65535 ? ?", U=uv2, V=uv2).Crop(0, 0, 0, -rg11D.height()/2)
  292.   Dmask2   = mt_lutxy(Ddiffg, rg11Dg, "x 128 - y 128 - * 0 < 0 255 ?", U=uv2, V=uv2)
  293.  
  294.   DD1      = Dither_merge16(rg11D, Ddiff, Dmask1, luma=false, U=uv2, V=uv2)
  295.   DD2      = Dither_merge16_8(DD1.MinBlur16_gen_null_diff(), DD1, Dmask2, luma=false, U=uv2, V=uv2)
  296.  
  297.   return clp.Dither_sub16(DD2, U=uv, V=uv, dif=true)
  298.   }
  299.  
  300.  
  301.   Function MinBlur16_limitdiff16(clip diff1, clip diff2, int "Y", int "U", int "V")
  302.   {
  303.   Y          = Default(Y,        3)
  304.   U          = Default(U,        3)
  305.   V          = Default(V,        3)
  306.  
  307.   abdiff1    = Dither_lut16(diff1, "x 32768 - abs", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  308.   abdiff2    = Dither_lut16(diff2, "x 32768 - abs", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  309.   abdiffdiff = Dither_sub16(abdiff1, abdiff2, Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1, dif=true)
  310.   bin        = Dither_lut16(abdiffdiff, "x 32768 <= 0 65535 ?", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  311.  
  312.   return Dither_merge16(diff1, diff2, bin, luma=false, Y=Y, U=U, V=V)
  313.   }
  314.  
  315.  
  316.   Function MinBlur16_gen_null_diff(clip input, bool "lsb_in")
  317.   {
  318.   lsb_in = Default(lsb_in, true)
  319.    
  320.   vers   = VersionNumber ()
  321.   p_t    = (vers < 2.60) ? "YV12" : Dither_undef ()
  322.    
  323.   input    = lsb_in ? input.Crop(0, 0, 0, -input.height/2) : input
  324.    
  325.   StackVertical(BlankClip(input, pixel_type=p_t, color_yuv=8421504), BlankClip(input, pixel_type=p_t, color_yuv=0))
  326.   }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement