Skip to content

Commit

Permalink
get_match_centers_scaling: Allow non-clip input
Browse files Browse the repository at this point in the history
  • Loading branch information
LightArrowsEXE committed Oct 7, 2024
1 parent c294940 commit b8790a5
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions lvsfunc/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import random
from typing import Any

from vstools import (CustomIndexError, CustomValueError, FuncExceptT, KwargsT,
check_variable_resolution, core, fallback, get_h, get_w,
vs)
from vstools import (CustomIndexError, CustomValueError, Dar, FuncExceptT,
KwargsT, check_variable_resolution, core, fallback, get_h,
get_w, vs)

__all__ = [
'colored_clips',
Expand Down Expand Up @@ -64,9 +64,10 @@ def colored_clips(


def get_match_centers_scaling(
clip: vs.VideoNode,
base_dimensions: vs.VideoNode | tuple[int, int] = (1920, 1080),
target_width: int | None = None,
target_height: int | None = 720,
dar: Dar | None = None,
func_except: FuncExceptT | None = None
) -> KwargsT:
"""
Expand Down Expand Up @@ -102,8 +103,8 @@ def get_match_centers_scaling(
The formula for calculating values we can use during desampling is simple:
* width: clip.width * (target_width - 1) / (clip.width - 1)
* height: clip.height * (target_height - 1) / (clip.height - 1)
* width: base_width * (target_width - 1) / (base_width - 1)
* height: base_height * (target_height - 1) / (base_height - 1)
Example usage:
Expand All @@ -117,18 +118,23 @@ def get_match_centers_scaling(
The output is meant to be passed to `vodesfunc.DescaleTarget` as keyword arguments,
but it may also apply to other functions that require similar parameters.
:param clip: The clip to base the calculations on.
:param target_width: Target width for the descale. This should probably be equal to the base width.
If not provided, this value is calculated using the `target_height`.
Default: None.
:param target_height: Target height for the descale. This should probably be equal to the base height.
If not provided, this value is calculated using the `target_width`.
Default: 720.
:param func_except: Function returned for custom error handling.
This should only be set by VS package developers.
:return: A dictionary with the keys, {width, height, base_width, base_height},
which can be passed directly to `vodesfunc.DescaleTarget` or similar functions.
:param base_dimensions: The base dimensions to base the calculations on. This may be derived from
a given clip or a tuple of (Width, Height).
Default: (1920, 1080)
:param target_width: Target width for the descale. This should probably be equal to the base width.
If not provided, this value is calculated using the `target_height`.
Default: None.
:param target_height: Target height for the descale. This should probably be equal to the base height.
If not provided, this value is calculated using the `target_width`.
Default: 720.
:param dar: Display aspect ratio. Used for calculating the width/height if either is None.
This is used for anamorphic sources. If None, derive from `base_dimensions`.
Default: None.
:param func_except: Function returned for custom error handling.
This should only be set by VS package developers.
:return: A dictionary with the keys, {width, height, base_width, base_height},
which can be passed directly to `vodesfunc.DescaleTarget` or similar functions.
"""

func = fallback(func_except, get_match_centers_scaling)
Expand All @@ -140,14 +146,20 @@ def get_match_centers_scaling(
if target is not None and (not isinstance(target, int) or target <= 0):
raise CustomValueError(f"`target_{name}` must be a positive integer or None.", func)

check_variable_resolution(clip, func)
if isinstance(base_dimensions, vs.VideoNode):
check_variable_resolution(base_dimensions, func)

base_dimensions = (base_dimensions.width, base_dimensions.height)

base_width, base_height = base_dimensions
dar = dar or Dar.from_size(*base_dimensions)

if target_height is None:
target_height = get_h(target_width, clip, 1)
target_height = get_h(target_width, dar, 1)
elif target_width is None:
target_width = get_w(target_height, clip, 1)
target_width = get_w(target_height, dar, 1)

width = clip.width * (target_width - 1) / (clip.width - 1)
height = clip.height * (target_height - 1) / (clip.height - 1)
width = base_width * (target_width - 1) / (base_width - 1)
height = base_height * (target_height - 1) / (base_height - 1)

return KwargsT(width=width, height=height, base_width=target_width, base_height=target_height)

0 comments on commit b8790a5

Please sign in to comment.