Skip to content

Commit b8790a5

Browse files
get_match_centers_scaling: Allow non-clip input
1 parent c294940 commit b8790a5

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

lvsfunc/util.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import random
33
from typing import Any
44

5-
from vstools import (CustomIndexError, CustomValueError, FuncExceptT, KwargsT,
6-
check_variable_resolution, core, fallback, get_h, get_w,
7-
vs)
5+
from vstools import (CustomIndexError, CustomValueError, Dar, FuncExceptT,
6+
KwargsT, check_variable_resolution, core, fallback, get_h,
7+
get_w, vs)
88

99
__all__ = [
1010
'colored_clips',
@@ -64,9 +64,10 @@ def colored_clips(
6464

6565

6666
def get_match_centers_scaling(
67-
clip: vs.VideoNode,
67+
base_dimensions: vs.VideoNode | tuple[int, int] = (1920, 1080),
6868
target_width: int | None = None,
6969
target_height: int | None = 720,
70+
dar: Dar | None = None,
7071
func_except: FuncExceptT | None = None
7172
) -> KwargsT:
7273
"""
@@ -102,8 +103,8 @@ def get_match_centers_scaling(
102103
103104
The formula for calculating values we can use during desampling is simple:
104105
105-
* width: clip.width * (target_width - 1) / (clip.width - 1)
106-
* height: clip.height * (target_height - 1) / (clip.height - 1)
106+
* width: base_width * (target_width - 1) / (base_width - 1)
107+
* height: base_height * (target_height - 1) / (base_height - 1)
107108
108109
Example usage:
109110
@@ -117,18 +118,23 @@ def get_match_centers_scaling(
117118
The output is meant to be passed to `vodesfunc.DescaleTarget` as keyword arguments,
118119
but it may also apply to other functions that require similar parameters.
119120
120-
:param clip: The clip to base the calculations on.
121-
:param target_width: Target width for the descale. This should probably be equal to the base width.
122-
If not provided, this value is calculated using the `target_height`.
123-
Default: None.
124-
:param target_height: Target height for the descale. This should probably be equal to the base height.
125-
If not provided, this value is calculated using the `target_width`.
126-
Default: 720.
127-
:param func_except: Function returned for custom error handling.
128-
This should only be set by VS package developers.
129-
130-
:return: A dictionary with the keys, {width, height, base_width, base_height},
131-
which can be passed directly to `vodesfunc.DescaleTarget` or similar functions.
121+
:param base_dimensions: The base dimensions to base the calculations on. This may be derived from
122+
a given clip or a tuple of (Width, Height).
123+
Default: (1920, 1080)
124+
:param target_width: Target width for the descale. This should probably be equal to the base width.
125+
If not provided, this value is calculated using the `target_height`.
126+
Default: None.
127+
:param target_height: Target height for the descale. This should probably be equal to the base height.
128+
If not provided, this value is calculated using the `target_width`.
129+
Default: 720.
130+
:param dar: Display aspect ratio. Used for calculating the width/height if either is None.
131+
This is used for anamorphic sources. If None, derive from `base_dimensions`.
132+
Default: None.
133+
:param func_except: Function returned for custom error handling.
134+
This should only be set by VS package developers.
135+
136+
:return: A dictionary with the keys, {width, height, base_width, base_height},
137+
which can be passed directly to `vodesfunc.DescaleTarget` or similar functions.
132138
"""
133139

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

143-
check_variable_resolution(clip, func)
149+
if isinstance(base_dimensions, vs.VideoNode):
150+
check_variable_resolution(base_dimensions, func)
151+
152+
base_dimensions = (base_dimensions.width, base_dimensions.height)
153+
154+
base_width, base_height = base_dimensions
155+
dar = dar or Dar.from_size(*base_dimensions)
144156

145157
if target_height is None:
146-
target_height = get_h(target_width, clip, 1)
158+
target_height = get_h(target_width, dar, 1)
147159
elif target_width is None:
148-
target_width = get_w(target_height, clip, 1)
160+
target_width = get_w(target_height, dar, 1)
149161

150-
width = clip.width * (target_width - 1) / (clip.width - 1)
151-
height = clip.height * (target_height - 1) / (clip.height - 1)
162+
width = base_width * (target_width - 1) / (base_width - 1)
163+
height = base_height * (target_height - 1) / (base_height - 1)
152164

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

0 commit comments

Comments
 (0)