Skip to content

Commit

Permalink
Use vszip functions where applicable (#39)
Browse files Browse the repository at this point in the history
* Update blur.py

* Update blur.py

* Update blur.py

* Update blur.py
  • Loading branch information
emotion3459 authored Sep 24, 2024
1 parent ecafb14 commit 2d82a92
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions vsrgtools/blur.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,33 @@ def box_blur(
if not radius:
return clip

fp16 = clip.format.sample_type == vs.FLOAT and clip.format.bits_per_sample == 16
box_args = (
planes,
radius, 0 if mode == ConvMode.VERTICAL else passes,
radius, 0 if mode == ConvMode.HORIZONTAL else passes
)

if radius > 12 and not fp16:
blurred = clip.std.BoxBlur(
planes,
radius, 0 if mode == ConvMode.VERTICAL else passes,
radius, 0 if mode == ConvMode.HORIZONTAL else passes
)
if hasattr(core, 'vszip'):
blurred = clip.vszip.BoxBlur(*box_args)
else:
matrix_size = radius * 2 | 1
fp16 = clip.format.sample_type == vs.FLOAT and clip.format.bits_per_sample == 16

if fp16:
matrix_size **= 2
if radius > 12 and not fp16:
blurred = clip.std.BoxBlur(*box_args)
else:
matrix_size = radius * 2 | 1

blurred = clip
for _ in range(passes):
if fp16:
blurred = norm_expr(blurred, [
ExprOp.matrix('x', radius, mode=mode), ExprOp.ADD * (matrix_size - 1), matrix_size, ExprOp.DIV
], planes)
else:
blurred = blurred.std.Convolution([1] * matrix_size, planes=planes, mode=mode)
matrix_size **= 2

blurred = clip
for _ in range(passes):
if fp16:
blurred = norm_expr(blurred, [
ExprOp.matrix('x', radius, mode=mode), ExprOp.ADD * (matrix_size - 1), matrix_size, ExprOp.DIV
], planes)
else:
blurred = blurred.std.Convolution([1] * matrix_size, planes=planes, mode=mode)

return blurred

Expand All @@ -110,7 +115,7 @@ def side_box_blur(

conv_m1 = partial(core.std.Convolution, matrix=half_kernel, planes=planes)
conv_m2 = partial(core.std.Convolution, matrix=half_kernel[::-1], planes=planes)
blur_pt = partial(core.std.BoxBlur, planes=planes)
blur_pt = partial(box_blur, planes=planes)

vrt_filters, hrz_filters = [
[
Expand Down Expand Up @@ -395,6 +400,9 @@ def bilateral(
if ref and clip.format != ref.format:
ref = depth(ref, clip)

clip = clip.bilateral.Bilateral(ref, sigmaS, sigmaR)
if hasattr(core, 'vszip'):
clip = clip.vszip.Bilateral(ref, sigmaS, sigmaR)
else:
clip = clip.bilateral.Bilateral(ref, sigmaS, sigmaR)

return depth(clip, bits)

0 comments on commit 2d82a92

Please sign in to comment.