Skip to content

funcs

pre_aa module-attribute

pre_aa = _pre_aa()

based_aa

based_aa(
    clip: VideoNode,
    rfactor: float = 2.0,
    mask: VideoNode | EdgeDetectT | Literal[False] = Prewitt,
    mask_thr: int = 60,
    pskip: bool = True,
    downscaler: ScalerT | None = None,
    supersampler: (
        ScalerT | ShaderFile | Path | Literal[False] | MissingT
    ) = MISSING,
    double_rate: bool = False,
    antialiaser: Antialiaser | None = None,
    prefilter: VideoNode | VSFunction | Literal[False] = False,
    postfilter: VSFunction | Literal[False] | None = None,
    show_mask: bool = False,
    planes: PlanesT = 0,
    **aa_kwargs: Any
) -> VideoNode
Source code
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
def based_aa(
    clip: vs.VideoNode, rfactor: float = 2.0,
    mask: vs.VideoNode | EdgeDetectT | Literal[False] = Prewitt, mask_thr: int = 60, pskip: bool = True,
    downscaler: ScalerT | None = None,
    supersampler: ScalerT | ShaderFile | Path | Literal[False] | MissingT = MISSING,
    double_rate: bool = False,
    antialiaser: Antialiaser | None = None,
    prefilter: vs.VideoNode | VSFunction | Literal[False] = False,
    postfilter: VSFunction | Literal[False] | None = None,
    show_mask: bool = False, planes: PlanesT = 0,
    **aa_kwargs: Any
) -> vs.VideoNode:

    func = FunctionUtil(clip, based_aa, planes, (vs.YUV, vs.GRAY))

    if supersampler is False:
        supersampler = downscaler = NoScale
    else:
        if supersampler is MISSING:
            try:
                from vsscale import ArtCNN  # noqa: F811
            except ModuleNotFoundError:
                raise CustomRuntimeError(
                    'You\'re missing the "vsscale" package! Install it with "pip install vsscale".', based_aa
                )

            supersampler = ArtCNN.C16F64()
        elif isinstance(supersampler, (str, Path)):
            try:
                from vsscale import PlaceboShader  # noqa: F811
            except ModuleNotFoundError:
                raise CustomRuntimeError(
                    'You\'re missing the "vsscale" package! Install it with "pip install vsscale".', based_aa
                )

            supersampler = PlaceboShader(supersampler)

    if rfactor <= 0.0:
        raise CustomValueError('rfactor must be greater than 0!', based_aa, rfactor)

    aaw, aah = [(round(r * rfactor) + 1) & ~1 for r in (func.work_clip.width, func.work_clip.height)]

    if downscaler is None:
        downscaler = Box if (
            max(aaw, func.work_clip.width) % min(aaw, func.work_clip.width) == 0
            and max(aah, func.work_clip.height) % min(aah, func.work_clip.height) == 0
        ) else Catrom

    if rfactor < 1.0:
        downscaler, supersampler = supersampler, downscaler

    if not isinstance(mask, vs.VideoNode) and mask is not False:
        mask = EdgeDetect.ensure_obj(mask, based_aa).edgemask(plane(func.work_clip, 0))
        mask = mask.std.Binarize(scale_mask(min(mask_thr, 255), 8, func.work_clip))

        mask = box_blur(mask.std.Maximum())
        mask = limiter(mask, func=based_aa)

    if show_mask:
        if mask is False:
            raise CustomValueError("Can't show mask when mask is False!", based_aa, mask)
        return mask

    if callable(prefilter):
        ss_clip = prefilter(func.work_clip)
    elif isinstance(prefilter, vs.VideoNode):
        FormatsMismatchError.check(based_aa, func.work_clip, prefilter)
        ss_clip = prefilter
    else:
        ss_clip = func.work_clip

    supersampler = Scaler.ensure_obj(supersampler, based_aa)
    downscaler = Scaler.ensure_obj(downscaler, based_aa)

    ss = supersampler.scale(ss_clip, aaw, aah)

    if antialiaser is None:
        antialiaser = Eedi3(mclip=Bilinear.scale(mask, ss.width, ss.height) if mask else None, sclip_aa=True)
        aa_kwargs = KwargsT(alpha=0.125, beta=0.25, vthresh0=12, vthresh1=24, field=1) | aa_kwargs

    if double_rate:
        aa = antialiaser.draa(ss, **aa_kwargs)
    else:
        aa = antialiaser.aa(ss, **aa_kwargs)

    aa = downscaler.scale(aa, func.work_clip.width, func.work_clip.height)

    if postfilter is None:
        aa_out = MeanMode.MEDIAN(aa, func.work_clip, bilateral(aa))
    elif callable(postfilter):
        aa_out = postfilter(aa)
    else:
        aa_out = aa

    if pskip:
        no_aa = downscaler.scale(ss, func.work_clip.width, func.work_clip.height)
        aa_out = norm_expr([func.work_clip, aa_out, aa, no_aa], "z a = x y ?")

    if mask:
        aa_out = func.work_clip.std.MaskedMerge(aa_out, mask)

    return func.return_clip(aa_out)

clamp_aa

clamp_aa(
    clip: VideoNode,
    strength: float = 1.0,
    mthr: float = 0.25,
    mask: VideoNode | EdgeDetectT | None = None,
    weak_aa: VideoNode | Antialiaser = Nnedi3(),
    strong_aa: VideoNode | Antialiaser = Eedi3(),
    opencl: bool | None = False,
    ref: VideoNode | None = None,
    planes: PlanesT = 0,
) -> VideoNode

Clamp a strong aa to a weaker one for the purpose of reducing the stronger's artifacts.

Parameters:

  • clip

    (VideoNode) –

    Clip to process.

  • strength

    (float, default: 1.0 ) –

    Set threshold strength for over/underflow value for clamping.

  • mthr

    (float, default: 0.25 ) –

    Binarize threshold for the mask, float.

  • mask

    (VideoNode | EdgeDetectT | None, default: None ) –

    Clip to use for custom mask or an EdgeDetect to use custom masker.

  • weak_aa

    (VideoNode | Antialiaser, default: Nnedi3() ) –

    Antialiaser for the weaker aa.

  • strong_aa

    (VideoNode | Antialiaser, default: Eedi3() ) –

    Antialiaser for the stronger aa.

  • opencl

    (bool | None, default: False ) –

    Whether to force OpenCL acceleration, None to leave as is.

  • ref

    (VideoNode | None, default: None ) –

    Reference clip for clamping.

Returns:

  • VideoNode

    Antialiased clip.

Source code
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def clamp_aa(
    clip: vs.VideoNode, strength: float = 1.0,
    mthr: float = 0.25, mask: vs.VideoNode | EdgeDetectT | None = None,
    weak_aa: vs.VideoNode | Antialiaser = Nnedi3(),
    strong_aa: vs.VideoNode | Antialiaser = Eedi3(),
    opencl: bool | None = False, ref: vs.VideoNode | None = None,
    planes: PlanesT = 0
) -> vs.VideoNode:
    """
    Clamp a strong aa to a weaker one for the purpose of reducing the stronger's artifacts.

    :param clip:                Clip to process.
    :param strength:            Set threshold strength for over/underflow value for clamping.
    :param mthr:                Binarize threshold for the mask, float.
    :param mask:                Clip to use for custom mask or an EdgeDetect to use custom masker.
    :param weak_aa:             Antialiaser for the weaker aa.
    :param strong_aa:           Antialiaser for the stronger aa.
    :param opencl:              Whether to force OpenCL acceleration, None to leave as is.
    :param ref:                 Reference clip for clamping.

    :return:                    Antialiased clip.
    """

    func = FunctionUtil(clip, clamp_aa, planes, (vs.YUV, vs.GRAY))

    if not isinstance(weak_aa, vs.VideoNode):
        if opencl is not None and hasattr(weak_aa, 'opencl'):
            weak_aa.opencl = opencl

        weak_aa = weak_aa.aa(func.work_clip)

    if not isinstance(strong_aa, vs.VideoNode):
        if opencl is not None and hasattr(strong_aa, 'opencl'):
            strong_aa.opencl = opencl

        strong_aa = strong_aa.aa(func.work_clip)

    ref = fallback(ref, func.work_clip)

    if func.luma_only:
        weak_aa = get_y(weak_aa)
        strong_aa = get_y(strong_aa)
        ref = get_y(ref)

    if func.work_clip.format.sample_type == vs.INTEGER:
        thr = strength * get_peak_value(func.work_clip)
    else:
        thr = strength / 219

    clamped = norm_expr(
        [ref, weak_aa, strong_aa, func.work_clip],
        'x y - D1! x z - D2! xor a D1@ abs D2@ abs < z y {thr} - y {thr} + clip z ? ?',
        thr=thr, planes=func.norm_planes
    )

    if mask:
        if not isinstance(mask, vs.VideoNode):
            bin_thr = scale_mask(mthr, 32, clip)

            mask = ScharrTCanny.ensure_obj(mask).edgemask(func.work_clip)  # type: ignore
            mask = box_blur(mask.std.Binarize(bin_thr).std.Maximum())
            mask = mask.std.Minimum().std.Deflate()

        clamped = func.work_clip.std.MaskedMerge(clamped, mask, func.norm_planes)  # type: ignore

    return func.return_clip(clamped)