Advertisement
mawen1250

HQDering mod v0.4

Oct 25th, 2012
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ######
  2. ###
  3. ### HQDering mod v0.4 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 "mthr", int "mrad", float "sigma", float "sigma2", int "sbsize", int "sosize", bool "sharp", int "drrep", bool "lsb_in", bool "lsb", int "dither") {
  12.  
  13.  mthr     = Default(mthr,   80     )   # threshold of edge mask, lower  value means more aggressive processing
  14.  mrad     = Default(mrad,   2      )   # radius    of ring mask, higher value means more aggressive processing
  15.  
  16.  sigma    = Default(sigma,  64.0   )   # dfttest: sigma
  17.  sigma2   = Default(sigma2, sigma/8)   # dfttest: sigma2
  18.  sbsize   = Default(sbsize, 4      )   # dfttest: length of the sides of the spatial window
  19.  sosize   = Default(sosize, 3      )   # dfttest: spatial overlap amount
  20.  
  21.  sharp    = Default(sharp,  false  )   # whether to use contra-sharpening to resharp deringed clip
  22.  drrep    = Default(drrep,  0      )   # use repair for details retention, recommended values are 13/12/1
  23.  
  24.  lsb_in   = Default(lsb_in, false  )   # input  clip is 16-bit stacked or not
  25.  lsb      = Default(lsb,    false  )   # output clip is 16-bit stacked or not
  26.  dither   = Default(dither, 6      )   # dither mode for 8-bit output
  27.  
  28.  input8   = lsb_in ? input.DitherPost(mode=0) : input
  29.  input16  = lsb_in ? input                    : input.Dither_convert_8_to_16()
  30.  
  31.  smoothed = Defined(smoothed) ? lsb_in ? smoothed : smoothed.Dither_convert_8_to_16()
  32.  \        : input16.dfttest(Y=true, U=false, V=false, sigma=sigma, sigma2=sigma2, sbsize=sbsize, sosize=sosize, tbsize=1, lsb_in=true, lsb=true)
  33.  
  34.  pre           = smoothed .Dither_removegrain16(4 , -1)
  35.  method        = pre      .Dither_removegrain16(11, -1)
  36.  sharpdiff     = pre      .Dither_sub16(method, U=1, V=1, dif=true)
  37.  allD          = input16  .Dither_sub16(smoothed, U=1, V=1, dif=true)
  38.  ssDD          = sharpdiff.Dither_repair16(allD, 1, -1)
  39.  ssDD          = ssDD     .HQDeringmod_limitdiff16(sharpdiff, U=1, V=1)
  40.  sclp16        = sharp     ?
  41.  \               smoothed .Dither_add16(ssDD, U=1, V=1, dif=true) : smoothed
  42.  sclp8         = sclp16   .DitherPost(mode=dither)
  43.  
  44.  sclp16        =  lsb ? drrep > 0 ? input16.Dither_repair16(sclp16, drrep, -1) : sclp16 : NOP()
  45.  sclp8         = !lsb ? drrep > 0 ? input8 .       Repair  (sclp8,  drrep, -1) : sclp8  : NOP()
  46.  
  47.  prewittm      = input8.mt_edge("prewitt", mthr, 255, 0, 0, V=1, U=1)
  48.  fmask         = mt_hysteresis(prewittm.RemoveGrain(4, -1), prewittm, U=1, V=1)
  49.  omask         = mrad > 1 ? fmask.mt_expand(U=1, V=1) : fmask
  50.  omask         = mrad > 2 ? omask.mt_expand(U=1, V=1) : omask
  51.  omask         = mrad > 3 ? omask.mt_expand(U=1, V=1) : omask
  52.  omask         = mrad > 4 ? omask.mt_expand(U=1, V=1) : omask
  53.  imask         = fmask.mt_inflate(U=1, V=1).mt_inpand(U=1, V=1)
  54.  ringmask      = omask.mt_lutxy(imask, "x 255 y - * 255 /", U=1, V=1)
  55.  
  56.  return lsb ? Dither_merge16_8(input16, sclp16, ringmask, luma=false, Y=3, U=2, V=2)
  57.  \          : mt_merge        (input8,  sclp8,  ringmask, luma=false, Y=3, U=2, V=2)
  58.  
  59. }
  60.  
  61. Function HQDeringmod_limitdiff16(clip diff1, clip diff2, int "Y", int "U", int "V")
  62. {
  63. Y          = Default(Y,        3)
  64. U          = Default(U,        3)
  65. V          = Default(V,        3)
  66.  
  67. abdiff1    = Dither_lut16(diff1, "x 32768 - abs", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  68. abdiff2    = Dither_lut16(diff2, "x 32768 - abs", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  69. abdiffdiff = Dither_sub16(abdiff1, abdiff2, Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1, dif=true)
  70. bin        = Dither_lut16(abdiffdiff, "x 32768 <= 0 65535 ?", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  71.  
  72. return Dither_merge16(diff1, diff2, bin, luma=false, Y=Y, U=U, V=V)
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement