Skip to content

Commit

Permalink
Add Resampler
Browse files Browse the repository at this point in the history
  • Loading branch information
Setsugennoao committed Aug 6, 2023
1 parent 7aafc7e commit 531de9b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
54 changes: 47 additions & 7 deletions vskernels/kernels/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
__all__ = [
'Scaler', 'ScalerT',
'Descaler', 'DescalerT',
'Resampler', 'ResamplerT',
'Kernel', 'KernelT'
]

Expand Down Expand Up @@ -141,7 +142,29 @@ def ensure_obj(
return BaseScaler.ensure_obj(cls, Descaler, descaler, UnknownDescalerError, [], func_except) # type: ignore


class Kernel(Scaler, Descaler):
class Resampler(vs_object):
@abstractmethod
@inject_self.cached
def resample(
self, clip: vs.VideoNode, format: int | VideoFormatT | HoldsVideoFormatT,
matrix: MatrixT | None = None, matrix_in: MatrixT | None = None, **kwargs: Any
) -> vs.VideoNode:
pass

@classmethod
def from_param(
cls: type[Resampler], resampler: ResamplerT | None = None, func_except: FuncExceptT | None = None
) -> type[Resampler]:
return BaseScaler.from_param(cls, Resampler, resampler, UnknownDescalerError, [], func_except) # type: ignore

@classmethod
def ensure_obj(
cls: type[Resampler], resampler: ResamplerT | None = None, func_except: FuncExceptT | None = None
) -> Resampler:
return BaseScaler.ensure_obj(cls, Resampler, resampler, UnknownDescalerError, [], func_except) # type: ignore


class Kernel(Scaler, Descaler, Resampler):
"""
Abstract scaling kernel interface.
Expand All @@ -153,6 +176,8 @@ class Kernel(Scaler, Descaler):
"""Scale function called internally when scaling/resampling/shifting"""
descale_function: GenericVSFunction
"""Descale function called internally when descaling"""
resample_function: GenericVSFunction
"""Resample function called internally when resampling"""

@inject_self.cached
def scale( # type: ignore[override]
Expand All @@ -171,7 +196,7 @@ def resample(
self, clip: vs.VideoNode, format: int | VideoFormatT | HoldsVideoFormatT,
matrix: MatrixT | None = None, matrix_in: MatrixT | None = None, **kwargs: Any
) -> vs.VideoNode:
return self.scale_function(clip, **self.get_matrix_args(clip, format, matrix, matrix_in, **kwargs))
return self.resample_function(clip, **self.get_resample_args(clip, format, matrix, matrix_in, **kwargs))

@overload
@inject_self.cached
Expand Down Expand Up @@ -259,7 +284,7 @@ def get_descale_args(
) -> dict[str, Any]:
return dict(src_top=shift[0], src_left=shift[1]) | self.get_params_args(True, clip, width, height, **kwargs)

def get_matrix_args(
def get_resample_args(
self, clip: vs.VideoNode, format: int | VideoFormatT | HoldsVideoFormatT,
matrix: MatrixT | None, matrix_in: MatrixT | None, **kwargs: Any
) -> dict[str, Any]:
Expand Down Expand Up @@ -290,10 +315,17 @@ def from_param(
) -> type[Descaler]:
...

@overload
@classmethod
def from_param(
cls: type[Kernel], kernel: ResamplerT | KernelT | None = None, func_except: FuncExceptT | None = None
) -> type[Resampler]:
...

@classmethod
def from_param(
cls: type[Kernel], kernel: ScalerT | DescalerT | KernelT | None = None, func_except: FuncExceptT | None = None
) -> type[Scaler] | type[Descaler] | type[Kernel]:
cls: type[Kernel], kernel: ScalerT | DescalerT | ResamplerT | KernelT | None = None, func_except: FuncExceptT | None = None
) -> type[Scaler] | type[Descaler] | type[Resampler] | type[Kernel]:
from ..util import excluded_kernels
return BaseScaler.from_param(
cls, Kernel, kernel, UnknownKernelError, excluded_kernels, func_except # type: ignore
Expand All @@ -320,10 +352,17 @@ def ensure_obj(
) -> Descaler:
...

@overload
@classmethod
def ensure_obj(
cls: type[Kernel], kernel: ResamplerT | KernelT | None = None, func_except: FuncExceptT | None = None
) -> Resampler:
...

@classmethod
def ensure_obj(
cls: type[Kernel], kernel: ScalerT | DescalerT | KernelT | None = None, func_except: FuncExceptT | None = None
) -> Scaler | Descaler | Kernel:
cls: type[Kernel], kernel: ScalerT | DescalerT | ResamplerT | KernelT | None = None, func_except: FuncExceptT | None = None
) -> Scaler | Descaler | Resampler | Kernel:
from ..util import excluded_kernels
return BaseScaler.ensure_obj(
cls, Kernel, kernel, UnknownKernelError, excluded_kernels, func_except # type: ignore
Expand All @@ -332,4 +371,5 @@ def ensure_obj(

ScalerT = Union[str, type[Scaler], Scaler]
DescalerT = Union[str, type[Descaler], Descaler]
ResamplerT = Union[str, type[Resampler], Resampler]
KernelT = Union[str, type[Kernel], Kernel]
4 changes: 2 additions & 2 deletions vskernels/kernels/bicubic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Bicubic(Kernel):
:param c: C-param for bicubic kernel
"""

scale_function = core.lazy.resize.Bicubic
scale_function = resample_function = core.lazy.resize.Bicubic
descale_function = core.lazy.descale.Debicubic

def __init__(self, b: float = 0, c: float = 1 / 2, **kwargs: Any) -> None:
Expand Down Expand Up @@ -206,7 +206,7 @@ class BicubicAuto(Kernel):
b + 2c = target - 1 for downsizing
"""

scale_function = core.lazy.resize.Bicubic
scale_function = resample_function = core.lazy.resize.Bicubic
descale_function = core.lazy.descale.Debicubic

def __init__(self, b: float | None = None, c: float | None = None, target: float = 1.0, **kwargs: Any) -> None:
Expand Down
6 changes: 3 additions & 3 deletions vskernels/kernels/resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
class Point(Kernel):
"""Built-in point resizer."""

scale_function = descale_function = core.lazy.resize.Point
scale_function = resample_function = descale_function = core.lazy.resize.Point


class Bilinear(Kernel):
"""Built-in bilinear resizer."""

scale_function = core.lazy.resize.Bilinear
scale_function = resample_function = core.lazy.resize.Bilinear
descale_function = core.lazy.descale.Debilinear


Expand All @@ -37,7 +37,7 @@ class Lanczos(Kernel):
:param taps: taps param for lanczos kernel
"""

scale_function = core.lazy.resize.Lanczos
scale_function = resample_function = core.lazy.resize.Lanczos
descale_function = core.lazy.descale.Delanczos

def __init__(self, taps: int = 3, **kwargs: Any) -> None:
Expand Down
6 changes: 3 additions & 3 deletions vskernels/kernels/spline.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Spline16(Kernel):
* VapourSynth-descale
"""

scale_function = core.lazy.resize.Spline16
scale_function = resample_function = core.lazy.resize.Spline16
descale_function = core.lazy.descale.Despline16


Expand All @@ -50,7 +50,7 @@ class Spline36(Kernel):
* VapourSynth-descale
"""

scale_function = core.lazy.resize.Spline36
scale_function = resample_function = core.lazy.resize.Spline36
descale_function = core.lazy.descale.Despline36


Expand All @@ -63,7 +63,7 @@ class Spline64(Kernel):
* VapourSynth-descale
"""

scale_function = core.lazy.resize.Spline64
scale_function = resample_function = core.lazy.resize.Spline64
descale_function = core.lazy.descale.Despline64


Expand Down

0 comments on commit 531de9b

Please sign in to comment.