Skip to content

Commit

Permalink
feat: update Two different function for BD Project (#3)
Browse files Browse the repository at this point in the history
* Update __init__.py

* Update __init__.py

* Update zvs.py

* Update xvs.py

* Update __init__.py

* Delete mbfunc/nnedi3_resample/nnedi3_resampleCL.py

* Delete mbfunc/nnedi3_resample/znedi3_resample.py

* Delete mbfunc/nnedi3_resample/nnedi3_resample.py

* Add files via upload

* Update __init__.py

* Update __init__.py

* Create __init__.py

* Add files via upload

* Update xvs.py

* Update sharpen.py

* Update sharpen.py

* Update sharpen.py

* Update sharpen.py

* Update __init__.py

* Add files via upload

* Add files via upload

* Update sharpen.py

* fix ci

---------

Co-authored-by: Tohrusky <65994850+Tohrusky@users.noreply.github.com>
  • Loading branch information
meibanf and Tohrusky authored Jan 17, 2025
1 parent 46b1c1b commit f6c0e0e
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 2,113 deletions.
2 changes: 1 addition & 1 deletion mbfunc/dehalo/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from mbfunc.dehalo.dehalo import FineDehalo, DeHalo_alpha # noqa
from .dehalo import * # noqa
1 change: 1 addition & 0 deletions mbfunc/enhance/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .sharpen import * # noqa
109 changes: 109 additions & 0 deletions mbfunc/enhance/sharpen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
from typing import Any

import vapoursynth as vs
from vapoursynth import core


def mwenhance(
diffclip: vs.VideoNode,
chroma: bool = False,
strength: float = 2.0,
szrp8: int = 8,
spwr: float = 1 / 4,
sdmp_lo: int = 4,
sdmp_hi: int = 48,
soft: float = 0,
expr: bool = True,
) -> vs.VideoNode:
if not isinstance(diffclip, vs.VideoNode):
raise TypeError("mwenhance: this is not a clip")

if strength < 0.0:
return diffclip

# Constants values
bits = diffclip.format.bits_per_sample
bps_mul8 = 1 << (bits - 8)
floor = 0
ceil = (1 << bits) - 1
neutral = 1 << (bits - 1)
szrp = szrp8 * bps_mul8
szrp8_sqr = szrp8**2
szrp_mul_strength = szrp * strength
szrp8_sqr_plus_sdmp_lo = szrp8_sqr + sdmp_lo
if sdmp_hi == 0:
szrp8_div_sdmp_hi_power4_plus_1 = 1.0
else:
szrp8_div_sdmp_hi_power4_plus_1 = (szrp8 / sdmp_hi) ** 4 + 1.0

if expr:
# Expr version
abs_diff_expr = f"x {neutral} - abs"
abs_diff8_expr = f"x {neutral} - {bps_mul8} / abs"
diff8_sqr_expr = f"x {neutral} - {bps_mul8} / x {neutral} - {bps_mul8} / *"
sign_mul_expr = f"x {neutral} - 0 >= 1 -1 ?"

res1_expr = f"{abs_diff_expr} {szrp} / {spwr} pow " f"{szrp_mul_strength} * {sign_mul_expr} *"

res2_expr = (
f"{diff8_sqr_expr} " f"{szrp8_sqr_plus_sdmp_lo} * " f"{diff8_sqr_expr} {sdmp_lo} + " f"{szrp8_sqr} * / "
)

if sdmp_hi == 0:
res3_expr = "0"
else:
res3_expr = f"{abs_diff8_expr} {sdmp_hi} / 4 pow"

enhanced_expr = (
f"x {neutral} = "
f"x {ceil} {floor} {neutral} "
f"{res1_expr} "
f"{res2_expr} * "
f"{szrp8_div_sdmp_hi_power4_plus_1} * "
f"1 {res3_expr} + / + max min ?"
)

enhanced_expr = f"x {neutral} = x {enhanced_expr} ?"

if diffclip.format.num_planes == 1:
enhanced_clip = core.akarin.Expr(diffclip, [enhanced_expr])
else:
enhanced_clip = core.akarin.Expr(
diffclip, [enhanced_expr, enhanced_expr if chroma else "", enhanced_expr if chroma else ""]
)

else:
# Orginal version
def _enhance_function(x: Any) -> int:
abs_diff = abs(x - neutral)
abs_diff8 = abs((x - neutral) / bps_mul8)
diff8_sqr = ((x - neutral) / bps_mul8) ** 2
sign_mul = 1 if x - neutral >= 0 else -1

res1 = ((abs_diff / szrp) ** spwr) * szrp_mul_strength * sign_mul
res2 = res2 = (diff8_sqr * szrp8_sqr_plus_sdmp_lo) / ((diff8_sqr + sdmp_lo) * szrp8_sqr)
res3 = 0 if sdmp_hi == 0 else (abs_diff8 / sdmp_hi) ** 4
enhanced = (res1 * res2 * szrp8_div_sdmp_hi_power4_plus_1) / (1 + res3)

val = round(neutral + enhanced)
return min(ceil, max(floor, val))

enhanced_clip = diffclip.std.Lut([0, 1, 2] if chroma else [0], function=_enhance_function)

# Optional softening
if soft > 0:
softened_clip = core.rgvs.RemoveGrain(enhanced_clip, [19] * enhanced_clip.format.num_planes)
if soft < 1:
result = core.std.Merge(enhanced_clip, softened_clip, [soft])
else:
result = softened_clip
limit_expr = f"x {neutral} - abs y {neutral} - abs <= x y ?"
if enhanced_clip.format.num_planes == 1:
result = core.akarin.Expr([enhanced_clip, result], [limit_expr])
else:
result = core.akarin.Expr(
[enhanced_clip, result], [limit_expr, limit_expr if chroma else "", limit_expr if chroma else ""]
)
return result
else:
return enhanced_clip
1 change: 1 addition & 0 deletions mbfunc/mask/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .mask import * # noqa
42 changes: 42 additions & 0 deletions mbfunc/mask/mask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Optional

import mvsfunc as mvf
import vapoursynth as vs


def mwlmask(
clip: vs.VideoNode, l1: int = 80, h1: int = 96, h2: Optional[int] = None, l2: Optional[int] = None
) -> vs.VideoNode:
# Constants values
sbitPS = clip.format.bits_per_sample
black = 0
white = (1 << sbitPS) - 1
l1_ = l1 << (sbitPS - 8)
h1_ = h1 << (sbitPS - 8)

if h2 is None:
h2_ = white
else:
h2_ = h2 << (sbitPS - 8)

if l2 is None:
l2_ = white
else:
l2_ = l2 << (sbitPS - 8)

# Base expression for the second ramp
if h2_ >= white:
expr2 = f"{white}"
else:
slope2 = white / (h2_ - l2_)
expr2 = f"x {h2_} <= {white} x {l2_} < x {l2_} - {slope2} * {black} ? ?"

# Expression for the first ramp
slope1 = white / (h1_ - l1_)
expr = f"x {l1_} <= {black} " f"x {h1_} < x {l1_} - {slope1} * {expr2} ? ?"

# Process only luma
plane_y = mvf.GetPlane(clip, 0)
plane_y = plane_y.rgvs.RemoveGrain(4)
mask = plane_y.std.Expr(expr=expr)
return mask
1 change: 1 addition & 0 deletions mbfunc/nnedi3_resample/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .nnedi3_resample import * # noqa
2 changes: 0 additions & 2 deletions mbfunc/nnedi3_resample/nnedi3_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import vapoursynth as vs
from vapoursynth import core

__version__ = "2"


def nnedi3_resample(
input,
Expand Down
Loading

0 comments on commit f6c0e0e

Please sign in to comment.