def overlaymask(clip, ncop=None, nced=None, op=None, ed=None, w=None, h=None, thr=50, thr_ed=None, maximum=3, inflate=2, exmask=None, bits=None, mute_exmask=None, black=None, white=None): core = vs.core thr_ed = fallback(thr_ed, thr) bits = fallback(bits, clip.format.bits_per_sample if exmask is None else exmask.format.bits_per_sample) fmt_yuv = core.register_format(vs.YUV, vs.INTEGER if bits < 32 else vs.FLOAT, bits, 0, 0) fmt_gray = core.register_format(vs.GRAY, vs.INTEGER if bits < 32 else vs.FLOAT, bits, 0, 0) mask_out = core.std.BlankClip(clip, w, h, fmt_gray.id, color=0) if exmask is None else core.resize.Point(exmask, format=fmt_gray.id, range=1, range_in=1) exmask = False if exmask is None else True w, h = mask_out.width, mask_out.height if mute_exmask is not None: mask_out = fvf.rfs(mask_out, mask_out.std.BlankClip(color=0), mute_exmask) if op is not None: op = [int(x) for x in op.strip('[]').split()] op_creds = clip[op[0]:op[1]+1] mask = core.std.Expr([op_creds, ncop], 'x y - abs').std.Binarize(thr) mask = core.resize.Point(mask, format=fmt_yuv.id, range=1, range_in=1) mask = core.std.Expr(split(mask), 'x y max') mask = haf.mt_expand_multi(mask, mode='ellipse', sw=maximum, sh=maximum) mask = iterate(mask, core.std.Inflate, inflate) mask = core.resize.Spline36(mask, w, h, format=fmt_gray.id) if exmask: clips = [mask, mask_out[op[0]:op[1]+1]] mask = core.std.Expr(clips, 'x y max') mask_out = mask_out[:op[0]] + mask + mask_out[op[1]+1:] if ed is not None: ed = [int(x) for x in ed.strip('[]').split()] ed_creds = clip[ed[0]:ed[1]+1] mask = core.std.Expr([ed_creds, nced], 'x y - abs').std.Binarize(thr_ed) mask = core.resize.Point(mask, format=fmt_yuv.id, range=1, range_in=1) mask = core.std.Expr(split(mask), 'x y max') mask = haf.mt_expand_multi(mask, mode='ellipse', sw=maximum, sh=maximum) mask = iterate(mask, core.std.Inflate, inflate) mask = core.resize.Spline36(mask, w, h, format=fmt_gray.id) if exmask: clips = [mask, mask_out[ed[0]:ed[1]+1]] mask = core.std.Expr(clips, 'x y max') mask_out = mask_out[:ed[0]] + mask + mask_out[ed[1]+1:] if black is not None: mask_out = fvf.rfs(mask_out, mask_out.std.BlankClip(color=0), black) if white is not None: mask_out = fvf.rfs(mask_out, mask_out.std.BlankClip(color=1 if bits==32 else (1<<bits)-1), white) return mask_out
def hardsub_mask(base: vs.VideoNode, sub: str, fontdir: str) -> vs.VideoNode: _, masksub = core.sub.TextFile(base, sub, fontdir=fontdir, blend=False) masksub = masksub.std.Binarize(1) masksub = hvf.mt_expand_multi(masksub, 'ellipse', sw=6, sh=4) masksub = hvf.mt_inflate_multi(masksub, radius=4).std.Convolution([1] * 9) return masksub
def debandmask(clip, lo=6144, hi=12288, lothr=320, hithr=384, mrad=2): """A luma adraptive mask from https://pastebin.com/SHQZjVJ5 meant as a faster version of the retinex-type deband mask lo and hi are the cutoffs for luma lothr and hithr are the Binarize thresholds of the mask at lo/hi luma levels luma values falling between lo and hi are scaled linearly from lothr to hithr""" f = clip.format bits = f.bits_per_sample isINT = f.sample_type == vs.INTEGER peak = (1 << bits) - 1 if isINT else 1 clip = clip.std.ShufflePlanes(0, vs.GRAY) ma = haf.mt_expand_multi(clip, mode='ellipse', sw=mrad, sh=mrad) mi = haf.mt_inpand_multi(clip, mode='ellipse', sw=mrad, sh=mrad) rmask = core.std.Expr([ma, mi], 'x y -') mexpr = 'x {lo} < y {lothr} >= {peak} 0 ? x {hi} > y {hithr} >= {peak} 0 ? y x {lo} - {r} / {tr} * {lothr} + >= {peak} 0 ? ? ?'.format( lo=lo, hi=hi, lothr=lothr, hithr=hithr, peak=peak, r=hi - lo, tr=hithr - lothr) return core.std.Expr([clip, rmask], mexpr)
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])
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])
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])
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])
def _hell_mask(mask: vs.VideoNode, pand: int): mask = lvf.scale.iterate(mask, core.std.Median, 2) mask = hvf.mt_expand_multi(mask, 'ellipse', 0, sw=pand, sh=pand) mask = hvf.mt_inpand_multi(mask, 'ellipse', 0, sw=pand - int(pand/2), sh=pand - int(pand/2)) mask = core.std.Expr(mask, 'x 25000 < 0 x 6 * ?') return mask
def filtering() -> Union[vs.VideoNode, Tuple[vs.VideoNode, vs.VideoNode]]: """Vapoursynth filtering""" src_cru = WEB_CRU.clip_cut src_aod = WEB_AOD.clip_cut _, masksub = core.sub.TextFile(src_aod, SUB, fontdir='fonts', blend=False) masksub = core.std.Binarize(masksub, 1) masksub = hvf.mt_expand_multi(masksub, 'ellipse', sw=6, sh=4) masksub = hvf.mt_inflate_multi(masksub, radius=4).std.Convolution([1] * 9) dehardsub = core.std.MaskedMerge(src_aod, src_cru, masksub) dehardsub = depth(dehardsub, 16) out = dehardsub lineart = ScharrG41().get_mask(get_y(out), 4000, multi=1.2).rgvs.RemoveGrain(3).std.Maximum().std.Minimum() luma = get_y(out) ssing = vdf.scale.fsrcnnx_upscale( luma, height=1620, shader_file='shaders/FSRCNNX_x2_16-0-4-1.glsl', downscaler=lambda c, w, h: core.resize.Bicubic(c, w, h, filter_param_a=-0.5, filter_param_b=0.25), profile='fast', ) sraing = sraa_eedi3(ssing, 13, gamma=100, nrad=2, mdis=15) down = core.resize.Bicubic(sraing, out.width, out.height, filter_param_a=-0.5, filter_param_b=0.25) masked = core.std.MaskedMerge(luma, down, lineart) merged = vdf.misc.merge_chroma(masked, out) out = merged contra = hvf.LSFmod(out, strength=80, Smode=3, edgemode=0, source=dehardsub) out = contra # I gave up on this ending = lvf.rfs(out, dehardsub, [(1368, 3524), (31072, out.num_frames - 1)]) out = ending dehalo = gf.MaskedDHA(out, rx=1.4, ry=1.4, darkstr=0, brightstr=0.8) out = dehalo dbgra_cru = _dbgra(src_cru) deband = core.std.MaskedMerge(out, dbgra_cru, depth(masksub, 16)) out = deband ref = src_cru rsc_m = vdf.mask.diff_rescale_mask(ref, 837, thr=80) rsc_m = depth(rsc_m, 16) ref = dehardsub credit = out # Ep Title creds = [ Credit([(3691, 3810)], vdf.mask.region_mask(rsc_m[3696], 1450, 0, 800, 0)), ] for cred in creds: credit = lvf.rfs(credit, core.std.MaskedMerge(out, ref, cred.mask), cred.range_frames) out = credit # return dehardsub, vdf.mask.region_mask(rsc_m, 1450, 0, 800, 0) # return dehardsub, out return depth(out, 10).std.Limiter(16 << 2, [235 << 2, 240 << 2], [0, 1, 2])
def filtering() -> Union[vs.VideoNode, Tuple[vs.VideoNode, vs.VideoNode]]: """Vapoursynth filtering""" src_cru = WEB_CRU.clip_cut src_aod = WEB_AOD.clip_cut _, masksub = core.sub.TextFile(src_aod, SUB, fontdir='fonts', blend=False) masksub = core.std.Binarize(masksub, 1) masksub = hvf.mt_expand_multi(masksub, 'ellipse', sw=6, sh=4) masksub = hvf.mt_inflate_multi(masksub, radius=4).std.Convolution([1] * 9) dehardsub = core.std.MaskedMerge(src_aod, src_cru, masksub) dehardsub = depth(dehardsub, 16) out = dehardsub lineart = ScharrG41().get_mask( get_y(out), 4000, multi=1.2).rgvs.RemoveGrain(3).std.Maximum().std.Minimum() luma = get_y(out) ssing = vdf.scale.fsrcnnx_upscale( luma, height=1620, shader_file='shaders/FSRCNNX_x2_16-0-4-1.glsl', downscaler=lambda c, w, h: core.resize.Bicubic( c, w, h, filter_param_a=-0.5, filter_param_b=0.25), profile='fast', ) sraing = sraa_eedi3(ssing, 13, gamma=100, nrad=2, mdis=15) down = core.resize.Bicubic(sraing, out.width, out.height, filter_param_a=-0.5, filter_param_b=0.25) masked = core.std.MaskedMerge(luma, down, lineart) merged = vdf.misc.merge_chroma(masked, out) out = merged contra = hvf.LSFmod(out, strength=80, Smode=3, edgemode=0, source=dehardsub) out = contra # I gave up on this ending = lvf.rfs(out, dehardsub, [(31782, out.num_frames - 1)]) out = ending dehalo = gf.MaskedDHA(out, rx=1.4, ry=1.4, darkstr=0, brightstr=0.8) out = dehalo dbgra_cru = _dbgra(src_cru) deband = core.std.MaskedMerge(out, dbgra_cru, depth(masksub, 16)) out = deband ref = src_cru rsc_m = vdf.mask.diff_rescale_mask(ref, 837, thr=80) rsc_m = depth(rsc_m, 16) ref = dehardsub credit = out # Beginning credits creds = [ Credit([(0, 117)], vdf.mask.region_mask(rsc_m[94], 700, 600, 300, 300)), Credit([(125, 233)], core.std.Expr([ vdf.mask.region_mask(rsc_m, 100, 1300, 120, 600), vdf.mask.region_mask(rsc_m, 100, 1700, 80, 600) ], 'x y max')[137]), Credit([(288, 399)], vdf.mask.region_mask(rsc_m[303], 0, 1200, 0, 500)), Credit([(288, 399)], vdf.mask.region_mask(rsc_m[303], 0, 1200, 0, 500)), Credit([(526, 631)], vdf.mask.region_mask(rsc_m[543], 1200, 0, 0, 500)), Credit([(644, 749)], vdf.mask.region_mask(rsc_m[672], 0, 1000, 0, 0)), Credit([(758, 851)], vdf.mask.region_mask(rsc_m[771], 1300, 0, 0, 400)), Credit([(859, 966)], vdf.mask.region_mask(rsc_m[935], 1325, 0, 0, 0)), Credit([(996, 1083)], vdf.mask.region_mask(rsc_m[1007], 1325, 0, 0, 0)), Credit([(1091, 1198)], vdf.mask.region_mask(rsc_m[1117], 1325, 0, 0, 0)), Credit([(1216, 1315)], vdf.mask.region_mask(rsc_m[1253], 800, 800, 400, 400)), Credit([(1333, 1439)], core.std.Expr([ vdf.mask.region_mask(rsc_m, 1265, 0, 850, 0), vdf.mask.region_mask(rsc_m, 1400, 0, 800, 0), vdf.mask.region_mask(rsc_m, 1000, 0, 950, 0) ], 'x y max z max')[1365]), ] for cred in creds: credit = lvf.rfs(credit, core.std.MaskedMerge(out, ref, cred.mask), cred.range_frames) # Ep Title creds = [ Credit([(1653, 1772)], vdf.mask.region_mask(rsc_m[1705], 100, 600, 40, 940)), ] for cred in creds: credit = lvf.rfs(credit, core.std.MaskedMerge(out, ref, cred.mask), cred.range_frames) out = credit # return dehardsub, rsc_m # return dehardsub, masksub return depth(out, 10).std.Limiter(16 << 2, [235 << 2, 240 << 2], [0, 1, 2])
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])
def cleaning(self, clip: vs.VideoNode, e: int) -> vs.VideoNode: """ Clean the clip for OCR. Parameters ---------- clip: The input clip cropped for the subtitles e: The expand ratio Returns ------- vs.VideoNode The cleaned clip """ black_clip = core.std.BlankClip( width=int(clip.width - 20), height=int(clip.height - 20), format=vs.GRAY8, color=0, ) rect = core.std.AddBorders(clip=black_clip, left=10, right=10, top=10, bottom=10, color=255) blank = core.std.BlankClip(clip, format=vs.GRAY8) if isinstance(self.inline_threshold, list) or isinstance( self.outline_threshold, list): clip_RGB = core.fmtc.resample(clip=clip, css="444") clip_RGB = core.fmtc.matrix(clip=clip_RGB, mat="709", col_fam=vs.RGB) clip_RGB = core.fmtc.bitdepth(clip=clip_RGB, bits=8) if isinstance(self.inline_threshold, int) and isinstance( self.outline_threshold, int): white_raw = core.std.Binarize(clip=clip, threshold=self.inline_threshold) bright_raw = core.std.Binarize(clip=clip, threshold=self.outline_threshold) elif isinstance(self.inline_threshold, int) and isinstance( self.outline_threshold, list): white_raw = core.std.ShufflePlanes(clips=clip, planes=0, colorfamily=vs.GRAY) white_raw = core.std.Binarize(clip=white_raw, threshold=self.inline_threshold) bright_raw = self.binarize_RGB(clip_RGB, self.outline_threshold) elif isinstance(self.inline_threshold, list) and isinstance( self.outline_threshold, int): white_raw = self.binarize_RGB(clip_RGB, self.inline_threshold) bright_raw = core.std.ShufflePlanes(clips=clip, planes=0, colorfamily=vs.GRAY) bright_raw = core.std.Binarize(clip=bright_raw, threshold=self.outline_threshold) else: white_raw = self.binarize_RGB(clip_RGB, self.inline_threshold) bright_raw = self.binarize_RGB(clip_RGB, self.outline_threshold) bright_out = core.std.Lut2(clipa=bright_raw, clipb=rect, function=lambda x, y: min(x, y)) bright_not = core.misc.Hysteresis(clipa=bright_out, clipb=bright_raw) bright_not = core.std.Invert(bright_not) white_txt = core.std.MaskedMerge(blank, white_raw, bright_not) white_lb = haf.mt_inpand_multi(src=white_txt, sw=int(e), sh=int(e), mode="ellipse") white_lb = haf.mt_expand_multi(src=white_lb, sw=int(e), sh=int(e), mode="ellipse") white_ub = haf.mt_inpand_multi(src=white_txt, sw=int(5 * e), sh=int(5 * e), mode="ellipse") white_ub = haf.mt_expand_multi(src=white_ub, sw=int(3 * e), sh=int(3 * e), mode="ellipse") white_ub = core.std.Invert(white_ub) white = core.std.MaskedMerge(blank, white_lb, white_ub) white = core.misc.Hysteresis(clipa=white, clipb=white_txt) clip_cleaning = core.std.MaskedMerge(blank, white_raw, white) clip_cleaning = core.std.Median(clip=clip_cleaning) return clip_cleaning