def do_filter(): """Vapoursynth filtering""" def _eedi3_instance(clip, eeargs, nnargs) -> vs.VideoNode: return core.eedi3m.EEDI3(clip, 0, True, **eeargs, sclip=_nnedi3_sclip(clip, nnargs)) def _nnedi3_sclip(clip, nnargs) -> vs.VideoNode: return core.nnedi3.nnedi3(clip, 0, True, **nnargs) def _rescale(clip, width, height, eeargs, nnargs) -> vs.VideoNode: clip = _eedi3_instance(clip, eeargs, nnargs).std.Transpose() clip = _eedi3_instance(clip, eeargs, nnargs).std.Transpose() return core.resize.Bicubic(clip, width, height, src_left=.5, src_top=.5) src = SRC_CUT denoise = mdf.hybriddenoise_mod(src, 0.5, 1.5, depth=32) h = 806 w = get_w(h) b, c = 0.3782, 0.3109 eedi3_args = dict(alpha=.2, beta=.3, gamma=1000, mdis=20, vcheck=3) nnedi3_args = dict(nns=4, nsize=0, qual=2, pscrn=1) luma = get_y(denoise) descale = core.descale.Debicubic(luma, w, h, b, c) scaled = _rescale(descale, src.width, src.height, eedi3_args, nnedi3_args) credit_m = vrf.drm(denoise, h, b=b, c=c, sw=4, sh=4) scaled = lvf.rfs(scaled, core.std.MaskedMerge(scaled, luma, credit_m), [(47, 134)]) merged = core.std.ShufflePlanes([scaled, denoise], [0, 1, 2], vs.YUV) merged = depth(merged, 16) deband_mask = lvf.denoise.detail_mask(merged, brz_a=2500, brz_b=1000) deband_a = dbs.f3kpf(merged, 17, 48, 48) deband_b = core.placebo.Deband(merged, radius=19, threshold=5, iterations=1, grain=2, planes=1|2|4) deband = lvf.rfs(deband_a, deband_b, [(2684, 2743)]) deband = core.std.MaskedMerge(deband, merged, deband_mask) deband = core.placebo.Deband(deband, iterations=0, grain=5, planes=1) grain = mdf.adptvgrnMod_mod(deband, 0.3, size=1.25, sharp=60, static=False, luma_scaling=6) final = lvf.rfs(grain, depth(src, 16), [(2904, src.num_frames-1)]) final = depth(final, 10) return final
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
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)
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)
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)
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)
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)