Esempio n. 1
0
    def aa_stonks(clip: vs.VideoNode) -> vs.VideoNode:
        mask = core.std.Prewitt(get_y(clip)).std.Binarize(4000)
        mask = iterate(mask, core.std.Maximum, 4)
        mask = iterate(mask, core.std.Minimum, 2)
        mask = iterate(mask, core.std.Deflate, 4)
        mask = vdf.region_mask(mask, 400, 400, 0, 0)

        descale = core.descale.Debicubic(depth(get_y(clip), 32), 1440, 810)
        upscale = core.caffe.Waifu2x(descale, 3, 2, model=6)
        downscale = core.descale.Debilinear(upscale, 1920, 1080)
        merged = vdf.merge_chroma(depth(downscale, 16), clip)
        merged = core.std.MaskedMerge(clip, merged, mask)

        return core.warp.AWarpSharp2(merged, 128, 2, depth=16)
Esempio n. 2
0
 def deband_strong(clip: vs.VideoNode) -> vs.VideoNode:
     mask = core.std.BlankClip(clip, format=vs.GRAY16, color=(256 << 8) - 1)
     mask = vdf.region_mask(mask, 0, 960, 0, 0)
     mask = iterate(mask, core.std.Inflate, 20)
     deband = placebo.Deband(clip, 24, 20, 3, 0)
     return core.std.MaskedMerge(clip, deband, mask)
Esempio n. 3
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut

    # Variables
    opstart, opend = 2111, 4268
    edstart, edend = 31650, 33809
    full_zone = [(18727, 18774), (31590, 31649), (33990, src.num_frames - 1)
                 ]  # eyecatch, episode name and next episode
    shabc_zone = [(edstart + 15, edstart + 1215),
                  (edstart + 1882, edstart + 2126)]
    h = 720
    w = get_w(h)

    # Bicubic sharp parts don't have bad edges
    edges_a = core.edgefixer.ContinuityFixer(src, *[[2, 1, 1]] * 4)
    edges_b = awf.bbmod(src, left=6, thresh=32, blur=200)
    edges = lvf.rfs(edges_a, edges_b, [(edstart + 1275, edstart + 1757)])
    edges = lvf.rfs(edges, src, [(opstart, opend)] + full_zone)
    out = depth(edges, 32)

    # Denoise
    ref = hvf.SMDegrain(depth(get_y(out), 16), thSAD=450)
    denoise = hybrid_denoise(out, 0.35, 1.4, dict(a=2, d=1),
                             dict(ref=depth(ref, 32)))
    out = denoise
    # denoise = out

    # Descale
    luma = get_y(out)
    lineart = vdf.edge_detect(luma, 'FDOG', 0.055,
                              (1, 1)).std.Median().std.BoxBlur(0, 1, 1, 1, 1)

    descale_a = core.descale.Despline36(luma, w,
                                        h).std.SetFrameProp('descaleKernel',
                                                            data='spline36')
    descale_b = core.descale.Debicubic(luma, w, h, 0, 1).std.SetFrameProp(
        'descaleKernel', data='sharp_bicubic')
    descale = lvf.rfs(descale_a, descale_b, shabc_zone)

    # Chroma reconstruction
    # y_m is the assumed mangled luma.
    # Descale 1080p -> Bad conversion in 422 720p -> Regular 1080p 420
    radius = 2
    y, u, v = descale, plane(out, 1), plane(out, 2)
    y_m = core.resize.Point(y, 640, 720,
                            src_left=-1).resize.Bicubic(960,
                                                        540,
                                                        filter_param_a=1 / 3,
                                                        filter_param_b=1 / 3)

    # 0.25 for 444 and 0.25 for right shifting
    y_m, u, v = [
        c.resize.Bicubic(w,
                         h,
                         src_left=0.25 + 0.25,
                         filter_param_a=0,
                         filter_param_b=.5) for c in [y_m, u, v]
    ]

    y_fixup = core.std.MakeDiff(y, y_m)
    yu, yv = Regress(y_m, u, v, radius=radius, eps=1e-7)

    u_fixup = ReconstructMulti(y_fixup, yu, radius=radius)
    u_r = core.std.MergeDiff(u, u_fixup)

    v_fixup = ReconstructMulti(y_fixup, yv, radius=radius)
    v_r = core.std.MergeDiff(v, v_fixup)

    # -0.5 * 720/1080 = -1/3
    # -1/3 for the right shift
    # https://forum.doom9.org/showthread.php?p=1802716#post1802716
    u_r, v_r = [
        c.resize.Bicubic(960,
                         540,
                         src_left=-1 / 3,
                         filter_param_a=-.5,
                         filter_param_b=.25) for c in [u_r, v_r]
    ]

    upscale = vdf.fsrcnnx_upscale(
        descale,
        height=h * 2,
        shader_file=r'shaders\FSRCNNX_x2_56-16-4-1.glsl',
        upscaler_smooth=eedi3_upscale,
        profile='zastin')

    antialias = sraa_eedi3(upscale,
                           3,
                           alpha=0.2,
                           beta=0.4,
                           gamma=40,
                           nrad=3,
                           mdis=20)

    downscale = muvf.SSIM_downsample(antialias,
                                     src.width,
                                     src.height,
                                     filter_param_a=0,
                                     filter_param_b=0)
    downscale = core.std.MaskedMerge(luma, downscale, lineart)

    merged_a = join([downscale, u_r, v_r])
    merged_b = vdf.merge_chroma(downscale, denoise)
    merged = lvf.rfs(merged_a, merged_b, shabc_zone)
    out = depth(merged, 16)

    warp = xvs.WarpFixChromaBlend(out, 80, 2, depth=8)
    out = warp

    dering = gf.MaskedDHA(out,
                          rx=1.25,
                          ry=1.25,
                          darkstr=0.05,
                          brightstr=1.0,
                          maskpull=48,
                          maskpush=140)
    out = dering

    qtgmc = hvf.QTGMC(out, Preset="Slower", InputType=1, ProgSADMask=2.0)
    qtgmc = vdf.fade_filter(out, out, qtgmc, edstart + 1522,
                            edstart + 1522 + 24)
    qtgmc = lvf.rfs(out, qtgmc, [(edstart + 1522 + 25, edstart + 1757)])
    out = qtgmc

    out = lvf.rfs(out, depth(denoise, 16), [(opstart, opend)])

    detail_dark_mask = detail_dark_mask_func(get_y(out),
                                             brz_a=8000,
                                             brz_b=6000)
    detail_light_mask = lvf.denoise.detail_mask(out, brz_a=2500, brz_b=1200)
    detail_mask = core.std.Expr([detail_dark_mask, detail_light_mask],
                                'x y +').std.Median()
    detail_mask_grow = iterate(detail_mask, core.std.Maximum, 2)
    detail_mask_grow = iterate(detail_mask_grow, core.std.Inflate,
                               2).std.BoxBlur(0, 1, 1, 1, 1)

    detail_mask = core.std.Expr([get_y(out), detail_mask_grow, detail_mask],
                                f'x {32<<8} < y z ?')

    deband = dumb3kdb(out, 22, 30)
    deband = core.std.MaskedMerge(deband, out, detail_mask)
    out = deband

    ref = get_y(out).std.PlaneStats()
    adgmask_a = core.adg.Mask(ref, 30)
    adgmask_b = core.adg.Mask(ref, 12)

    stgrain = sizedgrn(out, 0.1, 0.05, 1.05, sharp=80)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_b)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_a.std.Invert())

    dygrain = sizedgrn(out, 0.2, 0.05, 1.15, sharp=80, static=False)
    dygrain = core.std.MaskedMerge(out, dygrain, adgmask_a)
    grain = core.std.MergeDiff(dygrain, out.std.MakeDiff(stgrain))
    out = grain

    ref = depth(edges, 16)
    credit = out
    rescale_mask = vdf.diff_rescale_mask(ref, h, b=0, c=1, mthr=40, sw=0, sh=0)
    rescale_mask = vdf.region_mask(rescale_mask, *[10] * 4)
    rescale_mask = hvf.mt_expand_multi(rescale_mask,
                                       mode='ellipse',
                                       sw=4,
                                       sh=4).std.BoxBlur(0, 1, 1, 1, 1)

    credit = lvf.rfs(credit, ref, full_zone)
    credit = lvf.rfs(credit, core.std.MaskedMerge(credit, ref, rescale_mask),
                     [(edstart, edend)])
    out = credit

    return depth(out, 10).std.Limiter(16 << 2, [235 << 2, 240 << 2])
Esempio n. 4
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut
    src = depth(src, 16)
    out = src
    if out.num_frames < 34046:
        while out.num_frames != 34046:
            out += out[-1]
    opstart, opend = 0, 2157
    h = 720
    w = get_w(h)


    fixedges = awf.bbmod(out, 2, 2, 2, 2, 64<<8, 999)
    out = fixedges


    decomb = hvf.Vinverse(out)
    decomb = lvf.rfs(out, decomb, [(2187, 2195)])
    ref = decomb
    out = decomb

    from adptvgrnMod import adptvgrnMod
    first_denoise = hybrid_denoise(out, 0.35, 1.5)
    regrain = adptvgrnMod(first_denoise, 0.275, 0.175, 1.25, 45)
    regrain = lvf.rfs(out, regrain, [(28691, 28818)])
    out = regrain


    clean = core.knlm.KNLMeansCL(out, h=0.55, a=2, d=3, device_type='gpu', device_id=0, channels='UV')
    clean = core.knlm.KNLMeansCL(clean, h=0.55, a=2, d=3, device_type='gpu', device_id=0, channels='Y')
    diff_den = core.std.MakeDiff(out, clean)
    out = depth(clean, 32)



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

    descale = core.descale.Debilinear(luma, w, h)
    upscale = vdf.nnedi3_upscale(descale, correct_shift=False, pscrn=1).resize.Bicubic(src.width, src.height, src_left=.5, src_top=.5)
    rescale = core.std.MaskedMerge(luma, upscale, line_mask)

    merged = vdf.merge_chroma(rescale, out)
    out = depth(merged, 16)


    moozzi = warping(out, 0.4, 4)
    sharp = hvf.LSFmod(moozzi, strength=95, Smode=3, Lmode=1, edgemode=1, edgemaskHQ=True)
    out = sharp


    deband_mask = lvf.denoise.detail_mask(out, brz_a=2000, brz_b=1000)
    deband = dbs.f3kpf(out, 17, 30, 30)
    deband = core.std.MaskedMerge(deband, out, deband_mask)
    out = deband


    grain_org = core.std.MergeDiff(out, diff_den)
    out = grain_org


    credit_mask = vdf.diff_rescale_mask(ref, mthr=40, sw=5, sh=5)
    credit_mask = vdf.region_mask(credit_mask, 10, 10, 10, 10).std.Inflate().std.Inflate()
    antialias = lvf.sraa(ref, 2, 13, downscaler=core.resize.Bicubic)
    credit = lvf.rfs(out, core.std.MaskedMerge(out, antialias, credit_mask),
                     [(2188, 2305), (33926, src.num_frames-1)])
    credit = lvf.rfs(credit, core.std.MaskedMerge(out, ref, credit_mask), [(31684, 33925)])

    out = credit


    src_c, ncop = [clip.std.Median() for clip in [src, JPBD_NCOP.src_cut]]
    opening_mask = vdf.dcm(out, src_c[opstart:opend+1], ncop[:opend-opstart+1], opstart, opend, 3, 3)
    credit_mask = opening_mask.std.Convolution([1]*9)

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


    return depth(out, 10)
Esempio n. 5
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_clip
    src = depth(src, 16)
    src = src[:34016] + src[34016 + 24 * 2 + 22:-24]
    out = src

    h = 720
    w = get_w(h)

    fixedges = awf.bbmod(out, 2, 2, 2, 2, 64 << 8, 999)
    out = fixedges

    decomb = hvf.Vinverse(out)
    decomb = lvf.rfs(out, decomb, [(756, 764)])
    ref = decomb
    out = decomb

    from adptvgrnMod import adptvgrnMod
    first_denoise = hybrid_denoise(out, 0.35, 1.5)
    regrain = adptvgrnMod(first_denoise, 0.275, 0.175, 1.25, 45)
    regrain = lvf.rfs(out, regrain, [(23709, 23786), (30397, 30546)])
    out = regrain

    clean = core.knlm.KNLMeansCL(out,
                                 h=0.55,
                                 a=2,
                                 d=3,
                                 device_type='gpu',
                                 device_id=0,
                                 channels='UV')
    clean = core.knlm.KNLMeansCL(clean,
                                 h=0.55,
                                 a=2,
                                 d=3,
                                 device_type='gpu',
                                 device_id=0,
                                 channels='Y')
    diff_den = core.std.MakeDiff(out, clean)
    out = depth(clean, 32)

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

    descale = core.descale.Debilinear(luma, w, h)
    upscale = vdf.nnedi3_upscale(descale, correct_shift=False,
                                 pscrn=1).resize.Bicubic(src.width,
                                                         src.height,
                                                         src_left=.5,
                                                         src_top=.5)
    rescale = core.std.MaskedMerge(luma, upscale, line_mask)

    merged = vdf.merge_chroma(rescale, out)
    out = depth(merged, 16)

    moozzi = warping(out, 0.4, 4)
    sharp = hvf.LSFmod(moozzi,
                       strength=95,
                       Smode=3,
                       Lmode=1,
                       edgemode=1,
                       edgemaskHQ=True)
    out = sharp

    deband_mask = lvf.denoise.detail_mask(out, brz_a=2000, brz_b=1000)
    deband = dbs.f3kpf(out, 17, 30, 30)
    deband_b = core.neo_f3kdb.Deband(out, 31, 18, 18, 18, 0, 0, 4)
    import placebo
    deband_c = placebo.deband(out, 18, 6, 1, 2)
    deband = lvf.rfs(deband, deband_b, [(16328, 16607), (17272, 17447)])
    deband = lvf.rfs(deband, deband_c, [(23709, 23786), (30397, 30546),
                                        (31115, 31353)])
    deband = core.std.MaskedMerge(deband, out, deband_mask)
    out = deband

    grain_org = core.std.MergeDiff(out, diff_den)
    out = grain_org

    credit_mask = vdf.diff_rescale_mask(ref, mthr=40, sw=5, sh=5)
    credit_mask = vdf.region_mask(credit_mask, 10, 10, 10,
                                  10).std.Inflate().std.Inflate()
    credit = lvf.rfs(out, core.std.MaskedMerge(out, ref, credit_mask),
                     [(0, 661), (26731, 34015)])
    credit = lvf.rfs(credit, src, [(34016, src.num_frames - 1)])
    out = credit

    return depth(out, 10)
Esempio n. 6
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut
    src += src[-1]

    # Variables
    opstart, opend = 2206, 4362
    eptitle_s, eptitle_e = 4363, 4482
    edstart, edend = 31528, 33685
    preview_s, preview_e = 33686, src.num_frames - 1
    h = 720
    w = get_w(h)

    edges = core.edgefixer.ContinuityFixer(src, *[[2, 1, 1]] * 4)
    out = depth(edges, 32)

    ref = hvf.SMDegrain(depth(get_y(out), 16), thSAD=450)
    denoise = hybrid_denoise(out, 0.35, 1.75, dict(a=2, d=1),
                             dict(ref=depth(ref, 32)))
    out = denoise

    y = get_y(out)
    lineart = vdf.edge_detect(y, 'FDOG', 0.055,
                              (1, 1)).std.Median().std.Convolution([*[1] * 9])

    descale = core.descale.Debilinear(y, w, h)

    upscale = vdf.fsrcnnx_upscale(
        descale,
        height=h * 2,
        shader_file=r'shaders\FSRCNNX_x2_56-16-4-1.glsl',
        upscaler_smooth=eedi3_upscale,
        profile='slow',
        strength=85)

    antialias = sraa_eedi3(upscale, 9)

    downscale = muvf.SSIM_downsample(antialias,
                                     src.width,
                                     src.height,
                                     filter_param_a=0,
                                     filter_param_b=0)
    downscale = core.std.MaskedMerge(y, downscale, lineart)

    merged = vdf.merge_chroma(downscale, out)
    merged = lvf.rfs(merged, out, [(opstart, opend), (eptitle_s, eptitle_e),
                                   (preview_s, preview_e)])
    out = depth(merged, 16)

    detail_dark_mask = detail_dark_mask_func(get_y(out),
                                             brz_a=8000,
                                             brz_b=6000)
    detail_light_mask = lvf.denoise.detail_mask(out, brz_a=2500, brz_b=1200)
    detail_mask = core.std.Expr([detail_dark_mask, detail_light_mask],
                                'x y +').std.Median()
    detail_mask_grow = iterate(detail_mask, core.std.Maximum, 2)
    detail_mask_grow = iterate(detail_mask_grow, core.std.Inflate,
                               2).std.Convolution([*[1] * 9])

    detail_mask = core.std.Expr([get_y(out), detail_mask_grow, detail_mask],
                                f'x {32<<8} < y z ?')

    deband = dumb3kdbv2(out, 22, 24)
    deband = core.std.MaskedMerge(deband, out, detail_mask)
    out = deband

    ref = get_y(out).std.PlaneStats()
    adgmask_a = core.adg.Mask(ref, 30)
    adgmask_b = core.adg.Mask(ref, 12)

    stgrain = sizedgrn(out, 0.1, 0.05, 1.05, sharp=80)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_b)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_a.std.Invert())

    dygrain = sizedgrn(out, 0.2, 0.05, 1.15, sharp=80, static=False)
    dygrain = core.std.MaskedMerge(out, dygrain, adgmask_a)
    grain = core.std.MergeDiff(dygrain, out.std.MakeDiff(stgrain))
    out = grain

    ref = depth(src, 16)
    rescale_mask = vdf.drm(ref, 720, 'bilinear', mthr=30, sw=0, sh=0)
    rescale_mask = vdf.region_mask(rescale_mask, *[10] * 4)
    rescale_mask = hvf.mt_expand_multi(rescale_mask,
                                       mode='ellipse',
                                       sw=4,
                                       sh=4)
    rescale_mask = rescale_mask.std.Binarize(scale_value(
        100, 8, 16)).std.Inflate().std.Convolution([*[1] * 9])

    dehalo_ref = gf.MaskedDHA(ref,
                              rx=1.65,
                              ry=1.65,
                              darkstr=0.15,
                              brightstr=1.0,
                              maskpull=48,
                              maskpush=140)
    dehalo_mask_b = vdf.region_mask(rescale_mask, right=400)

    credit = out
    dehalo_range_b = [(18, 101), (16853, 16917)]
    credit = lvf.rfs(credit, core.std.MaskedMerge(credit, ref, rescale_mask,
                                                  0),
                     [(126, 160), (1722, 1761), (7749, 7795), (8189, 8232),
                      (8445, 8504), (9020, 9067), (9727, 9768), (12430, 12520),
                      (12876, 12971), (13608, 13646), (15833, 15879),
                      (20526, 20586), (20783, 20823), (21193, 21239),
                      (23812, 23854), (24018, 24059),
                      (edstart, edend)] + dehalo_range_b)

    credit = lvf.rfs(
        credit, core.std.MaskedMerge(credit, dehalo_ref, dehalo_mask_b, 0),
        dehalo_range_b)
    out = credit

    return depth(out, 10).std.Limiter(16 << 2, [235 << 2, 240 << 2])
Esempio n. 7
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 _line_mask(clip: vs.VideoNode, thr: int) -> vs.VideoNode:
        mask = core.std.Prewitt(clip)
        mask = core.std.Expr(mask, 'x 2 *').std.Median()
        mask = core.std.Expr(mask, f'x {thr} < x x 3 * ?')
        return mask.std.Inflate().std.Deflate()

    def _ssharp(clip: vs.VideoNode,
                strength: float,
                width: int,
                height: int,
                factor: float = 2,
                b: float = -1,
                c: float = 6) -> vs.VideoNode:
        source = clip
        sharp = core.resize.Bicubic(clip, clip.width*factor, clip.height*factor, \
            filter_param_a=b, filter_param_b=c).resize.Lanczos(width, height)

        source = core.resize.Spline64(source, sharp.width, sharp.height)

        sharp = core.rgvs.Repair(sharp, source, 13)
        sharp = mvf.LimitFilter(source,
                                sharp,
                                thrc=0.5,
                                elast=6,
                                brighten_thr=0.5,
                                planes=0)

        final = core.std.Expr([sharp, source],
                              f'x {strength} * y 1 {strength} - * +')
        return final

    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))

    def _perform_masks_credit(path: Path) -> List[MaskCredit]:
        return [
            MaskCredit(lvf.src(str(mask)), int(str(mask.stem).split('_')[2]),
                       int(str(mask.stem).split('_')[3]))
            for mask in path.glob('*')
        ]

    # pylint: disable=unused-argument
    def _diff(n: int, f: vs.VideoFrame, new: vs.VideoNode,
              adapt: vs.VideoNode) -> vs.VideoNode:
        psa = f.props['PlaneStatsAverage']
        if psa > 0.5:
            clip = new
        elif psa < 0.4:
            clip = adapt
        else:
            weight = (psa - 0.4) * 10
            clip = core.std.Merge(adapt, new, weight)
        return clip

    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

    opstart, opend = 1774, 3944
    edstart, edend = 31781, 33938
    src = JPBD.src_cut
    src = depth(src, 16)

    denoise_a = mdf.hybriddenoise_mod(src, 0.55, 2.25)
    denoise_b = mdf.hybriddenoise_mod(src, 0.55, 10)
    denoise = lvf.rfs(denoise_a, denoise_b, [(opstart, opstart + 44)])
    diff = core.std.MakeDiff(src, denoise, [0, 1, 2])

    luma = get_y(denoise)

    upscale = _nnedi3(luma, 1.5, dict(nsize=0, nns=3, qual=1, pscrn=1))
    sraa = _sraa(upscale, dict(nsize=0, nns=3, qual=1, pscrn=1),
                 dict(alpha=0.2, beta=0.5, gamma=80, nrad=3, mdis=18))
    sraa = core.rgvs.Repair(sraa, upscale, 3)
    rescale = _ssharp(sraa, 0.55, src.width, src.height, 2)

    artefacts_mask = core.std.BlankClip(rescale, color=(256 << 8) - 1)
    artefacts_mask = vdf.region_mask(artefacts_mask, 2, 2, 2, 2).std.Inflate()
    rescale = core.std.MaskedMerge(luma, rescale, artefacts_mask)

    lineart_mask = _line_mask(luma, 8000)
    antialias = core.std.MaskedMerge(luma, rescale, lineart_mask)

    antialias_merged = vdf.merge_chroma(antialias, denoise)

    src_c, src_ncop = [
        c.knlm.KNLMeansCL(a=6, h=20, d=0, device_type='gpu')
        for c in [src, JPBD_NCOP.src_cut[:opend - opstart + 1]]
    ]
    credit_mask = vdf.dcm(src, src_c[opstart:opend + 1], src_ncop, opstart,
                          opend, 2, 2).std.Deflate()
    credit = core.std.MaskedMerge(antialias_merged, denoise, credit_mask)

    masks_credit_ = _perform_masks_credit(Path('masks_' + JPBD.name[-2:] +
                                               '/'))

    for mask in masks_credit_:
        credit = lvf.rfs(
            credit,
            core.std.MaskedMerge(credit, denoise,
                                 to_gray(mask.mask, src).std.Deflate()),
            [(mask.start_frame, mask.end_frame)])

    deband_mask = lvf.denoise.detail_mask(credit, brz_a=3000, brz_b=1500)
    dark_mask = core.std.Expr([deband_mask, _ret_mask(credit, 12500)], 'x y +')
    deband_a = dbs.f3kpf(credit, 18, 36, 36)
    deband_b = dbs.f3kpf(credit, 18, 42, 42)
    deband_c = placebo.Deband(credit,
                              radius=12,
                              threshold=20,
                              iterations=3,
                              grain=0)
    deband_d = placebo.Deband(credit,
                              radius=10,
                              threshold=8,
                              iterations=2,
                              grain=0)
    deband = lvf.rfs(deband_a, deband_b, [(opstart + 483, opstart + 554)])
    deband = lvf.rfs(deband, deband_c, [(opstart, opstart + 44)])
    deband = lvf.rfs(deband, core.std.MaskedMerge(deband_d, credit, dark_mask),
                     [(opstart + 1070, opstart + 1103)])
    deband = lvf.rfs(deband, deband_d, [(opstart + 1104, opstart + 1124)])
    deband = core.std.MaskedMerge(deband, credit, deband_mask)

    grain_original = core.std.MergeDiff(deband, diff, [0, 1, 2])
    grain_original_nochroma = core.std.MergeDiff(deband, diff, [0])
    grain_new = core.neo_f3kdb.Deband(deband,
                                      preset='depth',
                                      grainy=32,
                                      grainc=32)
    grain_new_nochroma = vdf.merge_chroma(grain_new, deband)
    avg = core.std.PlaneStats(deband)
    adapt_mask = core.adg.Mask(get_y(avg), 28)
    grain_adapt = core.std.MaskedMerge(grain_new, grain_original, adapt_mask)

    gf3_args = dict(g1str=5,
                    g2str=3.85,
                    g3str=3.5,
                    g1shrp=50,
                    g2shrp=60,
                    g3shrp=60,
                    g1size=1.65,
                    g2size=1.60,
                    g3size=1.25)
    grain_a = hvf.GrainFactory3(deband, **gf3_args)
    grain_b = mdf.adptvgrnMod_mod(deband,
                                  2,
                                  size=1.5,
                                  sharp=60,
                                  static=False,
                                  luma_scaling=14)

    grain = core.std.FrameEval(
        deband, partial(_diff, new=grain_new, adapt=grain_adapt), avg)

    grain = lvf.rfs(grain, grain_original, [(5058, 5253), (7871, 7989)])
    grain = lvf.rfs(grain, grain_new_nochroma,
                    [(opstart + 117, opstart + 873),
                     (opstart + 921, opstart + 993)])
    grain = lvf.rfs(grain, grain_original_nochroma,
                    [(opstart + 874, opstart + 920),
                     (opstart + 994, opstart + 1069),
                     (opstart + 1125, opstart + 1202)])
    grain = lvf.rfs(grain, grain_a, [(opstart, opstart + 44)])
    grain = lvf.rfs(grain, grain_b, [(opstart + 1070, opstart + 1124)])

    stabilize = hvf.GSMC(src, radius=2, planes=0)
    ending = vdf.merge_chroma(stabilize, denoise)
    final = lvf.rfs(grain, ending, [(edstart, edend)])

    final = final[:opstart] + final[opstart + 2:opstart + 2160] + final[opend +
                                                                        1:]
    return depth(final, 10), opstart, opend
Esempio n. 8
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 _line_mask(clip: vs.VideoNode, thr: int) -> vs.VideoNode:
        mask = core.std.Prewitt(clip)
        mask = core.std.Expr(mask, 'x 2 *').std.Median()
        mask = core.std.Expr(mask, f'x {thr} < x x 3 * ?')
        return mask.std.Inflate().std.Deflate()

    def _ssharp(clip: vs.VideoNode,
                strength: float,
                width: int,
                height: int,
                factor: float = 2,
                b: float = -1,
                c: float = 6) -> vs.VideoNode:
        source = clip
        sharp = core.resize.Bicubic(clip, clip.width*factor, clip.height*factor, \
            filter_param_a=b, filter_param_b=c).resize.Lanczos(width, height)

        source = core.resize.Spline64(source, sharp.width, sharp.height)

        sharp = core.rgvs.Repair(sharp, source, 13)
        sharp = mvf.LimitFilter(source,
                                sharp,
                                thrc=0.5,
                                elast=6,
                                brighten_thr=0.5,
                                planes=0)

        final = core.std.Expr([sharp, source],
                              f'x {strength} * y 1 {strength} - * +')
        return final

    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))

    def _perform_masks_credit(path: Path) -> List[MaskCredit]:
        return [
            MaskCredit(lvf.src(str(mask)), int(str(mask.stem).split('_')[2]),
                       int(str(mask.stem).split('_')[3]))
            for mask in path.glob('*')
        ]

    def _w2x(clip: vs.VideoNode) -> vs.VideoNode:
        waifu2x = core.w2xc.Waifu2x(mvf.ToRGB(clip, depth=32), noise=2, scale=2) \
            .resize.Bicubic(clip.width, clip.height)
        return mvf.ToYUV(waifu2x, css='420', depth=16)

    def _perform_filtering_ending(clip: vs.VideoNode,
                                  adapt_mask: vs.VideoNode) -> vs.VideoNode:
        luma = get_y(clip)
        denoise_a = mvf.BM3D(luma, 2.25, 1)
        denoise_b = mvf.BM3D(luma, 1.25, 1)
        denoise = core.std.MaskedMerge(denoise_a, denoise_b, adapt_mask)
        grain = core.grain.Add(denoise, 0.3, constant=True)
        return core.std.MaskedMerge(denoise, grain, adapt_mask)

    # pylint: disable=unused-argument
    def _diff(n: int, f: vs.VideoFrame, new: vs.VideoNode,
              adapt: vs.VideoNode) -> vs.VideoNode:
        psa = f.props['PlaneStatsAverage']
        if psa > 0.5:
            clip = new
        elif psa < 0.4:
            clip = adapt
        else:
            weight = (psa - 0.4) * 10
            clip = core.std.Merge(adapt, new, weight)
        return clip

    def aa_stonks(clip: vs.VideoNode) -> vs.VideoNode:
        mask = core.std.Prewitt(get_y(clip)).std.Binarize(4000)
        mask = iterate(mask, core.std.Maximum, 4)
        mask = iterate(mask, core.std.Minimum, 2)
        mask = iterate(mask, core.std.Deflate, 4)
        mask = vdf.region_mask(mask, 400, 400, 0, 0)

        descale = core.descale.Debicubic(depth(get_y(clip), 32), 1440, 810)
        upscale = core.caffe.Waifu2x(descale, 3, 2, model=6)
        downscale = core.descale.Debilinear(upscale, 1920, 1080)
        merged = vdf.merge_chroma(depth(downscale, 16), clip)
        merged = core.std.MaskedMerge(clip, merged, mask)

        return core.warp.AWarpSharp2(merged, 128, 2, depth=16)

    opstart, opend = 1272, 3428
    edstart, edend = 31769, 33926
    src = JPBD.src_cut
    src = depth(src, 16)
    src = core.std.FreezeFrames(src, opstart + 2132, opend, opstart + 2132)

    denoise = mdf.hybriddenoise_mod(src, 0.55, 2.25)
    diff = core.std.MakeDiff(src, denoise, [0, 1, 2])

    luma = get_y(denoise)

    upscale = _nnedi3(luma, 1.5, dict(nsize=0, nns=3, qual=1, pscrn=1))
    sraa = _sraa(upscale, dict(nsize=0, nns=3, qual=1, pscrn=1),
                 dict(alpha=0.2, beta=0.5, gamma=80, nrad=3, mdis=18))
    sraa = core.rgvs.Repair(sraa, upscale, 3)
    rescale = _ssharp(sraa, 0.55, src.width, src.height, 2)

    artefacts_mask = core.std.BlankClip(rescale, color=(256 << 8) - 1)
    artefacts_mask = vdf.region_mask(artefacts_mask, 2, 2, 2, 2).std.Inflate()
    rescale = core.std.MaskedMerge(luma, rescale, artefacts_mask)

    lineart_mask = _line_mask(luma, 8000)
    antialias = core.std.MaskedMerge(luma, rescale, lineart_mask)

    antialias_merged = vdf.merge_chroma(antialias, denoise)

    antialias_strong_a = aa_stonks(denoise)
    antialias_strong_b = TAAmbk(denoise, 'Eedi3', cycle=3, mtype=0)
    antialias_merged = lvf.rfs(antialias_merged, antialias_strong_a,
                               [(4782, 4785)])
    antialias_merged = lvf.rfs(antialias_merged, antialias_strong_b,
                               [(11808, 11898)])

    src_c, src_ncop = [
        c.knlm.KNLMeansCL(a=6, h=20, d=0, device_type='gpu')
        for c in [src, JPBD_NCOP.src_cut[:opend - opstart + 1]]
    ]
    credit_mask = vdf.dcm(src, src_c[opstart:opend + 1], src_ncop, opstart,
                          opend, 2, 2).std.Deflate()
    credit = core.std.MaskedMerge(antialias_merged, denoise, credit_mask)

    masks_credit_ = _perform_masks_credit(Path('masks_' + JPBD.name[-2:] +
                                               '/'))

    for mask in masks_credit_:
        credit = lvf.rfs(
            credit,
            core.std.MaskedMerge(credit, denoise,
                                 to_gray(mask.mask, src).std.Deflate()),
            [(mask.start_frame, mask.end_frame)])

    deband_mask = lvf.denoise.detail_mask(credit, brz_a=3000, brz_b=1500)
    deband_a = dbs.f3kpf(credit, 18, 36, 36)
    deband_b = dbs.f3kpf(credit, 18, 42, 42)
    deband_c = placebo.Deband(credit,
                              radius=16,
                              threshold=4,
                              iterations=1,
                              grain=0)
    deband_d = placebo.Deband(deband_b,
                              radius=20,
                              threshold=5,
                              iterations=1,
                              grain=0)
    deband = lvf.rfs(deband_a, deband_b, [(opstart, opstart + 146)])
    deband = lvf.rfs(deband, deband_c, [(opstart + 1225, opstart + 1238)])
    deband = lvf.rfs(deband, deband_d, [(opstart + 970, opstart + 984)])
    deband = core.std.MaskedMerge(deband, credit, deband_mask)

    grain_original = core.std.MergeDiff(deband, diff, [0, 1, 2])
    grain_new = core.neo_f3kdb.Deband(deband,
                                      preset='depth',
                                      grainy=32,
                                      grainc=32)
    avg = core.std.PlaneStats(deband)
    adapt_mask = core.adg.Mask(get_y(avg), 28)
    grain_adapt = core.std.MaskedMerge(grain_new, grain_original, adapt_mask)

    grain = core.std.FrameEval(
        deband, partial(_diff, new=grain_new, adapt=grain_adapt), avg)

    grain = lvf.rfs(grain, grain_new, [(opstart + 147, opstart + 496),
                                       (opstart + 575, opstart + 644),
                                       (opstart + 702, opstart + 969),
                                       (opstart + 1076, opstart + 1117),
                                       (opstart + 1428, opstart + 1461),
                                       (opstart + 1859, opstart + 2035)])
    grain = lvf.rfs(grain, grain_original, [(14412, 14728), (31146, 31299)])

    w2x = _w2x(denoise).grain.Add(1, 0.5, constant=True)
    w2x = lvf.rfs(grain, w2x, [(opstart + 1211, opstart + 1224)])

    ending = _perform_filtering_ending(src, adapt_mask)
    ending = vdf.merge_chroma(ending, denoise)
    final = lvf.rfs(w2x, ending, [(edstart, edend)])

    return depth(final, 10)
Esempio n. 9
0
def do_filter():
    """Vapoursynth filtering"""
    # Source and dithering
    src = JPBD.src_cut
    src = depth(src, 16)
    out = src

    # Variables
    opstart, opend = 17310, 19468
    edstart, edend = 31889, src.num_frames - 1
    h = 846
    w = get_w(h)
    cubic_filters = ['catrom', 'mitchell', 'robidoux', 'robidoux sharp']
    cubic_filters = [vdf.get_bicubic_params(cf) for cf in cubic_filters]

    # Remove the dynamic grain
    degrain = hybrid_denoise(out, 0.35, 1.2, dict(a=2, d=1))
    out = degrain

    y = get_y(out)
    y32 = depth(y, 32)
    lineart = vdf.edge_detect(y32, 'kirsch', 0.055,
                              (1, 1)).std.Median().std.Inflate()

    # Use multiple descaling kernel for a sharper result
    descale_clips = [
        core.descale.Debicubic(y32, w, h, b, c) for b, c in cubic_filters
    ]
    descale = core.std.Expr(descale_clips,
                            'x y z a min min min x y z max max min')

    # Fix descaling artifacts (yes even for catrom there's still artifacts)
    conv = core.std.Convolution(descale, [1, 2, 1, 2, 0, 2, 1, 2, 1])
    thr, coef = 0.013, 3.2
    descale_fix = core.std.Expr([descale, conv],
                                f'x y - abs {thr} < y x ?').std.PlaneStats()
    adapt_mask = core.adg.Mask(
        descale_fix,
        12).std.Invert().std.Expr(f'x 0.80 - {coef} * 0.20 + 0 max 1 min')

    descale = core.std.MaskedMerge(descale, descale_fix, adapt_mask)

    # Double using eedi3+nnedi, fsrcnnx and a sharpener
    upscale = vdf.fsrcnnx_upscale(descale,
                                  w * 2,
                                  h * 2,
                                  r'shaders\FSRCNNX_x2_56-16-4-1.glsl',
                                  upscaler_smooth=eedi3_upscale,
                                  profile='zastin',
                                  sharpener=partial(gf.DetailSharpen,
                                                    sstr=1.25,
                                                    power=4))

    # Antialiasing by eedi3
    aa_strong = sraa_eedi3(upscale, 13, alpha=0.3, beta=0.5, gamma=40)
    aa = aa_strong

    # Rescale to 1080p with Bicubic b=0, c=0 AKA Hermite
    down = muvf.SSIM_downsample(aa,
                                src.width,
                                src.height,
                                filter_param_a=0,
                                filter_param_b=0)

    upscale = depth(core.std.MaskedMerge(y32, down, lineart), 16)

    merged = vdf.merge_chroma(upscale, out)
    out = merged

    # Very bad halo
    dehalo = rkt.rekt_fast(out,
                           lambda x: gf.MaskedDHA(x,
                                                  rx=4.5,
                                                  ry=4.5,
                                                  darkstr=0.05,
                                                  brightstr=1.0,
                                                  maskpull=48,
                                                  maskpush=140),
                           left=620,
                           right=440,
                           top=200,
                           bottom=176)
    mask_halo = get_y(out.std.MakeDiff(dehalo,
                                       0)).rgvs.RemoveGrain(2).std.Median()
    mask_halo = core.hist.Luma(mask_halo).std.Binarize(
        33000).std.Maximum().std.Inflate()
    mask_halo = vdf.region_mask(mask_halo,
                                left=630,
                                right=440,
                                top=200,
                                bottom=176)
    blur_dehalo = core.std.BoxBlur(dehalo, hradius=1, vradius=1)
    dehalo = core.std.MaskedMerge(dehalo, blur_dehalo, mask_halo)
    out = lvf.rfs(out, dehalo, [(15936, 15973)])

    # Deband with prefilter
    y = get_y(out)
    detail_light_mask = lvf.denoise.detail_mask(y, brz_a=2500, brz_b=1200)

    pf = iterate(out, core.std.Maximum, 2).std.Convolution([10] * 9, planes=0)
    diff = core.std.MakeDiff(out, pf)

    deband = core.f3kdb.Deband(pf,
                               17,
                               36,
                               36,
                               36,
                               12,
                               12,
                               2,
                               keep_tv_range=True,
                               output_depth=16)
    deband = core.std.MergeDiff(deband, diff)
    deband = core.std.MaskedMerge(deband, out, detail_light_mask)
    out = deband

    # Ending part
    ref = degrain
    pfed = ref.std.Median().std.Convolution([1] * 9, planes=0)
    diff_ed = core.std.MakeDiff(ref, pfed)
    deband_ed = core.f3kdb.Deband(pfed,
                                  22,
                                  30,
                                  30,
                                  30,
                                  32,
                                  32,
                                  2,
                                  keep_tv_range=True,
                                  output_depth=16)
    deband_ed = core.std.MergeDiff(deband_ed, diff_ed)
    out = lvf.rfs(out, deband_ed, [(edstart, edend)])

    # Regraining
    grain = adptvgrnMod(out,
                        0.25,
                        0.15,
                        size=out.height / h,
                        sharp=80,
                        luma_scaling=10,
                        static=True)
    out = grain

    # Restore 1080p stuff
    ref = src
    rescale_mask = vdf.drm(ref, h, mthr=65, sw=4, sh=4)

    credit = out
    credit = lvf.rfs(credit, core.std.MaskedMerge(credit, ref, rescale_mask,
                                                  0), [(opstart, opend),
                                                       (3917, 4024)])
    out = credit

    return depth(out, 10).std.Limiter(16 << 2, [235 << 2, 240 << 2])
Esempio n. 10
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut
    src = depth(src, 32)
    src += src[-1]

    h = 720
    w = get_w(h)
    b, c = vdf.get_bicubic_params('robidoux')
    full_stuff = [(313, 1993), (15602, 15757), (19240, 19295)]

    denoise = hybrid_denoise(src, 0.5, 2)
    denoise_b = hybrid_denoise(src, 0.6, 7.5, bm3d_args=dict(profile1='high'))
    deband = lvf.rfs(denoise, denoise_b, [(4002, 4061)])
    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)

    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, _aa_extra(depth(denoise, 16)), [(2794, 2949)])
    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 = core.std.MaskedMerge(deband, out, deband_mask)
    deband_b = rkt.rekt_fast(
        deband, lambda x: core.neo_f3kdb.Deband(
            x, 8, 96, 96, 96, sample_mode=1, keep_tv_range=True), 1112, 264,
        184, 0)
    deband = lvf.rfs(deband, deband_b, [(4002, 4061)])
    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)
    grain_b = adptvgrnMod(grain,
                          0.6,
                          size=1.85,
                          static=False,
                          luma_scaling=4,
                          grain_chroma=False)
    grain = lvf.rfs(grain, grain_b, [(204, 2049)])
    out = grain

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

    credit = lvf.rfs(out, core.std.MaskedMerge(out, ref, rescale_mask),
                     full_stuff)
    credit = lvf.rfs(
        credit,
        core.std.MaskedMerge(credit, ref,
                             vdf.region_mask(rescale_mask, 1450, 0, 947, 0)),
        [(33860, 33925)])
    credit = lvf.rfs(credit, src, [(0, 203), (2049, 2229), (31768, 33839)])
    out = credit

    smooth = gf.JohnFPS(out[31888:33840], 60000, 1001)
    clips = [out[:31888], smooth, out[33840:]]
    vfr = muvf.VFRSplice(clips, 'symphogearg_01_timecode.txt')
    cfr = core.std.AssumeFPS(vfr, src)
    out = cfr

    return depth(out, 10)
Esempio n. 11
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut
    src = depth(src, 16)
    out = src
    opstart, opend = 3106, 5263
    h = 720
    w = get_w(h)

    fixedges = awf.bbmod(out, 2, 2, 2, 2, 64 << 8, 999)
    out = fixedges

    decomb = hvf.Vinverse(out)
    decomb = lvf.rfs(out, decomb, [(10171, 10179)])
    ref = decomb
    out = decomb

    clean = core.knlm.KNLMeansCL(out,
                                 h=0.55,
                                 a=2,
                                 d=3,
                                 device_type='gpu',
                                 device_id=0,
                                 channels='UV')
    clean = core.knlm.KNLMeansCL(clean,
                                 h=0.55,
                                 a=2,
                                 d=3,
                                 device_type='gpu',
                                 device_id=0,
                                 channels='Y')
    diff_den = core.std.MakeDiff(out, clean)
    out = depth(clean, 32)

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

    descale = core.descale.Debilinear(luma, w, h)
    upscale = vdf.nnedi3_upscale(descale, correct_shift=False,
                                 pscrn=1).resize.Bicubic(src.width,
                                                         src.height,
                                                         src_left=.5,
                                                         src_top=.5)
    rescale = core.std.MaskedMerge(luma, upscale, line_mask)

    merged = vdf.merge_chroma(rescale, out)
    out = depth(merged, 16)

    moozzi = warping(out, 0.4, 4)
    sharp = hvf.LSFmod(moozzi,
                       strength=95,
                       Smode=3,
                       Lmode=1,
                       edgemode=1,
                       edgemaskHQ=True)
    out = sharp

    deband_mask = lvf.denoise.detail_mask(out, brz_a=2000, brz_b=1000)
    deband = dbs.f3kpf(out, 17, 30, 30)
    deband = core.std.MaskedMerge(deband, out, deband_mask)
    out = deband

    grain_org = core.std.MergeDiff(out, diff_den)
    out = grain_org

    credit_mask = vdf.diff_rescale_mask(ref, mthr=40, sw=5, sh=5)
    credit_mask = vdf.region_mask(credit_mask, 10, 10, 10,
                                  10).std.Inflate().std.Inflate()
    antialias = lvf.sraa(ref, 2, 13, downscaler=core.resize.Bicubic)
    credit = lvf.rfs(out, core.std.MaskedMerge(out, antialias, credit_mask),
                     [(10178, 10248), (21423, 22764)])

    out = credit

    src_c, ncop = [clip.std.Median() for clip in [src, JPBD_NCOP.src_cut]]
    opening_mask = vdf.dcm(out, src_c[opstart:opend + 1],
                           ncop[:opend - opstart + 1], opstart, opend, 3,
                           3).std.Convolution([1] * 9)

    credit = lvf.rfs(out, core.std.MaskedMerge(out, src, opening_mask),
                     [(opstart, opend)])
    out = credit

    logo_mask = lvf.src('credit_mask_logo.png', out).std.Convolution([1] * 9)
    credit = lvf.rfs(out, core.std.MaskedMerge(out, ref, logo_mask),
                     [(20992, 23225)])
    credit = lvf.rfs(credit, src, [(97766, src.num_frames - 1)])
    out = credit

    return depth(out, 10)
Esempio n. 12
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)
Esempio n. 13
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
Esempio n. 14
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut
    src = src.std.SetFrameProp('_Matrix', intval=1)

    # Variables
    edstart, edend = 165528, src.num_frames-1
    h = 844
    w = get_w(h)


    edges_a = core.edgefixer.ContinuityFixer(src, *[[1, 0, 0]]*4)
    edges_b = core.edgefixer.ContinuityFixer(src, [0, 0, 0], [1, 0, 0], [0, 0, 0], [1, 0, 0])
    edges = lvf.rfs(edges_a, edges_b, [(8900, 9001)])
    out = depth(edges, 32)


    ref = hvf.SMDegrain(depth(get_y(out), 16), thSAD=300)
    denoise = hybrid_denoise(out, 0.25, 1.05, dict(a=2, d=1), dict(ref=depth(ref, 32)))
    out = denoise



    y = get_y(out)
    lineart = vdf.edge_detect(y, 'FDOG', 0.065, (1, 1)).std.Median().std.BoxBlur(0, 1, 1, 1, 1)


    descale_clips = [core.resize.Bicubic(y, w, h, filter_param_a=1/3, filter_param_b=1/3),
                     core.descale.Debicubic(y, w, h, 0, 1/2),
                     core.descale.Debicubic(y, w, h, 1/3, 1/3)]
    descale = core.std.Expr(descale_clips, 'x y z min max y z max min z min')
    descale_bcsharp = core.descale.Debicubic(y, get_w(842), 842, 0, 1)
    descale_bcsharp = gf.MaskedDHA(depth(descale_bcsharp, 16), rx=1.4, ry=1.4, darkstr=0.05, brightstr=1.0, maskpull=48, maskpush=140)


    upsc_args = dict(shader_file='shaders/FSRCNNX_x2_56-16-4-1.glsl', upscaler_smooth=eedi3_upscale, profile='zastin',
                     sharpener=partial(gf.DetailSharpen, sstr=1.65, power=4, mode=0, med=True))

    upscale = vdf.fsrcnnx_upscale(descale, height=h*2, **upsc_args)
    antialias = sraa_eedi3(upscale, 3, alpha=0.2, beta=0.4, gamma=100, mdis=20, nrad=3)
    downscale_a = muvf.SSIM_downsample(antialias, src.width, src.height, kernel='Bicubic', filter_param_a=0, filter_param_b=0)


    upscale = eedi3_upscale(descale_bcsharp)
    antialias = sraa_eedi3(depth(upscale, 32), 3, alpha=0.2, beta=0.4, gamma=100, mdis=20, nrad=3)
    downscale_b = muvf.SSIM_downsample(antialias, src.width, src.height, kernel='Bicubic', filter_param_a=0, filter_param_b=0)



    downscale = lvf.rfs(downscale_a, downscale_b, [(52931, 53004), (53459, 53696), (54218, 54441),
                                                   (55137, 55254), (55420, 55570)])
    scaled = core.std.MaskedMerge(y, downscale, lineart)

    merged = vdf.merge_chroma(scaled, out)
    out = depth(merged, 16)



    crop = core.std.Crop(out, left=4, right=2)
    fb = core.fb.FillBorders(crop, 1, 1, 0, 0, mode="fillmargins")
    resize = core.resize.Bicubic(fb, 1920)
    out = lvf.rfs(out, resize, [(8900, 9001)])



    detail_light_mask = lvf.denoise.detail_mask(out, brz_a=2500, brz_b=1200)

    deband = dumb3kdb(out, 16, 42)
    deband = core.std.MaskedMerge(deband, out, detail_light_mask)
    out = deband





    ref = get_y(out).std.PlaneStats()
    adgmask_a = core.adg.Mask(ref, 30)
    adgmask_b = core.adg.Mask(ref, 12)

    stgrain = sizedgrn(out, 0.1, 0.05, 1.05, sharp=80, static=True)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_b)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_a.std.Invert())

    dygrain = sizedgrn(out, 0.2, 0.05, 1.15, sharp=80, static=False)
    dygrain = core.std.MaskedMerge(out, dygrain, adgmask_a)
    grain = core.std.MergeDiff(dygrain, out.std.MakeDiff(stgrain))
    out = grain




    ref = depth(src, 16)
    src_c, src_nced = [c.std.BoxBlur(0, 1, 1, 1, 1) for c in [src, JPBD_NCED.src_cut]]
    creditless_mask = vdf.dcm(ref, src_c[edstart:edend+1], src_nced[:edend-edstart+1], edstart, edend, 3, 3).std.Deflate()
    rescale_mask = vdf.drm(ref, h, b=1/3, c=1/3, mthr=30)
    rescale_mask = vdf.region_mask(rescale_mask, 20, 20, 20, 20).std.Binarize(6000).std.Maximum().std.Maximum().std.Inflate()

    credit = out
    credit = lvf.rfs(credit, ref, [(0, 672)])
    credit = lvf.rfs(credit, core.std.MaskedMerge(credit, ref, creditless_mask, 0), [(edstart, edend)])
    credit = lvf.rfs(credit, core.std.MaskedMerge(credit, ref, rescale_mask[5602], 0), [(5561, 5752)])
    out = credit


    return depth(out, 10).std.Limiter(16<<2, [235<<2, 240<<2])
Esempio n. 15
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut
    src = depth(src, 16)
    out = src
    if out.num_frames < 34046:
        while out.num_frames != 34046:
            out += out[-1]
    opstart, opend = 456, 2613
    edstart, edend = 31767, 33924
    h = 720
    w = get_w(h)


    fixedges = awf.bbmod(out, 2, 2, 2, 2, 64<<8, 999)
    out = fixedges

    clean = core.knlm.KNLMeansCL(out, h=0.55, a=2, d=3, device_type='gpu', device_id=0, channels='UV')
    clean = core.knlm.KNLMeansCL(clean, h=0.55, a=2, d=3, device_type='gpu', device_id=0, channels='Y')
    diff_den = core.std.MakeDiff(out, clean)
    out = depth(clean, 32)


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

    descale = core.descale.Debilinear(luma, w, h)
    upscale = vdf.nnedi3_upscale(descale, correct_shift=False, pscrn=1).resize.Bicubic(src.width, src.height, src_left=.5, src_top=.5)
    rescale = core.std.MaskedMerge(luma, upscale, line_mask)

    merged = vdf.merge_chroma(rescale, out)
    out = depth(merged, 16)


    moozzi = warping(out, 0.4, 4)
    sharp = hvf.LSFmod(moozzi, strength=95, Smode=3, Lmode=1, edgemode=1, edgemaskHQ=True)
    out = sharp


    deband_mask = lvf.denoise.detail_mask(out, brz_a=2000, brz_b=1000)
    deband = dbs.f3kpf(out, 17, 30, 30)
    deband = core.std.MaskedMerge(deband, out, deband_mask)
    out = deband


    grain_org = core.std.MergeDiff(out, diff_den)
    out = grain_org


    ref = fixedges
    credit_mask = vdf.diff_rescale_mask(ref, mthr=40, sw=5, sh=5)
    credit_mask = vdf.region_mask(credit_mask, 10, 10, 10, 10).std.Inflate().std.Inflate()
    antialias = lvf.sraa(ref, 2, 13, downscaler=core.resize.Bicubic)
    credit = lvf.rfs(out, core.std.MaskedMerge(out, antialias, credit_mask),
                     [(2660, 2733), (33925, src.num_frames-1)])
    out = credit


    src_c, ncop, nced = [clip.std.Median() for clip in [src, JPBD_NCOP.src_cut, JPBD_NCED.src_cut]]
    opening_mask = vdf.dcm(out, src_c[opstart:opend+1], ncop[:opend-opstart+1], opstart, opend, 3, 3)
    ending_mask = vdf.dcm(out, src_c[edstart:edend+1], nced[:edend-edstart+1], edstart, edend, 3, 3)
    credit_mask = core.std.Expr([opening_mask, ending_mask], 'x y +').std.Convolution([1]*9)

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


    return depth(out, 10)
Esempio n. 16
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut
    src = depth(src, 16)
    out = src

    h = 720
    w = get_w(h)

    fixedges = awf.bbmod(out, 2, 2, 2, 2, 64 << 8, 999)
    out = fixedges

    decomb = hvf.Vinverse(out)
    decomb = lvf.rfs(out, decomb, [(828, 836)])
    out = decomb

    clean = core.knlm.KNLMeansCL(out,
                                 h=0.55,
                                 a=2,
                                 d=3,
                                 device_type='gpu',
                                 device_id=0,
                                 channels='UV')
    clean = core.knlm.KNLMeansCL(clean,
                                 h=0.55,
                                 a=2,
                                 d=3,
                                 device_type='gpu',
                                 device_id=0,
                                 channels='Y')
    diff_den = core.std.MakeDiff(out, clean)
    out = depth(clean, 32)

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

    descale = core.descale.Debilinear(luma, w, h)
    upscale = vdf.nnedi3_upscale(descale, correct_shift=False,
                                 pscrn=1).resize.Bicubic(src.width,
                                                         src.height,
                                                         src_left=.5,
                                                         src_top=.5)
    rescale = core.std.MaskedMerge(luma, upscale, line_mask)

    merged = vdf.merge_chroma(rescale, out)
    out = depth(merged, 16)

    moozzi = warping(out, 0.4, 4)
    sharp = hvf.LSFmod(moozzi,
                       strength=95,
                       Smode=3,
                       Lmode=1,
                       edgemode=1,
                       edgemaskHQ=True)
    out = sharp

    deband_mask = lvf.denoise.detail_mask(out, brz_a=2000, brz_b=1000)
    deband = dbs.f3kpf(out, 17, 30, 30)
    deband = core.std.MaskedMerge(deband, out, deband_mask)
    out = deband

    grain_org = core.std.MergeDiff(out, diff_den)
    out = grain_org

    ref = decomb
    credit_mask = vdf.diff_rescale_mask(ref, mthr=40, sw=5, sh=5)
    credit_mask = vdf.region_mask(credit_mask, 10, 10, 10,
                                  10).std.Inflate().std.Inflate()
    credit = lvf.rfs(out, core.std.MaskedMerge(out, ref, credit_mask),
                     [(0, 725), (31741, 33864)])
    antialias = lvf.sraa(ref, 2, 13, downscaler=core.resize.Bicubic)
    credit = lvf.rfs(credit, core.std.MaskedMerge(out, antialias, credit_mask),
                     [(833, 933), (1060, 1229), (1716, 1887), (2665, 2830),
                      (4813, 4947), (8179, 8272), (13757, 13810),
                      (20630, 20737), (20866, 20951), (21338, 21409),
                      (21480, 21520), (24719, 24786), (24829, 24869),
                      (28206, 28298), (28299, 28426), (33927, 34046)])
    out = credit

    return depth(out, 10)
Esempio n. 17
0
def do_filter():
    """Vapoursynth filtering"""
    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 _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 _line_mask(clip: vs.VideoNode, thr: int) -> vs.VideoNode:
        mask = core.std.Prewitt(clip)
        mask = core.std.Expr(mask, 'x 2 *').std.Median()
        mask = core.std.Expr(mask, f'x {thr} < x x 3 * ?')
        return mask.std.Inflate().std.Deflate()

    def _ssharp(clip: vs.VideoNode, strength: float, width: int, height: int,
                factor: float = 2, b: float = -1, c: float = 6) -> vs.VideoNode:
        source = clip
        sharp = core.resize.Bicubic(clip, clip.width*factor, clip.height*factor, \
            filter_param_a=b, filter_param_b=c).resize.Lanczos(width, height)

        source = core.resize.Spline64(source, sharp.width, sharp.height)

        sharp = core.rgvs.Repair(sharp, source, 13)
        sharp = mvf.LimitFilter(source, sharp, thrc=0.5, elast=6, brighten_thr=0.5, planes=0)


        final = core.std.Expr([sharp, source], f'x {strength} * y 1 {strength} - * +')
        return final

    def _w2x(clip: vs.VideoNode) -> vs.VideoNode:
        waifu2x = core.w2xc.Waifu2x(mvf.ToRGB(clip, depth=32), noise=2, scale=2) \
            .resize.Bicubic(clip.width, clip.height)
        return mvf.ToYUV(waifu2x, css='420', depth=16)

    src = JPBD.src_cut
    opstart, opend = 0, src.num_frames - 1

    src = depth(src, 16)
    src = core.std.FreezeFrames(src, opstart+2132, opend, opstart+2132)



    denoise = mdf.hybriddenoise_mod(src, 0.55, 2.25)
    diff = core.std.MakeDiff(src, denoise, [0, 1, 2])



    luma = get_y(denoise)

    upscale = _nnedi3(luma, 1.5, dict(nsize=0, nns=3, qual=1, pscrn=1))
    sraa = _sraa(upscale, dict(nsize=0, nns=3, qual=1, pscrn=1),
                 dict(alpha=0.2, beta=0.5, gamma=80, nrad=3, mdis=18))
    sraa = core.rgvs.Repair(sraa, upscale, 3)
    rescale = _ssharp(sraa, 0.55, src.width, src.height, 2)

    artefacts_mask = core.std.BlankClip(rescale, color=(256 << 8) - 1)
    artefacts_mask = vdf.region_mask(artefacts_mask, 2, 2, 2, 2).std.Inflate()
    rescale = core.std.MaskedMerge(luma, rescale, artefacts_mask)

    lineart_mask = _line_mask(luma, 8000)
    antialias = core.std.MaskedMerge(luma, rescale, lineart_mask)
    antialias_merged = vdf.merge_chroma(antialias, denoise)




    deband_mask = lvf.denoise.detail_mask(antialias_merged, brz_a=3000, brz_b=1500)
    deband_a = dbs.f3kpf(antialias_merged, 18, 36, 36)
    deband_b = dbs.f3kpf(antialias_merged, 18, 42, 42)
    deband_c = placebo.Deband(antialias_merged, radius=16, threshold=4, iterations=1, grain=0)
    deband_d = placebo.Deband(deband_b, radius=20, threshold=5, iterations=1, grain=0)
    deband = lvf.rfs(deband_a, deband_b, [(opstart, opstart+146)])
    deband = lvf.rfs(deband, deband_c, [(opstart+1225, opstart+1238)])
    deband = lvf.rfs(deband, deband_d, [(opstart+970, opstart+984)])
    deband = core.std.MaskedMerge(deband, antialias_merged, deband_mask)




    grain_original = core.std.MergeDiff(deband, diff, [0, 1, 2])
    grain_new = core.neo_f3kdb.Deband(deband, preset='depth', grainy=32, grainc=32)
    adapt_mask = core.adg.Mask(get_y(deband).std.PlaneStats(), 28)
    grain = core.std.MaskedMerge(grain_new, grain_original, adapt_mask)
    grain = lvf.rfs(grain, grain_new, [(opstart+147, opstart+496), (opstart+575, opstart+644),
                                       (opstart+702, opstart+969), (opstart+1076, opstart+1117),
                                       (opstart+1428, opstart+1461), (opstart+1859, opstart+2035)])

    w2x = _w2x(denoise).grain.Add(1, 0.5, constant=True)
    final = lvf.rfs(grain, w2x, [(opstart+1211, opstart+1224)])

    return depth(final, 10)
Esempio n. 18
0
def do_filter():
    """Vapoursynth filtering"""
    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 _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 _line_mask(clip: vs.VideoNode, thr: int) -> vs.VideoNode:
        mask = core.std.Prewitt(clip)
        mask = core.std.Expr(mask, 'x 2 *').std.Median()
        mask = core.std.Expr(mask, f'x {thr} < x x 3 * ?')
        return mask.std.Inflate().std.Deflate()

    def _ssharp(clip: vs.VideoNode,
                strength: float,
                width: int,
                height: int,
                factor: float = 2,
                b: float = -1,
                c: float = 6) -> vs.VideoNode:
        source = clip
        sharp = core.resize.Bicubic(clip, clip.width*factor, clip.height*factor, \
            filter_param_a=b, filter_param_b=c).resize.Lanczos(width, height)

        source = core.resize.Spline64(source, sharp.width, sharp.height)

        sharp = core.rgvs.Repair(sharp, source, 13)
        sharp = mvf.LimitFilter(source,
                                sharp,
                                thrc=0.5,
                                elast=6,
                                brighten_thr=0.5,
                                planes=0)

        final = core.std.Expr([sharp, source],
                              f'x {strength} * y 1 {strength} - * +')
        return final

    # pylint: disable=unused-argument
    def _diff(n: int, f: vs.VideoFrame, new: vs.VideoNode,
              adapt: vs.VideoNode) -> vs.VideoNode:
        psa = f.props['PlaneStatsAverage']
        if psa > 0.5:
            clip = new
        elif psa < 0.4:
            clip = adapt
        else:
            weight = (psa - 0.4) * 10
            clip = core.std.Merge(adapt, new, weight)
        return clip

    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

    src = JPBD.src_cut
    opstart = OPSTART

    src = src[opstart + 4:opstart + 2160]
    white = core.std.BlankClip(src, color=[235, 128, 128])
    src = src[opstart:opstart + 2082] + white[:4] + src[opstart + 2082:]

    src = depth(src, 16)

    denoise_a = mdf.hybriddenoise_mod(src, 0.55, 2.25)
    denoise_b = mdf.hybriddenoise_mod(src, 0.55, 10)
    denoise = lvf.rfs(denoise_a, denoise_b, [(opstart, opstart + 44)])
    diff = core.std.MakeDiff(src, denoise, [0, 1, 2])

    luma = get_y(denoise)

    upscale = _nnedi3(luma, 1.5, dict(nsize=0, nns=3, qual=1, pscrn=1))
    sraa = _sraa(upscale, dict(nsize=0, nns=3, qual=1, pscrn=1),
                 dict(alpha=0.2, beta=0.5, gamma=80, nrad=3, mdis=18))
    sraa = core.rgvs.Repair(sraa, upscale, 3)
    rescale = _ssharp(sraa, 0.55, src.width, src.height, 2)

    artefacts_mask = core.std.BlankClip(rescale, color=(256 << 8) - 1)
    artefacts_mask = vdf.region_mask(artefacts_mask, 2, 2, 2, 2).std.Inflate()
    rescale = core.std.MaskedMerge(luma, rescale, artefacts_mask)

    lineart_mask = _line_mask(luma, 8000)
    antialias = core.std.MaskedMerge(luma, rescale, lineart_mask)
    antialias_merged = vdf.merge_chroma(antialias, denoise)

    deband_mask = lvf.denoise.detail_mask(antialias_merged,
                                          brz_a=3000,
                                          brz_b=1500)
    dark_mask = core.std.Expr(
        [deband_mask, _ret_mask(antialias_merged, 12500)], 'x y +')
    deband_a = dbs.f3kpf(antialias_merged, 18, 36, 36)
    deband_b = dbs.f3kpf(antialias_merged, 18, 42, 42)
    deband_c = placebo.Deband(antialias_merged,
                              radius=12,
                              threshold=20,
                              iterations=3,
                              grain=0)
    deband_d = placebo.Deband(antialias_merged,
                              radius=10,
                              threshold=8,
                              iterations=2,
                              grain=0)
    deband = lvf.rfs(deband_a, deband_b, [(opstart + 483, opstart + 554)])
    deband = lvf.rfs(deband, deband_c, [(opstart, opstart + 44)])
    deband = lvf.rfs(
        deband, core.std.MaskedMerge(deband_d, antialias_merged, dark_mask),
        [(opstart + 1070, opstart + 1103)])
    deband = lvf.rfs(deband, deband_d, [(opstart + 1104, opstart + 1124)])
    deband = core.std.MaskedMerge(deband, antialias_merged, deband_mask)

    grain_original = core.std.MergeDiff(deband, diff, [0, 1, 2])
    grain_original_nochroma = core.std.MergeDiff(deband, diff, [0])
    grain_new = core.neo_f3kdb.Deband(deband,
                                      preset='depth',
                                      grainy=32,
                                      grainc=32)
    grain_new_nochroma = core.neo_f3kdb.Deband(deband,
                                               preset='depth',
                                               grainy=32,
                                               grainc=0)
    avg = core.std.PlaneStats(deband)
    adapt_mask = core.adg.Mask(get_y(avg), 28)
    grain_adapt = core.std.MaskedMerge(grain_new, grain_original, adapt_mask)

    grain_a = hvf.GrainFactory3(deband, 5, 3.85, 3.5, 50, 60, 60, 1.65, 1.60,
                                1.25)
    grain_b = mdf.adptvgrnMod_mod(deband,
                                  2,
                                  size=1.5,
                                  sharp=60,
                                  static=False,
                                  luma_scaling=14)

    grain = core.std.FrameEval(
        deband, partial(_diff, new=grain_new, adapt=grain_adapt), avg)

    grain = lvf.rfs(grain, grain_new_nochroma,
                    [(opstart + 117, opstart + 873),
                     (opstart + 921, opstart + 993)])
    grain = lvf.rfs(grain, grain_original_nochroma,
                    [(opstart + 874, opstart + 920),
                     (opstart + 994, opstart + 1069),
                     (opstart + 1125, opstart + 1202)])
    grain = lvf.rfs(grain, grain_a, [(opstart, opstart + 44)])
    grain = lvf.rfs(grain, grain_b, [(opstart + 1070, opstart + 1124)])

    return depth(grain, 10)
Esempio n. 19
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut

    # Variables
    opstart, opend = 2038, 4196
    eptitle_s, eptitle_e = 4197, 4316
    edstart, edend = 31530, 33687
    preview_s, preview_e = 33688, src.num_frames - 1
    h = 720
    w = get_w(h)

    edges = core.edgefixer.ContinuityFixer(src, *[[2, 1, 1]] * 4)
    out = depth(edges, 32)

    ref = hvf.SMDegrain(depth(get_y(out), 16), thSAD=450)
    denoise = hybrid_denoise(out, 0.35, 1.75, dict(a=2, d=1),
                             dict(ref=depth(ref, 32)))
    out = denoise

    y = get_y(out)
    lineart = vdf.edge_detect(y, 'FDOG', 0.055,
                              (1, 1)).std.Median().std.Convolution([*[1] * 9])

    descale = core.descale.Debilinear(y, w, h)

    upscale = vdf.fsrcnnx_upscale(
        descale,
        height=h * 2,
        shader_file=r'shaders\FSRCNNX_x2_56-16-4-1.glsl',
        upscaler_smooth=eedi3_upscale,
        profile='slow',
        strength=85)

    antialias = sraa_eedi3(upscale, 9)

    downscale = muvf.SSIM_downsample(antialias,
                                     src.width,
                                     src.height,
                                     filter_param_a=0,
                                     filter_param_b=0)
    downscale = core.std.MaskedMerge(y, downscale, lineart)

    merged = vdf.merge_chroma(downscale, out)
    merged = lvf.rfs(merged, out, [(opstart, opend), (eptitle_s, eptitle_e),
                                   (preview_s, preview_e)])
    out = depth(merged, 16)

    detail_dark_mask = detail_dark_mask_func(get_y(out),
                                             brz_a=8000,
                                             brz_b=6000)
    detail_light_mask = lvf.denoise.detail_mask(out, brz_a=2500, brz_b=1200)
    detail_mask = core.std.Expr([detail_dark_mask, detail_light_mask],
                                'x y +').std.Median()
    detail_mask_grow = iterate(detail_mask, core.std.Maximum, 2)
    detail_mask_grow = iterate(detail_mask_grow, core.std.Inflate,
                               2).std.Convolution([*[1] * 9])

    detail_mask = core.std.Expr([get_y(out), detail_mask_grow, detail_mask],
                                f'x {32<<8} < y z ?')

    deband = dumb3kdbv2(out, 22, 24)
    deband = core.std.MaskedMerge(deband, out, detail_mask)

    deband_b = dumb3kdbv2(out, 24, 64)
    deband_b = core.std.MaskedMerge(deband_b, out, detail_light_mask)
    deband = lvf.rfs(deband, deband_b, [(opstart + 414, opstart + 496)])

    out = deband

    ref = get_y(out).std.PlaneStats()
    adgmask_a = core.adg.Mask(ref, 30)
    adgmask_b = core.adg.Mask(ref, 12)

    stgrain = sizedgrn(out, 0.1, 0.05, 1.05, sharp=80)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_b)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_a.std.Invert())

    dygrain = sizedgrn(out, 0.2, 0.05, 1.15, sharp=80, static=False)
    dygrain = core.std.MaskedMerge(out, dygrain, adgmask_a)
    grain = core.std.MergeDiff(dygrain, out.std.MakeDiff(stgrain))
    out = grain

    ref = depth(src, 16)
    rescale_mask = vdf.drm(ref, 720, 'bilinear', mthr=30, sw=0, sh=0)
    rescale_mask = vdf.region_mask(rescale_mask, *[10] * 4)
    rescale_mask = hvf.mt_expand_multi(rescale_mask,
                                       mode='ellipse',
                                       sw=4,
                                       sh=4)
    rescale_mask = rescale_mask.std.Binarize(scale_value(
        100, 8, 16)).std.Inflate().std.Convolution([*[1] * 9])

    credit = out
    credit = lvf.rfs(credit, core.std.MaskedMerge(credit, ref, rescale_mask,
                                                  0), [(8993, 9066),
                                                       (20104, 20223),
                                                       (edstart, edend)])

    out = credit

    return depth(out, 10).std.Limiter(16 << 2, [235 << 2, 240 << 2])
Esempio n. 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=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
Esempio n. 21
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut

    # Variables
    opstart, opend = 6161, 8319
    eptitle_s, eptitle_e = 8320, 8439
    edstart, edend = 31170, 33326
    preview_s, preview_e = 33687, src.num_frames - 1
    h = 720
    w = get_w(h)

    edges = core.edgefixer.ContinuityFixer(src, *[[2, 1, 1]] * 4)
    out = depth(edges, 32)

    ref = hvf.SMDegrain(depth(get_y(out), 16), thSAD=450)
    denoise = hybrid_denoise(out, 0.35, 1.75, dict(a=2, d=1),
                             dict(ref=depth(ref, 32)))
    out = denoise

    y = get_y(out)
    lineart = vdf.edge_detect(y, 'FDOG', 0.055,
                              (1, 1)).std.Median().std.Convolution([*[1] * 9])

    descale = core.descale.Debilinear(y, w, h)

    upscale = vdf.fsrcnnx_upscale(
        descale,
        height=h * 2,
        shader_file=r'shaders\FSRCNNX_x2_56-16-4-1.glsl',
        upscaler_smooth=eedi3_upscale,
        profile='slow',
        strength=85)

    antialias = sraa_eedi3(upscale, 9)

    downscale = muvf.SSIM_downsample(antialias,
                                     src.width,
                                     src.height,
                                     filter_param_a=0,
                                     filter_param_b=0)
    downscale = core.std.MaskedMerge(y, downscale, lineart)

    merged = vdf.merge_chroma(downscale, out)
    merged = lvf.rfs(merged, out, [(opstart, opend), (eptitle_s, eptitle_e),
                                   (preview_s, preview_e)])
    out = depth(merged, 16)

    detail_dark_mask = detail_dark_mask_func(get_y(out),
                                             brz_a=8000,
                                             brz_b=6000)
    detail_light_mask = lvf.denoise.detail_mask(out, brz_a=2500, brz_b=1200)
    detail_mask = core.std.Expr([detail_dark_mask, detail_light_mask],
                                'x y +').std.Median()
    detail_mask_grow = iterate(detail_mask, core.std.Maximum, 2)
    detail_mask_grow = iterate(detail_mask_grow, core.std.Inflate,
                               2).std.Convolution([*[1] * 9])

    detail_mask = core.std.Expr([get_y(out), detail_mask_grow, detail_mask],
                                f'x {32<<8} < y z ?')

    deband = dumb3kdbv2(out, 22, 24)
    deband = core.std.MaskedMerge(deband, out, detail_mask)
    out = deband

    ref = get_y(out).std.PlaneStats()
    adgmask_a = core.adg.Mask(ref, 30)
    adgmask_b = core.adg.Mask(ref, 12)

    stgrain = sizedgrn(out, 0.1, 0.05, 1.05, sharp=80)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_b)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_a.std.Invert())

    dygrain = sizedgrn(out, 0.2, 0.05, 1.15, sharp=80, static=False)
    dygrain = core.std.MaskedMerge(out, dygrain, adgmask_a)
    grain = core.std.MergeDiff(dygrain, out.std.MakeDiff(stgrain))
    out = grain

    ref = depth(src, 16)
    rescale_mask = vdf.drm(ref, 720, 'bilinear', mthr=30, sw=0, sh=0)
    rescale_mask = vdf.region_mask(rescale_mask, *[10] * 4)
    rescale_mask = hvf.mt_expand_multi(rescale_mask,
                                       mode='ellipse',
                                       sw=4,
                                       sh=4)
    rescale_mask = rescale_mask.std.Binarize(scale_value(
        100, 8, 16)).std.Inflate().std.Convolution([*[1] * 9])

    dehalo_ref = gf.MaskedDHA(ref,
                              rx=1.65,
                              ry=1.65,
                              darkstr=0.15,
                              brightstr=1.0,
                              maskpull=48,
                              maskpush=140)
    dehalo_mask_a = vdf.region_mask(rescale_mask, top=650)
    dehalo_mask_b = vdf.region_mask(rescale_mask, right=400)

    credit = out
    dehalo_range_a = [(25, 205), (518, 612), (2090, 2172), (14449, 14537)]
    dehalo_range_b = [(3893, 3981)]
    credit = lvf.rfs(credit, core.std.MaskedMerge(credit, ref, rescale_mask,
                                                  0),
                     [(2197, 2252), (4072, 4129), (4409, 4527), (5390, 5484),
                      (8473, 8603), (8610, 8669), (9826, 9909), (10666, 10713),
                      (12088, 12507), (14943, 15052), (17988, 18136),
                      (18897, 18976),
                      (edstart, edend)] + dehalo_range_a + dehalo_range_b)
    credit = lvf.rfs(
        credit, core.std.MaskedMerge(credit, dehalo_ref, dehalo_mask_a, 0),
        dehalo_range_a)
    credit = lvf.rfs(
        credit, core.std.MaskedMerge(credit, dehalo_ref, dehalo_mask_b, 0),
        dehalo_range_b)
    out = credit

    return depth(out, 10).std.Limiter(16 << 2, [235 << 2, 240 << 2])
Esempio n. 22
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 _line_mask(clip: vs.VideoNode, thr: int) -> vs.VideoNode:
        mask = core.std.Prewitt(clip)
        mask = core.std.Expr(mask, 'x 2 *').std.Median()
        mask = core.std.Expr(mask, f'x {thr} < x x 3 * ?')
        return mask.std.Inflate().std.Deflate()

    def _ssharp(clip: vs.VideoNode,
                strength: float,
                width: int,
                height: int,
                factor: float = 2,
                b: float = -1,
                c: float = 6) -> vs.VideoNode:
        source = clip
        sharp = core.resize.Bicubic(clip, clip.width*factor, clip.height*factor, \
            filter_param_a=b, filter_param_b=c).resize.Lanczos(width, height)

        source = core.resize.Spline64(source, sharp.width, sharp.height)

        sharp = core.rgvs.Repair(sharp, source, 13)
        sharp = mvf.LimitFilter(source,
                                sharp,
                                thrc=0.5,
                                elast=6,
                                brighten_thr=0.5,
                                planes=0)

        final = core.std.Expr([sharp, source],
                              f'x {strength} * y 1 {strength} - * +')
        return final

    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))

    def _perform_masks_credit(path: Path) -> List[MaskCredit]:
        return [
            MaskCredit(lvf.src(str(mask)), int(str(mask.stem).split('_')[2]),
                       int(str(mask.stem).split('_')[3]))
            for mask in path.glob('*')
        ]

    # pylint: disable=unused-argument
    def _diff(n: int, f: vs.VideoFrame, new: vs.VideoNode,
              adapt: vs.VideoNode) -> vs.VideoNode:
        psa = f.props['PlaneStatsAverage']
        if psa > 0.5:
            clip = new
        elif psa < 0.4:
            clip = adapt
        else:
            weight = (psa - 0.4) * 10
            clip = core.std.Merge(adapt, new, weight)
        return clip

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

    denoise = mdf.hybriddenoise_mod(src, 0.55, 2.25)
    diff = core.std.MakeDiff(src, denoise, [0, 1, 2])

    luma = get_y(denoise)

    upscale = _nnedi3(luma, 1.5, dict(nsize=0, nns=3, qual=1, pscrn=1))
    sraa = _sraa(upscale, dict(nsize=0, nns=3, qual=1, pscrn=1),
                 dict(alpha=0.2, beta=0.5, gamma=80, nrad=3, mdis=18))
    sraa = core.rgvs.Repair(sraa, upscale, 3)
    rescale = _ssharp(sraa, 0.55, src.width, src.height, 2)

    artefacts_mask = core.std.BlankClip(rescale, color=(256 << 8) - 1)
    artefacts_mask = vdf.region_mask(artefacts_mask, 2, 2, 2, 2).std.Inflate()
    rescale = core.std.MaskedMerge(luma, rescale, artefacts_mask)

    lineart_mask = _line_mask(luma, 8000)
    antialias = core.std.MaskedMerge(luma, rescale, lineart_mask)

    antialias_merged = vdf.merge_chroma(antialias, denoise)
    antialias_merged = lvf.rfs(antialias_merged, denoise, [(1298, 1415)])

    credit = antialias_merged

    masks_credit_ = _perform_masks_credit(Path('masks_' + JPBD.name[-2:] +
                                               '/'))

    for mask in masks_credit_:
        credit = lvf.rfs(
            credit,
            core.std.MaskedMerge(credit, denoise,
                                 to_gray(mask.mask, src).std.Deflate()),
            [(mask.start_frame, mask.end_frame)])

    deband_mask = lvf.denoise.detail_mask(credit, brz_a=3000, brz_b=1500)
    deband = dbs.f3kpf(credit, 18, 36, 36)
    deband = core.std.MaskedMerge(deband, credit, deband_mask)

    grain_original = core.std.MergeDiff(deband, diff, [0, 1, 2])
    grain_new = core.neo_f3kdb.Deband(deband,
                                      preset='depth',
                                      grainy=32,
                                      grainc=32)
    avg = core.std.PlaneStats(deband)
    adapt_mask = core.adg.Mask(get_y(avg), 28)
    grain_adapt = core.std.MaskedMerge(grain_new, grain_original, adapt_mask)

    grain = core.std.FrameEval(
        deband, partial(_diff, new=grain_new, adapt=grain_adapt), avg)

    grain = lvf.rfs(grain, grain_original, [(2282, 2447), (5454, 5529),
                                            (23822, 23883)])

    return depth(grain, 10)
Esempio n. 23
0
def do_filter():
    """Vapoursynth filtering"""
    src = JPBD.src_cut
    src += src[-1]

    # Variables
    opstart, opend = 8296, 10452
    eptitle_s, eptitle_e = 10453, 10572
    edstart, edend = 31528, 33685
    preview_s, preview_e = 33686, src.num_frames - 1
    h = 720
    w = get_w(h)

    edges = core.edgefixer.ContinuityFixer(src, *[[2, 1, 1]] * 4)
    out = depth(edges, 32)

    ref = hvf.SMDegrain(depth(get_y(out), 16), thSAD=450)
    denoise = hybrid_denoise(out, 0.35, 1.75, dict(a=2, d=1),
                             dict(ref=depth(ref, 32)))
    out = denoise

    y = get_y(out)
    lineart = vdf.edge_detect(y, 'FDOG', 0.055,
                              (1, 1)).std.Median().std.Convolution([*[1] * 9])

    descale = core.descale.Debilinear(y, w, h)

    upscale = vdf.fsrcnnx_upscale(
        descale,
        height=h * 2,
        shader_file=r'shaders\FSRCNNX_x2_56-16-4-1.glsl',
        upscaler_smooth=eedi3_upscale,
        profile='slow',
        strength=85)

    antialias = sraa_eedi3(upscale, 9)

    downscale = muvf.SSIM_downsample(antialias,
                                     src.width,
                                     src.height,
                                     filter_param_a=0,
                                     filter_param_b=0)
    downscale = core.std.MaskedMerge(y, downscale, lineart)

    merged = vdf.merge_chroma(downscale, out)
    merged = lvf.rfs(merged, out, [(opstart, opend), (eptitle_s, eptitle_e),
                                   (preview_s, preview_e)])
    out = depth(merged, 16)

    detail_dark_mask = detail_dark_mask_func(get_y(out),
                                             brz_a=8000,
                                             brz_b=6000)
    detail_light_mask = lvf.denoise.detail_mask(out, brz_a=2500, brz_b=1200)
    detail_mask = core.std.Expr([detail_dark_mask, detail_light_mask],
                                'x y +').std.Median()
    detail_mask_grow = iterate(detail_mask, core.std.Maximum, 2)
    detail_mask_grow = iterate(detail_mask_grow, core.std.Inflate,
                               2).std.Convolution([*[1] * 9])

    detail_mask = core.std.Expr([get_y(out), detail_mask_grow, detail_mask],
                                f'x {32<<8} < y z ?')

    deband = dumb3kdbv2(out, 22, 24)
    deband = core.std.MaskedMerge(deband, out, detail_mask)

    deband_b = dumb3kdbv2(out, 24, 64)
    deband_b = core.std.MaskedMerge(deband_b, out, detail_light_mask)
    deband = lvf.rfs(deband, deband_b, [(opstart + 414, opstart + 496)])

    out = deband

    ref = get_y(out).std.PlaneStats()
    adgmask_a = core.adg.Mask(ref, 30)
    adgmask_b = core.adg.Mask(ref, 12)

    stgrain = sizedgrn(out, 0.1, 0.05, 1.05, sharp=80)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_b)
    stgrain = core.std.MaskedMerge(out, stgrain, adgmask_a.std.Invert())

    dygrain = sizedgrn(out, 0.2, 0.05, 1.15, sharp=80, static=False)
    dygrain = core.std.MaskedMerge(out, dygrain, adgmask_a)
    grain = core.std.MergeDiff(dygrain, out.std.MakeDiff(stgrain))
    out = grain

    ref = depth(src, 16)
    rescale_mask = vdf.drm(ref, 720, 'bilinear', mthr=30, sw=0, sh=0)
    rescale_mask = vdf.region_mask(rescale_mask, *[10] * 4)
    rescale_mask = hvf.mt_expand_multi(rescale_mask,
                                       mode='ellipse',
                                       sw=4,
                                       sh=4)
    rescale_mask = rescale_mask.std.Binarize(scale_value(
        100, 8, 16)).std.Inflate().std.Convolution([*[1] * 9])

    dehalo_ref = gf.MaskedDHA(ref,
                              rx=1.65,
                              ry=1.65,
                              darkstr=0.15,
                              brightstr=1.0,
                              maskpull=48,
                              maskpush=140)
    dehalo_mask_a = vdf.region_mask(rescale_mask, top=650, right=200)

    credit = out
    dehalo_range_a = [(18, 130), (1291, 1355), (14691, 14846), (15126, 15189)]
    credit = lvf.rfs(credit, core.std.MaskedMerge(credit, ref, rescale_mask,
                                                  0),
                     [(131, 220), (400, 505), (817, 899), (1766, 1809),
                      (1925, 2041), (4033, 4116), (4342, 4453), (4964, 5031),
                      (6422, 6515), (7848, 7978),
                      (10606, 10728), (11619, 11701), (15339, 15457),
                      (edstart, edend)] + dehalo_range_a)
    credit = lvf.rfs(
        credit, core.std.MaskedMerge(credit, dehalo_ref, dehalo_mask_a, 0),
        dehalo_range_a)

    out = credit

    return depth(out, 10).std.Limiter(16 << 2, [235 << 2, 240 << 2])
Esempio n. 24
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 _line_mask(clip: vs.VideoNode, thr: int) -> vs.VideoNode:
        mask = core.std.Prewitt(clip)
        mask = core.std.Expr(mask, 'x 2 *').std.Median()
        mask = core.std.Expr(mask, f'x {thr} < x x 3 * ?')
        return mask.std.Inflate().std.Deflate()

    def _ssharp(clip: vs.VideoNode,
                strength: float,
                width: int,
                height: int,
                factor: float = 2,
                b: float = -1,
                c: float = 6) -> vs.VideoNode:
        source = clip
        sharp = core.resize.Bicubic(clip, clip.width*factor, clip.height*factor, \
            filter_param_a=b, filter_param_b=c).resize.Lanczos(width, height)

        source = core.resize.Spline64(source, sharp.width, sharp.height)

        sharp = core.rgvs.Repair(sharp, source, 13)
        sharp = mvf.LimitFilter(source,
                                sharp,
                                thrc=0.5,
                                elast=6,
                                brighten_thr=0.5,
                                planes=0)

        final = core.std.Expr([sharp, source],
                              f'x {strength} * y 1 {strength} - * +')
        return final

    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))

    def _perform_masks_credit(path: Path) -> List[MaskCredit]:
        return [
            MaskCredit(lvf.src(str(mask)), int(str(mask.stem).split('_')[2]),
                       int(str(mask.stem).split('_')[3]))
            for mask in path.glob('*')
        ]

    def _w2x(clip: vs.VideoNode) -> vs.VideoNode:
        waifu2x = core.w2xc.Waifu2x(mvf.ToRGB(clip, depth=32), noise=2, scale=2) \
            .resize.Bicubic(clip.width, clip.height)
        return mvf.ToYUV(waifu2x, css='420', depth=16)

    def _perform_filtering_ending(clip: vs.VideoNode,
                                  adapt_mask: vs.VideoNode) -> vs.VideoNode:
        luma = get_y(clip)
        denoise_a = mvf.BM3D(luma, 2.25, 1)
        denoise_b = mvf.BM3D(luma, 1.25, 1)
        denoise = core.std.MaskedMerge(denoise_a, denoise_b, adapt_mask)
        grain = core.grain.Add(denoise, 0.3, constant=True)
        return core.std.MaskedMerge(denoise, grain, adapt_mask)

    # pylint: disable=unused-argument
    def _diff(n: int, f: vs.VideoFrame, new: vs.VideoNode,
              adapt: vs.VideoNode) -> vs.VideoNode:
        psa = f.props['PlaneStatsAverage']
        if psa > 0.5:
            clip = new
        elif psa < 0.4:
            clip = adapt
        else:
            weight = (psa - 0.4) * 10
            clip = core.std.Merge(adapt, new, weight)
        return clip

    def un_ugly_background(clip: vs.VideoNode) -> vs.VideoNode:
        descale = core.descale.Debicubic(depth(clip, 32), 1505, 847, 1 / 3,
                                         1 / 3)

        def _fsrcnnx(clip: vs.VideoNode) -> vs.VideoNode:
            clip = core.placebo.Shader(clip.resize.Point(format=vs.YUV444P16),
                                       'Shaders/FSRCNNX_x2_56-16-4-1.glsl',
                                       clip.width * 2,
                                       clip.height * 2,
                                       filter='box')
            return get_y(clip)

        double = _fsrcnnx(depth(descale, 16))
        rescale = core.fmtc.resample(double,
                                     clip.width,
                                     clip.height,
                                     kernel='gauss',
                                     invks=True,
                                     invkstaps=2,
                                     taps=1,
                                     a1=32)
        blur = core.std.Convolution(rescale, [1, 1, 1, 1, 1, 1, 1, 1, 1])
        sharp = core.std.Expr([rescale, blur], 'x y - {} * x +'.format(1))
        mask = core.std.Sobel(luma).std.Binarize(40000).std.Maximum()
        return core.std.MaskedMerge(sharp, rescale, mask)

    opstart, opend = 0, 2157
    edstart, edend = 31767, 33924
    src = JPBD.src_cut
    src += src[-1]
    src = depth(src, 16)
    src = core.std.FreezeFrames(src, opstart + 2132, opend, opstart + 2132)

    denoise = mdf.hybriddenoise_mod(src, 0.55, 2.25)
    diff = core.std.MakeDiff(src, denoise, [0, 1, 2])

    luma = get_y(denoise)

    upscale = _nnedi3(luma, 1.5, dict(nsize=0, nns=3, qual=1, pscrn=1))
    sraa = _sraa(upscale, dict(nsize=0, nns=3, qual=1, pscrn=1),
                 dict(alpha=0.2, beta=0.5, gamma=80, nrad=3, mdis=18))
    sraa = core.rgvs.Repair(sraa, upscale, 3)
    rescale = _ssharp(sraa, 0.55, src.width, src.height, 2)

    artefacts_mask = core.std.BlankClip(rescale, color=(256 << 8) - 1)
    artefacts_mask = vdf.region_mask(artefacts_mask, 2, 2, 2, 2).std.Inflate()
    rescale = core.std.MaskedMerge(luma, rescale, artefacts_mask)

    lineart_mask = _line_mask(luma, 8000)
    antialias = core.std.MaskedMerge(luma, rescale, lineart_mask)
    sharp_rescale = un_ugly_background(luma)  # :monka:
    antialias = lvf.rfs(antialias, sharp_rescale, [(26419, 26705)])

    antialias_merged = vdf.merge_chroma(antialias, denoise)

    src_c, src_ncop = [
        c.knlm.KNLMeansCL(a=6, h=20, d=0, device_type='gpu')
        for c in [src, JPBD_NCOP.src_cut[:opend - opstart + 1]]
    ]
    credit_mask = vdf.dcm(src, src_c[opstart:opend + 1], src_ncop, opstart,
                          opend, 2, 2).std.Deflate()
    credit = core.std.MaskedMerge(antialias_merged, denoise, credit_mask)

    masks_credit_ = _perform_masks_credit(Path('masks_' + JPBD.name[-2:] +
                                               '/'))

    for mask in masks_credit_:
        credit = lvf.rfs(
            credit,
            core.std.MaskedMerge(credit, denoise,
                                 to_gray(mask.mask, src).std.Deflate()),
            [(mask.start_frame, mask.end_frame)])

    deband_mask = lvf.denoise.detail_mask(credit, brz_a=3000, brz_b=1500)
    deband_a = dbs.f3kpf(credit, 18, 36, 36)
    deband_b = dbs.f3kpf(credit, 18, 42, 42)
    deband_c = placebo.Deband(credit,
                              radius=16,
                              threshold=4,
                              iterations=1,
                              grain=0)
    deband_d = placebo.Deband(deband_b,
                              radius=20,
                              threshold=5,
                              iterations=1,
                              grain=0)
    deband_e = dbs.f3kpf(credit, 16, 24, 24)
    deband = lvf.rfs(deband_a, deband_b, [(opstart, opstart + 146)])
    deband = lvf.rfs(deband, deband_c, [(opstart + 1225, opstart + 1238)])
    deband = lvf.rfs(deband, deband_d, [(opstart + 970, opstart + 984)])
    deband = lvf.rfs(deband, deband_e, [(15919, 16285)])
    deband = core.std.MaskedMerge(deband, credit, deband_mask)

    grain_original = core.std.MergeDiff(deband, diff, [0, 1, 2])
    grain_new = core.neo_f3kdb.Deband(deband,
                                      preset='depth',
                                      grainy=32,
                                      grainc=32)
    avg = core.std.PlaneStats(deband)
    adapt_mask = core.adg.Mask(get_y(avg), 28)
    grain_adapt = core.std.MaskedMerge(grain_new, grain_original, adapt_mask)

    grain = core.std.FrameEval(
        deband, partial(_diff, new=grain_new, adapt=grain_adapt), avg)

    grain = lvf.rfs(grain, grain_new, [(opstart + 147, opstart + 496),
                                       (opstart + 575, opstart + 644),
                                       (opstart + 702, opstart + 969),
                                       (opstart + 1076, opstart + 1117),
                                       (opstart + 1428, opstart + 1461),
                                       (opstart + 1859, opstart + 2035)])
    grain = lvf.rfs(grain, grain_original, [(5118, 5255), (11480, 11585)])

    w2x = _w2x(denoise).grain.Add(1, 0.5, constant=True)
    w2x = lvf.rfs(grain, w2x, [(opstart + 1211, opstart + 1224)])

    ending = _perform_filtering_ending(src, adapt_mask)
    ending = vdf.merge_chroma(ending, denoise)
    final = lvf.rfs(w2x, ending, [(edstart, edend)])

    return depth(final, 10)