Advertisement
MonoS

Untitled

Dec 13th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.24 KB | None | 0 0
  1. import vapoursynth as vs
  2.  
  3. class InvalidArgument(Exception):
  4.     def __init__(self, value):
  5.         self.value = value
  6.     def __str__(self):
  7.         return repr(self.value)
  8.  
  9. class MonoS(object):
  10.     Gcutoff = 70
  11.     Ggain = 0.75
  12.    
  13.     #x < cutoff ? 0 : x * gain * (256+x) / 256
  14.     def f16(x):
  15.         if (x < Gcutoff):
  16.             return 0
  17.         else:
  18.             result = int(float(x) * Ggain * ((0xFFFF+1)+x) / (0xFFFF+1))
  19.             if result > 0xFFFF:
  20.                 return 0xFFFF
  21.             else:
  22.                 return result
  23.     def f8(x):
  24.         if (x < Gcutoff):
  25.             return 0
  26.         else:
  27.             result = int(float(x) * Ggain * ((0xFF+1)+x) / (0xFF+1))
  28.             if result > 0xFF:
  29.                 return 0xFF
  30.             else:
  31.                 return result
  32.    
  33.     def Luma16(x):
  34.         p = x << 4
  35.         if (p & (0xffff + 1)):
  36.             return (0xffff - (p&0xffff))
  37.         else:
  38.             return p&0xffff
  39.    
  40.     def MaskDetail16(clip, final_width, final_height, RGmode=3, cutoff=17990, gain=0.75, expandN=2, inflateN=1, blur_more=False, src_left=0, src_top=0, src_width=0, src_height=0, kernel="bilinear", invkstaps = 4, taps=4):
  41.        
  42.         core = vs.get_core()
  43.        
  44.         global Gcutoff
  45.         global Ggain
  46.  
  47.         Ggain = gain
  48.         Gcutoff = cutoff
  49.         clip = core.std.ShufflePlanes(clip, planes=0, colorfamily=vs.GRAY)
  50.         temp1 = core.fmtc.resample(clip, final_width, final_height, kernel=kernel, invks=True, invkstaps=invkstaps, taps=taps).fmtc.resample(clip.width, clip.height, kernel=kernel, taps=taps)
  51.         diff = core.std.MakeDiff(clip, temp1, 0)
  52.        
  53.         initial_mask = core.std.Lut(diff, function=MonoS.Luma16).rgvs.RemoveGrain(mode=[RGmode]).std.Lut(function=MonoS.f16)
  54.        
  55.         expanded = initial_mask
  56.         for i in range(0, expandN):
  57.             expanded = core.generic.Maximum(expanded, planes=[0])
  58.        
  59.         inflated = expanded
  60.         for i in range(0, inflateN):
  61.             inflated = core.generic.Inflate(inflated, planes=[0])
  62.        
  63.         final = core.fmtc.resample(inflated, final_width, final_height, src_left, src_top, src_width, src_height, taps=taps)
  64.        
  65.         if(blur_more):
  66.             final = core.rgvs.RemoveGrain(final, mode=[12,0,0])
  67.            
  68.         final = core.std.ShufflePlanes(final, planes=0, colorfamily=vs.GRAY)
  69.         return final
  70.  
  71.     def MaskDetail8(clip, final_width, final_height, RGmode=3, cutoff=70, gain=0.75, expandN=2, inflateN=1, blur_more=False, src_left=0, src_top=0, src_width=0, src_height=0, kernel="bilinear", invkstaps = 4, taps=4):
  72.        
  73.         core = vs.get_core()
  74.        
  75.         global Gcutoff
  76.         global Ggain
  77.  
  78.         Ggain = gain
  79.         Gcutoff = cutoff
  80.         clip = core.std.ShufflePlanes(clip, planes=0, colorfamily=vs.GRAY)
  81.         temp1 = core.fmtc.resample(clip, final_width, final_height, kernel=kernel, invks=True, invkstaps=invkstaps, taps=taps).fmtc.resample(clip.width, clip.height, kernel=kernel, taps=taps).fmtc.bitdepth(bits=8)
  82.         diff = core.std.MakeDiff(clip.fmtc.bitdepth(bits=8), temp1, 0)
  83.        
  84.         initial_mask = core.hist.Luma(diff).rgvs.RemoveGrain(mode=[RGmode]).std.Lut(function=MonoS.f8)
  85.        
  86.         expanded = initial_mask
  87.         for i in range(0, expandN):
  88.             expanded = core.generic.Maximum(expanded, planes=[0])
  89.        
  90.         inflated = expanded
  91.         for i in range(0, inflateN):
  92.             inflated = core.generic.Inflate(inflated, planes=[0])
  93.        
  94.         final = core.fmtc.resample(inflated, final_width, final_height, src_left, src_top, src_width, src_height, taps=taps).fmtc.bitdepth(bits=8)
  95.        
  96.         if(blur_more):
  97.             final = core.rgvs.RemoveGrain(final, mode=[12,0,0])
  98.            
  99.         final = core.std.ShufflePlanes(final, planes=0, colorfamily=vs.GRAY)
  100.         return final
  101.        
  102.     def MaskDetail(clip, final_width, final_height, RGmode=3, cutoff=0, gain=0.75, expandN=2, inflateN=1, blur_more=False, src_left=0, src_top=0, src_width=0, src_height=0, kernel="bilinear", invkstaps = 4, taps=4):
  103.         if ((clip.format.bits_per_sample == 8) or (clip.format.bits_per_sample == 16)):
  104.             if (clip.format.bits_per_sample == 8):
  105.                 if cutoff == 0:
  106.                     cutoff = 70
  107.                 return MonoS.MaskDetail8(clip, final_width, final_height, RGmode=3, cutoff=cutoff, gain=0.75, expandN=2, inflateN=1, blur_more=False, src_left=0, src_top=0, src_width=0, src_height=0, kernel="bilinear", invkstaps = 4, taps=4)
  108.             else:
  109.                 if cutoff == 0:
  110.                     cutoff = 17990
  111.                 return MonoS.MaskDetail16(clip, final_width, final_height, RGmode=3, cutoff=cutoff, gain=0.75, expandN=2, inflateN=1, blur_more=False, src_left=0, src_top=0, src_width=0, src_height=0, kernel="bilinear", invkstaps = 4, taps=4)
  112.         else:
  113.             raise InvalidArgument('Input clip must be 8 or 16 bit')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement