Advertisement
mawen1250

HQDering mod v1.0

Jan 11th, 2013
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 12.62 KB | None | 0 0
  1. ######
  2. ###
  3. ### HQDering mod v1.0 by mawen1250
  4. ###
  5. ### Requirements: masktools v2.0a48, RemoveGrain + Repair v1.0pre, RemoveGrainHD v0.5, dfttest v1.9.2, dither v1.22.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", int "nrmode", int "nrmodec", float "sigma", float "sigma2", int "sbsize", int "sosize", bool "sharp", int "drrep", bool "lsb_in", bool "lsb", int "dither", bool "show") {
  12.  
  13.  HD       = input.width >= 1280 || input.height >= 720 ? true : false
  14.  
  15.  Y        = Default(Y,      3       )
  16.  U        = Default(U,      2       )
  17.  V        = Default(V,      2       )
  18.  
  19.  lsb_in   = Default(lsb_in, false   )   # input  clip is 16-bit stacked or not
  20.  lsb      = Default(lsb,    false   )   # output clip is 16-bit stacked or not
  21.  dither   = Default(dither, 6       )   # dither mode for 8-bit output
  22.  
  23.  mthr     = Default(mthr,   60      )   # threshold of edge mask, lower  value means more aggressive processing
  24.  mrad     = Default(mrad,   2       )   # radius    of ring mask, higher value means more aggressive processing
  25.  
  26.  nrmode   = Default(nrmode, HD?2:1  )   # kernel of dering - 0: dfttest, 1: MinBlur(radius=1), 2: MinBlur(radius=2), 3: MinBlur(radius=3)
  27.  nrmodec  = Default(nrmodec,nrmode  )   # kernel of dering for chroma
  28.  sigma    = Default(sigma,  128.0   )   # dfttest: sigma for medium frequecies
  29.  sigma2   = Default(sigma2, sigma/16.0)   # dfttest: sigma for low&high frequecies
  30.  sbsize   = Default(sbsize, HD?8:6  )   # dfttest: length of the sides of the spatial window
  31.  sosize   = Default(sosize, HD?6:4  )   # dfttest: spatial overlap amount
  32.  sigma    = string (sigma           )
  33.  sigma2   = string (sigma2          )
  34.  
  35.  sharp    = Default(sharp,  false   )   # whether to use contra-sharpening to resharp deringed clip
  36.  drrep    = Default(drrep,  nrmode>0?lsb?13:24:0)   # use repair for details retention, recommended values are 24/23(only for lsb=false)/13/12/1
  37.  
  38.  show     = Default(show,   false   )   # whether to output mask clip instead of filtered clip
  39.  
  40.  Y        = min(Y, 3)
  41.  U        = min(U, 3)
  42.  V        = min(V, 3)
  43.  Yt       = Y == 3
  44.  Ut       = U == 3
  45.  Vt       = V == 3
  46.  Y31      = Yt ? 3 : 1
  47.  U31      = Ut ? 3 : 1
  48.  V31      = Vt ? 3 : 1
  49.  
  50.  input8   = lsb_in ? input.DitherPost(mode=lsb?-1:dither, Y=3, U=3, V=3) : input
  51.  input16  = lsb_in ? input                                               : input.Dither_convert_8_to_16()
  52.  
  53.  smoothc  = nrmodec <= 0 ? input.dfttest(Y=false, U=Ut, V=Vt, sbsize=sbsize, sosize=sosize, tbsize=1,
  54.  \                               sstring="0.0:"+sigma2+" 0.05:"+sigma+" 0.5:"+sigma+" 0.75:"+sigma2+" 1.0:0.0", lsb_in=lsb_in, lsb=lsb)
  55.  \                       : input.HQDeringmod_MinBlur(nrmodec, Y=1, U=U, V=V, lsb_in=lsb_in, lsb=lsb, dither=dither)
  56.  smoothed = Defined(smoothed) ? lsb_in ? lsb ? smoothed : smoothed.DitherPost(mode=dither) : lsb ? smoothed.Dither_convert_8_to_16() : smoothed : nrmode <= 0 ? input.dfttest(Y=Yt, U=(nrmode==nrmodec||!(Ut||Vt))?Ut:false, V=(nrmode==nrmodec||!(Ut||Vt))?Vt:false,
  57.  \                                      sbsize=sbsize, sosize=sosize, tbsize=1, lsb_in=lsb_in, lsb=lsb,
  58.  \                                      sstring="0.0:"+sigma2+" 0.05:"+sigma+" 0.5:"+sigma+" 0.75:"+sigma2+" 1.0:0.0")
  59.  \                      : input.HQDeringmod_MinBlur(nrmode, lsb_in=lsb_in, lsb=lsb, dither=dither,
  60.  \                                                  Y=Y, U=(nrmode==nrmodec||!(Ut||Vt))?U:1, V=(nrmode==nrmodec||!(Ut||Vt))?V:1)
  61.  smoothed = nrmode == nrmodec || !(Ut || Vt) ? smoothed : smoothed.MergeChroma(smoothc)
  62.  
  63.  pre           = lsb ? smoothed .Dither_removegrain16(Yt ? 4  : -1, Ut ? 4  : -1, Vt ? 4  : -1)
  64.  \                   : smoothed .         RemoveGrain(Yt ? 4  : -1, Ut ? 4  : -1, Vt ? 4  : -1)
  65.  method        = lsb ? pre      .Dither_removegrain16(Yt ? 11 : -1, Ut ? 11 : -1, Vt ? 11 : -1)
  66.  \                   : pre      .         RemoveGrain(Yt ? 11 : -1, Ut ? 11 : -1, Vt ? 11 : -1)
  67.  sharpdiff     = lsb ? pre      .Dither_sub16(method,   Y=Y31, U=U31, V=V31, dif=true)
  68.  \                   : pre      . mt_makediff(method,   Y=Y31, U=U31, V=V31)
  69.  allD          = lsb ? input16  .Dither_sub16(smoothed, Y=Y31, U=U31, V=V31, dif=true)
  70.  \                   : input8   . mt_makediff(smoothed, Y=Y31, U=U31, V=V31)
  71.  ssDD          = lsb ? sharpdiff.Dither_repair16(allD, Yt ? 1 : -1, Ut ? 1 : -1, Vt ? 1 : -1)
  72.  \                   : sharpdiff.         Repair(allD, Yt ? 1 : -1, Ut ? 1 : -1, Vt ? 1 : -1)
  73.  ssDD          = lsb ? ssDD     .HQDeringmod_limitdiff16(sharpdiff, Y=Y31, U=U31, V=V31)
  74.  \                   : ssDD     .mt_lutxy(sharpdiff, "x 128 - abs y 128 - abs <= x y ?", Y=Y31, U=U31, V=V31)
  75.  sclp          = !sharp ? smoothed :
  76.  \               lsb ? smoothed .Dither_add16(ssDD,     Y=Y31, U=U31, V=V31, dif=true)
  77.  \                   : smoothed .  mt_adddiff(ssDD,     Y=Y31, U=U31, V=V31)
  78.  
  79.  repclp        = drrep <= 0 ? sclp
  80.  \                          : lsb ? input16.Dither_repair16(sclp, Yt ? drrep : -1, Ut ? drrep : -1, Vt ? drrep : -1)
  81.  \                                : input8 .         Repair(sclp, Yt ? drrep : -1, Ut ? drrep : -1, Vt ? drrep : -1)
  82.  
  83.  prewittm      = input8.mt_edge("prewitt", mthr, 255, 0, 0, V=1, U=1)
  84.  fmask         = mt_hysteresis(prewittm.RemoveGrain(4, -1), prewittm, U=1, V=1)
  85.  omask         = mrad > 1 ? fmask.mt_expand(U=1, V=1) : fmask
  86.  omask         = mrad > 2 ? omask.mt_expand(U=1, V=1) : omask
  87.  omask         = mrad > 3 ? omask.mt_expand(U=1, V=1) : omask
  88.  omask         = mrad > 4 ? omask.mt_expand(U=1, V=1) : omask
  89.  imask         = fmask.mt_inflate(U=1, V=1).mt_inpand(U=1, V=1)
  90.  ringmaskl     = omask.mt_lutxy(imask, "x 255 y - * 255 /", U=1, V=1)
  91.  ringmask      = Ut || Vt ? ringmaskl.HQDeringmod_YtoYUV : ringmaskl
  92.  
  93.  return show ? ringmask
  94.  \           : lsb ? Dither_merge16_8(input16, repclp, ringmask, luma=false, Y=Y, U=U, V=V)
  95.  \                 : mt_merge        (input8,  repclp, ringmask, luma=false, Y=Y, U=U, V=V)
  96.  
  97. }
  98.  
  99.  
  100. Function HQDeringmod_MinBlur(clip clp, int "r", int "Y", int "U", int "V", bool "lsb_in", bool "lsb", int "dither"){
  101.  
  102. r     = Default(r,    1)
  103. Y     = Default(Y,    3)
  104. U     = Default(U,    3)
  105. V     = Default(V,    3)
  106.  
  107. Y2    = (Y==2) ? 1  :  Y
  108. Y4    = (Y==3) ? 4  : -1
  109. Y11   = (Y==3) ? 11 : -1
  110. Y20   = (Y==3) ? 20 : -1
  111. Ym2   = (Y==3) ? 2  : Y==2 ? 0 : -1
  112. Ym3   = (Y==3) ? 3  : Y==2 ? 0 : -1
  113. U2    = (U==2) ? 1  :  U
  114. U4    = (U==3) ? 4  : -1
  115. U11   = (U==3) ? 11 : -1
  116. U20   = (U==3) ? 20 : -1
  117. Um2   = (U==3) ? 2  : U==2 ? 0 : -1
  118. Um3   = (U==3) ? 3  : U==2 ? 0 : -1
  119. V2    = (V==2) ? 1  :  V
  120. V4    = (V==3) ? 4  : -1
  121. V11   = (V==3) ? 11 : -1
  122. V20   = (V==3) ? 20 : -1
  123. Vm2   = (V==3) ? 2  : V==2 ? 0 : -1
  124. Vm3   = (V==3) ? 3  : V==2 ? 0 : -1
  125.  
  126. lsb_in   = Default(lsb_in,   false)
  127. lsb      = Default(lsb,      false)
  128. dither   = Default(dither,       6)
  129.  
  130. clp8  = lsb_in ? clp.DitherPost(mode=dither, Y=3, U=3, V=3) : clp
  131. clp16 = lsb_in ? clp                                        : clp.Dither_convert_8_to_16()
  132.  
  133. RG11  = (r<=1) ? lsb ? clp16.Dither_removegrain16(Y11, U11, V11)
  134. \                    : clp8 .         RemoveGrain(Y11, U11, V11)
  135. \     : (r==2) ? lsb ? clp16.Dither_removegrain16(Y11, U11, V11).Dither_removegrain16(Y20, U20, V20)
  136. \                    : clp8 .         RemoveGrain(Y11, U11, V11).         RemoveGrain(Y20, U20, V20)
  137. \     :          lsb ? clp16.Dither_removegrain16(Y11, U11, V11).Dither_removegrain16(Y20, U20, V20).Dither_removegrain16(Y20, U20, V20)
  138. \                    : clp8 .         RemoveGrain(Y11, U11, V11).         RemoveGrain(Y20, U20, V20).         RemoveGrain(Y20, U20, V20)
  139.  
  140. RG4   = (r<=1) ? lsb_in&&lsb ? clp16.Dither_removegrain16(Y4, U4, V4)
  141. \                            : clp8 .RemoveGrain(Y4, U4, V4)
  142. \     : (r==2) ? lsb_in&&lsb ? clp16.Dither_median16(2, 2, 0, Y=Y2, U=U2, V=V2)
  143. \                            : clp8 .Quantile(radius_y=Ym2, radius_u=Um2, radius_v=Vm2)
  144. \     :          lsb_in&&lsb ? clp16.Dither_median16(3, 3, 0, Y=Y2, U=U2, V=V2)
  145. \                            : clp8 .Quantile(radius_y=Ym3, radius_u=Um3, radius_v=Vm3)
  146. RG4   = !lsb_in&&lsb ? RG4  .Dither_convert_8_to_16() : RG4
  147.  
  148. RG11D = lsb ? NOP() : mt_makediff(clp8,  RG11, Y=Y2, U=U2, V=V2)
  149. RG4D  = lsb ? NOP() : mt_makediff(clp8,  RG4,  Y=Y2, U=U2, V=V2)
  150.  
  151. DD    = lsb ? NOP() : mt_lutxy(RG11D, RG4D, "x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?", Y=Y2, U=U2, V=V2)
  152. end   = lsb ? HQDeringmod_min_dif16(RG11, RG4, clp16, Y=Y, U=U, V=V)
  153. \           : clp8.mt_makediff(DD, Y=Y, U=U, V=V)
  154.  
  155. return end
  156. }
  157.  
  158.  
  159. Function HQDeringmod_limitdiff16(clip diff1, clip diff2, int "Y", int "U", int "V")
  160. {
  161. Y          = Default(Y,        3)
  162. U          = Default(U,        3)
  163. V          = Default(V,        3)
  164.  
  165. sh         = diff1.height/2
  166.  
  167. nulldiff   = diff1.HQDeringmod_gen_null_diff(lsb_in=true)
  168. maxdif     = Dither_max_dif16(diff1, diff2, nulldiff, Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  169. bin_stack  = mt_lutxy(diff1, maxdif, "x y == 255 0 ?", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  170. bin_msb    = bin_stack.Crop(0, 0, 0, -sh)
  171. bin_lsb    = bin_stack.Crop(0, +sh, 0, 0)
  172. bin        = mt_logic(bin_msb, bin_lsb, "min", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  173.  
  174. return Dither_merge16_8(diff1, diff2, bin, luma=false, Y=Y, U=U, V=V)
  175. }
  176.  
  177.  
  178. Function HQDeringmod_min_dif16(clip src1, clip src2, clip ref, int "Y", int "U", int "V")
  179. {
  180. Y          = Default(Y,        3)
  181. U          = Default(U,        3)
  182. V          = Default(V,        3)
  183.  
  184. sh         = ref.height/2
  185.  
  186. maxdif     = Dither_max_dif16(src1, src2, ref, Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  187. bin_stack  = mt_lutxy(src1, maxdif, "x y == 255 0 ?", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  188. bin_msb    = bin_stack.Crop(0, 0, 0, -sh)
  189. bin_lsb    = bin_stack.Crop(0, +sh, 0, 0)
  190. bin        = mt_logic(bin_msb, bin_lsb, "min", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  191.  
  192. return Dither_merge16_8(src1, src2, bin, luma=false, Y=Y, U=U, V=V)
  193. }
  194.  
  195.  
  196. Function HQDeringmod_gen_null_diff(clip input, bool "lsb_in")
  197. {
  198.     lsb_in = Default(lsb_in, true)
  199.    
  200.     vers   = VersionNumber ()
  201.     p_t    = (vers < 2.60) ? "YV12" : Dither_undef ()
  202.    
  203.     input    = lsb_in ? input.Crop(0, 0, 0, -input.height/2) : input
  204.    
  205.     StackVertical(BlankClip(input, pixel_type=p_t, color_yuv=8421504), BlankClip(input, pixel_type=p_t, color_yuv=0))
  206. }
  207.  
  208.  
  209. Function HQDeringmod_YtoYUV(clip inputl, string "colorspace")
  210. {
  211.  sw          = inputl.width
  212.  sh          = inputl.height
  213.  wmod4       = (sw/4*4==sw) ? true : false
  214.  hmod4       = (sh/4*4==sh) ? true : false
  215.  
  216.  icolorspace = inputl.YtoYUV_GetCSP
  217.  ocolorspace = Defined(colorspace) ? colorspace : icolorspace
  218.  
  219.  try {
  220.    inputp = inputl.ConvertToY8
  221.    inputc = ocolorspace == "YV24" ? inputp
  222.    \      : ocolorspace == "YV16" ? inputp.BicubicResize(sw/2, sh  , -0.50)
  223.    \      :                         inputp.BicubicResize(sw/2, sh/2, -0.50)
  224.    
  225.    output = YtoUV(inputc, inputc, inputp)
  226.  
  227.  } catch ( error_msg ) {
  228.    inputp = wmod4&&hmod4 ? inputl : inputl.PointResize(wmod4?sw:sw+2, hmod4?sh:sh+2, 0, 0, wmod4?sw:sw+2, hmod4?sh:sh+2)
  229.    inputc = inputp.BicubicResize(sw/2, sh/2, -0.50)
  230.    
  231.    output = YtoUV(inputc, inputc, inputp)
  232.    output = wmod4&&hmod4 ? output : output.Crop(0, 0, wmod4?0:-2, hmod4?0:-2)
  233.  
  234.  }
  235.  
  236.  return output
  237.  
  238.  Function YtoYUV_GetCSP(clip c) {
  239.    return c.IsPlanar ? c.IsYV12 ? "YV12" :
  240.    \                   c.IsYV16 ? "YV16" :
  241.    \                   c.IsYV24 ? "YV24" : c.GetCSP_Y8_YV411 :
  242.    \      c.IsYUY2   ? "YUY2"   :
  243.    \      c.IsRGB32  ? "RGB32"  :
  244.    \      c.IsRGB24  ? "RGB24"  : "Unknown"
  245.  
  246.    Function GetCSP_Y8_YV411(clip c) {
  247.      try {
  248.        c.UtoY
  249.        csp = "YV411"
  250.      } catch ( error_msg ) {
  251.       csp = "Y8"
  252.      }
  253.      return csp
  254.    }
  255.  }
  256. }
  257.  
  258.  
  259. 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") {
  260.  
  261.  w             = input.width
  262.  h             = input.height
  263.  
  264.  target_width  = Default(target_width,      w)
  265.  target_height = Default(target_height,     h)
  266.  src_left      = Default(src_left,          0)
  267.  src_top       = Default(src_top,           0)
  268.  src_width     = Default(src_width,         w)
  269.  src_height    = Default(src_height,        h)
  270.  chroma        = Default(chroma,         true)
  271.  
  272.  try {
  273.    inputp = chroma ? input  : input .ConvertToY8
  274.    resize = inputp.Spline36Resize(target_width, target_height, src_left, src_top, src_width, src_height)
  275.    resize = chroma ? resize : resize.ConvertToYV12
  276.  } catch ( error_msg ) {
  277.    resize = input.Spline36Resize(target_width, target_height, src_left, src_top, src_width, src_height)
  278.  }
  279.  
  280.   return resize
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement