Advertisement
Zastin

luma adaptive debanding mask for AVS & VS (AVS fix 6-4-19)

Jul 18th, 2017
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. # meant as a faster version of the retinex-type deband mask
  2. # lo and hi are the cutoffs for luma
  3. # lothr and hithr are the Binarize thresholds of the mask at lo/hi luma levels
  4. # luma values falling between lo and hi are scaled linearly from lothr to hithr
  5.  
  6. ### Vapoursynth ###
  7. import vapoursynth as vs
  8. import havsfunc as haf
  9.  
  10. def debandmask(clip, lo=6144, hi=12288, lothr=320, hithr=384, mrad=2):
  11. core = vs.core
  12.  
  13. f = clip.format
  14. bits = f.bits_per_sample
  15. isINT = f.sample_type==vs.INTEGER
  16.  
  17. peak = (1 << bits) - 1 if isINT else 1
  18. clip = clip.std.ShufflePlanes(0, vs.GRAY)
  19.  
  20. ma = haf.mt_expand_multi(clip, mode='ellipse', sw=mrad, sh=mrad)
  21. mi = haf.mt_inpand_multi(clip, mode='ellipse', sw=mrad, sh=mrad)
  22.  
  23. rmask = core.std.Expr([ma, mi], 'x y -')
  24.  
  25. mexpr = 'x {lo} < y {lothr} >= {peak} 0 ? x {hi} > y {hithr} >= {peak} 0 ? y x {lo} - {r} / {tr} * {lothr} + >= {peak} 0 ? ? ?'.format(lo=lo, hi=hi, lothr=lothr, hithr=hithr, peak=peak, r=hi-lo, tr=hithr-lothr)
  26.  
  27. return core.std.Expr([clip, rmask], mexpr)
  28.  
  29.  
  30. ### Avisynth+ version, fixed by saiclabs from D-Z0N3 ###
  31. Function debandmask(clip c, int lo, int hi, int lothr, int hithr, int mrad, bool "stack16_in", bool "stack16_out")
  32. {
  33. stack16_in = Default(stack16_in, false)
  34. stack16_out = Default(stack16_out, false)
  35.  
  36. c
  37. Assert(lo >= 0 && hi >= 0 && lothr >= 0 && hithr >= 0, "lo/hi/lothr/hithr must be >= 0")
  38. Assert(lo <= 255 && hi <= 255 && lothr <= 255 && hithr <= 255, "lo/hi/lothr/hithr must be <= 255")
  39.  
  40. stack16_in == false ? last : ConvertFromStacked()
  41. s = last
  42. ExtractY()
  43.  
  44. rmask = Dither_build_gf3_range_mask (mrad)
  45.  
  46. mexpr = "x "+String(lo)+" scaleb < y "+String(lothr)+" scaleb >= range_max 0 ? x "+String(hi)+" scaleb > y "+String(hithr)+" scaleb >= range_max 0 ? y x "+String(lo)+" scaleb - "+String(hi)+" scaleb "+String(lo)+" scaleb - / "+String(hithr)+" scaleb "+String(lothr)+" scaleb - * "+String(lothr)+" scaleb + >= range_max 0 ? ? ?"
  47.  
  48. stack16_out ? BitsPerComponent(s) != 16 ? s.mt_lutxy(rmask, mexpr).ConvertBits(16).ConvertToStacked() : s.mt_lutxy(rmask, mexpr).ConvertToStacked() : s.mt_lutxy(rmask, mexpr)
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement