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: CustomEnum

Methods:

  • __call__

    Give a mathematically perfect solution to decombing fades made after telecine

Attributes:

Average class-attribute instance-attribute

Brighten class-attribute instance-attribute

Brighten: FixInterlacedFades = object()

Darken class-attribute instance-attribute

__call__

__call__(
    clip: VideoNode,
    colors: float | list[float] | PlanesT = 0.0,
    planes: PlanesT = None,
    func: FuncExceptT | None = None,
) -> VideoNode

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.

  • colors

    (float | list[float] | PlanesT, default: 0.0 ) –

    Fade source/target color (floating-point plane averages).

Returns:

  • VideoNode

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

Source code
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
def __call__(
    self, clip: vs.VideoNode, colors: float | list[float] | PlanesT = 0.0,
    planes: PlanesT = None, func: FuncExceptT | None = None
) -> vs.VideoNode:
    """
    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!

    :param clip:                            Clip to process.
    :param colors:                          Fade source/target color (floating-point plane averages).

    :return:                                Clip with fades to/from `colors` accurately deinterlaced.
                                            Frames that don't contain such fades may be damaged.
    """
    func = func or self.__class__

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

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

    for i in f.norm_planes:
        fields = fields.std.PlaneStats(None, i, f'P{i}')

    props_clip = core.akarin.PropExpr(
        [f.work_clip, fields[::2], fields[1::2]], lambda: {  # type: ignore[misc]
            f'f{t}Avg{i}': f'{c}.P{i}Average {color} -'  # type: ignore[has-type]
            for t, c in ['ty', 'bz']
            for i, color in zip(f.norm_planes, f.norm_seq(colors, 0))
        }
    )

    expr_mode, expr_mode_chroma = (
        ('min', '<') if self == self.Darken else ('max', '>') if self == self.Brighten else ('+ 2 /', '+ 2 /')
    )

    expr_header = 'Y 2 % x.fbAvg{i} x.ftAvg{i} ? AVG! AVG@ 0 = x x {color} - '
    expr_footer = ' AVG@ / * {color} + ?'

    expr_luma = expr_header + 'x.ftAvg{i} x.fbAvg{i} {expr_mode}' + expr_footer
    expr_chroma = expr_luma if self == self.Average else (
        expr_header + 'x.ftAvg{i} abs x.fbAvg{i} abs {expr_mode} x.ftAvg{i} x.fbAvg{i} ?' + expr_footer
    )

    fix = norm_expr(
        props_clip, (expr_luma, expr_chroma),
        planes, i=f.norm_planes, color=colors,
        expr_mode=(expr_mode, expr_mode_chroma),
        func=func
    )

    return f.return_clip(fix)

telop_resample

Bases: CustomIntEnum

Methods:

  • __call__

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

Attributes:

TXT30p_on_24telecined class-attribute instance-attribute

TXT30p_on_24telecined = 2

TXT60i_on_24duped class-attribute instance-attribute

TXT60i_on_24duped = 1

TXT60i_on_24telecined class-attribute instance-attribute

TXT60i_on_24telecined = 0

__call__

__call__(bobbed_clip: VideoNode, pattern: int, **mv_args: Any) -> VideoNode

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:

  • bobbed_clip

    (VideoNode) –

    Bobbed clip. Framerate must be 60000/1001.

  • pattern

    (int) –

    First frame in the pattern.

  • mv_args

    (Any, default: {} ) –

    Arguments to pass on to MVTools, used for motion compensation.

Returns:

  • VideoNode

    Decimated clip with text resampled down to 24p.

Raises:

  • InvalidFramerateError

    Bobbed clip does not have a framerate of 60000/1001 (59.94)

Source code
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 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
120
121
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
def __call__(self, bobbed_clip: vs.VideoNode, pattern: int, **mv_args: Any) -> vs.VideoNode:
    """
    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).

    :param bobbed_clip:             Bobbed clip. Framerate must be 60000/1001.
    :param pattern:                 First frame in the pattern.
    :param mv_args:                 Arguments to pass on to MVTools, used for motion compensation.

    :return:                        Decimated clip with text resampled down to 24p.

    :raises InvalidFramerateError:  Bobbed clip does not have a framerate of 60000/1001 (59.94)
    """

    assert check_variable(bobbed_clip, telop_resample)

    InvalidFramerateError.check(telop_resample, bobbed_clip, (60000, 1001))

    invpos = (5 - pattern * 2 % 5) % 5

    offset = [0, 0, -1, 1, 1][pattern]
    pattern = [0, 1, 0, 0, 1][pattern]
    direction = [-1, -1, 1, 1, 1][pattern]

    ivtc_fps, ivtc_fps_div = (dict[str, Any](fpsnum=x, fpsden=1001) for x in (24000, 12000))

    pos = []
    assumefps = 0

    interlaced = self in (telop_resample.TXT60i_on_24telecined, telop_resample.TXT60i_on_24duped)
    decimate = self is telop_resample.TXT60i_on_24duped

    cycle = 10 // (1 + interlaced)

    def bb(idx: int, cut: bool = False) -> vs.VideoNode:
        if cut:
            return bobbed_clip[cycle:].std.SelectEvery(cycle, [idx])
        return bobbed_clip.std.SelectEvery(cycle, [idx])

    def intl(clips: list[vs.VideoNode], toreverse: bool, halv: list[int]) -> vs.VideoNode:
        clips = [c[::2] if i in halv else c for i, c in enumerate(clips)]
        if not toreverse:
            clips = list(reversed(clips))
        return core.std.Interleave(clips)

    if interlaced:
        if decimate:
            cleanpos = 4
            pos = [1, 2]

            if invpos > 2:
                pos = [6, 7]
                assumefps = 2
            elif invpos > 1:
                pos = [2, 6]
                assumefps = 1
        else:
            cleanpos = 1
            pos = [3, 4]

            if invpos > 1:
                cleanpos = 6
                assumefps = 1

            if invpos > 3:
                pos = [4, 8]
                assumefps = 1

        clean = bobbed_clip.std.SelectEvery(cycle, [cleanpos - invpos])
        jitter = bobbed_clip.std.SelectEvery(cycle, [p - invpos for p in pos])

        if assumefps:
            jitter = core.std.AssumeFPS(
                bobbed_clip[0] * assumefps + jitter, **(ivtc_fps_div if cleanpos == 6 else ivtc_fps)
            )

        mv = MVTools(jitter, **mv_args)
        mv.analyze()
        comp = mv.flow_interpolate(interleave=False)

        out = intl([*comp, clean], decimate, [0])
        offs = 3 if decimate else 2

        return out[invpos // offs:]

    if pattern == 0:
        c1pos = [0, 2, 7, 5]
        c2pos = [3, 4, 9, 8]

        if offset == -1:
            c1pos = [2, 7, 5, 10]

        if offset == 1:
            c2pos = []
            c2 = core.std.Interleave([bb(4), bb(5), bb(0, True), bb(9)])
    else:
        c1pos = [2, 4, 9, 7]
        c2pos = [0, 1, 6, 5]

        if offset == 1:
            c1pos = []
            c1 = core.std.Interleave([bb(3), bb(5), bb(0, True), bb(8)])

        if offset == -1:
            c2pos = [1, 6, 5, 10]

    if c1pos:
        c1 = bobbed_clip.std.SelectEvery(cycle, [c + offset for c in c1pos])

    if c2pos:
        c2 = bobbed_clip.std.SelectEvery(cycle, [c + offset for c in c2pos])

    if offset == -1:
        c1, c2 = (core.std.AssumeFPS(bobbed_clip[0] + c, **ivtc_fps) for c in (c1, c2))

    mv1 = MVTools(c1, **mv_args)
    mv1.analyze()
    fix1 = mv1.flow_interpolate(time=50 + direction * 25, interleave=False)

    mv2 = MVTools(c2, **mv_args)
    mv2.analyze()
    fix2 = mv2.flow_interpolate(interleave=False)

    return intl([*fix1, *fix2], pattern == 0, [0, 1])

vinverse

vinverse(
    clip: VideoNode,
    comb_blur: GenericVSFunction | VideoNode = partial(sbr, mode=VERTICAL),
    contra_blur: GenericVSFunction | 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,
) -> VideoNode

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

    (GenericVSFunction | VideoNode, default: partial(sbr, mode=VERTICAL) ) –

    Filter used to remove combing.

  • contra_blur

    (GenericVSFunction | 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
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
def vinverse(
    clip: vs.VideoNode,
    comb_blur: GenericVSFunction | vs.VideoNode = partial(sbr, mode=ConvMode.VERTICAL),
    contra_blur: GenericVSFunction | 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
) -> vs.VideoNode:
    """
    A simple but effective script to remove residual combing. Based on an AviSynth script by Didée.

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

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

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

    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
    )