Skip to content

funcs

Classes:

Functions:

  • vinverse

    A simple but effective script to remove residual combing. Based on an AviSynth script by Didée.

FixInterlacedFades

Bases: CustomIntEnum

Methods:

  • __call__

    Give a mathematically perfect solution to decombing fades made after telecine

Attributes:

  • AVERAGE

    Adjust the average of each field to color.

  • MATCH

    Match to the field closest to color.

AVERAGE class-attribute instance-attribute

AVERAGE = 0

Adjust the average of each field to color.

MATCH class-attribute instance-attribute

MATCH = 1

Match to the field closest to color.

__call__

__call__(
    clip: VideoNode,
    color: float | Sequence[float] = 0.0,
    planes: PlanesT = None,
) -> ConstantFormatVideoNode

Give a mathematically perfect solution to decombing fades made after telecine (which made perfect IVTC impossible) that start or end in a solid color.

Steps between the frames are not adjusted, so they will remain uneven depending on the telecine pattern, but the decombing is blur-free, ensuring minimum information loss. However, this may cause small amounts of combing to remain due to error amplification, especially near the solid-color end of the fade.

This is an improved version of the Fix-Telecined-Fades plugin.

Make sure to run this after IVTC!

Parameters:

  • clip

    (VideoNode) –

    Clip to process.

  • color

    (float | Sequence[float], default: 0.0 ) –

    Fade source/target color (floating-point plane averages). Accepts a single float or a sequence of floats to control the color per plane.

Returns:

  • ConstantFormatVideoNode

    Clip with fades to/from color accurately deinterlaced. Frames that don't contain such fades may be

  • ConstantFormatVideoNode

    damaged.

Source code
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
def __call__(
    self, clip: vs.VideoNode, color: float | Sequence[float] = 0.0, planes: PlanesT = None
) -> ConstantFormatVideoNode:
    """
    Give a mathematically perfect solution to decombing fades made *after* telecine
    (which made perfect IVTC impossible) that start or end in a solid color.

    Steps between the frames are not adjusted, so they will remain uneven depending on the telecine pattern,
    but the decombing is blur-free, ensuring minimum information loss. However, this may cause small amounts
    of combing to remain due to error amplification, especially near the solid-color end of the fade.

    This is an improved version of the Fix-Telecined-Fades plugin.

    Make sure to run this *after* IVTC!

    Args:
        clip: Clip to process.
        color: Fade source/target color (floating-point plane averages). Accepts a single float or a sequence of
            floats to control the color per plane.

    Returns:
        Clip with fades to/from `color` accurately deinterlaced. Frames that don't contain such fades may be
        damaged.
    """

    func = FunctionUtil(clip, self.__class__, planes, vs.YUV, 32)

    fields = limiter(func.work_clip).std.SeparateFields(tff=True)

    fields = norm_expr(fields, "x {color} - abs", planes, color=color, func=self.__class__)
    for i in func.norm_planes:
        fields = fields.std.PlaneStats(None, i, f"P{i}")

    props_clip = core.akarin.PropExpr(
        [func.work_clip, fields[::2], fields[1::2]],
        lambda: {f"f{f}Avg{i}": f"{c}.P{i}Average" for f, c in zip("tb", "yz") for i in func.norm_planes},
    )

    expr = (
        "Y 2 % x.fbAvg{i} x.ftAvg{i} ? AVG! "
        "AVG@ x {color} - x.ftAvg{i} x.fbAvg{i} {expr_mode} AVG@ / * {color} + x ?"
    )

    fix = norm_expr(
        props_clip,
        expr,
        planes,
        i=func.norm_planes,
        color=color,
        expr_mode="+ 2 /" if self == self.AVERAGE else "min",
        func=self.__class__,
    )

    return func.return_clip(fix)

InterpolateOverlay

Bases: CustomEnum

Methods:

  • __call__

    Virtually oversamples the video to 120 fps with motion interpolation on credits only, and decimates to 24 fps.

Attributes:

DEC_TXT60 class-attribute instance-attribute

DEC_TXT60 = (10, (6, 4, 2, 0, 8))

For 60i overlaid on top of 24d.

IVTC_TXT30 class-attribute instance-attribute

IVTC_TXT30 = (9, (5, 13, 21, 29, 37))

For 30p overlaid on top of 24t.

IVTC_TXT60 class-attribute instance-attribute

IVTC_TXT60 = (10, (4, 2, 0, 8, 6))

For 60i overlaid on top of 24t.

__call__

__call__(
    clip: VideoNode,
    pattern: int,
    vectors: MotionVectors | None = None,
    preset: MVToolsPreset = HQ_COHERENCE,
    blksize: int | tuple[int, int] = 8,
    refine: int = 1,
    thsad_recalc: int | None = None,
    export_globals: Literal[False] = ...,
) -> ConstantFormatVideoNode
__call__(
    clip: VideoNode,
    pattern: int,
    vectors: MotionVectors | None = None,
    preset: MVToolsPreset = HQ_COHERENCE,
    blksize: int | tuple[int, int] = 8,
    refine: int = 1,
    thsad_recalc: int | None = None,
    export_globals: Literal[True] = ...,
) -> tuple[ConstantFormatVideoNode, MVTools]
__call__(
    clip: VideoNode,
    pattern: int,
    vectors: MotionVectors | None = None,
    preset: MVToolsPreset = HQ_COHERENCE,
    blksize: int | tuple[int, int] = 8,
    refine: int = 1,
    thsad_recalc: int | None = None,
    export_globals: bool = ...,
) -> ConstantFormatVideoNode | tuple[ConstantFormatVideoNode, MVTools]
__call__(
    clip: VideoNode,
    pattern: int,
    vectors: MotionVectors | None = None,
    preset: MVToolsPreset = HQ_COHERENCE,
    blksize: int | tuple[int, int] = 8,
    refine: int = 1,
    thsad_recalc: int | None = None,
    export_globals: bool = False,
) -> ConstantFormatVideoNode | tuple[ConstantFormatVideoNode, MVTools]

Virtually oversamples the video to 120 fps with motion interpolation on credits only, and decimates to 24 fps. Requires manually specifying the 3:2 pulldown pattern (the clip must be split into parts if it changes).

Parameters:

  • clip

    (VideoNode) –

    Bob-deinterlaced clip.

  • vectors

    (MotionVectors | None, default: None ) –

    Motion vectors to use.

  • pattern

    (int) –

    First frame of any clean-combed-combed-clean-clean sequence.

  • preset

    (MVToolsPreset, default: HQ_COHERENCE ) –

    MVTools preset defining base values for the MVTools object. Default is HQ_COHERENCE.

  • blksize

    (int | tuple[int, int], default: 8 ) –

    Size of a block. Larger blocks are less sensitive to noise, are faster, but also less accurate.

  • refine

    (int, default: 1 ) –

    Number of times to recalculate motion vectors with halved block size.

  • thsad_recalc

    (int | None, default: None ) –

    Only bad quality new vectors with a SAD above this will be re-estimated by search. thsad value is scaled to 8x8 block size.

  • export_globals

    (bool, default: False ) –

    Whether to return the MVTools object.

Returns:

  • ConstantFormatVideoNode | tuple[ConstantFormatVideoNode, MVTools]

    Decimated clip with text resampled down to 24p.

Source code
 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
132
133
134
135
136
137
138
def __call__(
    self,
    clip: vs.VideoNode,
    pattern: int,
    vectors: MotionVectors | None = None,
    preset: MVToolsPreset = MVToolsPreset.HQ_COHERENCE,
    blksize: int | tuple[int, int] = 8,
    refine: int = 1,
    thsad_recalc: int | None = None,
    export_globals: bool = False,
) -> ConstantFormatVideoNode | tuple[ConstantFormatVideoNode, MVTools]:
    """
    Virtually oversamples the video to 120 fps with motion interpolation on credits only, and decimates to 24 fps.
    Requires manually specifying the 3:2 pulldown pattern (the clip must be split into parts if it changes).

    Args:
        clip: Bob-deinterlaced clip.
        vectors: Motion vectors to use.
        pattern: First frame of any clean-combed-combed-clean-clean sequence.
        preset: MVTools preset defining base values for the MVTools object. Default is HQ_COHERENCE.
        blksize: Size of a block. Larger blocks are less sensitive to noise, are faster, but also less accurate.
        refine: Number of times to recalculate motion vectors with halved block size.
        thsad_recalc: Only bad quality new vectors with a SAD above this will be re-estimated by search. thsad value
            is scaled to 8x8 block size.
        export_globals: Whether to return the MVTools object.

    Returns:
        Decimated clip with text resampled down to 24p.
    """

    def _floor_div_tuple(x: tuple[int, int]) -> tuple[int, int]:
        return x[0] // 2, x[1] // 2

    assert check_variable(clip, self.__class__)

    step, lookup = self.value
    offset = lookup[pattern % 5]
    offsets = [(offset + i * step) % 40 for i in range(4)]

    blksize = blksize if isinstance(blksize, tuple) else (blksize, blksize)

    mv = MVTools(clip, vectors=vectors, **preset | KwargsT(search_clip=partial(prefilter_to_full_range, slope=1)))

    if not vectors:
        mv.analyze(tr=1, blksize=blksize, overlap=_floor_div_tuple(blksize))

        for _ in range(refine):
            blksize = _floor_div_tuple(blksize)
            overlap = _floor_div_tuple(blksize)

        mv.recalculate(thsad=thsad_recalc, blksize=blksize, overlap=overlap)

    comp = mv.flow_interpolate(multi=4)
    fixed = core.std.SelectEvery(comp, 40, sorted(offsets))

    return (fixed, mv) if export_globals else fixed

vinverse

vinverse(
    clip: VideoNode,
    comb_blur: VSFunctionKwArgs[VideoNode, VideoNode] | VideoNode = partial(
        sbr, mode=VERTICAL
    ),
    contra_blur: VSFunctionKwArgs[VideoNode, VideoNode] | VideoNode = BINOMIAL(
        mode=VERTICAL
    ),
    contra_str: float = 2.7,
    amnt: int | float | None = None,
    scl: float = 0.25,
    thr: int | float = 0,
    planes: PlanesT = None,
) -> ConstantFormatVideoNode

A simple but effective script to remove residual combing. Based on an AviSynth script by Didée.

Parameters:

  • clip

    (VideoNode) –

    Clip to process.

  • comb_blur

    (VSFunctionKwArgs[VideoNode, VideoNode] | VideoNode, default: partial(sbr, mode=VERTICAL) ) –

    Filter used to remove combing.

  • contra_blur

    (VSFunctionKwArgs[VideoNode, VideoNode] | VideoNode, default: BINOMIAL(mode=VERTICAL) ) –

    Filter used to calculate contra sharpening.

  • contra_str

    (float, default: 2.7 ) –

    Strength of contra sharpening.

  • amnt

    (int | float | None, default: None ) –

    Change no pixel by more than this in 8bit.

  • thr

    (int | float, default: 0 ) –

    Skip processing if abs(clip - comb_blur(clip)) < thr

  • scl

    (float, default: 0.25 ) –

    Scale factor for vshrpD * vblurD < 0.

Source code
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
def vinverse(
    clip: vs.VideoNode,
    comb_blur: VSFunctionKwArgs[vs.VideoNode, vs.VideoNode] | vs.VideoNode = partial(sbr, mode=ConvMode.VERTICAL),
    contra_blur: VSFunctionKwArgs[vs.VideoNode, vs.VideoNode] | vs.VideoNode = BlurMatrix.BINOMIAL(
        mode=ConvMode.VERTICAL
    ),
    contra_str: float = 2.7,
    amnt: int | float | None = None,
    scl: float = 0.25,
    thr: int | float = 0,
    planes: PlanesT = None,
) -> ConstantFormatVideoNode:
    """
    A simple but effective script to remove residual combing. Based on an AviSynth script by Didée.

    Args:
        clip: Clip to process.
        comb_blur: Filter used to remove combing.
        contra_blur: Filter used to calculate contra sharpening.
        contra_str: Strength of contra sharpening.
        amnt: Change no pixel by more than this in 8bit.
        thr: Skip processing if abs(clip - comb_blur(clip)) < thr
        scl: Scale factor for vshrpD * vblurD < 0.
    """

    blurred = comb_blur(clip, planes=planes) if callable(comb_blur) else comb_blur

    blurred2 = contra_blur(blurred, planes=planes) if callable(contra_blur) else contra_blur

    assert check_variable(clip, vinverse)
    assert check_variable(blurred, vinverse)
    assert check_variable(blurred2, vinverse)

    FormatsMismatchError.check(vinverse, clip, blurred, blurred2)

    expr = (
        "x y - D1! D1@ abs D1A! D1A@ {thr} < x y z - {sstr} * D2! D1A@ D2@ abs < D1@ D2@ ? D3! "
        "D1@ D2@ xor D3@ {scl} * D3@ ? y + "
    )

    if amnt is not None:
        expr += "x {amnt} - x {amnt} + clip "
        amnt = scale_delta(amnt, 8, clip)

    return norm_expr(
        [clip, blurred, blurred2],
        f"{expr} ?",
        planes,
        sstr=contra_str,
        amnt=amnt,
        scl=scl,
        thr=scale_delta(thr, 8, clip),
        func=vinverse,
    )