Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. import vapoursynth as vs
  2.  
  3.  
  4. def nnedi3_rpow2(clip, rfactor=2, width=None, height=None, correct_shift=True,
  5. kernel="spline36", nsize=0, nns=3, qual=None, etype=None, pscrn=None):
  6. """nnedi3_rpow2 is for enlarging images by powers of 2.
  7.  
  8. Args:
  9. rfactor (int): Image enlargement factor.
  10. Must be a power of 2 in the range [2 to 1024].
  11. correct_shift (bool): If False, the shift is not corrected.
  12. The correction is accomplished by using the subpixel
  13. cropping capability of fmtc's resizers.
  14. width (int): If correcting the image center shift by using the
  15. "correct_shift" parameter, width/height allow you to set a
  16. new output resolution.
  17. kernel (string): Sets the resizer used for correcting the image
  18. center shift that nnedi3_rpow2 introduces. This can be any of
  19. fmtc kernels, such as "cubic", "spline36", etc.
  20. spline36 is the default one.
  21. nnedi3_args (mixed): For help with nnedi3 args
  22. refert to nnedi3 documentation.
  23. """
  24. core = vs.get_core()
  25.  
  26. # Setting up variables
  27.  
  28. if width is None:
  29. width = clip.width*rfactor
  30. if height is None:
  31. height = clip.height*rfactor
  32. hshift = 0.0
  33. vshift = -0.5
  34. pkdnnedi = dict(dh=True, nsize=nsize, nns=nns, qual=qual, etype=etype, pscrn=pscrn)
  35. pkdchroma = dict(kernel=kernel, sy=-0.5, planes=[2, 3, 3])
  36.  
  37. tmp = 1
  38. times = 0
  39. while tmp < rfactor:
  40. tmp *= 2
  41. times += 1
  42.  
  43. # Checks
  44.  
  45. if rfactor < 2 or rfactor > 1024:
  46. raise ValueError("nnedi3_rpow2: rfactor must be between 2 and 1024")
  47.  
  48. if tmp != rfactor:
  49. raise ValueError("nnedi3_rpow2: rfactor must be a power of 2")
  50.  
  51. if hasattr(core, 'nnedi3cl') is not True:
  52. raise RuntimeError("nnedi3_rpow2: nnedi3 plugin is required")
  53.  
  54. if correct_shift or clip.format.subsampling_h:
  55. if hasattr(core, 'fmtc') is not True:
  56. raise RuntimeError("nnedi3_rpow2: fmtconv plugin is required")
  57.  
  58. # Processing
  59.  
  60. last = clip
  61.  
  62. for i in range(times):
  63. field = 1 if i == 0 else 0
  64. last = core.nnedi3cl.NNEDI3CL(last, field=field, **pkdnnedi)
  65. last = core.std.Transpose(last)
  66. if last.format.subsampling_w:
  67. # Apparently always using field=1 for the horizontal pass somehow
  68. # keeps luma/chroma alignment.
  69. field = 1
  70. hshift = hshift*2 - 0.5
  71. else:
  72. hshift = -0.5
  73. last = core.nnedi3cl.NNEDI3CL(last, field=field, **pkdnnedi)
  74. last = core.std.Transpose(last)
  75.  
  76. # Correct vertical shift of the chroma.
  77.  
  78. if clip.format.subsampling_h:
  79. last = core.fmtc.resample(last, w=last.width, h=last.height, **pkdchroma)
  80.  
  81. if correct_shift is True:
  82. last = core.fmtc.resample(last, w=width, h=height, kernel=kernel, sx=hshift, sy=vshift)
  83.  
  84. if last.format.id != clip.format.id:
  85. last = core.fmtc.bitdepth(last, csp=clip.format.id)
  86.  
  87. return last
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement