Advertisement
Guest User

Untitled

a guest
May 21st, 2018
349
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from vapoursynth import core
  2. import vapoursynth as vs
  3. import math
  4.  
  5. """
  6. A vapoursynth port of avisynth's bbmod, same usage, add 10 bit support.
  7. """
  8.  
  9. def bbmod2(c, cTop = None, cBottom = None, cLeft = None, cRight = None, thresh = 128, blur = 999):
  10.     if blur < 1:
  11.         raise TypeError("BalanceBordersMod2: Blur parameter must be > 0")
  12.     if thresh < 1:
  13.         raise TypeError("BalanceBordersMod2: Thresh parameter must be > 0")
  14.  
  15.     bits = c.format.bits_per_sample
  16.  
  17.     c = btb2(c, cTop, thresh, blur, bits).std.Transpose().std.FlipHorizontal() if cTop > 0 else core.std.Transpose(c).std.FlipHorizontal()
  18.     c = btb2(c, cLeft, thresh, blur, bits).std.Transpose().std.FlipHorizontal() if cLeft > 0 else core.std.Transpose(c).std.FlipHorizontal()
  19.     c = btb2(c, cBottom, thresh, blur, bits).std.Transpose().std.FlipHorizontal() if cBottom > 0 else core.std.Transpose(c).std.FlipHorizontal()
  20.     c = btb2(c, cRight, thresh, blur, bits).std.Transpose().std.FlipHorizontal() if cRight > 0 else core.std.Transpose(c).std.FlipHorizontal()
  21.     return c
  22.  
  23. def btb2(c, cTop, thresh, blur, bits):
  24.     cWidth = c.width
  25.     cHeight = c.height
  26.     cTop = min(cTop, cHeight - 1)
  27.     blurWidth = max(8,math.floor(cWidth/blur))
  28.    
  29.     if bits == 8:
  30.         c2 = core.resize.Point(c, width = cWidth*2, height = cHeight*2)
  31.         last = core.std.Crop(c2, 0, 0, cTop*2, cHeight*2-cTop*2-2)
  32.         last = core.resize.Point(last, cWidth*2, cTop*2)
  33.         referenceBlurChroma = core.std.Expr(last, "x 128 - abs 2 *").resize.Bicubic(blurWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0).resize.Bicubic(cWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0)
  34.         referenceBlur = core.resize.Bicubic(last, blurWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0).resize.Bicubic(cWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0)
  35.    
  36.         original = core.std.Crop(c2, 0, 0, 0, c2.height-(cTop*2))
  37.         last = core.resize.Bicubic(original, blurWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0)
  38.         originalBlurChroma = core.std.Expr(last, "x 128 - abs 2 *").resize.Bicubic(blurWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0).resize.Bicubic(cWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0)
  39.         originalBlur = core.resize.Bicubic(last, blurWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0).resize.Bicubic(cWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0)
  40.    
  41.         balancedChroma = core.std.Expr([original, originalBlurChroma, referenceBlurChroma],["","z y / 8 min 0.4 max x 128 - * 128 +", "z y / 8 min 0.4 max x 128 - * 128 +"])
  42.         balancedLuma = core.std.Expr([balancedChroma, originalBlur, referenceBlur], ["z 16 - y 16 - / 8 min 0.4 max x 16 - * 16 +", "z y - x +", "z y - x +"])
  43.    
  44.         difference = core.std.MakeDiff(balancedLuma, original)
  45.    
  46.         tp = str(128+thresh)
  47.         tm = str(128-thresh)
  48.         difference = core.std.Expr(difference, "x "+tp+" > "+tp+" x ?")
  49.         difference = core.std.Expr(difference, "x "+tm+" < "+tm+" x ?")
  50.    
  51.         last = core.std.MergeDiff(original, difference)
  52.         return core.std.StackVertical([last,core.std.Crop(c2,0,0,cTop*2,c2.height-cTop*2-(cHeight-cTop)*2)]).resize.Point(cWidth,cHeight)
  53.        
  54.     if bits == 10:
  55.         c2 = core.resize.Point(c, width = cWidth*2, height = cHeight*2)
  56.         last = core.std.Crop(c2, 0, 0, cTop*2, cHeight*2-cTop*2-2)
  57.         last = core.resize.Point(last, cWidth*2, cTop*2)
  58.         referenceBlurChroma = core.std.Expr(last, "x 512 - abs 2 *").resize.Bicubic(blurWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0).resize.Bicubic(cWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0)
  59.         referenceBlur = core.resize.Bicubic(last, blurWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0).resize.Bicubic(cWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0)
  60.    
  61.         original = core.std.Crop(c2, 0, 0, 0, c2.height-(cTop*2))
  62.         last = core.resize.Bicubic(original, blurWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0)
  63.         originalBlurChroma = core.std.Expr(last, "x 512 - abs 2 *").resize.Bicubic(blurWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0).resize.Bicubic(cWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0)
  64.         originalBlur = core.resize.Bicubic(last, blurWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0).resize.Bicubic(cWidth*2, cTop*2, filter_param_a = 1, filter_param_b = 0)
  65.    
  66.         balancedChroma = core.std.Expr([original, originalBlurChroma, referenceBlurChroma],["","z y / 8 min 0.4 max x 512 - * 512 +", "z y / 8 min 0.4 max x 512 - * 512 +"])
  67.         balancedLuma = core.std.Expr([balancedChroma, originalBlur, referenceBlur], ["z 64 - y 64 - / 8 min 0.4 max x 64 - * 64 +", "z y - x +", "z y - x +"])
  68.    
  69.         difference = core.std.MakeDiff(balancedLuma, original)
  70.    
  71.         tp = str(512+thresh)
  72.         tm = str(512-thresh)
  73.         difference = core.std.Expr(difference, "x "+tp+" > "+tp+" x ?")
  74.         difference = core.std.Expr(difference, "x "+tm+" < "+tm+" x ?")
  75.    
  76.         last = core.std.MergeDiff(original, difference)
  77.         return core.std.StackVertical([last,core.std.Crop(c2,0,0,cTop*2,c2.height-cTop*2-(cHeight-cTop)*2)]).resize.Point(cWidth,cHeight)
Advertisement
RAW Paste Data Copied
Advertisement