Advertisement
mawen1250

HQDering mod v0.6

Nov 24th, 2012
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ######
  2. ###
  3. ### HQDering mod v0.6 by mawen1250
  4. ###
  5. ### Requirements: masktools v2.0a48, RemoveGrain v1.0pre, dfttest v1.9.2, dither v1.21.0
  6. ###
  7. ### Applies deringing by using a smart smoother near edges (where ringing occurs) only.
  8. ###
  9. ######
  10.  
  11. Function HQDeringmod(clip input, clip "smoothed", int "Y", int "U", int "V", int "mthr", int "mrad", float "sigma", float "sigma2", int "sbsize", int "sosize", bool "sharp", int "drrep", bool "lsb_in", bool "lsb", int "dither") {
  12.  
  13.  HD       = input.width > 1024 || input.height > 576 ? true : false
  14.  
  15.  Y        = Default(Y,      3       )
  16.  U        = Default(U,      2       )
  17.  V        = Default(V,      2       )
  18.  
  19.  mthr     = Default(mthr,   HD?40:60)   # threshold of edge mask, lower  value means more aggressive processing
  20.  mrad     = Default(mrad,   2       )   # radius    of ring mask, higher value means more aggressive processing
  21.  
  22.  sigma    = Default(sigma,  64.0    )   # dfttest: sigma
  23.  sigma2   = Default(sigma2, sigma/8 )   # dfttest: sigma2
  24.  sbsize   = Default(sbsize, HD?6:4  )   # dfttest: length of the sides of the spatial window
  25.  sosize   = Default(sosize, HD?4:3  )   # dfttest: spatial overlap amount
  26.  
  27.  sharp    = Default(sharp,  false   )   # whether to use contra-sharpening to resharp deringed clip
  28.  drrep    = Default(drrep,  0       )   # use repair for details retention, recommended values are 13/12/1
  29.  
  30.  lsb_in   = Default(lsb_in, false   )   # input  clip is 16-bit stacked or not
  31.  lsb      = Default(lsb,    false   )   # output clip is 16-bit stacked or not
  32.  dither   = Default(dither, 6       )   # dither mode for 8-bit output
  33.  
  34.  Y        = min(Y, 3)
  35.  U        = min(U, 3)
  36.  V        = min(V, 3)
  37.  Yt       = Y == 3
  38.  Ut       = U == 3
  39.  Vt       = V == 3
  40.  Y31      = Yt ? 3 : 1
  41.  U31      = Ut ? 3 : 1
  42.  V31      = Vt ? 3 : 1
  43.  
  44.  input8   = lsb_in ? input.DitherPost(mode=0, Y=Y, U=U, V=V) : input
  45.  input16  = lsb_in ? input                                   : input.Dither_convert_8_to_16()
  46.  
  47.  smoothed = Defined(smoothed) ? lsb_in ? smoothed : smoothed.Dither_convert_8_to_16()
  48.  \        : input16.dfttest(Y=Yt, U=Ut, V=Vt, sigma=sigma, sigma2=sigma2, sbsize=sbsize, sosize=sosize, tbsize=1, lsb_in=true, lsb=true)
  49.  
  50.  pre           = smoothed .Dither_removegrain16(Yt ? 4  : -1, Ut ? 4  : -1, Vt ? 4  : -1)
  51.  method        = pre      .Dither_removegrain16(Yt ? 11 : -1, Ut ? 11 : -1, Vt ? 11 : -1)
  52.  sharpdiff     = pre      .Dither_sub16(method,   Y=Y31, U=U31, V=V31, dif=true)
  53.  allD          = input16  .Dither_sub16(smoothed, Y=Y31, U=U31, V=V31, dif=true)
  54.  ssDD          = sharpdiff.Dither_repair16(allD, Yt ? 1 : -1, Ut ? 1 : -1, Vt ? 1 : -1)
  55.  ssDD          = ssDD     .HQDeringmod_limitdiff16(sharpdiff, Y=Y31, U=U31, V=V31)
  56.  sclp16        = sharp     ?
  57.  \               smoothed .Dither_add16(ssDD,     Y=Y31, U=U31, V=V31, dif=true) : smoothed
  58.  sclp8         = sclp16   .DitherPost(mode=dither, Y=Y31, U=U31, V=V31)
  59.  
  60.  sclp16        =  lsb ? drrep > 0 ? input16.Dither_repair16(sclp16, Yt ? drrep : -1, Ut ? drrep : -1, Vt ? drrep : -1) : sclp16 : NOP()
  61.  sclp8         = !lsb ? drrep > 0 ? input8 .       Repair  (sclp8,  Yt ? drrep : -1, Ut ? drrep : -1, Vt ? drrep : -1) : sclp8  : NOP()
  62.  
  63.  prewittm      = input8.mt_edge("prewitt", mthr, 255, 0, 0, V=1, U=1)
  64.  fmask         = mt_hysteresis(prewittm.RemoveGrain(4, -1), prewittm, U=1, V=1)
  65.  omask         = mrad > 1 ? fmask.mt_expand(U=1, V=1) : fmask
  66.  omask         = mrad > 2 ? omask.mt_expand(U=1, V=1) : omask
  67.  omask         = mrad > 3 ? omask.mt_expand(U=1, V=1) : omask
  68.  omask         = mrad > 4 ? omask.mt_expand(U=1, V=1) : omask
  69.  imask         = fmask.mt_inflate(U=1, V=1).mt_inpand(U=1, V=1)
  70.  ringmaskl     = omask.mt_lutxy(imask, "x 255 y - * 255 /", U=1, V=1)
  71.  ringmask      = Ut || Vt ? ringmaskl.HQDeringmod_YtoYUV : ringmaskl
  72.  
  73.  return lsb ? Dither_merge16_8(input16, sclp16, ringmask, luma=false, Y=Y, U=U, V=V)
  74.  \          : mt_merge        (input8,  sclp8,  ringmask, luma=false, Y=U, U=U, V=V)
  75.  
  76. }
  77.  
  78.  
  79. Function HQDeringmod_limitdiff16(clip diff1, clip diff2, int "Y", int "U", int "V")
  80. {
  81. Y          = Default(Y,        3)
  82. U          = Default(U,        3)
  83. V          = Default(V,        3)
  84.  
  85. abdiff1    = Dither_lut16(diff1, "x 32768 - abs", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  86. abdiff2    = Dither_lut16(diff2, "x 32768 - abs", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  87. abdiffdiff = Dither_sub16(abdiff1, abdiff2, Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1, dif=true)
  88. bin        = Dither_lut16(abdiffdiff, "x 32768 <= 0 65535 ?", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  89.  
  90. return Dither_merge16(diff1, diff2, bin, luma=false, Y=Y, U=U, V=V)
  91. }
  92.  
  93.  
  94. Function HQDeringmod_YtoYUV(clip inputl, string "colorspace")
  95. {
  96.  sw          = inputl.width
  97.  sh          = inputl.height
  98.  wmod4       = (sw/4*4==sw) ? true : false
  99.  hmod4       = (sh/4*4==sh) ? true : false
  100.  
  101.  icolorspace = inputl.YtoYUV_GetCSP
  102.  ocolorspace = Defined(colorspace) ? colorspace : icolorspace
  103.  
  104.  try {
  105.    inputp = inputl.ConvertToY8
  106.    inputc = ocolorspace == "YV24" ? inputp
  107.    \      : ocolorspace == "YV16" ? inputp.BicubicResize(sw/2, sh  , -0.50)
  108.    \      :                         inputp.BicubicResize(sw/2, sh/2, -0.50)
  109.    
  110.    output = YtoUV(inputc, inputc, inputp)
  111.  
  112.  } catch ( error_msg ) {
  113.    inputp = wmod4&&hmod4 ? inputl : inputl.PointResize(wmod4?sw:sw+2, hmod4?sh:sh+2, 0, 0, wmod4?sw:sw+2, hmod4?sh:sh+2)
  114.    inputc = inputp.BicubicResize(sw/2, sh/2, -0.50)
  115.    
  116.    output = YtoUV(inputc, inputc, inputp)
  117.    output = wmod4&&hmod4 ? output : output.Crop(0, 0, wmod4?0:-2, hmod4?0:-2)
  118.  
  119.  }
  120.  
  121.  return output
  122.  
  123.  Function YtoYUV_GetCSP(clip c) {
  124.    return c.IsPlanar ? c.IsYV12 ? "YV12" :
  125.    \                   c.IsYV16 ? "YV16" :
  126.    \                   c.IsYV24 ? "YV24" : c.GetCSP_Y8_YV411 :
  127.    \      c.IsYUY2   ? "YUY2"   :
  128.    \      c.IsRGB32  ? "RGB32"  :
  129.    \      c.IsRGB24  ? "RGB24"  : "Unknown"
  130.  
  131.    Function GetCSP_Y8_YV411(clip c) {
  132.      try {
  133.        c.UtoY
  134.        csp = "YV411"
  135.      } catch ( error_msg ) {
  136.       csp = "Y8"
  137.      }
  138.      return csp
  139.    }
  140.  }
  141. }
  142.  
  143.  
  144. Function HQDeringmod_Spline36Resize(clip input, int "target_width", int "target_height", float "src_left", float "src_top", float "src_width", float "src_height", bool "chroma") {
  145.  
  146.  w             = input.width
  147.  h             = input.height
  148.  
  149.  target_width  = Default(target_width,      w)
  150.  target_height = Default(target_height,     h)
  151.  src_left      = Default(src_left,          0)
  152.  src_top       = Default(src_top,           0)
  153.  src_width     = Default(src_width,         w)
  154.  src_height    = Default(src_height,        h)
  155.  chroma        = Default(chroma,         true)
  156.  
  157.  try {
  158.    inputp = chroma ? input  : input .ConvertToY8
  159.    resize = inputp.Spline36Resize(target_width, target_height, src_left, src_top, src_width, src_height)
  160.    resize = chroma ? resize : resize.ConvertToYV12
  161.  } catch ( error_msg ) {
  162.    resize = input.Spline36Resize(target_width, target_height, src_left, src_top, src_width, src_height)
  163.  }
  164.  
  165.   return resize
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement