Advertisement
Guest User

DebilinearM

a guest
Jul 20th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* DebilinearM v1.3.1 mod 1.2
  2.  
  3. DebilinearM is a wrapper function for the Debilinear and Debicubic plugins that masks parts of the frame that aren't upscaled,
  4. such as text overlays, and uses a regular resize kernel to downscale those areas. It works by downscaling the input
  5. clip to the target resolution with Debilinear or Debicubic, upscaling it again, comparing it to the original clip,
  6. and masking pixels that have a difference greater than the specified threshold.
  7.  
  8. ##### Requirements #####
  9.  
  10. Plugins:
  11.   Debicubic
  12.   Debilinear
  13.   Dither
  14.   MaskTools2
  15.  
  16. Scripts:
  17.   ResizeX
  18.  
  19. Color formats:
  20.   YV12
  21. */
  22.  
  23. function DebilinearM(clip input, int target_width, int target_height, int "thr", int "expand", int "inflate",
  24. \                    string "kernel", int "taps", float "a1", float "a2", bool "cubic", float "b", float "c",
  25. \                    bool "chroma", bool "lsb_inout", int "showmask", bool "DeResizeMT") {
  26.  
  27. # Default settings and other variable assignment
  28. thr       = Default(thr, 10)
  29. expand    = Default(expand, 1)
  30. inflate   = Default(inflate, 2)
  31. kernel    = Default(kernel, "Spline36")
  32. cubic     = Default(cubic, false)
  33. chroma    = Default(chroma, true)
  34. lsb_inout = Default(lsb_inout, false)
  35. DeRMT     = Default(DeResizeMT, false)
  36. showmask  = Default(showmask, 0)
  37.  
  38. Assert(target_width  > 0, "DebilinearM: target width must be greater than 0")
  39. Assert(target_height > 0, "DebilinearM: target height must be greater than 0")
  40. Assert(thr >= 0 && thr <= 255, "DebilinearM: thr must be in the range 0 to 255")
  41. Assert(showmask > -1 && showmask < 3, "DebilinearM: showmask must be 0, 1, or 2")
  42.  
  43. w = input.Width()
  44. h = lsb_inout ? input.Height()/2 : input.Height()
  45.  
  46. uvint = chroma ? 3
  47. \              : 1
  48.  
  49. # Resizing
  50. input_8bit = lsb_inout ? input.DitherPost(mode=-1) : input
  51.  
  52.     DeRMT ?
  53. \   Eval("""
  54.         dbi      = input.ResizeX(target_width,target_height, a1=b, a2=c, chroma=chroma, desampling=true, kernel=cubic ? "Bicubic" : "Bilinear", lsb_in=lsb_inout, lsb_out=lsb_inout)
  55.         dbi2     = dbi.ResizeX(w,h, kernel=cubic ? "Bicubic" : "Bilinear", a1=b, a2=c, chroma=chroma, lsb_in=lsb_inout, lsb_out=false, dither_mode=-1)
  56.         """)
  57. \ : cubic ?
  58. \   Eval("""
  59.         dbi      = chroma ? input.Debicubic(target_width,target_height, b=b, c=c, lsb_inout=lsb_inout)
  60.         \                 : input.DebicubicY(target_width,target_height, b=b, c=c, lsb_inout=lsb_inout)
  61.         dbi_8bit = chroma ? input_8bit.Debicubic(target_width,target_height, b=b, c=c)
  62.         \                 : input_8bit.DebicubicY(target_width,target_height, b=b, c=c)
  63.         dbi2     = lsb_inout ? dbi_8bit.ResizeX(w,h, kernel="Bicubic", a1=b, a2=c, chroma=chroma)
  64.         \                    : dbi.ResizeX(w,h, kernel="Bicubic", a1=b, a2=c, chroma=chroma)
  65.         """)
  66. \ : Eval("""
  67.         dbi      = chroma ? input.Debilinear(target_width,target_height, lsb_inout=lsb_inout)
  68.         \                 : input.DebilinearY(target_width,target_height, lsb_inout=lsb_inout)
  69.         dbi_8bit = chroma ? input_8bit.Debilinear(target_width,target_height)
  70.         \                 : input_8bit.DebilinearY(target_width,target_height)
  71.         dbi2     = lsb_inout ? dbi_8bit.ResizeX(w,h, kernel="Bilinear", chroma=chroma)
  72.         \                    : dbi.ResizeX(w,h, kernel="Bilinear", chroma=chroma)
  73.         """)
  74.  
  75. rs = input.ResizeX(target_width,target_height, kernel=kernel, taps=taps, a1=a1, a2=a2, chroma=chroma, lsb_in=lsb_inout, lsb_out=lsb_inout)
  76.  
  77. # Masking
  78. diffmask = mt_lutxy(input_8bit,dbi2, "x y - abs", use_expr=1, U=uvint, V=uvint).mt_binarize(threshold=thr, U=uvint, V=uvint)
  79. diffmask = showmask == 2 ? diffmask.DebilinearM_expand(expand=expand, U=uvint, V=uvint)
  80. \                                  .DebilinearM_inflate(inflate=inflate, U=uvint, V=uvint)
  81. \                        : diffmask.ResizeX(target_width,target_height, kernel="Bilinear", chroma=chroma)
  82. \                                  .mt_binarize(threshold=3, U=uvint, V=uvint)
  83. \                                  .DebilinearM_expand(expand=expand, U=uvint, V=uvint)
  84. \                                  .DebilinearM_inflate(inflate=inflate, U=uvint, V=uvint)
  85.  
  86. merged = lsb_inout ? Dither_merge16_8(dbi,rs,diffmask, u=uvint, v=uvint)
  87. \                  : mt_merge(dbi,rs,diffmask, U=uvint, V=uvint)
  88.  
  89. return showmask > 0 ? diffmask
  90. \                   : merged
  91. }
  92.  
  93. # DebicubicM alias
  94. function DebicubicM(clip input, int target_width, int target_height, int "thr", int "expand", int "inflate",
  95. \                   string "kernel", int "taps", float "a1", float "a2", float "b", float "c",
  96. \                   bool "chroma", bool "lsb_inout", int "showmask", bool "DeResizeMT") {
  97.  
  98. return DebilinearM(input,target_width,target_height,thr,expand,inflate,kernel,taps,a1,a2,true,b,c,chroma,lsb_inout,showmask,DeResizeMT)
  99. }
  100.  
  101.  
  102. # Helper functions
  103. function DebilinearM_expand(clip input, int "expand", int "U", int "V") {
  104. return expand > 0 ? DebilinearM_expand(input.mt_expand(mode="both", U=U, V=V), expand-1)
  105. \                 : input
  106. }
  107.  
  108. function DebilinearM_inflate(clip input, int "inflate", int "U", int "V") {
  109. return inflate > 0 ? DebilinearM_inflate(input.mt_inflate(U=U, V=V), inflate-1)
  110. \                  : input
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement