示例#1
0
文件: tests.py 项目: dnjulek/kagefunc
 def test_retinex_edgemask(self):
     mask = kgf.retinex_edgemask(self.BLACK_SAMPLE_CLIP)
     self.assert_same_bitdepth(mask, self.BLACK_SAMPLE_CLIP)
     self.assert_same_length(mask, self.BLACK_SAMPLE_CLIP)
     self.assertEqual(mask.format.color_family, vs.GRAY)
     # request a frame to see if that errors
     mask.get_frame(0)
示例#2
0
 def _ret_mask(clip: vs.VideoNode, thr: int) -> vs.VideoNode:
     mask = kgf.retinex_edgemask(clip)
     mask = core.std.Median(mask).std.Binarize(thr)
     mask = iterate(mask, core.std.Median, 2)
     mask = iterate(mask, core.std.Maximum, 3)
     mask = iterate(mask, core.std.Minimum, 2)
     return mask
示例#3
0
def _fsrlineart(clip: vs.VideoNode, width: int, height: int) -> vs.VideoNode:
    clip = clip.resize.Point(1440, 810)
    assert clip.format is not None
    nn = depth(nnedi3_double(depth(clip, 16)), clip.format.bits_per_sample)
    fsr = fsrcnnx_upscale(clip, width, height, upscaled_smooth=nn, shader_file=FSRCNNX)
    mask = retinex_edgemask(depth(fsr.std.ShufflePlanes(0, vs.GRAY), 16))
    mask = mask.std.Binarize(scale_thresh(0.65, mask)).std.Maximum()
    mask = depth(mask, clip.format.bits_per_sample, range_in=CRange.FULL, range=CRange.FULL)
    return core.std.MaskedMerge(nn.resize.Bicubic(width, height, filter_param_a=0, filter_param_b=1/2), fsr, mask)
示例#4
0
def nneedi3_clamp(clip: vs.VideoNode,
                  mask: vs.VideoNode = None,
                  ret_mask: bool = False, show_mask: bool = False,
                  opencl: bool = False,
                  strength: int = 1,
                  alpha: float = 0.25, beta: float = 0.5, gamma: int = 40,
                  nrad: int = 2, mdis: int = 20,
                  nsize: int = 3, nns: int = 3,
                  qual: int = 1) -> vs.VideoNode:
    funcname = "nneedi3_clamp"
    """
    Script written by Zastin. What it does is clamp the "change" done by eedi3 to the "change" of nnedi3.
    This should fix every issue created by eedi3. For example: https://i.imgur.com/hYVhetS.jpg

    :param mask:                Allows for user to use their own mask
    :param ret_mask: bool:   Whether or not to use a binarized kgf.retinex_edgemask
                                to replace more lineart with nnedi3
    :param show_mask: bool:     Whether or not to return the mask instead of the processed clip
    :param opencl: bool:        Allows TAAmbk to use opencl acceleration when anti-aliasing
    :param strength:            (Default value = 1)
    :param alpha: float:        (Default value = 0.25)
    :param beta: float:         (Default value = 0.5)
    :param gamma:               (Default value = 40)
    :param nrad:                (Default value = 2)
    :param mdis:                (Default value = 20)
    :param nsize:               (Default value = 3)
    :param nns:                 (Default value = 3)
    :param qual:                (Default value = 1)
    """
    bits = clip.format.bits_per_sample - 8
    thr = strength * (1 >> bits)
    strong = TAAmbk(clip, aatype='Eedi3', alpha=alpha, beta=beta, gamma=gamma, nrad=nrad, mdis=mdis, mtype=0,
                    opencl=opencl)
    weak = TAAmbk(clip, aatype='Nnedi3', nsize=nsize, nns=nns, qual=qual, mtype=0, opencl=opencl)
    expr = 'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr)

    if clip.format.num_planes > 1:
        expr = [expr, '']
    aa = core.std.Expr([strong, weak, clip], expr)

    if mask:
        merged = clip.std.MaskedMerge(aa, mask, planes=0)
    elif ret_mask:
        mask = kgf.retinex_edgemask(clip, 1).std.Binarize()
        merged = clip.std.MaskedMerge(aa, mask, planes=0)
    else:
        mask = clip.std.Prewitt(planes=0).std.Binarize(planes=0).std.Maximum(planes=0).std.Convolution([1] * 9, planes=0)
        mask = get_y(mask)
        merged = clip.std.MaskedMerge(aa, mask, planes=0)

    if show_mask:
        return mask
    return merged if clip.format.color_family == vs.GRAY else core.std.ShufflePlanes([merged, clip], [0, 1, 2], vs.YUV)
示例#5
0
def _inverse_mask(clip: vs.VideoNode,
                  reupscaled: vs.VideoNode,
                  franges: List[FadeRange] = [],
                  dranges: List[Range] = []) -> vs.VideoNode:
    reupscaled = reupscaled.resize.Bicubic(format=clip.format.id)
    line_mask = retinex_edgemask(clip, 0.0001).std.Binarize(10000)
    fade_mask = _fade_ranges_with_refs(clip, reupscaled, franges)
    mask = core.std.Expr([fade_mask, line_mask.std.Invert()], "x y +")
    if dranges:
        detail_mask = descale_detail_mask(clip, reupscaled, threshold=1500)
        mask_with_detail = core.std.Expr([mask, detail_mask], "x y +")
        mask = replace_ranges(mask, mask_with_detail, dranges)
    return mask.resize.Bicubic(format=clip.format.id)
示例#6
0
def do_filter():
    """Vapoursynth filtering"""
    def _nneedi3_clamp(clip: vs.VideoNode, strength: int = 1)-> vs.VideoNode:
        bits = clip.format.bits_per_sample - 8
        thr = strength * (1 >> bits)

        luma = get_y(clip)

        def _strong(clip: vs.VideoNode)-> vs.VideoNode:
            args = dict(alpha=0.25, beta=0.5, gamma=40, nrad=2, mdis=20, vcheck=3)
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip, luma.width, luma.height, src_left=-.5, src_top=-.5)

        def _weak(clip: vs.VideoNode)-> vs.VideoNode:
            args = dict(nsize=3, nns=2, qual=2)
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip, luma.width, luma.height, src_left=-.5, src_top=-.5)

        clip_aa = core.std.Expr([_strong(luma), _weak(luma), luma],
                                'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr))
        return vdf.merge_chroma(clip_aa, clip)

    src = JPBD.src_cut
    src = depth(src, 16)

    denoise = CoolDegrain(src, tr=2, thsad=48, blksize=8, overlap=4, plane=4)


    antialias = _nneedi3_clamp(denoise)


    predenoise = mClean(antialias, thSAD=200, chroma=False)
    detail_mask = lvf.denoise.detail_mask(predenoise, rad=2, radc=2, brz_a=3250, brz_b=1250)
    ret_mask = kgf.retinex_edgemask(predenoise).std.Binarize(9250).std.Median().std.Inflate()
    line_mask = core.std.Expr([detail_mask, ret_mask], 'x y max')


    deband = core.neo_f3kdb.Deband(antialias, 17, 42, 42, 42, 12, 0, sample_mode=4, keep_tv_range=True)
    deband = core.std.MaskedMerge(deband, antialias, line_mask)


    grain = kgf.adaptive_grain(deband, 0.25, luma_scaling=10)

    final = core.resize.Bicubic(grain, format=vs.YUV420P10, dither_type='error_diffusion')
    final = core.std.Limiter(final, 16, [235 << 2, 240 << 2])

    return depth(final, 10)
示例#7
0
文件: lvsfunc.py 项目: RisGar/lvsfunc
def nneedi3_clamp(clip: vs.VideoNode, strength: int = 1,
                  mask: vs.VideoNode = None,
                  ret_mask: bool = False, show_mask: bool = False,
                  opencl: bool = False) -> vs.VideoNode:
    funcname = "nneedi3_clamp"
    """
    Script written by Zastin. What it does is clamp the "change" done by eedi3 to the "change" of nnedi3.
    This should fix every issue created by eedi3. For example: https://i.imgur.com/hYVhetS.jpg

    :param strength:            Set threshold strength
    :param mask:                Allows for user to use their own mask
    :param ret_mask: bool:      Replace default mask with a retinex edgemask
    :param show_mask: bool:     Return mask
    :param opencl: bool:        Opencl acceleration
    """
    bits = clip.format.bits_per_sample - 8
    thr = strength * (1 >> bits)
    strong = TAAmbk(clip, aatype='Eedi3', alpha=0.25, beta=0.5, gamma=40, nrad=2, mdis=20, mtype=0,
                    opencl=opencl)
    weak = TAAmbk(clip, aatype='Nnedi3', nsize=3, nns=3, qual=1, mtype=0, opencl=opencl)
    expr = 'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr)

    if clip.format.num_planes > 1:
        expr = [expr, '']
    aa = core.std.Expr([strong, weak, clip], expr)

    if mask:
        merged = clip.std.MaskedMerge(aa, mask, planes=0)
    elif ret_mask:
        mask = kgf.retinex_edgemask(clip, 1).std.Binarize()
        merged = clip.std.MaskedMerge(aa, mask, planes=0)
    else:
        mask = clip.std.Prewitt(planes=0).std.Binarize(planes=0).std.Maximum(planes=0).std.Convolution([1] * 9, planes=0)
        mask = get_y(mask)
        merged = clip.std.MaskedMerge(aa, mask, planes=0)

    if show_mask:
        return mask
    return merged if clip.format.color_family == vs.GRAY else core.std.ShufflePlanes([merged, clip], [0, 1, 2], vs.YUV)
示例#8
0
def deband(clip: vs.VideoNode, hard: List[Range],
           harder: List[Range]) -> vs.VideoNode:
    line = retinex_edgemask(clip).std.Binarize(9500).rgvs.RemoveGrain(3) \
        .std.Inflate()
    nf3kdb = clip.neo_f3kdb.Deband(range=18,
                                   y=32,
                                   cb=24,
                                   cr=24,
                                   grainy=24,
                                   grainc=0,
                                   output_depth=16,
                                   sample_mode=4)
    nf3kdb = core.std.MaskedMerge(nf3kdb, clip, line)
    placebo = clip.placebo.Deband(iterations=3,
                                  threshold=3,
                                  radius=24,
                                  grain=4)
    placebo2 = clip.placebo.Deband(iterations=3,
                                   threshold=5,
                                   radius=32,
                                   grain=4)
    debanded = replace_ranges(nf3kdb, placebo, hard)
    debanded = replace_ranges(debanded, placebo2, harder)
    return debanded
示例#9
0
def RainbowSmooth(clip, radius=3, lthresh=0, hthresh=220, mask="original"):
    core = vs.core
    
    if isinstance(mask, str):
        if mask == "original":
            mask = core.std.Expr(clips=[clip.std.Maximum(planes=0), clip.std.Minimum(planes=0)], expr=["x y - 90 > 255 x y - 255 90 / * ?", "", ""])
        elif mask == "prewitt":
            mask = core.std.Prewitt(clip=clip, planes=0)
        elif mask == "sobel":
            mask = core.std.Sobel(clip=clip, planes=0)
        elif mask == "tcanny":
            mask = core.tcanny.TCanny(clip)
        elif mask == "fast_sobel":
            import kagefunc as kage

            mask = kage.fast_sobel(clip)
        elif mask == "kirsch":
            import kagefunc as kage

            mask = kage.kirsch(clip)
        elif mask == "retinex_edgemask":
            import kagefunc as kage

            mask = kage.retinex_edgemask(clip)

    lderain = clip

    if lthresh > 0:
        lderain = clip.smoothuv.SmoothUV(radius=radius, threshold=lthresh, interlaced=False)

    hderain = clip.smoothuv.SmoothUV(radius=radius, threshold=hthresh, interlaced=False)

    if hthresh > lthresh:
        return core.std.MaskedMerge(clipa=lderain, clipb=hderain, mask=mask, planes=[1, 2], first_plane=True)
    else:
        return lderain
示例#10
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut
    src = depth(src, 32)

    h = 720
    w = get_w(h)
    b, c = vdf.get_bicubic_params('robidoux')
    opstart, opend = 1392, 3788
    edstart, edend = 31889, src.num_frames - 1
    full_stuff = [(3801, 3908), (16203, 16358)]

    denoise = hybrid_denoise(src, 0.5, 2)
    out = denoise

    luma = get_y(out)
    line_mask = vdf.edge_detect(luma, 'FDOG', 0.05, (1, 1))

    descale = core.descale.Debicubic(luma, w, h, b, c)
    upscale = vdf.fsrcnnx_upscale(descale, None, descale.height * 2,
                                  '_shaders/FSRCNNX_x2_56-16-4-1.glsl',
                                  core.resize.Point)
    upscale_smooth = vdf.nnedi3_upscale(descale, pscrn=1)
    upscale = lvf.rfs(upscale, upscale_smooth, [(18547, 18586)])

    antialias = single_rate_antialiasing(upscale,
                                         13,
                                         alpha=0.3,
                                         beta=0.45,
                                         gamma=320,
                                         mdis=18)

    scaled = muvf.SSIM_downsample(antialias,
                                  src.width,
                                  src.height,
                                  kernel='Bicubic')
    rescale = core.std.MaskedMerge(luma, scaled, line_mask)
    merged = vdf.merge_chroma(rescale, out)
    out = depth(merged, 16)

    antialias = lvf.rfs(
        out,
        lvf.sraa(out,
                 1.65,
                 9,
                 alpha=0.3,
                 beta=0.45,
                 gamma=240,
                 nrad=3,
                 mdis=25), [(opstart + 840, opstart + 881)])
    out = antialias

    # Slight sharp though CAS
    sharp = hvf.LSFmod(out,
                       strength=75,
                       Smode=3,
                       Lmode=1,
                       edgemode=1,
                       edgemaskHQ=True)
    out = sharp

    dering = gf.HQDeringmod(out, thr=16, darkthr=0.1)
    out = dering

    warp = xvs.WarpFixChromaBlend(out, thresh=48, depth=8)
    out = warp

    preden = core.knlm.KNLMeansCL(out,
                                  d=0,
                                  a=3,
                                  h=0.6,
                                  device_type='GPU',
                                  channels='Y')
    deband_mask = lvf.denoise.detail_mask(preden, brz_a=2000, brz_b=800, rad=4)

    deband = dbs.f3kpf(out, 17, 42, 42)
    deband_b = placebo.deband(out, 22, 6, 2)
    deband = lvf.rfs(deband, deband_b, [(opstart + 1515, opstart + 1603)])

    deband_c = placebo.deband(out, 17, 8, 3)
    import kagefunc
    deband_mask_c = kagefunc.retinex_edgemask(preden)
    deband_mask_c = iterate(deband_mask_c, core.std.Maximum,
                            3).std.Binarize(15000).std.Deflate()
    deband = lvf.rfs(deband,
                     core.std.MaskedMerge(deband_c, deband, deband_mask_c),
                     [(7409, 7600), (7960, 8055)])

    deband = core.std.MaskedMerge(deband, out, deband_mask)

    out = deband

    adg_mask = core.adg.Mask(out.std.PlaneStats(),
                             20).std.Expr(f'x x {128<<8} - 0.25 * +')
    grain = core.grain.Add(out, 0.2, constant=True)
    grain = core.std.MaskedMerge(out, grain, adg_mask, 0)
    out = grain

    rescale_mask = vdf.drm(luma, b=b, c=c, sw=4, sh=4)
    ref, rescale_mask, src, src_ncop, src_nced = [
        depth(x, 16) for x in
        [denoise, rescale_mask, src, JPBD_NCOP.src_cut, JPBD_NCED.src_cut]
    ]

    credit = lvf.rfs(out, core.std.MaskedMerge(out, ref, rescale_mask),
                     full_stuff)
    out = credit

    src_c, src_ncop, src_nced = [
        c.knlm.KNLMeansCL(a=7, h=35, d=0, device_type='gpu')
        for c in [src, src_ncop, src_nced]
    ]

    opening_mask = vdf.dcm(out, src_c[opstart:opend + 1],
                           src_ncop[:opend - opstart + 1], opstart, opend, 4,
                           4).std.Inflate()
    ending_mask = vdf.dcm(out, src_c[edstart:edend + 1],
                          src_nced[:edend - edstart + 1], edstart, edend, 4,
                          4).std.Inflate()
    credit_mask = core.std.Expr([opening_mask, ending_mask], 'x y +')

    credit = lvf.rfs(out, core.std.MaskedMerge(out, ref, credit_mask),
                     [(opstart, opend), (edstart, edend)])
    out = credit

    return depth(out, 10)
示例#11
0
文件: lvsfunc.py 项目: RisGar/lvsfunc
def smart_descale(clip: vs.VideoNode,
                  res: List[int],
                  b: float = 1/3, c: float = 1/3,
                  thresh1: float = 0.03, thresh2: float = 0.7,
                  no_mask: float = False,
                  show_mask: bool = False, show_dmask: bool = False,
                  sraa_upscale: bool = False, rfactor: float = 1.5,
                  sraa_sharp: bool = False) -> vs.VideoNode:
    funcname = "smart_descale"
    """
    Original function written by kageru and modified into a general function by me.
    For more information and comments I suggest you check out the original script:
        https://git.kageru.moe/kageru/vs-scripts/src/branch/master/abyss1.py

    A descaling function that compares relative errors between multiple resolutions and descales accordingly.
    Most of this code was leveraged from kageru's Made in Abyss script.
    As this is an incredibly complex function, I will offer only minimal support.

    :param res: List[int]:             A list of resolutions to descale to. For example: [900, 871, 872, 877]
    :param thresh1: float:             Threshold for when a frame will be descaled.
    :param thresh2: float:             Threshold for when a frame will not be descaled.
    :param sraa_upscale: bool:         Use upscaled_sraa to upscale the frames (warning: very slow!)
    :param rfactor: float:             Image enlargement factor for upscaled_sraa
    """
    def _descaling(clip: vs.VideoNode, h: int, b: float, c: float):
        # Descale and return a tuple of descaled clip and diff mask between that and the original.
        down = clip.descale.Debicubic(get_w(h), h, b, c)
        up = down.resize.Bicubic(clip.width, clip.height, filter_param_a=b, filter_param_b=c)
        diff = core.std.Expr([clip, up], 'x y - abs').std.PlaneStats()
        return down, diff

    def _select(n, y, debic_list, sraa_upscale, rfactor, f):
        # This simply descales to each of those and selects the most appropriate for each frame.
        errors = [x.props.PlaneStatsAverage for x in f]
        y_deb = debic_list[errors.index(min(errors))]
        dmask = core.std.Expr([y, y_deb.resize.Bicubic(clip.width, clip.height)], 'x y - abs 0.025 > 1 0 ?').std.Maximum().std.SetFrameProp("_descaled_resolution", intval=y_deb.height)

        if sraa_upscale:
            up = upscaled_sraa(y_deb, rfactor, h=clip.height).resize.Bicubic(clip.width, clip.height)
        else:
            up = fvf.Depth(nnedi3_rpow2(fvf.Depth(y_deb, 16), nns=4, correct_shift=True, width=clip.width, height=clip.height), 32)
        return core.std.ClipToProp(up, dmask)

    def _square():
        top = core.std.BlankClip(length=len(y), format=vs.GRAYS, height=4, width=10, color=[1])
        side = core.std.BlankClip(length=len(y), format=vs.GRAYS, height=2, width=4, color=[1])
        center = core.std.BlankClip(length=len(y), format=vs.GRAYS, height=2, width=2, color=[0])
        t1 = core.std.StackHorizontal([side, center, side])
        return core.std.StackVertical([top, t1, top])

    def _restore_original(n, f, clip: vs.VideoNode, orig: vs.VideoNode, thresh_a: float, thresh_b: float):
        # Just revert the entire scaling if the difference is too big. This should catch the 1080p scenes.
        if f.props.PlaneStatsAverage < thresh_a:
            return clip.std.SetFrameProp("_descaled", intval=1)
        elif f.props.PlaneStatsAverage > thresh_b:
            return orig.std.SetFrameProp("_descaled", intval=0)
        return core.std.Merge(clip, orig, (f.props.PlaneStatsAverage - thresh_a) * 20).std.SetFrameProp("_descaled", intval=2)

    # Error handling
    if len(res) < 2:
        return error(funcname, 'This function requires more than two resolutions to descale to')


    og = clip
    clip32 = fvf.Depth(clip, 32)
    if one_plane(clip32):
        y = get_y(clip32)
    else:
        y, u, v = split(clip32)

    debic_listp = [_descaling(y, h, b, c) for h in res]
    debic_list = [a[0] for a in debic_listp]
    debic_props = [a[1] for a in debic_listp]

    y_deb = core.std.FrameEval(y, partial(_select, y=y, debic_list=debic_list,
                                                  sraa_upscale=sraa_upscale, rfactor=rfactor), prop_src=debic_props)
    # TO-DO: It returns a frame size error here for whatever reason. Need to figure out what causes it and fix it
    dmask = core.std.PropToClip(y_deb)
    if show_dmask:
        return dmask
    # TO-DO: Figure out how to make it properly round depending on resolution (although this should usually be 1080p anyway)
    line = core.std.StackHorizontal([_square()]*192)
    full_squares = core.std.StackVertical([line]*108)

    artifacts = core.misc.Hysteresis(dmask.resize.Bicubic(clip32.width, clip32.height, _format=vs.GRAYS),
                                     core.std.Expr([get_y(clip32).tcanny.TCanny(sigma=3), full_squares], 'x y min'))

    ret_raw = kgf.retinex_edgemask(fvf.Depth(clip, 16))
    ret = ret_raw.std.Binarize(30).rgvs.RemoveGrain(3)

    mask = core.std.Expr([ret.resize.Point(_format=vs.GRAYS), kgf.iterate(artifacts, core.std.Maximum, 3)], 'x y -').std.Binarize(0.4)

    mask = mask.std.Inflate().std.Convolution(matrix=[1]*9).std.Convolution(matrix=[1]*9)
    if show_mask:
        return mask

    y = core.std.MaskedMerge(y, y_deb, mask)
    merged = join([y, u, v]) if not one_plane(og) else y
    merged = fvf.Depth(merged, get_depth(og))
    if no_mask:
        return merged

    dmask = dmask.std.PlaneStats() # TO-DO: It returns a frame size error here for whatever reason. Need to figure out what causes it and fix it
    return merged.std.FrameEval(partial(_restore_original, clip=merged, orig=og, thresh_a=thresh1, thresh_b=thresh2), prop_src=dmask)#.std.SetFrameProp("_descaled_resolution", intval=y_deb.height)
示例#12
0
def do_filter():
    """Vapoursynth filtering"""
    def _nneedi3_clamp(clip: vs.VideoNode, strength: int = 1) -> vs.VideoNode:
        bits = clip.format.bits_per_sample - 8
        thr = strength * (1 >> bits)

        luma = get_y(clip)

        def _strong(clip: vs.VideoNode) -> vs.VideoNode:
            args = dict(alpha=0.25,
                        beta=0.5,
                        gamma=40,
                        nrad=2,
                        mdis=20,
                        vcheck=3)
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip,
                                        luma.width,
                                        luma.height,
                                        src_left=-.5,
                                        src_top=-.5)

        def _weak(clip: vs.VideoNode) -> vs.VideoNode:
            args = dict(nsize=3, nns=2, qual=2)
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip,
                                        luma.width,
                                        luma.height,
                                        src_left=-.5,
                                        src_top=-.5)

        clip_aa = core.std.Expr(
            [_strong(luma), _weak(luma), luma],
            'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr))
        return vdf.merge_chroma(clip_aa, clip)

    def _perform_endcard(path: str, ref: vs.VideoNode) -> vs.VideoNode:
        endcard = lvf.src(path).std.AssumeFPS(ref)
        endcard = core.std.CropRel(endcard, left=6, top=20, right=0, bottom=6)
        endcard = core.resize.Bicubic(endcard,
                                      ref.width,
                                      ref.height,
                                      vs.RGBS,
                                      dither_type='error_diffusion')

        endcard = iterate(
            endcard, partial(core.w2xc.Waifu2x, noise=3, scale=1, photo=True),
            2)

        endcard = core.resize.Bicubic(endcard,
                                      format=vs.YUV444PS,
                                      matrix_s='709',
                                      dither_type='error_diffusion')
        endcard = lvf.util.quick_resample(
            endcard,
            lambda c: core.neo_f3kdb.Deband(c, 15, 36, 36, 36, 24, 24, 4))

        return Tweak(endcard, sat=1.2, bright=-0.05, cont=1.2)

    src = JPBD.src_cut
    src = depth(src, 16)
    edstart = 31769

    denoise_a = CoolDegrain(src, tr=2, thsad=48, blksize=8, overlap=4, plane=4)
    denoise_b = CoolDegrain(src, tr=3, thsad=96, blksize=8, overlap=4, plane=4)
    denoise = lvf.rfs(denoise_a, denoise_b, [(edstart + 1870, edstart + 1900)])

    antialias_a = _nneedi3_clamp(denoise)
    downscaler = lambda c, w, h: core.fmtc.resample(
        c, w, h, kernel='gauss', invks=True, invkstaps=1, taps=1, a1=32)
    antialias_b = rekt_fast(
        denoise,
        lambda c: lvf.sraa(c, 1.45, rep=13, downscaler=downscaler),
        top=482)
    antialias = lvf.rfs(antialias_a, antialias_b, [(8185, 8280)])

    predenoise = mClean(antialias, thSAD=200, chroma=False)
    detail_mask = lvf.denoise.detail_mask(predenoise,
                                          rad=2,
                                          radc=2,
                                          brz_a=3250,
                                          brz_b=1250)
    ret_mask = kgf.retinex_edgemask(predenoise).std.Binarize(
        9250).std.Median().std.Inflate()
    line_mask = core.std.Expr([detail_mask, ret_mask], 'x y max')

    deband_a = core.neo_f3kdb.Deband(antialias,
                                     17,
                                     42,
                                     42,
                                     42,
                                     12,
                                     0,
                                     sample_mode=4,
                                     keep_tv_range=True)
    deband_a = core.std.MaskedMerge(deband_a, antialias, line_mask)
    deband_b = core.neo_f3kdb.Deband(deband_a,
                                     18,
                                     48,
                                     48,
                                     48,
                                     0,
                                     0,
                                     sample_mode=2,
                                     keep_tv_range=True)

    deband = lvf.rfs(deband_a, deband_b, [(edstart + 1870, edstart + 1900)])
    deband = lvf.rfs(deband, deband_b, [(13611, 13661), (20022, 20057)])

    grain = kgf.adaptive_grain(deband, 0.25, luma_scaling=10)

    endcard = _perform_endcard(
        '[BDMV][200610][Magia Record][Vol.3]/Scans/endcard6_front_descreen.png',
        src)
    endcard_length = 119
    final = core.std.Splice([grain, endcard * endcard_length], True)
    final = core.resize.Bicubic(final,
                                format=vs.YUV420P10,
                                dither_type='error_diffusion')
    final = core.std.Limiter(final, 16, [235 << 2, 240 << 2])

    return depth(final, 10), endcard_length
示例#13
0
def nneedi3_clamp(clip: vs.VideoNode,
                  strength: int = 1,
                  mask: Optional[vs.VideoNode] = None,
                  ret_mask: bool = False,
                  show_mask: bool = False,
                  opencl: bool = False) -> vs.VideoNode:
    """
    A function that clamps eedi3 to nnedi3 for the purpose of reducing eedi3 artifacts.
    This should fix every issue created by eedi3. For example: https://i.imgur.com/hYVhetS.jpg

    Original function written by Zastin, modified by LightArrowsEXE.

    Dependencies:

    * kagefunc (optional: retinex edgemask)
    * vapoursynth-retinex (optional: retinex edgemask)
    * vapoursynth-tcanny (optional: retinex edgemask)
    * vapoursynth-eedi3
    * vapoursynth-nnedi3 or znedi3
    * vapoursynth-nnedi3cl (optional: opencl)
    * vsTAAmbk

    :param clip:                Input clip
    :param strength:            Set threshold strength (Default: 1)
    :param mask:                Clip to use for custom mask (Default: None)
    :param ret_mask:            Replace default mask with a retinex edgemask (Default: False)
    :param show_mask:           Return mask instead of clip (Default: False)
    :param opencl:              OpenCL acceleration (Default: False)

    :return:                    Antialiased clip
    """
    try:
        from vsTAAmbk import TAAmbk
    except ModuleNotFoundError:
        raise ModuleNotFoundError(
            "nnedi3_clamp: missing dependency 'vsTAAmbk'")

    bits = clip.format.bits_per_sample - 8
    thr = strength * (1 >> bits)
    strong = TAAmbk(clip,
                    aatype='Eedi3',
                    alpha=0.25,
                    beta=0.5,
                    gamma=40,
                    nrad=2,
                    mdis=20,
                    mtype=0,
                    opencl=opencl)
    weak = TAAmbk(clip,
                  aatype='Nnedi3',
                  nsize=3,
                  nns=3,
                  qual=1,
                  mtype=0,
                  opencl=opencl)
    expr = 'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr)

    if clip.format.num_planes > 1:
        expr = [expr, '']
    aa = core.std.Expr([strong, weak, clip], expr)

    if mask:
        merged = clip.std.MaskedMerge(aa, mask, planes=0)
    elif ret_mask:
        try:
            import kagefunc as kgf
        except ModuleNotFoundError:
            raise ModuleNotFoundError(
                "nnedi3_clamp: missing dependency 'kagefunc'")
        mask = kgf.retinex_edgemask(clip, 1).std.Binarize()
        merged = clip.std.MaskedMerge(aa, mask, planes=0)
    else:
        mask = clip.std.Prewitt(planes=0).std.Binarize(planes=0).std.Maximum(
            planes=0).std.Convolution([1] * 9, planes=0)
        mask = get_y(mask)
        merged = clip.std.MaskedMerge(aa, mask, planes=0)

    if show_mask:
        return mask
    return merged if clip.format.color_family == vs.GRAY else core.std.ShufflePlanes(
        [merged, clip], [0, 1, 2], vs.YUV)
示例#14
0
def do_filter():
    """Vapoursynth filtering"""
    def _nneedi3_clamp(clip: vs.VideoNode, strength: int = 1) -> vs.VideoNode:
        bits = clip.format.bits_per_sample - 8
        thr = strength * (1 >> bits)

        luma = get_y(clip)

        def _strong(clip: vs.VideoNode) -> vs.VideoNode:
            args = dict(alpha=0.25,
                        beta=0.5,
                        gamma=40,
                        nrad=2,
                        mdis=20,
                        vcheck=3)
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip,
                                        luma.width,
                                        luma.height,
                                        src_left=-.5,
                                        src_top=-.5)

        def _weak(clip: vs.VideoNode) -> vs.VideoNode:
            args = dict(nsize=3, nns=2, qual=2)
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip,
                                        luma.width,
                                        luma.height,
                                        src_left=-.5,
                                        src_top=-.5)

        clip_aa = core.std.Expr(
            [_strong(luma), _weak(luma), luma],
            'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr))
        return vdf.merge_chroma(clip_aa, clip)

    def _perform_endcard(path: str, ref: vs.VideoNode) -> vs.VideoNode:
        endcard = lvf.src(path).std.AssumeFPS(ref)
        endcard = core.std.CropRel(endcard,
                                   left=10,
                                   top=17,
                                   right=17,
                                   bottom=23)
        endcard = core.resize.Bicubic(endcard,
                                      ref.width,
                                      ref.height,
                                      vs.RGBS,
                                      dither_type='error_diffusion')

        endcard = iterate(
            endcard, partial(core.w2xc.Waifu2x, noise=3, scale=1, photo=True),
            2)

        endcard = core.resize.Bicubic(endcard,
                                      format=vs.YUV444PS,
                                      matrix_s='709',
                                      dither_type='error_diffusion')

        return Tweak(endcard, sat=1.2, bright=-0.05, cont=1.2)

    src = JPBD.src_cut
    src = depth(src, 16)

    denoise = CoolDegrain(src, tr=2, thsad=48, blksize=8, overlap=4, plane=4)

    antialias_a = _nneedi3_clamp(denoise)

    antialias_b = lvf.sraa(denoise, rep=6)
    antialias = lvf.rfs(antialias_a, antialias_b, [(29453, 29476),
                                                   (29510, 29532),
                                                   (29640, 29663),
                                                   (29775, 29798),
                                                   (29866, 29889),
                                                   (30011, 30034)])

    predenoise = mClean(antialias, thSAD=200, chroma=False)
    detail_mask = lvf.denoise.detail_mask(predenoise,
                                          rad=2,
                                          radc=2,
                                          brz_a=3250,
                                          brz_b=1250)
    ret_mask = kgf.retinex_edgemask(predenoise).std.Binarize(
        9250).std.Median().std.Inflate()
    line_mask = core.std.Expr([detail_mask, ret_mask], 'x y max')

    deband = core.neo_f3kdb.Deband(antialias,
                                   17,
                                   42,
                                   42,
                                   42,
                                   12,
                                   0,
                                   sample_mode=4,
                                   keep_tv_range=True)
    deband = core.std.MaskedMerge(deband, antialias, line_mask)

    grain_a = kgf.adaptive_grain(deband, 0.25, luma_scaling=10)
    grain_b = adptvgrnMod(deband,
                          2,
                          size=2,
                          luma_scaling=2,
                          static=False,
                          grain_chroma=False)
    grain = lvf.rfs(grain_a, grain_b, [(5149, 5598), (8691, 10137)])

    borders_mask = vdf.region_mask(
        src.std.BlankClip(format=vs.GRAY16, color=(256 << 8) - 1), 240 + 2,
        240 + 2, 0, 0)
    borders = core.std.MaskedMerge(src, grain, borders_mask)
    borders = lvf.rfs(grain, borders, [(5149, 5598)])

    endcard = _perform_endcard(
        '[BDMV][200304][Magia Record][Vol.1]/Scans/endcard1_front_descreen.png',
        src)
    endcard_length = 119
    final = core.std.Splice([borders, endcard * endcard_length], True)
    final = core.resize.Bicubic(final,
                                format=vs.YUV420P10,
                                dither_type='error_diffusion')
    final = core.std.Limiter(final, 16, [235 << 2, 240 << 2])

    return depth(final, 10), endcard_length
示例#15
0
def do_filter():
    """Vapoursynth filtering"""
    def _nneedi3_clamp(clip: vs.VideoNode, strength: int = 1)-> vs.VideoNode:
        bits = clip.format.bits_per_sample - 8
        thr = strength * (1 >> bits)

        luma = get_y(clip)

        def _strong(clip: vs.VideoNode)-> vs.VideoNode:
            args = dict(alpha=0.25, beta=0.5, gamma=40, nrad=2, mdis=20, vcheck=3)
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip, luma.width, luma.height, src_left=-.5, src_top=-.5)

        def _weak(clip: vs.VideoNode)-> vs.VideoNode:
            args = dict(nsize=3, nns=2, qual=2)
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip, luma.width, luma.height, src_left=-.5, src_top=-.5)

        clip_aa = core.std.Expr([_strong(luma), _weak(luma), luma],
                                'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr))
        return vdf.merge_chroma(clip_aa, clip)


    def _perform_endcard(path: str, ref: vs.VideoNode)-> vs.VideoNode:
        endcard = lvf.src(path).std.AssumeFPS(ref)
        endcard = core.std.CropRel(endcard, left=7, top=0, right=11, bottom=22)
        endcard = core.resize.Bicubic(endcard, ref.width, ref.height, vs.RGBS, dither_type='error_diffusion')

        endcard = core.w2xc.Waifu2x(endcard, noise=3, scale=1, photo=True)

        endcard = core.resize.Bicubic(endcard, format=vs.YUV444PS, matrix_s='709', dither_type='error_diffusion')
        endcard = lvf.util.quick_resample(endcard, lambda c: core.neo_f3kdb.Deband(c, 15, 36, 36, 36, 24, 24, 4))

        return Tweak(endcard, sat=1.4, cont=1.2)


    def _perform_motion_mask(clip: vs.VideoNode, brz: int)-> vs.VideoNode:
        clip = depth(clip, 8)
        sup = core.hqdn3d.Hqdn3d(clip).neo_fft3d.FFT3D().mv.Super(sharp=1)
        fv1 = core.mv.Analyse(sup, isb=False, delta=1, truemotion=False, dct=2)
        fv2 = core.mv.Analyse(sup, isb=True, delta=1, truemotion=True, dct=2)

        momask1 = core.mv.Mask(clip, fv1, ml=2, kind=1)
        momask2 = core.mv.Mask(clip, fv2, ml=3, kind=1)
        momask = core.std.Merge(momask1, momask2).rgvs.RemoveGrain(3).std.Binarize(brz)
        momask = momask.std.Minimum().std.Minimum()

        return depth(get_y(momask), 16)


    src = JPBD.src_cut
    src = depth(src, 16)

    denoise_a = CoolDegrain(src, tr=2, thsad=48, blksize=8, overlap=4, plane=4)
    denoise = denoise_a


    antialias = _nneedi3_clamp(denoise)
    antialias_a = TAAmbk(antialias, aatype='Eedi3', cycle=3)
    antialias_b = lvf.sraa(denoise, 2, 13)

    motion_mask = _perform_motion_mask(denoise[16914:17001], 140).std.FreezeFrames([0, 18, 57], [2, 38, 86], [2, 38, 57])
    motion_mask = insert_clip(src.std.BlankClip(format=vs.GRAY16), motion_mask, 16914)


    antialias = lvf.rfs(antialias, core.std.MaskedMerge(antialias_a, antialias, motion_mask), [(16914, 17000)])
    antialias = lvf.rfs(antialias, antialias_b, [(18084, 18179)])


    predenoise = mClean(antialias, thSAD=200, chroma=False)
    detail_mask = lvf.denoise.detail_mask(predenoise, rad=2, radc=2, brz_a=3250, brz_b=1250)
    ret_mask = kgf.retinex_edgemask(predenoise).std.Binarize(9250).std.Median().std.Inflate()
    line_mask = core.std.Expr([detail_mask, ret_mask], 'x y max')


    deband_a = core.neo_f3kdb.Deband(antialias, 17, 42, 42, 42, 12, 0, sample_mode=4, keep_tv_range=True)
    deband_a = core.std.MaskedMerge(deband_a, antialias, line_mask)
    deband = deband_a


    grain = kgf.adaptive_grain(deband, 0.25, luma_scaling=10)


    ending = lvf.rfs(grain, src, [(32079, 33098)])


    endcard = _perform_endcard(r'[BDMV][200902][Magia Record][Vol.5]\Scans\endcard13_front_descreen.png', src)
    endcard_length = 119
    final = core.std.Splice([ending, endcard * endcard_length], True)
    final = core.resize.Bicubic(final, format=vs.YUV420P10, dither_type='error_diffusion')
    final = core.std.Limiter(final, 16, [235 << 2, 240 << 2])

    return depth(final, 10), endcard_length
示例#16
0
def do_filter():
    """Vapoursynth filtering"""
    def _nneedi3_clamp(clip: vs.VideoNode, strength: int = 1) -> vs.VideoNode:
        bits = clip.format.bits_per_sample - 8
        thr = strength * (1 >> bits)

        luma = get_y(clip)

        def _strong(clip: vs.VideoNode) -> vs.VideoNode:
            args = dict(alpha=0.25,
                        beta=0.5,
                        gamma=40,
                        nrad=2,
                        mdis=20,
                        vcheck=3)
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip,
                                        luma.width,
                                        luma.height,
                                        src_left=-.5,
                                        src_top=-.5)

        def _weak(clip: vs.VideoNode) -> vs.VideoNode:
            args = dict(nsize=3, nns=2, qual=2)
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip,
                                        luma.width,
                                        luma.height,
                                        src_left=-.5,
                                        src_top=-.5)

        clip_aa = core.std.Expr(
            [_strong(luma), _weak(luma), luma],
            'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr))
        return vdf.merge_chroma(clip_aa, clip)

    def _perform_endcard(path: str, ref: vs.VideoNode) -> vs.VideoNode:
        endcard = lvf.src(path).std.AssumeFPS(ref)
        endcard = core.std.CropRel(endcard,
                                   left=14,
                                   top=12,
                                   right=18,
                                   bottom=23)
        endcard = core.resize.Bicubic(endcard,
                                      ref.width,
                                      ref.height,
                                      vs.RGBS,
                                      dither_type='error_diffusion')

        endcard = iterate(
            endcard, partial(core.w2xc.Waifu2x, noise=3, scale=1, photo=True),
            2)

        endcard = core.resize.Bicubic(endcard,
                                      format=vs.YUV444PS,
                                      matrix_s='709',
                                      dither_type='error_diffusion')

        return Tweak(endcard, sat=1.2, bright=-0.05, cont=1.2)

    src = JPBD.src_cut
    src = depth(src, 16)
    edstart = 32012

    denoise_a = CoolDegrain(src, tr=2, thsad=48, blksize=8, overlap=4, plane=4)
    denoise_b = CoolDegrain(src, tr=3, thsad=96, blksize=8, overlap=4, plane=4)
    denoise = lvf.rfs(denoise_a, denoise_b, [(edstart + 1870, edstart + 1900)])

    antialias_a = _nneedi3_clamp(denoise)
    antialias_b = lvf.sraa(denoise, 2, rep=13)
    antialias = lvf.rfs(antialias_a, antialias_b, [(14667, 14696)])

    predenoise = mClean(antialias, thSAD=200, chroma=False)
    detail_mask = lvf.denoise.detail_mask(predenoise,
                                          rad=2,
                                          radc=2,
                                          brz_a=3250,
                                          brz_b=1250)
    adapt_mask = core.adg.Mask(predenoise.std.PlaneStats(), 40)
    ret_mask = kgf.retinex_edgemask(predenoise).std.Binarize(
        9250).std.Median().std.Inflate()
    line_mask = core.std.Expr([detail_mask, ret_mask], 'x y max')

    deband_a = core.neo_f3kdb.Deband(antialias,
                                     17,
                                     42,
                                     42,
                                     42,
                                     12,
                                     0,
                                     sample_mode=4,
                                     keep_tv_range=True)
    deband_a = core.std.MaskedMerge(deband_a, antialias, line_mask)
    deband_b = core.neo_f3kdb.Deband(deband_a,
                                     18,
                                     48,
                                     48,
                                     48,
                                     0,
                                     0,
                                     sample_mode=2,
                                     keep_tv_range=True)
    deband_ca = dbs.f3kbilateral(deband_b, 8, 270, 270)
    deband_cb = dbs.f3kbilateral(deband_b, 12, 110, 110)
    deband_c = core.std.MaskedMerge(deband_ca, deband_cb, adapt_mask)

    deband = lvf.rfs(deband_a, deband_b, [(edstart + 1870, edstart + 1900),
                                          (13041, 13180), (13932, 13949),
                                          (14121, 14168), (14647, 14666)])
    deband = lvf.rfs(deband, deband_c, [(14667, 14669), (14673, 14674),
                                        (14676, 14677)])

    grain = kgf.adaptive_grain(deband, 0.25, luma_scaling=10)

    crop = core.std.Crop(grain, 0, 0, 132, 132)
    edgefixe = core.edgefixer.ContinuityFixer(crop, 0, [2, 1, 1], 0, [1, 1, 1])
    borders = core.std.AddBorders(edgefixe, 0, 0, 132, 132)
    borders = lvf.rfs(grain, borders, [(0, 2905)])

    endcard = _perform_endcard(
        '[BDMV][200304][Magia Record][Vol.1]/Scans/endcard3_front_descreen.png',
        src)
    endcard_length = 117
    final = core.std.Splice([borders, endcard * endcard_length], True)
    final = core.resize.Bicubic(final,
                                format=vs.YUV420P10,
                                dither_type='error_diffusion')
    final = core.std.Limiter(final, 16, [235 << 2, 240 << 2])

    return depth(final, 10), endcard_length
示例#17
0
cunny = cunny.vivtc.VDecimate()
cunny = cunny[:44972]
cunny = cunny.std.Crop(left=6, right=4)
cunny = awf.fb(cunny, top=1)
cunny = cunny.resize.Bicubic(src_top=1)
cunny = cunny.fb.FillBorders(top=1, bottom=1, mode="fixborders")
cunny = cunny.vinverse.Vinverse()
black = core.tcm.TColorMask(cunny.std.Minimum(), ["$000000"],
                            tolerance=7,
                            bt601=True,
                            gray=False,
                            lutthr=9)
cunny = rektlvls(cunny, colnum=[1, 709], colval=[-9, 35])
aa = taa.TAAmbk(cunny, "Nnedi3")
sangnom = taa.TAAmbk(cunny, "Nnedi3SangNom")
mask = kgf.retinex_edgemask(cunny).std.Binarize(200).std.Deflate(threshold=250)
cunny = cunny.placebo.Deband(threshold=6, radius=16, grain=4)
cunny = awf.ReplaceFrames(core.std.MaskedMerge(cunny, aa, mask),
                          core.std.MaskedMerge(cunny, sangnom, mask),
                          "[4708 4850] [5290 5421] [5542 5698] [5770 5803]")
no_bbmod = cunny
censor = cunny.fb.FillBorders(top=2, mode="fixborders")
censor = awf.bbmod(censor, top=4, thresh=200, blur=10)
censor = awf.bbmod(censor, right=4, thresh=2, blur=5)
censor = awf.bbmod(censor, left=4, thresh=6, blur=8)
censor = awf.bbmod(censor, right=1)
cunny = awf.bbmod(cunny, right=4, top=4, thresh=2, blur=5)
cunny = awf.bbmod(cunny, left=4, thresh=6, blur=8)
cunny = awf.bbmod(cunny, right=1)
cunny = core.std.MaskedMerge(cunny, no_bbmod, black)
cunny = awf.ReplaceFrames(cunny, no_bbmod, "[0 15] [42581 44935]")
示例#18
0
def faggotdb_mod(clip: vs.VideoNode,
                 thrY=40,
                 thrC=None,
                 radiusY=15,
                 radiusC=15,
                 CbY=44,
                 CrY=44,
                 CbC=44,
                 CrC=44,
                 grainY=32,
                 grainC=None,
                 grainCC=0,
                 sample_mode=2,
                 neo=True,
                 dynamic_grainY=False,
                 dynamic_grainC=False,
                 tv_range=True,
                 mask="retinex",
                 binarize=False,
                 binarize_thr=70,
                 grayscale=True,
                 bitresamp=False,
                 outbits=None,
                 blurmask=True,
                 horizblur=2,
                 vertblur=2) -> vs.VideoNode:

    funcName = "faggotdb_mod"  # name kept to be fallback compatible

    # Original Idea: Author who created Fag3kdb. Edited by AlucardSama04; additional modifications by l00t

    if not isinstance(clip, vs.VideoNode):
        raise TypeError(f"{funcName}: This is not a clip")

    if outbits is None:
        outbits = clip.format.bits_per_sample

    if bitresamp:
        if clip.format.bits_per_sample != outbits:
            clip = fvf.Depth(
                clip,
                bits=outbits)  # instead of error, auto convert to 16 bits
    elif clip.format.bits_per_sample != outbits:
        raise TypeError(f"{funcName}: Input-output bitdepth mismatch")

    # if not isinstance(mask, vs.VideoNode):
    # raise vs.Error(f"{funcName}: mask' only clip inputs")

    if mask in [-1]:  # more user friendly if we do the masking intentionally
        mask = clip
        blurmask = False
    elif mask in [0, "retinex"]:
        mask = kgf.retinex_edgemask(clip)
    elif mask in [1, "kirsch"]:
        mask = kgf.kirsch(clip)
    elif mask in [2, "Sobel"]:
        mask = core.std.Sobel(clip, scale=1)
    elif mask in [3, "Prewitt"]:
        mask = core.std.Prewitt(clip)
    elif mask in [4, "GF"]:
        mask = fvf.GradFun3(clip, mask=2, debug=1)
    else:
        raise ValueError(f"{funcName}: Unknown Mask Mode")

    if grayscale:
        mask = core.std.ShufflePlanes(mask, planes=0, colorfamily=vs.GRAY)

    if binarize:  # binarize threshold should be adjusted according to bitdepth
        mask = core.std.Binarize(mask, threshold=binarize_thr)

    if blurmask:
        mask = core.std.BoxBlur(mask, hradius=horizblur, vradius=vertblur)

    if thrC is None:
        thrC = int(round(thrY / 2))

    if grainC is None:
        grainC = int(round(grainY / 2))

    if grainCC is None:
        grainCC = 0

    f3kdb = core.neo_f3kdb.Deband if neo else core.f3kdb.Deband

    U = plane(clip, 1)
    V = plane(clip, 2)

    U = f3kdb(U,
              range=radiusC,
              y=thrC,
              cb=CbC,
              cr=CrC,
              grainy=grainC,
              grainc=0,
              sample_mode=sample_mode,
              dynamic_grain=dynamic_grainC,
              keep_tv_range=tv_range,
              output_depth=outbits)

    V = f3kdb(V,
              range=radiusC,
              y=thrC,
              cb=CbC,
              cr=CrC,
              grainy=grainC,
              grainc=0,
              sample_mode=sample_mode,
              dynamic_grain=dynamic_grainC,
              keep_tv_range=tv_range,
              output_depth=outbits)

    filtered = core.std.ShufflePlanes([clip, U, V], [0, 0, 0], vs.YUV)

    filtered = f3kdb(filtered,
                     range=radiusY,
                     y=thrY,
                     cb=CbY,
                     cr=CrY,
                     grainy=grainY,
                     grainc=grainCC,
                     sample_mode=sample_mode,
                     dynamic_grain=dynamic_grainY,
                     keep_tv_range=tv_range,
                     output_depth=outbits
                     )  # if grainCC > 0 UV planes will be debanded once again

    return core.std.MaskedMerge(filtered, clip, mask)
示例#19
0
def do_filter():
    """Vapoursynth filtering"""
    def _sraa(clip: vs.VideoNode, nnargs: dict, eeargs: dict) -> vs.VideoNode:
        def _nnedi3(clip):
            return clip.nnedi3.nnedi3(0, False, **nnargs)

        def _eedi3(clip, sclip):
            return clip.eedi3m.EEDI3(0, False, **eeargs, sclip=sclip)

        clip = _eedi3(clip, _nnedi3(clip)).std.Transpose()
        clip = _eedi3(clip, _nnedi3(clip)).std.Transpose()
        return clip

    def _nnedi3(clip: vs.VideoNode, factor: float, args: dict) -> vs.VideoNode:
        upscale = clip.std.Transpose().nnedi3.nnedi3(0, True, **args) \
            .std.Transpose().nnedi3.nnedi3(0, True, **args)
        return core.resize.Spline36(upscale,
                                    clip.width * factor,
                                    clip.height * factor,
                                    src_top=.5,
                                    src_left=.5)

    def area_resize(clip: vs.VideoNode, width: int,
                    height: int) -> vs.VideoNode:
        blank = core.std.BlankClip(clip, format=vs.GRAY16, color=128 << 8)
        clip = join([clip, blank, blank])
        clip = core.area.AreaResize(clip, width, height)
        return get_y(clip)

    src = JPBD.src_cut
    src = depth(src, 16)

    flashback = [(31665, 31987), (68524, 68569)]

    denoise = CoolDegrain(src, tr=1, thsad=48, blksize=8, overlap=4, plane=4)
    denoise = lvf.rfs(denoise, src, flashback)

    dering = hvf.EdgeCleaner(denoise, 10, rmode=3, smode=1, hot=True)

    luma = get_y(dering)
    line_mask = TAAmbk(luma, mtype=2, showmask=1).std.Inflate()
    upscale = _nnedi3(luma, 2, dict(nsize=4, nns=4, qual=2, pscrn=2))
    sraa = _sraa(upscale, dict(nsize=0, nns=3, qual=1, pscrn=1),
                 dict(alpha=0.2, beta=0.5, gamma=40, nrad=3, mdis=20))
    sraa = core.rgvs.Repair(sraa, upscale, 13)
    antialias = area_resize(sraa, src.width, src.height)
    antialias = lvf.rfs(core.std.MaskedMerge(luma, antialias, line_mask),
                        antialias, [(22503, 22789)])
    antialias_merged = vdf.merge_chroma(antialias, denoise)

    deband_mask_a = lvf.denoise.detail_mask(antialias_merged,
                                            brz_a=3000,
                                            brz_b=1200)
    deband_mask_b = kgf.retinex_edgemask(antialias_merged)
    deband_mask = core.std.Expr([deband_mask_a, deband_mask_b], 'x y +')
    deband = dbs.f3kpf(antialias_merged, 18, 30, 30)
    deband = core.std.MaskedMerge(deband, antialias_merged, deband_mask)
    deband = hvf.ContraSharpening(deband, antialias_merged)
    deband = lvf.rfs(deband, antialias_merged, flashback)

    grain = core.neo_f3kdb.Deband(deband, preset='depth', grainy=24)
    grain_a = kgf.adaptive_grain(grain)
    grain_b = core.grain.Add(grain, 1, 0, constant=True)
    grain = lvf.rfs(grain_a, grain_b, flashback)

    mask_borders = core.std.BlankClip(grain,
                                      format=vs.GRAY16,
                                      color=(256 << 8) - 1)
    mask_borders = vdf.region_mask(mask_borders, 0, 0, 132, 132).std.Invert()
    final = lvf.rfs(grain, core.std.MaskedMerge(grain, src, mask_borders),
                    flashback + [(65388, 65413), (113621, 113763)])

    smooth = xvs.mvfrc(src[146178:src.num_frames], preset='slow')
    vfr = muvf.VFRSplice([final[:146178], smooth], 'sora_timecode.txt')

    return depth(vfr, 10)
示例#20
0
def do_filter():
    """Vapoursynth filtering"""
    def _nneedi3_clamp(clip: vs.VideoNode, strength: int = 1) -> vs.VideoNode:
        bits = clip.format.bits_per_sample - 8
        thr = strength * (1 >> bits)

        luma = get_y(clip)

        def _strong(clip: vs.VideoNode) -> vs.VideoNode:
            args = dict(alpha=0.25,
                        beta=0.5,
                        gamma=40,
                        nrad=2,
                        mdis=20,
                        vcheck=3)
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip,
                                        luma.width,
                                        luma.height,
                                        src_left=-.5,
                                        src_top=-.5)

        def _weak(clip: vs.VideoNode) -> vs.VideoNode:
            args = dict(nsize=3, nns=2, qual=2)
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose()
            return core.resize.Spline36(clip,
                                        luma.width,
                                        luma.height,
                                        src_left=-.5,
                                        src_top=-.5)

        clip_aa = core.std.Expr(
            [_strong(luma), _weak(luma), luma],
            'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr))
        return vdf.merge_chroma(clip_aa, clip)

    def _perform_endcard(path: str, ref: vs.VideoNode) -> vs.VideoNode:
        endcard = lvf.src(path).std.AssumeFPS(ref)
        endcard = core.std.CropRel(endcard, left=4, top=12, right=0, bottom=21)
        endcard = core.resize.Bicubic(endcard,
                                      ref.width,
                                      ref.height,
                                      vs.RGBS,
                                      dither_type='error_diffusion')

        endcard = iterate(
            endcard, partial(core.w2xc.Waifu2x, noise=3, scale=1, photo=True),
            2)

        endcard = core.resize.Bicubic(endcard,
                                      format=vs.YUV444PS,
                                      matrix_s='709',
                                      dither_type='error_diffusion')
        endcard = lvf.util.quick_resample(
            endcard,
            lambda c: core.neo_f3kdb.Deband(c, 15, 36, 36, 36, 24, 24, 4))

        return Tweak(endcard, sat=1.2, bright=-0.05, cont=1.2)

    def to_gray(clip: vs.VideoNode, ref: vs.VideoNode) -> vs.VideoNode:
        clip = core.std.AssumeFPS(clip, ref)
        return core.resize.Point(clip,
                                 format=vs.GRAY16,
                                 matrix_s=mvf.GetMatrix(ref))

    src = JPBD.src_cut
    src = depth(src, 16)
    edstart = 30618

    denoise_a = CoolDegrain(src, tr=2, thsad=48, blksize=8, overlap=4, plane=4)
    denoise_b = CoolDegrain(src, tr=3, thsad=96, blksize=8, overlap=4, plane=4)
    denoise = lvf.rfs(denoise_a, denoise_b, [(edstart + 1870, edstart + 1900)])

    antialias_a = _nneedi3_clamp(denoise)
    antialias_b = TAAmbk(denoise, aatype='Eedi3', mtype=1)
    antialias = lvf.rfs(antialias_a, antialias_b, [(9478, 9501)])

    predenoise = mClean(antialias, thSAD=200, chroma=False)
    detail_mask = lvf.denoise.detail_mask(predenoise,
                                          rad=2,
                                          radc=2,
                                          brz_a=3250,
                                          brz_b=1250)
    ret_mask = kgf.retinex_edgemask(predenoise).std.Binarize(
        9250).std.Median().std.Inflate()
    line_mask = core.std.Expr([detail_mask, ret_mask], 'x y max')

    deband_a = core.neo_f3kdb.Deband(antialias,
                                     17,
                                     42,
                                     42,
                                     42,
                                     12,
                                     0,
                                     sample_mode=4,
                                     keep_tv_range=True)
    deband_a = core.std.MaskedMerge(deband_a, antialias, line_mask)
    deband_b = core.neo_f3kdb.Deband(deband_a,
                                     18,
                                     48,
                                     48,
                                     48,
                                     0,
                                     0,
                                     sample_mode=2,
                                     keep_tv_range=True)
    deband_c = core.neo_f3kdb.Deband(deband_a,
                                     20,
                                     64,
                                     64,
                                     64,
                                     24,
                                     0,
                                     sample_mode=2,
                                     keep_tv_range=True)

    deband = lvf.rfs(deband_a, deband_b, [(edstart + 1870, edstart + 1900)])
    deband = lvf.rfs(
        deband,
        core.std.MaskedMerge(
            deband_c, deband_a,
            to_gray(lvf.src('masks/magia_07_mask_01.png'), src)),
        [(6729, 6865)])

    grain = kgf.adaptive_grain(deband, 0.25, luma_scaling=10)

    borders = vdf.region_mask(grain, 240, 240, 0, 0)
    borders = lvf.rfs(grain, borders, [(25623, 25670)])

    endcard = _perform_endcard(
        '[BDMV][200610][Magia Record][Vol.3]/Scans/endcard7_front_descreen.png',
        src)
    endcard_length = 119
    final = core.std.Splice([borders, endcard * endcard_length], True)
    final = core.resize.Bicubic(final,
                                format=vs.YUV420P10,
                                dither_type='error_diffusion')
    final = core.std.Limiter(final, 16, [235 << 2, 240 << 2])

    return depth(final, 10), endcard_length