Advertisement
MonoS

MaskDetail v2

Dec 14th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.57 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 = 17990
  11.     Ggain = 0.75
  12.     Glowpassthr = 1542
  13.     Gpclevelthr = 59881
  14.    
  15.     #x < cutoff ? 0 : x * gain * (256+x) / 256
  16.     def f16(x):
  17.         if (x < Gcutoff):
  18.             return 0
  19.         else:
  20.             result = int(float(x) * Ggain * ((0xFFFF+1)+x) / (0xFFFF+1))
  21.             if result > 0xFFFF:
  22.                 return 0xFFFF
  23.             else:
  24.                 return result
  25.    
  26.     def Luma16(x):
  27.         p = x << 4
  28.         if (p & (0xffff + 1)):
  29.             return (0xffff - (p&0xffff))
  30.         else:
  31.             return p&0xffff
  32.    
  33.     def LowPassLUT16(x):
  34.         p = x - 0x1000
  35.         if (p > 0):
  36.             if (p - Glowpassthr > 0):
  37.                 return x - Glowpassthr
  38.             else:
  39.                 return 0x7FFF
  40.         else:
  41.             if (p + Glowpassthr < 0):
  42.                 return x + Glowpassthr
  43.             else:
  44.                 return 0x7FFF
  45.    
  46.     def PCLevelLUT(x):
  47.         if (x > Gpclevelthr):
  48.             return x
  49.         else:
  50.             return 0
  51.    
  52.     def MaskDetail(clip, final_width, final_height, RGmode=3, cutoff=-1, 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, mode="normal", lowpasskernel="blackman", lowpassintaps=4, lowpassouttaps=3, lowpassthr=-1, exportlowpass=False, pclevelthr=-1):
  53.        
  54.         core = vs.get_core()       
  55.        
  56.         if ((clip.format.bits_per_sample == 8) or (clip.format.bits_per_sample == 16)):
  57.             if (clip.format.bits_per_sample == 8):
  58.                 if cutoff == -1:
  59.                     cutoff = 17990
  60.                 else:
  61.                     cutoff = cutoff * 65535/255
  62.                
  63.                 if lowpassthr == -1:
  64.                     lowpassthr = 1542
  65.                 else:
  66.                     lowpassthr = lowpassthr * 65535/255
  67.                
  68.                 if pclevelthr == -1:
  69.                     pclevelthr = 59881
  70.                 else:
  71.                     pclevelthr = pclevelthr * 65535/255
  72.                    
  73.                
  74.                 startclip = core.fmtc.bitdepth(clip, bits=16)
  75.             else:
  76.                 if cutoff == -1:
  77.                     cutoff = 17990
  78.                
  79.                 if lowpassthr == -1:
  80.                     lowpassthr = 1542
  81.  
  82.                 if pclevelthr == -1:
  83.                     pclevelthr = 59881             
  84.                
  85.                 startclip = clip
  86.         else:
  87.             raise InvalidArgument('Input clip must be 8 or 16 bit')
  88.        
  89.         global Gcutoff
  90.         global Ggain
  91.         global Glowpassthr
  92.         global Gpclevelthr
  93.         Ggain = gain
  94.         Gcutoff = cutoff
  95.         Glowpassthr = lowpassthr
  96.         Gpclevelthr = pclevelthr
  97.        
  98.         startclip = core.std.ShufflePlanes(startclip, planes=0, colorfamily=vs.GRAY)   
  99.        
  100.         if((mode == "lowpass") or (mode == "lowpasspc")):
  101.             lowpass = core.fmtc.resample(startclip, startclip.width*2, startclip.height*2, kernel=lowpasskernel, taps=lowpassintaps).fmtc.resample(startclip.width, startclip.height, kernel=lowpasskernel, taps=lowpassouttaps)
  102.            
  103.             if exportlowpass:
  104.                 return lowpass
  105.            
  106.             startclip = core.std.Lut(lowpass, function=MonoS.LowPassLUT16)
  107.        
  108.         if((mode == "pclevel") or (mode == "lowpasspc")):
  109.             diff = core.std.Lut(startclip, function=MonoS.PCLevelLUT)
  110.         else:      
  111.             temp1 = core.fmtc.resample(startclip, final_width, final_height, kernel=kernel, invks=True, invkstaps=invkstaps, taps=taps).fmtc.resample(clip.width, clip.height, kernel=kernel, taps=taps)
  112.             diff = core.std.MakeDiff(startclip, temp1, 0)
  113.        
  114.         initial_mask = core.std.Lut(diff, function=MonoS.Luma16).rgvs.RemoveGrain(mode=[RGmode]).std.Lut(function=MonoS.f16)
  115.        
  116.         expanded = initial_mask
  117.         for i in range(0, expandN):
  118.             expanded = core.generic.Maximum(expanded, planes=[0])
  119.        
  120.         inflated = expanded
  121.         for i in range(0, inflateN):
  122.             inflated = core.generic.Inflate(inflated, planes=[0])
  123.        
  124.         final = core.fmtc.resample(inflated, final_width, final_height, src_left, src_top, src_width, src_height, taps=taps)
  125.        
  126.         if(blur_more):
  127.             final = core.rgvs.RemoveGrain(final, mode=[12,0,0])
  128.            
  129.         final = core.std.ShufflePlanes(final, planes=0, colorfamily=vs.GRAY)
  130.        
  131.         if (clip.format.bits_per_sample == 8):
  132.             final = core.fmtc.bitdepth(final, bits=8, mode=1)
  133.        
  134.         return finalimport vapoursynth as vs
  135.  
  136. class InvalidArgument(Exception):
  137.     def __init__(self, value):
  138.         self.value = value
  139.     def __str__(self):
  140.         return repr(self.value)
  141.  
  142. class MonoS(object):
  143.     Gcutoff = 17990
  144.     Ggain = 0.75
  145.     Glowpassthr = 1542
  146.     Gpclevelthr = 59881
  147.    
  148.     #x < cutoff ? 0 : x * gain * (256+x) / 256
  149.     def f16(x):
  150.         if (x < Gcutoff):
  151.             return 0
  152.         else:
  153.             result = int(float(x) * Ggain * ((0xFFFF+1)+x) / (0xFFFF+1))
  154.             if result > 0xFFFF:
  155.                 return 0xFFFF
  156.             else:
  157.                 return result
  158.    
  159.     def Luma16(x):
  160.         p = x << 4
  161.         if (p & (0xffff + 1)):
  162.             return (0xffff - (p&0xffff))
  163.         else:
  164.             return p&0xffff
  165.    
  166.     def LowPassLUT16(x):
  167.         p = x - 0x1000
  168.         if (p > 0):
  169.             if (p - Glowpassthr > 0):
  170.                 return x - Glowpassthr
  171.             else:
  172.                 return 0x7FFF
  173.         else:
  174.             if (p + Glowpassthr < 0):
  175.                 return x + Glowpassthr
  176.             else:
  177.                 return 0x7FFF
  178.    
  179.     def PCLevelLUT(x):
  180.         if (x > Gpclevelthr):
  181.             return x
  182.         else:
  183.             return 0
  184.    
  185.     def MaskDetail(clip, final_width, final_height, RGmode=3, cutoff=-1, 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, mode="normal", lowpasskernel="blackman", lowpassintaps=4, lowpassouttaps=3, lowpassthr=-1, exportlowpass=False, pclevelthr=-1):
  186.        
  187.         core = vs.get_core()       
  188.        
  189.         if ((clip.format.bits_per_sample == 8) or (clip.format.bits_per_sample == 16)):
  190.             if (clip.format.bits_per_sample == 8):
  191.                 if cutoff == -1:
  192.                     cutoff = 17990
  193.                 else:
  194.                     cutoff = cutoff * 65535/255
  195.                
  196.                 if lowpassthr == -1:
  197.                     lowpassthr = 1542
  198.                 else:
  199.                     lowpassthr = lowpassthr * 65535/255
  200.                
  201.                 if pclevelthr == -1:
  202.                     pclevelthr = 59881
  203.                 else:
  204.                     pclevelthr = pclevelthr * 65535/255
  205.                    
  206.                
  207.                 startclip = core.fmtc.bitdepth(clip, bits=16)
  208.             else:
  209.                 if cutoff == -1:
  210.                     cutoff = 17990
  211.                
  212.                 if lowpassthr == -1:
  213.                     lowpassthr = 1542
  214.  
  215.                 if pclevelthr == -1:
  216.                     pclevelthr = 59881             
  217.                
  218.                 startclip = clip
  219.         else:
  220.             raise InvalidArgument('Input clip must be 8 or 16 bit')
  221.        
  222.         global Gcutoff
  223.         global Ggain
  224.         global Glowpassthr
  225.         global Gpclevelthr
  226.         Ggain = gain
  227.         Gcutoff = cutoff
  228.         Glowpassthr = lowpassthr
  229.         Gpclevelthr = pclevelthr
  230.        
  231.         startclip = core.std.ShufflePlanes(startclip, planes=0, colorfamily=vs.GRAY)   
  232.        
  233.         if((mode == "lowpass") or (mode == "lowpasspc")):
  234.             lowpass = core.fmtc.resample(startclip, startclip.width*2, startclip.height*2, kernel=lowpasskernel, taps=lowpassintaps).fmtc.resample(startclip.width, startclip.height, kernel=lowpasskernel, taps=lowpassouttaps)
  235.            
  236.             if exportlowpass:
  237.                 return lowpass
  238.            
  239.             startclip = core.std.Lut(lowpass, function=MonoS.LowPassLUT16)
  240.        
  241.         if((mode == "pclevel") or (mode == "lowpasspc")):
  242.             diff = core.std.Lut(startclip, function=MonoS.PCLevelLUT)
  243.         else:      
  244.             temp1 = core.fmtc.resample(startclip, final_width, final_height, kernel=kernel, invks=True, invkstaps=invkstaps, taps=taps).fmtc.resample(clip.width, clip.height, kernel=kernel, taps=taps)
  245.             diff = core.std.MakeDiff(startclip, temp1, 0)
  246.        
  247.         initial_mask = core.std.Lut(diff, function=MonoS.Luma16).rgvs.RemoveGrain(mode=[RGmode]).std.Lut(function=MonoS.f16)
  248.        
  249.         expanded = initial_mask
  250.         for i in range(0, expandN):
  251.             expanded = core.generic.Maximum(expanded, planes=[0])
  252.        
  253.         inflated = expanded
  254.         for i in range(0, inflateN):
  255.             inflated = core.generic.Inflate(inflated, planes=[0])
  256.        
  257.         final = core.fmtc.resample(inflated, final_width, final_height, src_left, src_top, src_width, src_height, taps=taps)
  258.        
  259.         if(blur_more):
  260.             final = core.rgvs.RemoveGrain(final, mode=[12,0,0])
  261.            
  262.         final = core.std.ShufflePlanes(final, planes=0, colorfamily=vs.GRAY)
  263.        
  264.         if (clip.format.bits_per_sample == 8):
  265.             final = core.fmtc.bitdepth(final, bits=8, mode=1)
  266.        
  267.         return final
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement