Advertisement
mawen1250

GrainStabilizeMC v0.3

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