Skip to content

Commit

Permalink
WIP broken code
Browse files Browse the repository at this point in the history
  • Loading branch information
Setsugennoao committed Mar 5, 2024
1 parent 4e918ac commit 8574da3
Show file tree
Hide file tree
Showing 16 changed files with 468 additions and 752 deletions.
4 changes: 2 additions & 2 deletions docs/installation/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ Dependencies

The following VapourSynth libraries are required:

* `descale <https://github.com/Irrational-Encoding-Wizardry/descale>`_
* `fmtconv <https://github.com/EleonoreMizo/fmtconv>`_
* `vsresize2 <https://github.com/Jaded-Encoding-Thaumaturgy/vsresize2>`_
* `descale <https://github.com/Jaded-Encoding-Thaumaturgy/descale>`_
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
requirements = Path('requirements.txt').read_text()

# stubs comand
# vsgenstubs4 std resize descale fmtc placebo -o stubs
# vsgenstubs4 std vsresize2 descale placebo -o stubs


setuptools.setup(
Expand Down
5 changes: 2 additions & 3 deletions vskernels/kernels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from .abstract import * # noqa: F401, F403
from .bicubic import * # noqa: F401, F403
from .complex import * # noqa: F401, F403
from .fmtconv import * # noqa: F401, F403
from .impulse import * # noqa: F401, F403
from .custom import * # noqa: F401, F403
from .ewa import * # noqa: F401, F403
from .placebo import * # noqa: F401, F403
from .resize import * # noqa: F401, F403
from .spline import * # noqa: F401, F403
from .various import * # noqa: F401, F403
from .zimg import * # noqa: F401, F403
4 changes: 2 additions & 2 deletions vskernels/kernels/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init_subclass__(cls) -> None:
if not _finished_loading_abstract:
return

from .zimg import ZimgComplexKernel
from .complex import CustomComplexKernel
from ..util import abstract_kernels

if cls in abstract_kernels:
Expand All @@ -151,7 +151,7 @@ def __init_subclass__(cls) -> None:
if 'kernel_radius' in cls.__dict__.keys():
return

mro = [cls, *({*cls.mro()} - {*ZimgComplexKernel.mro()})]
mro = [cls, *({*cls.mro()} - {*CustomComplexKernel.mro()})]

for sub_cls in mro:
if hasattr(sub_cls, '_static_kernel_radius'):
Expand Down
72 changes: 25 additions & 47 deletions vskernels/kernels/bicubic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from math import sqrt
from typing import Any

from vstools import CustomValueError, core, vs, inject_self
from vstools import CustomValueError, inject_self

from .zimg import ZimgComplexKernel
from .complex import CustomComplexKernel
from .helpers import bic_vals, poly3

__all__ = [
'Bicubic',
Expand All @@ -23,35 +24,32 @@
]


class Bicubic(ZimgComplexKernel):
class Bicubic(CustomComplexKernel):
"""
Built-in bicubic resizer.
Default: b=0, c=0.5
Dependencies:
* VapourSynth-descale
:param b: B-param for bicubic kernel
:param c: C-param for bicubic kernel
"""

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:
self.b = b
self.c = c
super().__init__(**kwargs)

def get_params_args(
self, is_descale: bool, clip: vs.VideoNode, width: int | None = None, height: int | None = None, **kwargs: Any
) -> dict[str, Any]:
args = super().get_params_args(is_descale, clip, width, height, **kwargs)
if is_descale:
return args | dict(b=self.b, c=self.c)
return args | dict(filter_param_a=self.b, filter_param_b=self.c)
@inject_self
def kernel(self, *, x: float) -> float: # type: ignore
x, b, c = abs(x), self.b, self.c

if (x < 1.0):
return poly3(x, bic_vals.p0(b, c), 0.0, bic_vals.p2(b, c), bic_vals.p3(b, c))

if (x < 2.0):
return poly3(x, bic_vals.q0(b, c), bic_vals.q1(b, c), bic_vals.q2(b, c), bic_vals.q3(b, c))

return 0.0

@inject_self.property
def kernel_radius(self) -> int: # type: ignore
Expand Down Expand Up @@ -136,47 +134,27 @@ def __init__(self, **kwargs: Any) -> None:
super().__init__(b=b, c=c, **kwargs)


class BicubicAuto(ZimgComplexKernel):
class BicubicAuto(Bicubic):
"""
Kernel that follows the rule of:
b + 2c = target
"""

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:
def __init__(self, b: float | None = None, c: float | None = None, **kwargs: Any) -> None:
if None not in {b, c}:
raise CustomValueError("You can't specify both b and c!", self.__class__)

self.b = b
self.c = c
self.target = target
self.b, self.c = self._get_bc_args(b, c)

super().__init__(**kwargs)

def get_params_args(
self, is_descale: bool, clip: vs.VideoNode, width: int | None = None, height: int | None = None, **kwargs: Any
) -> dict[str, Any]:
args = super().get_params_args(is_descale, clip, width, height, **kwargs)

b, c = self._get_bc_args()

if is_descale:
return args | dict(b=b, c=c)
return args | dict(filter_param_a=b, filter_param_b=c)

def _get_bc_args(self) -> tuple[float, float]:
autob = 0.0 if self.b is None else self.b
autoc = 0.5 if self.c is None else self.c
def _get_bc_args(self, b: float | None, c: float | None) -> tuple[float, float]:
autob = 0.0 if b is None else b
autoc = 0.5 if c is None else c

if self.c is not None and self.b is None:
autob = self.target - 2 * self.c
elif self.c is None and self.b is not None:
autoc = (self.target - self.b) / 2
if c is not None and b is None:
autob = 1.0 - 2 * c
elif c is None and b is not None:
autoc = (1.0 - b) / 2

return autob, autoc

@inject_self.property
def kernel_radius(self) -> int: # type: ignore
return Bicubic(*self._get_bc_args()).kernel_radius
36 changes: 31 additions & 5 deletions vskernels/kernels/complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from functools import lru_cache
from typing import TYPE_CHECKING, Any, SupportsFloat, TypeVar, Union, cast

from math import ceil
from stgpytools import inject_kwargs_params
from vstools import (
CustomIntEnum, Dar, KwargsT, Resolution, Sar, VSFunctionAllArgs, check_correct_subsampling, inject_self, padder, vs
)

from ..types import Center, LeftShift, Slope, TopShift
from .abstract import Descaler, Kernel, Resampler, Scaler
from .custom import CustomKernel

__all__ = [
'BorderHandling',
Expand All @@ -19,7 +20,10 @@
'KeepArScaler',

'ComplexScaler', 'ComplexScalerT',
'ComplexKernel', 'ComplexKernelT'
'ComplexKernel', 'ComplexKernelT',

'CustomComplexKernel',
'CustomComplexTapsKernel'
]

XarT = TypeVar('XarT', Sar, Dar)
Expand Down Expand Up @@ -86,7 +90,7 @@ def func(
has_custom_op = hasattr(self, f'_linear_{op_name}')
operation = cast(
VSFunctionAllArgs,
getattr(self, f'_linear_{op_name}') if has_custom_op else getattr(super(), op_name) # type: ignore
getattr(self, f'_linear_{op_name}') if has_custom_op else getattr(super(), op_name)
)

if sigmoid:
Expand Down Expand Up @@ -152,7 +156,7 @@ def _get_kwargs_keep_ar(

return kwargs

def _handle_crop_resize_kwargs( # type: ignore[override]
def _handle_crop_resize_kwargs(
self, clip: vs.VideoNode, width: int, height: int, shift: tuple[TopShift, LeftShift],
sar: Sar | bool | float | None, dar: Dar | bool | float | None, **kwargs: Any
) -> tuple[KwargsT, tuple[TopShift, LeftShift], Sar | None]:
Expand Down Expand Up @@ -215,7 +219,7 @@ def scale( # type: ignore[override]

padded = border_handling.prepare_clip(clip, self.kernel_radius)

shift, clip = tuple( # type: ignore
shift, clip = tuple(
s + ((p - c) // 2) for s, c, p in zip(shift, *((x.width, x.height) for x in (clip, padded)))
), padded

Expand Down Expand Up @@ -251,5 +255,27 @@ class ComplexKernel(Kernel, LinearDescaler, ComplexScaler): # type: ignore
...


class CustomComplexKernel(CustomKernel, ComplexKernel): # type: ignore
if TYPE_CHECKING:
@inject_self.cached
@inject_kwargs_params
def descale( # type: ignore[override]
self, clip: vs.VideoNode, width: int, height: int, shift: tuple[TopShift, LeftShift] = (0, 0),
*, blur: float = 1.0, border_handling: BorderHandling, ignore_mask: vs.VideoNode | None = None,
linear: bool = False, sigmoid: bool | tuple[Slope, Center] = False, **kwargs: Any
) -> vs.VideoNode:
...


class CustomComplexTapsKernel(CustomComplexKernel):
def __init__(self, taps: float, **kwargs: Any) -> None:
self.taps = taps
super().__init__(**kwargs)

@inject_self.property
def kernel_radius(self) -> int: # type: ignore
return ceil(self.taps)


ComplexScalerT = Union[str, type[ComplexScaler], ComplexScaler]
ComplexKernelT = Union[str, type[ComplexKernel], ComplexKernel]
41 changes: 41 additions & 0 deletions vskernels/kernels/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from __future__ import annotations
from stgpytools import inject_self

from vstools import vs, core
from typing import Any
from .abstract import Kernel

from typing import TypeVar


__all__ = [
'CustomKernel'
]


class CustomKernel(Kernel):
@inject_self
def kernel(self: CustomKernelT, *, x: float) -> float:
raise NotImplementedError

def scale_function(self, clip: vs.VideoNode, width: int, height: int, *args: Any, blur: float = 1.0, **kwargs: Any) -> vs.VideoNode:
return core.resize.Custom(clip, self.kernel, self.kernel_radius, width, height, *args, **kwargs)

resample_function = scale_function

def descale_function(self, clip: vs.VideoNode, width: int, height: int, *args: Any, **kwargs: Any) -> vs.VideoNode:
return core.descale.Decustom(clip, width, height, self.kernel, self.kernel_radius, *args, **kwargs)

def get_params_args(
self, is_descale: bool, clip: vs.VideoNode, width: int | None = None, height: int | None = None, **kwargs: Any
) -> dict[str, Any]:
args = super().get_params_args(is_descale, clip, width, height, **kwargs)

if not is_descale:
for key in ('border_handling', 'ignore_mask', 'force', 'force_h', 'force_v'):
args.pop(key, None)

return args


CustomKernelT = TypeVar('CustomKernelT', bound=CustomKernel)
79 changes: 79 additions & 0 deletions vskernels/kernels/ewa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from __future__ import annotations

from typing import Any

from .placebo import Placebo

__all__ = [
'EwaBicubic',
'EwaJinc',
'EwaLanczos',
'EwaGinseng',
'EwaHann',
'EwaHannSoft',
'EwaRobidoux',
'EwaRobidouxSharp',
]


class EwaBicubic(Placebo):
_kernel = 'ewa_robidoux'

def __init__(self, b: float = 0.0, c: float = 0.5, radius: int | None = None, **kwargs: Any) -> None:
radius = kwargs.pop('taps', radius)

if radius is None:
from .bicubic import Bicubic

radius = Bicubic(b, c).kernel_radius

super().__init__(radius, b, c, **kwargs)


class EwaLanczos(Placebo):
_kernel = 'ewa_lanczos'

def __init__(self, taps: float = 3.2383154841662362076499, **kwargs: Any) -> None:
super().__init__(taps, None, None, **kwargs)


class EwaJinc(Placebo):
_kernel = 'ewa_jinc'

def __init__(self, taps: float = 3.2383154841662362076499, **kwargs: Any) -> None:
super().__init__(taps, None, None, **kwargs)


class EwaGinseng(Placebo):
_kernel = 'ewa_ginseng'

def __init__(self, taps: float = 3.2383154841662362076499, **kwargs: Any) -> None:
super().__init__(taps, None, None, **kwargs)


class EwaHann(Placebo):
_kernel = 'ewa_hann'

def __init__(self, taps: float = 3.2383154841662362076499, **kwargs: Any) -> None:
super().__init__(taps, None, None, **kwargs)


class EwaHannSoft(Placebo):
_kernel = 'haasnsoft'

def __init__(self, taps: float = 3.2383154841662362076499, **kwargs: Any) -> None:
super().__init__(taps, None, None, **kwargs)


class EwaRobidoux(Placebo):
_kernel = 'ewa_robidoux'

def __init__(self, **kwargs: Any) -> None:
super().__init__(None, None, None, **kwargs)


class EwaRobidouxSharp(Placebo):
_kernel = 'ewa_robidouxsharp'

def __init__(self, **kwargs: Any) -> None:
super().__init__(None, None, None, **kwargs)
Loading

0 comments on commit 8574da3

Please sign in to comment.