From 90d490cbf07830176adf6669cdde473b6020f2c5 Mon Sep 17 00:00:00 2001 From: Riven Skaye Date: Fri, 18 Nov 2022 10:56:55 +0100 Subject: [PATCH 1/3] Add mod4 output to `get_w` --- tests/test_vsutil.py | 1 + vsutil/info.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_vsutil.py b/tests/test_vsutil.py index 22b095b..0c8aa19 100644 --- a/tests/test_vsutil.py +++ b/tests/test_vsutil.py @@ -162,6 +162,7 @@ def test_get_w(self): self.assertEqual(vsutil.get_w(480, only_even=False), 853) self.assertEqual(vsutil.get_w(1080, 4 / 3), 1440) self.assertEqual(vsutil.get_w(1080), 1920) + self.assertEqual(vsutil.get_w(849, mod4=True), 1508) def test_iterate(self): def double_number(x: int) -> int: diff --git a/vsutil/info.py b/vsutil/info.py index aaae411..4b79276 100644 --- a/vsutil/info.py +++ b/vsutil/info.py @@ -94,7 +94,7 @@ def get_subsampling(clip: vs.VideoNode, /) -> Union[None, str]: raise ValueError('Unknown subsampling.') -def get_w(height: int, aspect_ratio: float = 16 / 9, *, only_even: bool = True) -> int: +def get_w(height: int, aspect_ratio: float = 16 / 9, *, only_even: bool = True, mod4: bool = False) -> int: """Calculates the width for a clip with the given height and aspect ratio. >>> get_w(720) @@ -107,10 +107,14 @@ def get_w(height: int, aspect_ratio: float = 16 / 9, *, only_even: bool = True) :param only_even: Will return the nearest even integer. ``True`` by default because it imitates the math behind most standard resolutions (e.g. 854x480). + :param mod4: Ensure output is mod4, for when subsampling and/or interlacing require it. + Implies ``only_even`` as mod4 is always even as well. :return: Calculated width based on input `height`. """ width = height * aspect_ratio + if mod4: + return round(width / 4) * 4 if only_even: return round(width / 2) * 2 return round(width) From 1c0ffeebeb4c1fcc84d7358128adfc0a4aa07450 Mon Sep 17 00:00:00 2001 From: Riven Skaye Date: Fri, 18 Nov 2022 23:22:04 +0100 Subject: [PATCH 2/3] Use `mod` and old behavior, deprecate `only_even` --- vsutil/info.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/vsutil/info.py b/vsutil/info.py index 4b79276..0c9a15d 100644 --- a/vsutil/info.py +++ b/vsutil/info.py @@ -94,7 +94,7 @@ def get_subsampling(clip: vs.VideoNode, /) -> Union[None, str]: raise ValueError('Unknown subsampling.') -def get_w(height: int, aspect_ratio: float = 16 / 9, *, only_even: bool = True, mod4: bool = False) -> int: +def get_w(height: int, aspect_ratio: float = 16 / 9, *, only_even: Optional[bool] = None, mod: Optional[int] = None) -> int: """Calculates the width for a clip with the given height and aspect ratio. >>> get_w(720) @@ -107,17 +107,22 @@ def get_w(height: int, aspect_ratio: float = 16 / 9, *, only_even: bool = True, :param only_even: Will return the nearest even integer. ``True`` by default because it imitates the math behind most standard resolutions (e.g. 854x480). - :param mod4: Ensure output is mod4, for when subsampling and/or interlacing require it. - Implies ``only_even`` as mod4 is always even as well. + This parameter has been deprecated in favor of the ``mod`` param. For old behavior + use ``mod=2`` for ``True`` and ``mod=1`` for ``False`` + :param mod: Ensure output is divisible by this number, for when subsampling or filter + restrictions set specific requirements (e.g. 4 for interlaced content). + Defaults to 2 to mimic the math behind most standard resolutions. + Any values passed to this argument will override ``only_even`` behavior! :return: Calculated width based on input `height`. """ width = height * aspect_ratio - if mod4: - return round(width / 4) * 4 - if only_even: - return round(width / 2) * 2 - return round(width) + if only_even is not None: + import warnings + warnings.warn("only_even is deprecated.", DeprecationWarning) + + mod = func.fallback(mod, 2 if only_even in [None, True] else 1) + return round(width / mod) * mod def is_image(filename: str, /) -> bool: From 1c0ffee1980619184be4137909fe3dec3e4c0873 Mon Sep 17 00:00:00 2001 From: Riven Skaye Date: Fri, 18 Nov 2022 23:25:44 +0100 Subject: [PATCH 3/3] Adjust test case --- tests/test_vsutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_vsutil.py b/tests/test_vsutil.py index 0c8aa19..f8a75e0 100644 --- a/tests/test_vsutil.py +++ b/tests/test_vsutil.py @@ -162,7 +162,7 @@ def test_get_w(self): self.assertEqual(vsutil.get_w(480, only_even=False), 853) self.assertEqual(vsutil.get_w(1080, 4 / 3), 1440) self.assertEqual(vsutil.get_w(1080), 1920) - self.assertEqual(vsutil.get_w(849, mod4=True), 1508) + self.assertEqual(vsutil.get_w(849, mod=4), 1508) def test_iterate(self): def double_number(x: int) -> int: