Skip to content

Commit

Permalink
Adapt to new vskernels api
Browse files Browse the repository at this point in the history
  • Loading branch information
Setsugennoao committed Dec 14, 2023
1 parent adddf10 commit 31f03b1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
27 changes: 24 additions & 3 deletions vsscale/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def scale( # type: ignore
ExprOp.ADD, zip(weights, ExprOp.MUL), expr_suffix=[sum(weights), ExprOp.DIV]
)

@property
def kernel_radius(self) -> int:
return max(scaler.kernel_radius for scaler, _ in self.scalers)


@dataclass
class ClampScaler(GenericScaler):
Expand Down Expand Up @@ -127,22 +131,26 @@ def __post_init__(self) -> None:
if self.undershoot is None:
self.undershoot = self.overshoot

self._reference = None if isinstance(self.reference, vs.VideoNode) else self.ensure_scaler(self.reference)
self._ref_scaler = self.ensure_scaler(self.ref_scaler)

@inject_self
def scale( # type: ignore
self, clip: vs.VideoNode, width: int, height: int, shift: tuple[float, float] = (0, 0),
*, smooth: vs.VideoNode | None = None, **kwargs: Any
) -> vs.VideoNode:
assert (self.undershoot or self.undershoot == 0) and (self.overshoot or self.overshoot == 0)

ref = self.ensure_scaler(self.ref_scaler).scale(clip, width, height, shift, **kwargs)
ref = self._ref_scaler.scale(clip, width, height, shift, **kwargs)

if isinstance(self.reference, vs.VideoNode):
smooth = self.reference # type: ignore

if shift != (0, 0):
smooth = self._kernel.shift(smooth, shift) # type: ignore
else:
smooth = self.ensure_scaler(self.reference).scale(clip, width, height, shift) # type: ignore
assert self._reference
smooth = self._reference.scale(clip, width, height, shift) # type: ignore

assert smooth

Expand Down Expand Up @@ -187,6 +195,12 @@ def scale( # type: ignore

return merged

@property
def kernel_radius(self) -> int:
if self._reference:
return max(self._reference.kernel_radius, self._ref_scaler.kernel_radius)
return self._ref_scaler.kernel_radius


class UnsharpLimitScaler(GenericScaler):
"""Limit a scaler with a masked unsharping."""
Expand All @@ -213,6 +227,7 @@ def __init__(
self.merge_mode = merge_mode

self.reference = reference
self._reference = None if isinstance(self.reference, vs.VideoNode) else self.ensure_scaler(self.reference)
self.ref_scaler = self.ensure_scaler(ref_scaler)

self.args = args
Expand All @@ -231,7 +246,7 @@ def scale( # type: ignore
if shift != (0, 0):
smooth = self._kernel.shift(smooth, shift) # type: ignore
else:
smooth = self.ensure_scaler(self.reference).scale(clip, width, height, shift) # type: ignore
smooth = self._reference.scale(clip, width, height, shift) # type: ignore

assert smooth

Expand All @@ -247,6 +262,12 @@ def scale( # type: ignore

return combine([smooth, fsrcnnx, smooth_sharp], ExprOp.MIN)

@property
def kernel_radius(self) -> int:
if self._reference:
return max(self._reference.kernel_radius, self.ref_scaler.kernel_radius)
return self.ref_scaler.kernel_radius


@dataclass
class MergedFSRCNNX(ClampScaler):
Expand Down
4 changes: 4 additions & 0 deletions vsscale/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
'ScalingArgs'
]

__abstract__ = [
'GenericScaler'
]


class _GeneriScaleNoShift(Protocol):
def __call__(self, clip: vs.VideoNode, width: int, height: int, *args: Any, **kwds: Any) -> vs.VideoNode:
Expand Down
14 changes: 13 additions & 1 deletion vsscale/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any, ClassVar, Literal

from vsexprtools import complexpr_available, expr_func, norm_expr
from vskernels import Bicubic, Hermite, LinearScaler, ScalerT, SetsuCubic, ZewiaCubic
from vskernels import Hermite, LinearScaler, ScalerT, SetsuCubic, ZewiaCubic
from vsrgtools import box_blur, gauss_blur
from vstools import (
DependencyNotFoundError, KwargsT, Matrix, MatrixT, PlanesT, VSFunction, check_ref_clip, check_variable, core, depth,
Expand Down Expand Up @@ -63,6 +63,10 @@ def scale( # type: ignore[override]

return core.dpid.DpidRaw(clip, ref, **kwargs) # type: ignore

@inject_self.property
def kernel_radius(self) -> int:
return self.ref.kernel_radius


class SSIM(LinearScaler):
"""
Expand Down Expand Up @@ -135,6 +139,10 @@ def _linear_scale(

return expr_func([self.filter_func(m), self.filter_func(r), l1, self.filter_func(t)], 'x y z * + a -')

@inject_self.property
def kernel_radius(self) -> int:
return self.ref.kernel_radius


@dataclass
class DLISR(GenericScaler):
Expand Down Expand Up @@ -171,6 +179,8 @@ def scale( # type: ignore

return self._finish_scale(output, clip, width, height, shift, matrix)

_static_kernel_radius = 2


class _BaseWaifu2x:
_model: ClassVar[int]
Expand Down Expand Up @@ -358,6 +368,8 @@ def scale( # type:ignore

return self._finish_scale(wclip, clip, width, height, shift, matrix, is_upscale)

_static_kernel_radius = 2


class Waifu2x(BaseWaifu2x):
_model = 6
Expand Down
2 changes: 2 additions & 0 deletions vsscale/shaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
class PlaceboShaderMeta(GenericScaler):
shader_file: str | Path | ShaderFile

_static_kernel_radius = 2


@dataclass
class PlaceboShaderBase(PlaceboShaderMeta):
Expand Down

0 comments on commit 31f03b1

Please sign in to comment.