Skip to content

Commit

Permalink
Update vskernels dependency, remove get_matrix/Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
LightArrowsEXE committed Jul 5, 2022
1 parent 2d376f8 commit 48b2034
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 99 deletions.
4 changes: 2 additions & 2 deletions lvsfunc/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
from typing import Any, Callable, Dict, Iterable, Iterator, List, Literal, Sequence, Set, Tuple, TypeVar, overload

import vapoursynth as vs
from vskernels import Catrom
from vskernels import Catrom, get_matrix
from vsutil import depth, get_subsampling, get_w
from vsutil import split as split_planes

from .dehardsub import hardsub_mask
from .exceptions import ClipsAndNamedClipsError, InvalidFormatError, NotEqualFormatsError, VariableFormatError
from .render import clip_async_render
from .types import Direction
from .util import check_variable, check_variable_format, check_variable_resolution, get_matrix, get_prop
from .util import check_variable, check_variable_format, check_variable_resolution, get_prop

core = vs.core

Expand Down
4 changes: 2 additions & 2 deletions lvsfunc/deblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from typing import Any, Dict, List, Literal, Sequence, SupportsFloat, Tuple, cast

import vapoursynth as vs
from vskernels import Bicubic, Kernel, Point, get_kernel
from vskernels import Bicubic, Kernel, Matrix, Point, get_kernel
from vsutil import Dither, depth, get_depth

from .helpers import _check_has_nvidia
from .types import Matrix, Range
from .types import Range
from .util import check_variable, get_prop, replace_ranges

core = vs.core
Expand Down
17 changes: 0 additions & 17 deletions lvsfunc/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
import vapoursynth as vs
from vskernels import Kernel, get_kernel

from .types import Matrix

__all__: List[str] = [
'ClipsAndNamedClipsError',
'CompareSameKernelError',
'FramePropError',
'InvalidFormatError',
'InvalidFramerateError',
'MatrixError',
'NotEqualFormatsError',
'TopFieldFirstError',
'VariableFormatError',
Expand Down Expand Up @@ -116,20 +113,6 @@ def __init__(self, function: str, frameprop: str,
super().__init__(self.message.format(func=self.function, frameprop=frameprop))


class MatrixError(ValueError):
"""Raised when there is an error with the matrix."""

def __init__(self, function: str, matrix: Matrix | int,
message: str = "{func}: 'There was an error with your matrix \"{matrix}\"!'") -> None:
if isinstance(matrix, int):
matrix = Matrix(matrix)

self.function: str = function
self.matrix: int = matrix
self.message: str = message
super().__init__(self.message.format(func=self.function, matrix=matrix))


class TopFieldFirstError(ValueError):
"""Raised when the user must pass a TFF argument."""

Expand Down
19 changes: 1 addition & 18 deletions lvsfunc/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import vapoursynth as vs
from vsutil import is_image

from .types import IndexExists, Matrix, VSIdxFunction
from .types import IndexExists, VSIdxFunction

core = vs.core

Expand All @@ -18,7 +18,6 @@
'_check_index_exists',
'_generate_dgi',
'_get_dgidx',
'_get_matrix_from_res',
'_load_dgi',
'_tail'
]
Expand Down Expand Up @@ -99,19 +98,3 @@ def _load_dgi(path: str, film_thr: float, src_filter: VSIdxFunction,
props |= dict(dgi_fieldop=1, _FieldBased=0)

return src_filter(path, **index_args).std.SetFrameProps(**props)


def _get_matrix_from_res(frame: vs.VideoFrame | vs.VideoNode) -> Matrix:
"""Return matrix based on the frame dimensions."""
if isinstance(frame, vs.VideoNode):
frame = frame.get_frame(0)

w, h = frame.width, frame.height

if frame.format.color_family == vs.RGB:
return Matrix(0)
elif w <= 1024 and h <= 576:
return Matrix(6)
elif w <= 2048 and h <= 1536:
return Matrix(1)
return Matrix(9)
4 changes: 2 additions & 2 deletions lvsfunc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from typing import Any, List, Sequence, Tuple

import vapoursynth as vs
from vskernels import Bicubic, Catrom, Kernel
from vskernels import Bicubic, Catrom, Kernel, Matrix
from vsutil import depth, get_depth, is_image, scale_value

from .exceptions import InvalidMatrixError
from .helpers import _check_index_exists, _generate_dgi, _get_dgidx, _load_dgi, _tail
from .mask import BoundingBox
from .types import IndexExists, Matrix, Position, Range, Size
from .types import IndexExists, Position, Range, Size
from .util import check_variable, get_prop, match_clip, normalize_ranges, replace_ranges

core = vs.core
Expand Down
3 changes: 1 addition & 2 deletions lvsfunc/noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
from typing import Any, Dict, List

import vapoursynth as vs
from vskernels import Bicubic, Kernel, get_kernel
from vskernels import Bicubic, Kernel, Matrix, get_kernel
from vsutil import Dither, Range, depth, get_depth, get_y, iterate, join, plane

from .types import Matrix
from .util import check_variable, get_prop

core = vs.core
Expand Down
6 changes: 3 additions & 3 deletions lvsfunc/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from typing import Any, Callable, Dict, List, cast

import vapoursynth as vs
from vskernels import Bicubic, BicubicSharp, Catrom, Kernel, Spline36, get_kernel
from vskernels import Bicubic, BicubicSharp, Catrom, Kernel, Spline36, get_kernel, get_matrix
from vsutil import depth, get_depth, get_w, get_y, iterate, join, plane

from .exceptions import CompareSameKernelError
from .types import CURVES, CreditMask, CustomScaler, Resolution, ScaleAttempt, VSFunction
from .util import (check_variable, check_variable_format, check_variable_resolution, get_coefs, get_matrix,
get_matrix_curve, get_prop, quick_resample, scale_thresh)
from .util import (check_variable, check_variable_format, check_variable_resolution, get_coefs, get_matrix_curve,
get_prop, quick_resample, scale_thresh)

try:
from cytoolz import functoolz
Expand Down
56 changes: 4 additions & 52 deletions lvsfunc/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
from typing import Any, Callable, Dict, List, Tuple, Type, cast

import vapoursynth as vs
import vskernels.types as kernel_type
from typing_extensions import TypeGuard
from vskernels import Bicubic, Kernel, get_kernel
from vskernels import Bicubic, Kernel, Matrix, get_kernel, get_matrix
from vsutil import depth, get_subsampling, get_w, get_y

from .exceptions import (InvalidFormatError, InvalidMatrixError, MatrixError, VariableFormatError,
VariableResolutionError)
from .helpers import _get_matrix_from_res
from .types import CURVES, Coefs, F, Matrix, Range, T, _VideoNode
from .exceptions import InvalidFormatError, InvalidMatrixError, VariableFormatError, VariableResolutionError
from .types import CURVES, Coefs, F, Range, T, _VideoNode

core = vs.core

Expand All @@ -29,7 +26,6 @@
'frames_since_bookmark',
'get_coefs',
'get_matrix_curve',
'get_matrix',
'get_prop',
'load_bookmarks',
'normalize_ranges',
Expand Down Expand Up @@ -347,50 +343,6 @@ def check_variable(clip: vs.VideoNode, function: str) -> TypeGuard[_VideoNode]:
return True


def get_matrix(frame: vs.VideoNode | vs.VideoFrame, strict: bool = False) -> Matrix:
"""
Get the matrix of a clip or frame.
By default this function will first check the `_Matrix` prop for a valid matrix.
If the matrix is not set, it will guess based on the resolution.
If you want it to be strict and raise an error if no matrix is set, set ``strict=True``.
:param clip: Clip or Frame to process.
:param strict: Whether to be strict about the matrix.
If ``True``, checks just the `_Matrix` prop.
If ``False``, will check the `_Matrix` prop and make a guess if `_Matrix=Matrix.UNKNOWN`.
Default: False.
:return: Value representing a matrix.
:raise MatrixError: The matrix was undefined.
:raise MatrixError: The matrix is reserved.
:raise MatrixError: VapourSynth no longer supports the matrix.
:raise MatrixError: The matrix is unsupported.
:raise MatrixError: Some kind of unknown error occured.
"""
if isinstance(frame, vs.VideoNode):
assert check_variable_format(frame, "get_matrix")

frame = frame.get_frame(0)

matrix = get_prop(frame, "_Matrix", int)

match matrix:
case 2 if strict: raise MatrixError("get_matrix", matrix, "{func}: 'Matrix is undefined.'")
case 2: return _get_matrix_from_res(frame)
case 3: raise MatrixError("get_matrix", matrix, "{func}: 'Matrix is reserved.'")
case 8 if core.version_number() >= 55:
raise MatrixError("get_matrix", matrix, "{func}: 'VapourSynth no longer supports {matrix}.'")
case _ if matrix > 14:
raise MatrixError("get_matrix", matrix, "{func}: 'This matrix is current unsupported. "
"If you believe this to be in error, please leave an issue "
"in the lvsfunc GitHub repository.'")

return Matrix(matrix)


def get_matrix_curve(matrix: Matrix) -> CURVES:
"""
Return the matrix curve based on a given ``matrix``.
Expand Down Expand Up @@ -670,7 +622,7 @@ def match_clip(clip: vs.VideoNode, ref: vs.VideoNode,
clip = kernel.scale(clip, ref.width, ref.height) if dimensions else clip

if vformat:
clip = kernel.resample(clip, format=ref.format, matrix=cast(kernel_type.Matrix, get_matrix(ref)))
clip = kernel.resample(clip, format=ref.format, matrix=get_matrix(ref))

if matrices:
ref_frame = ref.get_frame(0)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VapourSynth>=59
rich>=8.0.0
toolz>=0.9.0.1
vs-kernels>=1.0.2
vskernels>=1.0.5
vsrgtools>=0.1.3
vsutil>=0.8.0

0 comments on commit 48b2034

Please sign in to comment.