Skip to content

postprocess

Functions:

  • decrease_size

    Forcibly reduce the required bitrate to encode a clip by blurring away noise and grain

decrease_size

decrease_size(
    clip: VideoNode,
    sigmaS: float = 10.0,
    sigmaR: float = 0.009,
    min_in: int = 180,
    max_in: int = 230,
    gamma: float = 1.0,
    mask: VideoNode | tuple[float, float] | tuple[float, float, EdgeDetectT] = (
        0.0496,
        0.125,
        FDoGTCanny,
    ),
    prefilter: bool | tuple[int, int] | float = True,
    planes: PlanesT = None,
    show_mask: bool = False,
    **kwargs: Any
) -> VideoNode

Forcibly reduce the required bitrate to encode a clip by blurring away noise and grain in areas they won't be visible in.

Grain and noise in really bright areas can be incredibly hard to spot for even experienced encoders, and will eat up a lot of extra bitrate. As this grain is invisible, there's little reason to go out of your way to better preserve it, and aq-modes like aq-mode 3 already incentivize the encoder to spend more bits in darker areas anyway.

A gradient mask is used internally to prevent "hard edges" from forming on the boundaries of the mask. Additionally, an edgemask is used to prevent clearly-defined detail from being blurred away.

Parameters:

  • clip

    (VideoNode) –

    Clip to process.

  • sigmaS

    (float, default: 10.0 ) –

    Sigma of Gaussian function to calculate spatial weight. See the vsrgtools.bilateral documentation for more information. Default: 10.0.

  • sigmaR

    (float, default: 0.009 ) –

    Sigma of Gaussian function to calculate range weight. See the vsrgtools.bilateral documentation for more information. Default: 0.009.

  • min_in

    (int, default: 180 ) –

    Starting pixel value for the gradient mask. Must be a value between 0–255. Low values are not recommended, as this will start to blur a lot more detail. Default: 180.

  • max_in

    (int, default: 230 ) –

    Ending pixel value for the gradient mask. Must be a value between 0–255. This value must be greater than min_in. Any pixel values above this will be fully masked. Default: 230.

  • mask

    (VideoNode | tuple[float, float] | tuple[float, float, EdgeDetectT], default: (0.0496, 0.125, FDoGTCanny) ) –

    Mask node for masking out details from the blur.

  • prefilter

    (bool | tuple[int, int] | float, default: True ) –

    Prefilter the clip prior to masked blurring. If you pass a float, a gauss blur will be used with the value determining its sigma. If you pass a tuple of floats, a box blur will be used. The first value is the radii, and the second is the number of passes. If you pass True, it defaults to box_blur(2, 4). Set False to disable. Default: True.

  • planes

    (PlanesT, default: None ) –

    Planes to process. If None, all planes. Default: None.

  • show_mask

    (bool, default: False ) –

    Return the gradient mask clip. Default: False.

  • kwargs

    (Any, default: {} ) –

    Additional keyword arguments to pass to bilateral.

Returns:

  • VideoNode

    Clip with the brightest areas, as defined by the gradient mask, heavily blurred.

Raises:

  • IndexError

    min_in is greater than max_in.

  • InvalidColorFamilyError

    Input clip is not a YUV clip.

  • InvalidColorFamilyError

    A VideoNode is passed to mask and the clip is not a GRAY clip.

Source code
 19
 20
 21
 22
 23
 24
 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
 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
def decrease_size(
    clip: vs.VideoNode, sigmaS: float = 10.0, sigmaR: float = 0.009,
    min_in: int = 180, max_in: int = 230, gamma: float = 1.0,
    mask: vs.VideoNode | tuple[float, float] | tuple[float, float, EdgeDetectT] = (0.0496, 0.125, FDoGTCanny),
    prefilter: bool | tuple[int, int] | float = True, planes: PlanesT = None, show_mask: bool = False, **kwargs: Any
) -> vs.VideoNode:
    """
    Forcibly reduce the required bitrate to encode a clip by blurring away noise and grain
    in areas they won't be visible in.

    Grain and noise in really bright areas can be incredibly hard to spot for even experienced encoders,
    and will eat up a lot of extra bitrate. As this grain is invisible, there's little reason
    to go out of your way to better preserve it, and aq-modes like aq-mode 3 already incentivize
    the encoder to spend more bits in darker areas anyway.

    A gradient mask is used internally to prevent "hard edges" from forming on the boundaries of the mask.
    Additionally, an edgemask is used to prevent clearly-defined detail from being blurred away.

    :param clip:        Clip to process.
    :param sigmaS:      Sigma of Gaussian function to calculate spatial weight.
                        See the `vsrgtools.bilateral` documentation for more information.
                        Default: 10.0.
    :param sigmaR:      Sigma of Gaussian function to calculate range weight.
                        See the `vsrgtools.bilateral` documentation for more information.
                        Default: 0.009.
    :param min_in:      Starting pixel value for the gradient mask. Must be a value between 0–255.
                        Low values are not recommended, as this will start to blur a lot more detail.
                        Default: 180.
    :param max_in:      Ending pixel value for the gradient mask. Must be a value between 0–255.
                        This value must be greater than `min_in`. Any pixel values above this will be fully masked.
                        Default: 230.
    :param mask:        Mask node for masking out details from the blur.
    :param prefilter:   Prefilter the clip prior to masked blurring.

                        If you pass a float, a gauss blur will be used with the value determining its sigma.
                        If you pass a tuple of floats, a box blur will be used.
                        The first value is the radii, and the second is the number of passes.
                        If you pass `True`, it defaults to `box_blur(2, 4)`.
                        Set `False` to disable.

                        Default: True.
    :param planes:      Planes to process. If None, all planes. Default: None.
    :param show_mask:   Return the gradient mask clip. Default: False.
    :param kwargs:      Additional keyword arguments to pass to bilateral.

    :return:            Clip with the brightest areas, as defined by the gradient mask, heavily blurred.

    :raises IndexError:                 `min_in` is greater than `max_in`.
    :raises InvalidColorFamilyError:    Input clip is not a YUV clip.
    :raises InvalidColorFamilyError:    A VideoNode is passed to `mask` and the clip is not a GRAY clip.
    """
    assert check_variable(clip, decrease_size)

    if min_in > max_in:
        raise CustomIndexError('The blur min must be lower than max!', decrease_size, dict(min=min_in, max=max_in))

    InvalidColorFamilyError.check(clip, vs.YUV, decrease_size)

    planes = normalize_planes(clip, planes)

    pre = get_y(clip)

    if isinstance(mask, vs.VideoNode):
        InvalidColorFamilyError.check(mask, vs.GRAY, decrease_size)
        check_ref_clip(pre, mask)
    else:
        pm_min, pm_max, *emask = mask

        if pm_min > pm_max:
            raise CustomIndexError('The mask min must be lower than max!', decrease_size, dict(min=pm_min, max=pm_max))

        pm_min = scale_mask(pm_min, 32, clip)
        pm_max = scale_mask(pm_max, 32, clip)

        yuv444 = Bilinear.resample(
            range_mask(clip, rad=3, radc=2), clip.format.replace(subsampling_h=0, subsampling_w=0)
        )

        if emask:
            mask = EdgeDetect.ensure_obj(emask[0]).edgemask(pre)
        else:
            mask = FDoGTCanny.edgemask(pre)

        mask = mask.std.Maximum().std.Minimum()

        mask_planes = flatten_vnodes(yuv444, mask, split_planes=True)

        mask = norm_expr(
            mask_planes, f'x y max z max {pm_min} < 0 {ExprToken.RangeMax} ? a max {pm_max} < 0 {ExprToken.RangeMax} ?',
            func=decrease_size
        )

        mask = box_blur(mask, 1, 2)

    if prefilter is True:
        prefilter = (2, 4)

    if prefilter:
        if isinstance(prefilter, tuple):
            pre = box_blur(pre, *prefilter)
        else:
            pre = gauss_blur(pre, prefilter)

    minf = scale_value(min_in, 8, pre)
    maxf = scale_value(max_in, 8, pre)

    mask = norm_expr(
        [pre, mask],
        f'x {ExprOp.clamp(minf, maxf)} {minf} - {maxf} {minf} - / {1 / gamma} '
        f'pow {ExprOp.clamp(0, 1)} {ExprToken.RangeMax} * y -', planes,
        func=decrease_size
    )

    if show_mask:
        return mask

    denoise = bilateral(clip, sigmaS=sigmaS, sigmaR=sigmaR, **kwargs)

    return clip.std.MaskedMerge(denoise, mask, planes)