Ejemplo n.º 1
0
def antialias(clip: vs.VideoNode,
              stronger: Optional[List[Range]] = None,
              strong: Optional[List[Range]] = None,
              weak: Optional[List[Range]] = None,
              noaa: Optional[List[Range]] = None,
              dehalo: bool = True,
              sharpen: bool = False) -> vs.VideoNode:
    def _sraa_pp_sharpdehalo(sraa: vs.VideoNode) -> vs.VideoNode:
        if sharpen:  # all this really seems to do is make the haloing worse, will not be using!
            y = LSFmod(vsutil.get_y(sraa),
                       strength=70,
                       Smode=3,
                       edgemode=0,
                       source=vsutil.get_y(clip))
            sraa = core.std.ShufflePlanes([y, sraa],
                                          planes=[0, 1, 2],
                                          colorfamily=vs.YUV)
        sraa = MaskedDHA(sraa, rx=1.7, ry=1.7, darkstr=0,
                         brightstr=0.75) if dehalo else sraa
        return sraa

    mask = combine_mask(clip, weak or [])
    clamp = sraa_clamp(clip, mask=mask, postprocess=_sraa_pp_sharpdehalo)
    sraa = core.std.MaskedMerge(
        clip,
        upscaled_sraa(clip, rfactor=2, downscaler=Bicubic(b=0, c=1 / 2).scale),
        mask)
    sraa13 = upscaled_sraa(clip,
                           rfactor=1.3,
                           downscaler=Bicubic(b=0, c=1 / 2).scale)
    return replace_ranges(
        replace_ranges(replace_ranges(clamp, clip, noaa or []), sraa, strong
                       or []), sraa13, stronger or [])
Ejemplo n.º 2
0
def transpose_sraa(clip: vs.VideoNode, **aa_args: Dict[str,
                                                       Any]) -> vs.VideoNode:
    from lvsfunc.aa import upscaled_sraa
    from lvsfunc.kernels import Bicubic

    sraa_args: Dict[str, Any] = dict(rfactor=1.3, downscaler=Bicubic().scale)
    sraa_args |= aa_args

    aa = upscaled_sraa(clip.std.Transpose(), **sraa_args)
    return upscaled_sraa(aa.std.Transpose(), **sraa_args)
Ejemplo n.º 3
0
def antialias(clip: vs.VideoNode,
              weak: Optional[List[Range]] = None,
              strong: Optional[List[Range]] = None,
              stronger: Optional[List[Range]] = None,
              noaa: Optional[List[Range]] = None) -> vs.VideoNode:
    mask = partial(antialiasing.combine_mask, weak=weak or [])
    clamp = antialiasing.sraa_clamp(clip, mask=mask)
    sraa = upscaled_sraa(clip, rfactor=2)
    sraa = core.std.MaskedMerge(clip, sraa, mask(sraa))
    sraa_13 = upscaled_sraa(clip, rfactor=1.3)
    sraa_13 = core.std.MaskedMerge(clip, sraa_13, mask(sraa_13))
    return replace_ranges(
        replace_ranges(replace_ranges(clamp, clip, noaa or []), sraa, strong
                       or []), sraa_13, stronger or [])
Ejemplo n.º 4
0
def antialias(clip: vs.VideoNode, strong: Optional[List[Range]] = None,
              sangnom: Optional[List[Tuple[Range, List[BoundingBox]]]] = None) -> vs.VideoNode:
    clamp = sraa_clamp(clip, mask=mask_strong)
    if strong or sangnom:
        sraa_13 = upscaled_sraa(clip, rfactor=1.3)
        sraa_13 = core.std.MaskedMerge(clip, sraa_13, mask_strong(sraa_13))
        clamp = replace_ranges(clamp, sraa_13, strong or [])
    if sangnom:
        sn = upscaled_sraa(sraa_13, aafun=lambda c: c.sangnom.SangNom())
        sn = core.std.MaskedMerge(sraa_13, sn, mask_strong(sn))
        for r, ms in sangnom:
            mask = core.std.BlankClip(clip.std.ShufflePlanes(planes=0, colorfamily=vs.GRAY))
            for m in ms:
                mask = core.std.Expr([mask, m.get_mask(clip)], "x y max")
            clamp = replace_ranges(clamp, core.std.MaskedMerge(clamp, sn, mask), r)
    return clamp
Ejemplo n.º 5
0
def antialiasing(clip: vs.VideoNode, strength: float = 1.4) -> vs.VideoNode:
    from lvsfunc.aa import clamp_aa, nneedi3_clamp, upscaled_sraa

    bits, clip = _get_bits(clip)

    aa_weak = nneedi3_clamp(clip)
    aa_str = upscaled_sraa(clip)
    aa_clamped = clamp_aa(clip, aa_weak, aa_str, strength=strength)
    return aa_clamped if bits == 16 else depth(aa_clamped, bits)
Ejemplo n.º 6
0
def _sraa_frameeval(n: int, clip: vs.VideoNode, w: int,
                    h: int) -> vs.VideoNode:
    frame = clip.get_frame(n)
    if frame.height < 1080:
        rfactor = 2.5
    else:
        rfactor = 1.5
    return upscaled_sraa(clip.resize.Bicubic(frame.width, frame.height),
                         rfactor=rfactor,
                         h=h,
                         ar=w / h)
Ejemplo n.º 7
0
def clamped_aa(clip: vs.VideoNode,
               rep: int = 17,
               strength: float = 1.0,
               **sraa_args: Dict[str, Any]) -> vs.VideoNode:
    from lvsfunc.aa import clamp_aa, nnedi3, taa, upscaled_sraa

    aa_args: Dict[str, Any] = dict(rfactor=1.35)
    aa_args |= sraa_args

    aa_weak = taa(clip, nnedi3(opencl=True))
    aa_strong = upscaled_sraa(clip, **aa_args)
    aa_clamped = clamp_aa(clip, aa_weak, aa_strong, strength=strength)
    return core.rgvs.Repair(aa_clamped, clip, rep)
Ejemplo n.º 8
0
def stupid_op_scenefilter(aa: vs.VideoNode, deb: vs.VideoNode,
                          start: Optional[int]) -> vs.VideoNode:
    if start is None:
        return aa
    aastrong, masks = get_op_scenefilters(start)
    sraa = replace_ranges(
        aa,
        upscaled_sraa(deb, rfactor=2, downscaler=Bicubic(b=0, c=1 / 2).scale),
        aastrong)
    for mask in masks:
        sraa = replace_ranges(
            sraa, core.std.MaskedMerge(sraa, aa, mask.get_mask(deb, deb)),
            mask.ranges)
    return sraa
Ejemplo n.º 9
0
def wd_scenefilter(aa: vs.VideoNode, deb: vs.VideoNode,
                   start: Optional[int]) -> vs.VideoNode:
    # this does icky things to the credits but until we get NC it's way worth it
    if start is None:
        return aa
    WD_AA_STRONG: List[Range] = [
        (start + 26, start + 33),
        (start + 34, start + 41),
        (start + 984, start + 1076),
        (start + 1077, start + 1169),
        (start + 1170, start + 1262),
        (start + 1263, start + 1355),
        (start + 1468, start + 1617),
    ]
    sraa = upscaled_sraa(deb,
                         rfactor=1.6,
                         downscaler=Bicubic(b=0, c=1 / 2).scale)
    return replace_ranges(aa, sraa, WD_AA_STRONG)
Ejemplo n.º 10
0
def main() -> Union[vs.VideoNode, Tuple[vs.VideoNode, ...]]:
    """Vapoursynth filtering"""
    # Don't worry, this script is barely readable anymore even for me
    import rekt
    from awsmfunc import bbmod
    from havsfunc import ContraSharpening
    from lvsfunc.aa import upscaled_sraa
    from lvsfunc.comparison import stack_compare
    from lvsfunc.mask import BoundingBox
    from lvsfunc.util import replace_ranges
    from vardefunc import dcm
    from vardefunc.deband import dumb3kdb
    from vardefunc.mask import FreyChen
    from vsutil import depth, get_y, insert_clip

    src = JP_BD.clip_cut
    b = core.std.BlankClip(src, length=1)

    if opstart is not False:
        src_NCOP = NCOP.clip_cut
        op_scomp = stack_compare(
            src[opstart:opstart + src_NCOP.num_frames - 1] + b,
            src_NCOP[:-op_offset] + b,
            make_diff=True)  # noqa
    if edstart is not False:
        src_NCED = NCED.clip_cut
        #ed_scomp = stack_compare(src[edstart:edstart+src_NCED.num_frames-1]+b, src_NCED[:-ed_offset]+b, make_diff=True)  # noqa

    # Masking credits
    op_mask = dcm(
        src, src[opstart:opstart+src_NCOP.num_frames], src_NCOP,
        start_frame=opstart, thr=25, prefilter=True) if opstart is not False \
        else get_y(core.std.BlankClip(src))
    ed_mask = dcm(
        src, src[edstart:edstart+src_NCED.num_frames], src_NCED,
        start_frame=edstart, thr=25, prefilter=True) if edstart is not False \
        else get_y(core.std.BlankClip(src))
    credit_mask = core.std.Expr([op_mask, ed_mask], expr='x y +')
    credit_mask = depth(credit_mask, 16).std.Binarize()

    # Fixing an animation f**k-up
    for f in op_replace:
        src = insert_clip(src, src[f], f - 1)

    # Edgefixing
    ef = rekt.rektlvls(
        src,
        prot_val=[16, 235],
        min=16,
        max=235,
        rownum=[0, src.height - 1],
        rowval=[16, 16],
        colnum=[0, src.width - 1],
        colval=[16, 16],
    )

    bb_y = bbmod(ef,
                 left=1,
                 top=1,
                 right=1,
                 bottom=1,
                 thresh=32,
                 y=True,
                 u=False,
                 v=False)
    bb_uv = bbmod(bb_y,
                  left=2,
                  top=2,
                  right=2,
                  bottom=2,
                  y=False,
                  u=True,
                  v=True)
    bb32 = depth(bb_uv, 32)

    # --- Very specific filtering that should probably be removed for future episodes entirely
    # Fading for some... well, fades.
    fade = flt.fader(bb32, start_frame=4437, end_frame=4452)
    fade = flt.fader(fade, start_frame=18660, end_frame=18697)

    # Different sport names
    rkt_aa_ranges: Union[Union[int, None, Tuple[Optional[int], Optional[int]]],
                         List[Union[int, None,
                                    Tuple[Optional[int],
                                          Optional[int]]]], None] = [  # noqa
                                              (8824, 8907), (9828, 9911),
                                              (10995, 11078), (11904, 12010)
                                          ]

    rkt_aa = rekt.rekt_fast(fade,
                            fun=lambda x: upscaled_sraa(x),
                            top=854,
                            left=422,
                            bottom=60,
                            right=422)
    rkt_aa = replace_ranges(fade, rkt_aa, rkt_aa_ranges)

    # QP GET!
    rkt_aa2_ranges: Union[Union[int, None, Tuple[Optional[int],
                                                 Optional[int]]],
                          List[Union[int, None,
                                     Tuple[Optional[int],
                                           Optional[int]]]], None] = [  # noqa
                                               (26333, 26333), (28291, 28344),
                                               (30271, 30364)
                                           ]
    rkt_aa2 = rekt.rekt_fast(rkt_aa,
                             fun=lambda x: upscaled_sraa(x),
                             top=714,
                             left=354,
                             bottom=224,
                             right=354)
    rkt_aa2 = replace_ranges(fade, rkt_aa, rkt_aa2_ranges)

    # Table stuff
    rkt_aa3_ranges: Union[Union[int, None, Tuple[Optional[int],
                                                 Optional[int]]],
                          List[Union[int, None,
                                     Tuple[Optional[int],
                                           Optional[int]]]], None] = [  # noqa
                                               (32797, 32952)
                                           ]
    rkt_aa3 = rekt.rekt_fast(rkt_aa2,
                             fun=lambda x: upscaled_sraa(x),
                             top=488,
                             left=312,
                             bottom=390,
                             right=850)
    rkt_aa3 = replace_ranges(fade, rkt_aa2, rkt_aa3_ranges)

    rkt_aa = rkt_aa3

    # Clipping error Nyatalante/Achilles
    fixed_frame = source(
        JP_BD.workdir.to_str() + r"/assets/01/FGCBD_01_8970_fixed.png", rkt_aa)
    fix_error = replace_ranges(rkt_aa, fixed_frame, [(8968, 8970)])

    sp_out32 = fix_error
    sp_out16 = depth(sp_out32, 16)

    # Rescaling
    l_mask = FreyChen().get_mask(get_y(sp_out16)).morpho.Close(size=6)
    l_mask = core.std.Binarize(l_mask, 24 << 8).std.Maximum().std.Maximum()
    scaled, credit_mask = flt.rescaler(sp_out32,
                                       height=720,
                                       shader_file=shader)
    scaled = core.std.MaskedMerge(sp_out16, scaled, l_mask)
    scaled = core.std.MaskedMerge(scaled, sp_out16, credit_mask)

    # Denoising
    denoise = flt.multi_denoise(scaled, l_mask)

    # Anti-aliasing
    aa_clamped = flt.clamp_aa(denoise, strength=3.0)
    aa_rfs = replace_ranges(denoise, aa_clamped, aa_ranges)

    # Fix edges because they get f****d during the denoising and AA stages
    box = BoundingBox((1, 1), (src.width - 2, src.height - 2))
    fix_edges = core.std.MaskedMerge(scaled, aa_rfs, box.get_mask(scaled))

    # Regular debanding, but then...
    deband = flt.multi_debander(fix_edges, sp_out16)

    # --- More very specific filtering that should almost definitely be removed for other episodes
    boxes = [
        BoundingBox((0, 880), (1920, 200)),
        BoundingBox((1367, 0), (552, 1080)),
        BoundingBox((1143, 0), (466, 83)),
        BoundingBox((1233, 84), (237, 84)),
    ]

    boxed_deband = flt.placebo_debander(fix_edges,
                                        iterations=2,
                                        threshold=8,
                                        radius=14,
                                        grain=4)
    boxed_mask = core.std.BlankClip(
        boxed_deband.std.ShufflePlanes(planes=0, colorfamily=vs.GRAY))
    for bx in boxes:  # Gonna be awfully slow but OH WELLLLLL
        boxed_mask = core.std.Expr(
            [boxed_mask, bx.get_mask(boxed_mask)], "x y max")

    boxed_deband_merged = core.std.MaskedMerge(deband, boxed_deband,
                                               boxed_mask)
    boxed_deband_merged = replace_ranges(deband, boxed_deband_merged,
                                         [(20554, 20625)])

    stronger_deband = dumb3kdb(fix_edges, radius=18, threshold=[48, 32])
    stronger_deband = replace_ranges(boxed_deband_merged, stronger_deband,
                                     [(22547, 22912), (28615, 28846),
                                      (33499, 33708), (37223, 37572),
                                      (37636, 37791), (38047, 38102),
                                      (38411, 38733)])

    deband_csharp = ContraSharpening(stronger_deband, scaled, 17)

    sp_out = deband_csharp

    # Back to regular graining
    grain = flt.grain(sp_out)

    return grain
Ejemplo n.º 11
0
def sraa(clip: vs.VideoNode,
         rfactor: list[float] = [1.5, 1.5, 1.5],
         planes: list[int] = [0, 1, 2]) -> vs.VideoNode:
    from typing import Callable
    from lvsfunc.aa import upscaled_sraa
    from vsutil import get_y, split, join, get_w
    from functools import partial

    if isinstance(rfactor, float): rfactor = [rfactor, rfactor, rfactor]

    def nnedi3(opencl: bool = True,
               **override: Any) -> Callable[[vs.VideoNode], vs.VideoNode]:

        nnedi3_args: Dict[str, Any] = dict(field=0,
                                           dh=True,
                                           nsize=3,
                                           nns=3,
                                           qual=1)
        nnedi3_args.update(override)

        def _nnedi3(clip: vs.VideoNode) -> vs.VideoNode:
            return clip.nnedi3cl.NNEDI3CL(**nnedi3_args) if opencl \
                else clip.nnedi3.nnedi3(**nnedi3_args)

        return _nnedi3

    def _nnedi3_supersample(clip: vs.VideoNode,
                            width: int,
                            height: int,
                            opencl: bool = True) -> vs.VideoNode:

        nnargs: Dict[str, Any] = dict(field=0, dh=True, nsize=0, nns=4, qual=2)
        _nnedi3 = nnedi3(opencl=opencl, **nnargs)
        up_y = _nnedi3(get_y(clip))
        up_y = up_y.resize.Spline36(height=height, src_top=0.5).std.Transpose()
        up_y = _nnedi3(up_y)
        up_y = up_y.resize.Spline36(height=width, src_top=0.5)
        return up_y

    def eedi3(opencl: bool = True,
              **override: Any) -> Callable[[vs.VideoNode], vs.VideoNode]:

        eedi3_args: Dict[str, Any] = dict(field=0,
                                          dh=True,
                                          alpha=0.25,
                                          beta=0.5,
                                          gamma=40,
                                          nrad=2,
                                          mdis=20)
        eedi3_args.update(override)

        def _eedi3(clip: vs.VideoNode) -> vs.VideoNode:
            return clip.eedi3m.EEDI3CL(**eedi3_args) if opencl \
                else clip.eedi3m.EEDI3(**eedi3_args)

        return _eedi3

    def _eedi3_singlerate(clip: vs.VideoNode,
                          opencl: bool = False) -> vs.VideoNode:
        eeargs: Dict[str, Any] = dict(field=0,
                                      dh=False,
                                      alpha=0.2,
                                      beta=0.6,
                                      gamma=40,
                                      nrad=2,
                                      mdis=20)
        nnargs: Dict[str, Any] = dict(field=0,
                                      dh=False,
                                      nsize=0,
                                      nns=4,
                                      qual=2)
        y = get_y(clip)
        return eedi3(sclip=nnedi3(**nnargs)(y), **eeargs, opencl=opencl)(y)

    planar = split(clip)

    if 0 in planes:
        planar[0] = upscaled_sraa(planar[0],
                                  rfactor=rfactor[0],
                                  supersampler=partial(_nnedi3_supersample,
                                                       opencl=True),
                                  aafun=partial(_eedi3_singlerate,
                                                opencl=True))
    if 1 in planes:
        planar[1] = upscaled_sraa(planar[1],
                                  rfactor=rfactor[1],
                                  supersampler=partial(_nnedi3_supersample,
                                                       opencl=False),
                                  aafun=partial(_eedi3_singlerate,
                                                opencl=False))
    if 2 in planes:
        planar[2] = upscaled_sraa(planar[2],
                                  rfactor=rfactor[2],
                                  supersampler=partial(_nnedi3_supersample,
                                                       opencl=False),
                                  aafun=partial(_eedi3_singlerate,
                                                opencl=False))

    return join(planar)