Skip to content

funcs

Functions:

  • based_aa

    Perform based anti-aliasing on a video clip.

  • clamp_aa

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

  • pre_aa

based_aa

based_aa(
    clip: VideoNode,
    rfactor: float = 2.0,
    mask: VideoNode | EdgeDetectT | Literal[False] = Prewitt,
    mask_thr: int = 60,
    pscale: float = 0.0,
    downscaler: ScalerLike | None = None,
    supersampler: ScalerLike | Literal[False] = ArtCNN,
    antialiaser: AntiAliaser | None = None,
    prefilter: (
        VideoNode
        | VSFunctionNoArgs[VideoNode, ConstantFormatVideoNode]
        | Literal[False]
    ) = False,
    postfilter: (
        VSFunctionNoArgs[VideoNode, ConstantFormatVideoNode]
        | Literal[False]
        | None
    ) = None,
    show_mask: bool = False,
    **aa_kwargs: Any
) -> VideoNode

Perform based anti-aliasing on a video clip.

This function works by super- or downsampling the clip and applying an AntiAliaser to that image. The result is then merged with the original clip using an edge mask, and it's limited to areas where the AntiAliaser was actually applied.

Sharp supersamplers will yield better results, so long as they do not introduce too much ringing. For downscalers, you will want to use a neutral kernel.

Parameters:

  • clip

    (VideoNode) –

    Clip to process.

  • rfactor

    (float, default: 2.0 ) –

    Resize factor for supersampling. Values above 1.0 are recommended. Lower values may be useful for particularly extremely aliased content. Values closer to 1.0 will perform faster at the cost of precision. This value must be greater than 0.0. Default: 2.0.

  • mask

    (VideoNode | EdgeDetectT | Literal[False], default: Prewitt ) –

    Edge detection mask or function to generate it. Default: Prewitt.

  • mask_thr

    (int, default: 60 ) –

    Threshold for edge detection mask. Only used if an EdgeDetect class is passed to mask. Default: 60.

  • pscale

    (float, default: 0.0 ) –

    Scale factor for the supersample-downscale process change.

  • downscaler

    (ScalerLike | None, default: None ) –

    Scaler used for downscaling after anti-aliasing. This should ideally be a relatively sharp kernel that doesn't introduce too much haloing. If None, downscaler will be set to Box if the scale factor is an integer (after rounding), and Catrom otherwise. If rfactor is below 1.0, the downscaler will be used before antialiasing instead, and the supersampler will be used to scale the clip back to its original resolution. Default: None.

  • supersampler

    (ScalerLike | Literal[False], default: ArtCNN ) –

    Scaler used for supersampling before anti-aliasing. If False, no supersampling is performed. If rfactor is below 1.0, the downscaler will be used before antialiasing instead, and the supersampler will be used to scale the clip back to its original resolution. The supersampler should ideally be fairly sharp without introducing too much ringing. Default: ArtCNN (R8F64). If True, both fields will be processed separately, which may improve anti-aliasing strength at the cost of increased processing time and detail loss. Default: False.

  • antialiaser

    (AntiAliaser | None, default: None ) –

    Antialiaser used for anti-aliasing. If None, EEDI3 will be selected with these default settings: (alpha=0.125, beta=0.25, vthresh0=12, vthresh1=24, field=1).

  • prefilter

    (VideoNode | VSFunctionNoArgs[VideoNode, ConstantFormatVideoNode] | Literal[False], default: False ) –

    Prefilter to apply before anti-aliasing. Must be a VideoNode, a function that takes a VideoNode and returns a VideoNode, or False. Default: False.

  • postfilter

    (VSFunctionNoArgs[VideoNode, ConstantFormatVideoNode] | Literal[False] | None, default: None ) –

    Postfilter to apply after anti-aliasing. Must be a function that takes a VideoNode and returns a VideoNode, or None. If None, applies a median-filtered bilateral smoother to clean halos created during antialiasing. Default: None.

  • show_mask

    (bool, default: False ) –

    If True, returns the edge detection mask instead of the processed clip. Default: False

Returns:

  • VideoNode

    Anti-aliased clip or edge detection mask if show_mask is True.

Raises:

  • CustomValueError

    If rfactor is not above 0.0, or invalid prefilter/postfilter is passed.

Source code
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
def based_aa(
    clip: vs.VideoNode,
    rfactor: float = 2.0,
    mask: vs.VideoNode | EdgeDetectT | Literal[False] = Prewitt,
    mask_thr: int = 60,
    pscale: float = 0.0,
    downscaler: ScalerLike | None = None,
    supersampler: ScalerLike | Literal[False] = ArtCNN,
    antialiaser: AntiAliaser | None = None,
    prefilter: vs.VideoNode | VSFunctionNoArgs[vs.VideoNode, ConstantFormatVideoNode] | Literal[False] = False,
    postfilter: VSFunctionNoArgs[vs.VideoNode, ConstantFormatVideoNode] | Literal[False] | None = None,
    show_mask: bool = False, **aa_kwargs: Any
) -> vs.VideoNode:
    """
    Perform based anti-aliasing on a video clip.

    This function works by super- or downsampling the clip and applying an AntiAliaser to that image.
    The result is then merged with the original clip using an edge mask, and it's limited
    to areas where the AntiAliaser was actually applied.

    Sharp supersamplers will yield better results, so long as they do not introduce too much ringing.
    For downscalers, you will want to use a neutral kernel.

    :param clip:                  Clip to process.
    :param rfactor:               Resize factor for supersampling. Values above 1.0 are recommended.
                                  Lower values may be useful for particularly extremely aliased content.
                                  Values closer to 1.0 will perform faster at the cost of precision.
                                  This value must be greater than 0.0. Default: 2.0.
    :param mask:                  Edge detection mask or function to generate it. Default: Prewitt.
    :param mask_thr:              Threshold for edge detection mask.
                                  Only used if an EdgeDetect class is passed to `mask`. Default: 60.
    :param pscale:                Scale factor for the supersample-downscale process change.
    :param downscaler:            Scaler used for downscaling after anti-aliasing. This should ideally be
                                  a relatively sharp kernel that doesn't introduce too much haloing.
                                  If None, downscaler will be set to Box if the scale factor is an integer
                                  (after rounding), and Catrom otherwise.
                                  If rfactor is below 1.0, the downscaler will be used before antialiasing instead,
                                  and the supersampler will be used to scale the clip back to its original resolution.
                                  Default: None.
    :param supersampler:          Scaler used for supersampling before anti-aliasing. If False, no supersampling
                                  is performed. If rfactor is below 1.0, the downscaler will be used before
                                  antialiasing instead, and the supersampler will be used to scale the clip
                                  back to its original resolution.
                                  The supersampler should ideally be fairly sharp without
                                  introducing too much ringing.
                                  Default: ArtCNN (R8F64).
                                  If True, both fields will be processed separately, which may improve
                                  anti-aliasing strength at the cost of increased processing time and detail loss.
                                  Default: False.
    :param antialiaser:           Antialiaser used for anti-aliasing. If None, EEDI3 will be selected with these default settings:
                                  (alpha=0.125, beta=0.25, vthresh0=12, vthresh1=24, field=1).
    :param prefilter:             Prefilter to apply before anti-aliasing.
                                  Must be a VideoNode, a function that takes a VideoNode and returns a VideoNode,
                                  or False. Default: False.
    :param postfilter:            Postfilter to apply after anti-aliasing.
                                  Must be a function that takes a VideoNode and returns a VideoNode, or None.
                                  If None, applies a median-filtered bilateral smoother to clean halos
                                  created during antialiasing. Default: None.
    :param show_mask:             If True, returns the edge detection mask instead of the processed clip.
                                  Default: False

    :return:                      Anti-aliased clip or edge detection mask if show_mask is True.

    :raises CustomValueError:     If rfactor is not above 0.0, or invalid prefilter/postfilter is passed.
    """

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

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

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

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

        if show_mask:
            return mask

    if supersampler is False:
        supersampler = downscaler = NoScale[Catrom]
        rfactor = 1.0

    aaw, aah = [round(dimension * rfactor) for dimension 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

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

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

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

        if TYPE_CHECKING:
            assert check_variable_format(prefilter, func.func)

        ss_clip = prefilter
    else:
        ss_clip = func.work_clip

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

    if not antialiaser:
        antialiaser = EEDI3(
            alpha=0.125, beta=0.25, gamma=40, vthresh=(12, 24, None),
            mclip=Bilinear().scale(mask, ss.width, ss.height) if mask else None,
            sclip=ss
        )

    aa = antialiaser.antialias(ss, **aa_kwargs)

    aa = downscaler.scale(aa, func.work_clip.width, func.work_clip.height)  # type: ignore

    if pscale != 1.0 and not isinstance(supersampler, NoScale):
        no_aa = downscaler.scale(ss, func.work_clip.width, func.work_clip.height)
        aa = norm_expr([func.work_clip, aa, no_aa], 'x z x - {pscale} * + y z - +', pscale=pscale, func=func.func)  # type: ignore

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

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

    return func.return_clip(aa)

clamp_aa

clamp_aa(
    clip: VideoNode,
    strength: float = 1.0,
    mthr: float = 0.25,
    mask: VideoNode | EdgeDetectT | Literal[False] = False,
    weak_aa: VideoNode | AntiAliaser | None = None,
    strong_aa: VideoNode | AntiAliaser | None = None,
    ref: VideoNode | None = None,
    planes: PlanesT = 0,
) -> ConstantFormatVideoNode

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 | Literal[False], default: False ) –

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

  • weak_aa

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

    AntiAliaser for the weaker aa. Default is NNEDI3.

  • strong_aa

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

    AntiAliaser for the stronger aa. Default is EEDI3.

  • ref

    (VideoNode | None, default: None ) –

    Reference clip for clamping.

Returns:

  • ConstantFormatVideoNode

    Antialiased clip.

Source code
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 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
def clamp_aa(
    clip: vs.VideoNode,
    strength: float = 1.0,
    mthr: float = 0.25,
    mask: vs.VideoNode | EdgeDetectT | Literal[False] = False,
    weak_aa: vs.VideoNode | AntiAliaser | None = None,
    strong_aa: vs.VideoNode | AntiAliaser | None = None,
    ref: vs.VideoNode | None = None,
    planes: PlanesT = 0
) -> ConstantFormatVideoNode:
    """
    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. Default is NNEDI3.
    :param strong_aa:           AntiAliaser for the stronger aa. Default is EEDI3.
    :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 weak_aa is None:
            weak_aa = NNEDI3()

        weak_aa = weak_aa.antialias(func.work_clip)

    if not isinstance(strong_aa, vs.VideoNode):
        if strong_aa is None:
            strong_aa = EEDI3()

        strong_aa = strong_aa.antialias(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(
        [func.work_clip, ref, weak_aa, strong_aa],
        'y z - D1! y a - D2! D1@ D2@ xor x D1@ abs D2@ abs < a z {thr} - z {thr} + clip a ? ?',
        thr=thr, planes=func.norm_planes, func=func.func
    )

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

            mask = EdgeDetect.ensure_obj(mask).edgemask(func.work_clip)
            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)

    return func.return_clip(clamped)

pre_aa

pre_aa(
    clip: VideoNode,
    sharpener: VSFunctionNoArgs[VideoNode, VideoNode] = partial(
        unsharpen, blur=partial(gauss_blur, mode=VERTICAL, sigma=1)
    ),
    antialiaser: AntiAliaser = NNEDI3(),
    direction: AADirection = BOTH,
    planes: PlanesT = None,
) -> VideoNode
Source code
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def pre_aa(
    clip: vs.VideoNode,
    sharpener: VSFunctionNoArgs[vs.VideoNode, vs.VideoNode] = partial(
        unsharpen, blur=partial(gauss_blur, mode=ConvMode.VERTICAL, sigma=1)
    ),
    antialiaser: AntiAliaser = NNEDI3(),
    direction: AntiAliaser.AADirection = AntiAliaser.AADirection.BOTH,
    planes: PlanesT = None,
) -> vs.VideoNode:
    func = FunctionUtil(clip, pre_aa, planes)

    wclip = func.work_clip

    for x in AntiAliaser.AADirection:
        if direction in (x, AntiAliaser.AADirection.BOTH):
            if x == AntiAliaser.AADirection.HORIZONTAL:
                wclip = antialiaser.transpose(wclip)

            aa = antialiaser.antialias(wclip, AntiAliaser.AADirection.VERTICAL)
            sharp = sharpener(wclip)
            limit = MeanMode.MEDIAN(wclip, aa, sharp)

            if x == AntiAliaser.AADirection.HORIZONTAL:
                wclip = antialiaser.transpose(limit)

    return func.return_clip(wclip)