Exemple #1
0
def nr_f3kdb(src,
             range=16,
             y=64,
             cb=64,
             cr=64,
             grainy=0,
             grainc=0,
             dyn_grain=False,
             keep_tv_range=True,
             thr=0.35,
             thrc=None,
             elast=2.5,
             output_depth=16):
    if thrc == None: thrc = thr
    nr = core.rgvs.RemoveGrain(src, 20)
    diff = core.std.MakeDiff(src, nr)
    db = core.f3kdb.Deband(nr,
                           range,
                           y,
                           cb,
                           cr,
                           grainy,
                           grainc,
                           keep_tv_range=keep_tv_range,
                           output_depth=output_depth)
    db = core.std.MergeDiff(db, diff)
    db = mvf.LimitFilter(db, src, thr=thr, thrc=thrc, elast=elast)
    return db
Exemple #2
0
 def output(self, aaed):
     if self.process_depth != self.clip_bits:
         return mvf.LimitFilter(self.clip,
                                mvf.Depth(aaed, self.clip_bits),
                                thr=1.0,
                                elast=2.0)
     else:
         return aaed
Exemple #3
0
def gaussian_usm(src, sigma=1.0, sigmaV=None, thr=1.0, thrc=None, elast=3.0):

    if sigmaV == None: sigmaV = sigma
    if thrc == None: thrc = thr

    blur = core.bilateral.Gaussian(src, sigma, sigmaV)
    diff = core.std.MakeDiff(src, blur)
    sharp = core.std.MergeDiff(src, diff)

    res = mvf.LimitFilter(sharp, src, thr=thr, thrc=thrc, elast=elast)

    return res
Exemple #4
0
    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 MinBlur(clp, r=1, planes=None):
    if not isinstance(clp, vs.VideoNode):
        raise vs.Error('MinBlur: This is not a clip')

    if planes is None:
        planes = list(range(clp.format.num_planes))
    elif isinstance(planes, int):
        planes = [planes]

    matrix1 = [1, 2, 1, 2, 4, 2, 1, 2, 1]
    matrix2 = [1, 1, 1, 1, 1, 1, 1, 1, 1]

    if r <= 0:
        RG11 = sbr(clp, planes=planes)
        RG4 = clp.std.Median(planes=planes)
    elif r == 1:
        RG11 = clp.std.Convolution(matrix=matrix1, planes=planes)
        RG4 = clp.std.Median(planes=planes)
    elif r == 2:
        RG11 = clp.std.Convolution(
            matrix=matrix1, planes=planes).std.Convolution(matrix=matrix2,
                                                           planes=planes)
        RG4 = clp.ctmf.CTMF(radius=2, planes=planes)
    else:
        RG11 = clp.std.Convolution(
            matrix=matrix1, planes=planes).std.Convolution(
                matrix=matrix2, planes=planes).std.Convolution(matrix=matrix2,
                                                               planes=planes)
        if clp.format.bits_per_sample == 16:
            s16 = clp
            RG4 = clp.fmtc.bitdepth(bits=12, planes=planes, dmode=1).ctmf.CTMF(
                radius=3, planes=planes).fmtc.bitdepth(bits=16, planes=planes)
            RG4 = mvf.LimitFilter(s16, RG4, thr=0.0625, elast=2, planes=planes)
        else:
            RG4 = clp.ctmf.CTMF(radius=3, planes=planes)

    expr = 'x y - x z - * 0 < x x y - abs x z - abs < y z ? ?'
    return core.std.Expr([clp, RG11, RG4],
                         expr=[
                             expr if i in planes else ''
                             for i in range(clp.format.num_planes)
                         ])
Exemple #6
0
def TAAmbk(clip,
           aatype=1,
           aatypeu=None,
           aatypev=None,
           preaa=0,
           strength=0.0,
           cycle=0,
           mtype=None,
           mclip=None,
           mthr=None,
           mthr2=None,
           mlthresh=None,
           mpand=(1, 0),
           txtmask=0,
           txtfade=0,
           thin=0,
           dark=0.0,
           sharp=0,
           aarepair=0,
           postaa=None,
           src=None,
           stabilize=0,
           down8=True,
           showmask=0,
           opencl=False,
           opencl_device=0,
           **args):
    core = vs.get_core()
    aatypeu = aatype if aatypeu is None else aatypeu
    aatypev = aatype if aatypev is None else aatypev

    if mtype is None:
        mtype = 0 if preaa == 0 and True not in (aatype, aatypeu,
                                                 aatypev) else 1

    if postaa is None:
        postaa = True if abs(sharp) > 70 or (0.4 < abs(sharp) < 1) else False

    if src is None:
        src = clip
    else:
        if clip.format.id != src.format.id:
            raise ValueError(MODULE_NAME +
                             ': clip format and src format mismatch.')
        elif clip.width != src.width or clip.height != src.height:
            raise ValueError(MODULE_NAME +
                             ': clip resolution and src resolution mismatch.')

    preaa_clip = clip if preaa == 0 else daa(clip, preaa)

    if thin == 0 and dark == 0:
        edge_enhanced_clip = preaa_clip
    elif thin != 0 and dark != 0:
        edge_enhanced_clip = haf.Toon(core.warp.AWarpSharp2(preaa_clip,
                                                            depth=int(thin)),
                                      str=float(dark))
    elif thin == 0:
        edge_enhanced_clip = haf.Toon(preaa_clip, str=float(dark))
    else:
        edge_enhanced_clip = core.warp.AWarpSharp2(preaa_clip, depth=int(thin))

    aa_kernel = {
        1: AAEedi2,
        2: AAEedi3,
        3: AANnedi3,
        4: AANnedi3UpscaleSangNom,
        5: AASpline64NRSangNom,
        6: AASpline64SangNom,
        -1: AAEedi2SangNom,
        -2: AAEedi3SangNom,
        -3: AANnedi3SangNom,
        'Eedi2': AAEedi2,
        'Eedi3': AAEedi3,
        'Nnedi3': AANnedi3,
        'Nnedi3UpscaleSangNom': AANnedi3UpscaleSangNom,
        'Spline64NrSangNom': AASpline64NRSangNom,
        'Spline64SangNom': AASpline64SangNom,
        'Eedi2SangNom': AAEedi2SangNom,
        'Eedi3SangNom': AAEedi3SangNom,
        'Nnedi3SangNom': AANnedi3SangNom,
        'PointSangNom': AAPointSangNom
    }

    aaed_clip = None
    if clip.format.color_family is vs.YUV:
        y = core.std.ShufflePlanes(edge_enhanced_clip, 0, vs.GRAY)
        u = core.std.ShufflePlanes(edge_enhanced_clip, 1, vs.GRAY)
        v = core.std.ShufflePlanes(edge_enhanced_clip, 2, vs.GRAY)
        if aatype != 0:
            try:
                y = aa_kernel[aatype](y,
                                      strength,
                                      down8,
                                      opencl=opencl,
                                      opencl_device=opencl_device,
                                      **args).out()
                cycle_y = cycle
                while cycle_y > 0:
                    y = aa_kernel[aatype](y,
                                          strength,
                                          down8,
                                          opencl=opencl,
                                          opencl_device=opencl_device,
                                          **args).out()
                    cycle_y -= 1
                y = mvf.Depth(
                    y, clip.format.bits_per_sample) if down8 is True else y
            except KeyError:
                raise ValueError(MODULE_NAME + ': unknown aatype.')
        if aatypeu != 0:
            try:
                u = aa_kernel[aatypeu](
                    u,
                    0,
                    down8,
                    opencl=opencl,
                    opencl_device=opencl_device,
                    **args).out()  # Won't do predown for u plane
                cycle_u = cycle
                while cycle_u > 0:
                    u = aa_kernel[aatypeu](u,
                                           0,
                                           down8,
                                           opencl=opencl,
                                           opencl_device=opencl_device,
                                           **args).out()
                    cycle_u -= 1
                u = mvf.Depth(
                    u, clip.format.bits_per_sample) if down8 is True else u
            except KeyError:
                raise ValueError(MODULE_NAME + ': unknown aatypeu.')
        if aatypev != 0:
            try:
                v = aa_kernel[aatypev](
                    v,
                    0,
                    down8,
                    opencl=opencl,
                    opencl_device=opencl_device,
                    **args).out()  # Won't do predown for v plane
                cycle_v = cycle
                while cycle_v > 0:
                    v = aa_kernel[aatypev](v,
                                           0,
                                           down8,
                                           opencl=opencl,
                                           opencl_device=opencl_device,
                                           **args).out()
                    cycle_v -= 1
                v = mvf.Depth(
                    v, clip.format.bits_per_sample) if down8 is True else v
            except KeyError:
                raise ValueError(MODULE_NAME + ': unknown aatypev.')
        aaed_clip = core.std.ShufflePlanes([y, u, v], [0, 0, 0], vs.YUV)
    elif clip.format.color_family is vs.GRAY:
        y = edge_enhanced_clip
        if aatype != 0:
            try:
                y = aa_kernel[aatype](y, strength, down8, **args).out()
                cycle_y = cycle
                while cycle_y > 0:
                    y = aa_kernel[aatype](y, strength, down8, **args).out()
                    cycle_y -= 1
                aaed_clip = mvf.Depth(
                    y, clip.format.bits_per_sample) if down8 is True else y
            except KeyError:
                raise ValueError(MODULE_NAME + ': unknown aatype.')
    else:
        raise ValueError(MODULE_NAME + ': Unsupported color family.')

    abs_sharp = abs(sharp)
    if sharp >= 1:
        sharped_clip = haf.LSFmod(aaed_clip,
                                  strength=int(abs_sharp),
                                  defaults='old',
                                  source=src)
    elif sharp > 0:
        per = int(40 * abs_sharp)
        matrix = [-1, -2, -1, -2, 52 - per, -2, -1, -2, -1]
        sharped_clip = core.std.Convolution(aaed_clip, matrix)
    elif sharp == 0:
        sharped_clip = aaed_clip
    elif sharp > -1:
        sharped_clip = haf.LSFmod(aaed_clip,
                                  strength=round(abs_sharp * 100),
                                  defaults='fast',
                                  source=src)
    elif sharp == -1:
        blured = core.rgvs.RemoveGrain(
            aaed_clip, mode=20 if aaed_clip.width > 1100 else 11)
        diff = core.std.MakeDiff(aaed_clip, blured)
        diff = core.rgvs.Repair(diff,
                                core.std.MakeDiff(src, aaed_clip),
                                mode=13)
        sharped_clip = core.std.MergeDiff(aaed_clip, diff)
    else:
        sharped_clip = aaed_clip

    postaa_clip = sharped_clip if postaa is False else soothe(
        sharped_clip, src, 24)
    repaired_clip = postaa_clip if aarepair == 0 else core.rgvs.Repair(
        src, postaa_clip, aarepair)
    stabilized_clip = repaired_clip if stabilize == 0 else temporal_stabilize(
        repaired_clip, src, stabilize)

    if mclip is not None:
        mask = mclip
        try:
            masked_clip = core.std.MaskedMerge(src,
                                               stabilized_clip,
                                               mask,
                                               first_plane=True)
        except vs.Error:
            raise RuntimeError(MODULE_NAME +
                               ': Something wrong with your mclip. '
                               'Maybe resolution or bit_depth mismatch.')
    elif mtype != 0:
        if mtype == 1 or mtype is 'Canny':
            opencl_device = args.get('opencl_device', 0)
            mthr = 1.2 if mthr is None else mthr
            mthr2 = 8.0 if mthr2 is None else mthr2
            mask = MaskCanny(clip,
                             sigma=mthr,
                             t_h=mthr2,
                             lthresh=mlthresh,
                             mpand=mpand,
                             opencl=opencl,
                             opencl_devices=opencl_device).out()
        elif mtype == 2 or mtype is 'Sobel':
            mthr = 1.2 if mthr is None else mthr
            mthr2 = 48 if mthr2 is None else mthr2
            mask = MaskSobel(clip,
                             sigma=mthr,
                             binarize=mthr2,
                             lthresh=mlthresh,
                             mpand=mpand).out()
        elif mtype == 3 or mtype is 'prewitt':
            mthr = 62 if mthr is None else mthr
            mask = MaskPrewitt(clip,
                               factor=mthr,
                               lthresh=mlthresh,
                               mpand=mpand).out()
        else:
            raise ValueError(MODULE_NAME + ': unknown mtype.')
        masked_clip = core.std.MaskedMerge(src, stabilized_clip, mask)
    else:
        masked_clip = stabilized_clip

    if txtmask > 0 and clip.format.color_family is not vs.GRAY:
        text_mask = FadeTextMask(clip, lthr=txtmask, fade_nums=txtfade).out()
        txt_protected_clip = core.std.MaskedMerge(masked_clip,
                                                  src,
                                                  text_mask,
                                                  first_plane=True)
    else:
        txt_protected_clip = masked_clip

    if clip.format.bits_per_sample > 8 and down8 is True:
        clamped_clip = mvf.LimitFilter(src,
                                       txt_protected_clip,
                                       thr=1.0,
                                       elast=2.0)
    else:
        clamped_clip = txt_protected_clip

    try:
        if showmask == -1:
            return text_mask
        elif showmask == 1:
            return mask
        elif showmask == 2:
            return core.std.StackVertical([
                core.std.ShufflePlanes([mask, core.std.BlankClip(src)],
                                       [0, 1, 2], vs.YUV), src
            ])
        elif showmask == 3:
            return core.std.Interleave([
                core.std.ShufflePlanes([mask, core.std.BlankClip(src)],
                                       [0, 1, 2], vs.YUV), src
            ])
        else:
            return clamped_clip
    except UnboundLocalError:
        raise RuntimeError(MODULE_NAME +
                           ': No mask to show if you don\'t have one.')
def nnedi3_dh(input,
              field=1,
              nsize=None,
              nns=None,
              qual=None,
              etype=None,
              pscrn=None,
              device=None,
              fast=None,
              flat_kernel=None,
              flat_a1=None,
              flat_a2=None,
              flat_taps=None):
    core = vs.core

    sFormat = input.format
    sSType = sFormat.sample_type
    sbitPS = sFormat.bits_per_sample
    sVSubS = 1 << sFormat.subsampling_h

    if fast is None:
        fast = False
    if flat_kernel is None:
        flat_kernel = "nnedi3"
    else:
        flat_kernel = flat_kernel.lower()

    if (field == 0 or field == 1) and ((fast and sbitPS > 8)):
        if flat_kernel == "nnedi3":
            flat_kernel = "bicubic"
        input8 = mvf.Depth(input, depth=8, sample=vs.INTEGER)
        nn = core.nnedi3cl.NNEDI3CL(input8,
                                    field=field,
                                    dh=True,
                                    nsize=nsize,
                                    nns=nns,
                                    qual=qual,
                                    etype=etype,
                                    pscrn=pscrn,
                                    device=device)
        lr = core.fmtc.resample(input,
                                sy=[-0.5, -0.5 * sVSubS] if field == 0 else 0,
                                scaleh=1,
                                scalev=2,
                                kernel=flat_kernel,
                                a1=flat_a1,
                                a2=flat_a2,
                                taps=flat_taps,
                                center=False)
        return mvf.LimitFilter(mvf.Depth(lr, depth=sbitPS, sample=sSType),
                               mvf.Depth(nn, depth=sbitPS, sample=sSType),
                               thr=1.0,
                               elast=2.0)
    elif flat_kernel == "nnedi3":
        return core.nnedi3cl.NNEDI3CL(input,
                                      field=field,
                                      dh=True,
                                      nsize=nsize,
                                      nns=nns,
                                      qual=qual,
                                      etype=etype,
                                      pscrn=pscrn,
                                      device=device)
    else:
        nn = core.nnedi3cl.NNEDI3CL(input,
                                    field=field,
                                    dh=True,
                                    nsize=nsize,
                                    nns=nns,
                                    qual=qual,
                                    etype=etype,
                                    pscrn=pscrn,
                                    device=device)
        lr = core.fmtc.resample(input,
                                sy=[-0.5, -0.5 * sVSubS] if field == 0 else 0,
                                scaleh=1,
                                scalev=2,
                                kernel=flat_kernel,
                                a1=flat_a1,
                                a2=flat_a2,
                                taps=flat_taps,
                                center=False)
        return mvf.LimitFilter(mvf.Depth(lr, depth=sbitPS, sample=sSType),
                               mvf.Depth(nn, depth=sbitPS, sample=sSType),
                               thr=1.0,
                               elast=2.0)
Exemple #8
0
def mts(src):
    src8 = mvf.ToYUV(src, css='420')
    src16 = core.fmtc.bitdepth(src8,bits=16)

    # Denoise
    src_rgb = mvf.ToRGB(src16,depth=32)
    #nr16_rgb = core.pdn.BM3DBasic(src_rgb, sigma=[0.5, 0.2, 0.2],group_size=16, bm_range=8)
    nr16_rgb = core.pdn.BM3DBasic(src_rgb, sigma=[1.0, 0.5, 0.5])
    nr16 = mvf.ToYUV(nr16_rgb, css="420",depth=16)
    noise16 = core.std.MakeDiff(src16, nr16)

    # Mask
    pre16 = core.std.ShufflePlanes([nr16,src16],[0,1,2],vs.YUV)
    pre8 = mvf.Depth(pre16, depth=8, dither=5)

    eemask = core.tcanny.TCanny(pre8,sigma=0.6,op=2,gmmax=255,planes=[0,1,2],mode=1)
    eemask_u = core.std.ShufflePlanes(eemask, [1,1,1], vs.YUV).fmtc.resample(1920,1080,sx=0.25,css="420")
    eemask_v = core.std.ShufflePlanes(eemask, [2,2,2], vs.YUV).fmtc.resample(1920,1080,sx=0.25,css="420")
    weighted = core.std.Expr([eemask,eemask_u,eemask_v],["x 64 * y + z +",""],vs.YUV420P16)

    luma = core.std.ShufflePlanes(pre8, 0, vs.YUV).resize.Bilinear(format=vs.YUV420P8)
    eemask = core.std.Expr([eemask,luma],["x x * 20 * 255 y - dup * 0.2 * + 50000 min","x x * 30 * 255 y - dup * 0.3 * + 60000 min"],vs.YUV420P16)
    eemask = core.std.Maximum(eemask,planes=[0,1,2])
    eemask = core.rgvs.RemoveGrain(eemask, [20,11]).rgvs.RemoveGrain([20,11]).rgvs.RemoveGrain([20,11])

    aamask = core.std.Binarize(weighted, 12000, 0)

    nrmasks = core.std.Binarize(weighted, 4500, 0)

    dmaskb = core.std.Binarize(weighted, 3000, 0)
    dmaskm = core.std.Binarize(weighted, 3500, 0)
    dmasks = core.std.Binarize(weighted, 3800, 0)
    dmask_dark = core.misc.Hysteresis(dmaskm, dmaskb)
    dmask_bright = core.misc.Hysteresis(dmasks, dmaskm)
    dmask = core.std.Expr([src16, dmask_dark, dmask_bright], "x 24672 < y z ?")

    nrmaskg = core.tcanny.TCanny(pre8,sigma=1.5,t_l=8,t_h=15,op=2,planes=0)
    nrmaskb = core.tcanny.TCanny(pre8,sigma=1.2,t_l=8,t_h=11,op=2,planes=0)

    nrmask = core.std.Expr([nrmaskg,nrmaskb,nrmasks,pre8,dmask],["a 20 < 65535 a 64 < x 257 * b max a 160 < y 257 * z ? ? ?","",""],vs.YUV420P16)
    nrmask = core.std.Maximum(nrmask,0).std.Maximum(0).std.Minimum(0).std.Minimum(0)
    nrmask = core.rgvs.RemoveGrain(core.rgvs.RemoveGrain(nrmask,[20,0]),[20,0])


    Y = core.std.ShufflePlanes(src16, 0, vs.YUV).resize.Bicubic(1920, 1080, format=vs.YUV420P16, filter_param_a=0, filter_param_b=0.5)
    U = core.std.ShufflePlanes(src16, [1,1,1], vs.YUV).resize.Bicubic(1920, 1080, format=vs.YUV420P16, filter_param_a=0, filter_param_b=0.5)
    V = core.std.ShufflePlanes(src16, [2,2,2], vs.YUV).resize.Bicubic(1920, 1080, format=vs.YUV420P16, filter_param_a=0, filter_param_b=0.5)
    textmask0 = core.std.Expr([Y,U,V], ["x 60000 > y 32768 - abs 768 < and z 32768 - abs 768 < and 65535 0 ?","0"])
    #textmasks = core.std.Expr([Y,U,V], ["x 58000 > y 32768 - abs 512 < and z 32768 - abs 512 < and 65535 0 ?","0"])
    textmask1 = core.std.Minimum(textmask0, 0).std.Minimum(planes=0).std.Minimum(planes=0).std.Maximum(0).std.Maximum(0).std.Maximum(0)
    textmask2 = core.misc.Hysteresis(textmask1, textmask0, planes=0)
    textmask = core.std.Expr([textmask0, textmask2], "x y > x 0 ?")
    #textmask = core.misc.Hysteresis(textmasks, textmaskb, planes=0)
    textmask = core.std.Maximum(textmask,0).std.Maximum(0).std.Maximum(0)#.std.Maximum(0)#.std.Maximum(0)#.std.Maximum(0)#.std.Minimum(0)

    debd = core.f3kdb.Deband(nr16,8,48,32,32,0,0,output_depth=16)
    debd = core.f3kdb.Deband(debd,15,32,24,24,0,0,output_depth=16)
    debd = mvf.LimitFilter(debd,nr16,thr=0.7,thrc=0.5,elast=2.0)
    debd = core.std.MaskedMerge(debd, nr16, nrmask, first_plane=1)

    w  = 1920
    h  = 1080
    oaa_y = core.std.ShufflePlanes(nr16, 0, vs.GRAY)

    aa_y = core.eedi2.EEDI2(oaa_y,field=1,mthresh=10,lthresh=20,vthresh=20,maxd=24,nt=50).fmtc.resample(w,h,sy=-0.5, kernel='bicubic', a1=0, a2=0.5,).std.Transpose()
    aa_y = core.eedi2.EEDI2(aa_y,field=1,mthresh=10,lthresh=20,vthresh=20,maxd=24,nt=50).fmtc.resample(h,w,sy=-0.5, kernel='bicubic', a1=0, a2=0.5,).std.Transpose()

    aa_clip = core.std.ShufflePlanes([aa_y,nr16], [0,1,2], vs.YUV)
    aaed = core.std.MaskedMerge(debd, aa_clip, aamask, 0, True)
    aaed = core.std.MaskedMerge(aaed, debd, textmask, 0, False)

    dif = core.std.MakeDiff(aaed, core.rgvs.RemoveGrain(aaed,20))
    sharp = core.std.MergeDiff(aaed, dif)
    sharped = core.std.MaskedMerge(sharp, aaed, eemask, [0,1,2], False)

    noise16 = core.std.Expr(noise16,["x 32768 - 1.05 * 32768 +",""])
    nullclip = core.std.Expr(src16,["32768",""])
    nrweight = core.std.Expr(pre8, ["x 48 - 0 max dup * 5 * ",""], vs.YUV420P16)
    noise16 = core.std.MaskedMerge(noise16,nullclip,nrweight,0,True)
    res = core.std.MergeDiff(sharped,noise16,0)

    return res