Skip to content

mixed_rescale

DiffMode

Bases: IntEnum

Mode used to calculate the difference between rescale and input clip.

Source code in vodesfunc/rescale_ext/mixed_rescale.py
class DiffMode(IntEnum):
    """Mode used to calculate the difference between rescale and input clip."""

    MAE = 1
    """Mean Absolute Error"""

    MSE = 2
    """Mean Squared Error"""

    GET_NATIVE = 3
    """Weird headcraft from getnative"""

GET_NATIVE = 3 class-attribute instance-attribute

Weird headcraft from getnative

MAE = 1 class-attribute instance-attribute

Mean Absolute Error

MSE = 2 class-attribute instance-attribute

Mean Squared Error

MixedRB

Implementation of MixedRescale for RescaleBuilder(s)

This is just a stop-gap solution until we (mostly Setsu) can cook up something better.

Example Usage:

upscaler = Waifu2x("trt", 1, fp16=True)

builders = [
    RescaleBuilder(src).descale(Bilinear(border_handling=1), 1280, 720),
    RescaleBuilder(src).descale(BicubicSharp, 1280, 720),
]

# This will be run on all of the above
builders = [
    b.double(upscaler)
    .linemask(KirschTCanny, Bilinear, lthr=50 / 255, hthr=150 / 255, inflate_iter=2)
    .errormask(expand=2)
    .downscale(Hermite(linear=True))
    for b in builders
]

mixed = MixedRB(*builders)
rescaled = mixed.get_upscaled()
Source code in vodesfunc/rescale_ext/mixed_rescale.py
class MixedRB:
    """
    Implementation of MixedRescale for RescaleBuilder(s)

    This is just a stop-gap solution until we (mostly Setsu) can cook up something better.

    Example Usage:

    ```py
    upscaler = Waifu2x("trt", 1, fp16=True)

    builders = [
        RescaleBuilder(src).descale(Bilinear(border_handling=1), 1280, 720),
        RescaleBuilder(src).descale(BicubicSharp, 1280, 720),
    ]

    # This will be run on all of the above
    builders = [
        b.double(upscaler)
        .linemask(KirschTCanny, Bilinear, lthr=50 / 255, hthr=150 / 255, inflate_iter=2)
        .errormask(expand=2)
        .downscale(Hermite(linear=True))
        for b in builders
    ]

    mixed = MixedRB(*builders)
    rescaled = mixed.get_upscaled()
    ```
    """

    def __init__(self, *targets: RescBuildMixed, diffmode: DiffMode = DiffMode.GET_NATIVE, crop_diff: bool = True) -> None:
        """
        A naive per-frame diff approach of trying to get the best descale.
        Credits to Setsu for most of this class.
        """
        y = targets[0].funcutil.work_clip

        for i, d in enumerate(targets):
            d.index = i + 1
            d.diffmode = diffmode
            d.crop_diff = crop_diff
            d._add_index_to_clips()

        prop_srcs = [d.get_diff() for d in targets]
        targets_idx = tuple(range(len(targets)))

        blank = core.std.BlankClip(None, 1, 1, vs.GRAY8, y.num_frames, keep=True)

        map_prop_srcs = [blank.std.CopyFrameProps(prop_src).akarin.Expr("x.PlaneStatsAverage", vs.GRAYS) for prop_src in prop_srcs]

        base_frame, idx_frames = blank.get_frame(0), []

        for i in targets_idx:
            fcurr = base_frame.copy()

            fcurr[0][0, 0] = i

            idx_frames.append((i, fcurr))

        def _select(n: int, f: vs.VideoFrame) -> vs.VideoFrame:
            return min(idx_frames, key=lambda i: f[i[0]][0][0, 0])[1]

        _select_clip = blank.std.ModifyFrame(map_prop_srcs, _select)

        def _selector(clips: list[vs.VideoNode | None]) -> vs.VideoNode:
            base = next(filter(None, clips), None)

            if base is None:
                raise ValueError("Requested clip was None")

            base = base.std.BlankClip(keep=True)
            clips = [c or base for c in clips]

            return core.std.FrameEval(base, lambda n, f: clips[f[0][0, 0]], _select_clip)

        self.upscaled = _selector([t.final()[1] for t in targets])
        self.final = self.upscaled

        self.rescaled = _selector([t.rescaled for t in targets])
        self.credit_mask = _selector([t._return_creditmask() for t in targets])
        self.line_mask = _selector([t._return_linemask() for t in targets])

    def get_upscaled(self, *_) -> vs.VideoNode:
        return self.upscaled

__init__(*targets, diffmode=DiffMode.GET_NATIVE, crop_diff=True)

A naive per-frame diff approach of trying to get the best descale. Credits to Setsu for most of this class.

Source code in vodesfunc/rescale_ext/mixed_rescale.py
def __init__(self, *targets: RescBuildMixed, diffmode: DiffMode = DiffMode.GET_NATIVE, crop_diff: bool = True) -> None:
    """
    A naive per-frame diff approach of trying to get the best descale.
    Credits to Setsu for most of this class.
    """
    y = targets[0].funcutil.work_clip

    for i, d in enumerate(targets):
        d.index = i + 1
        d.diffmode = diffmode
        d.crop_diff = crop_diff
        d._add_index_to_clips()

    prop_srcs = [d.get_diff() for d in targets]
    targets_idx = tuple(range(len(targets)))

    blank = core.std.BlankClip(None, 1, 1, vs.GRAY8, y.num_frames, keep=True)

    map_prop_srcs = [blank.std.CopyFrameProps(prop_src).akarin.Expr("x.PlaneStatsAverage", vs.GRAYS) for prop_src in prop_srcs]

    base_frame, idx_frames = blank.get_frame(0), []

    for i in targets_idx:
        fcurr = base_frame.copy()

        fcurr[0][0, 0] = i

        idx_frames.append((i, fcurr))

    def _select(n: int, f: vs.VideoFrame) -> vs.VideoFrame:
        return min(idx_frames, key=lambda i: f[i[0]][0][0, 0])[1]

    _select_clip = blank.std.ModifyFrame(map_prop_srcs, _select)

    def _selector(clips: list[vs.VideoNode | None]) -> vs.VideoNode:
        base = next(filter(None, clips), None)

        if base is None:
            raise ValueError("Requested clip was None")

        base = base.std.BlankClip(keep=True)
        clips = [c or base for c in clips]

        return core.std.FrameEval(base, lambda n, f: clips[f[0][0, 0]], _select_clip)

    self.upscaled = _selector([t.final()[1] for t in targets])
    self.final = self.upscaled

    self.rescaled = _selector([t.rescaled for t in targets])
    self.credit_mask = _selector([t._return_creditmask() for t in targets])
    self.line_mask = _selector([t._return_linemask() for t in targets])