lvsfunc.scale

(De)scaling and conversion functions and wrappers.

lvsfunc.scale.comparative_descale(clip[, ...])

Easy wrapper to descale to SharpBicubic and an additional kernel, compare them, and then pick one or the other.

lvsfunc.scale.comparative_restore(clip[, ...])

Companion function to go with comparative_descale to reupscale the clip for descale detail masking.

lvsfunc.scale.descale(clip[, upscaler, ...])

A unified descaling function.

lvsfunc.scale.descale_detail_mask([clip, ...])

Generate a detail mask given a clip and a clip rescaled with the same kernel.

lvsfunc.scale.gamma2linear(clip, curve[, ...])

Converts from gamma to linear.

lvsfunc.scale.linear2gamma(clip, curve[, ...])

Converts from linear to gamma.

lvsfunc.scale.mixed_rescale(clip[, width, ...])

InsaneAA rewrite to be a much saner and easier to read function.

lvsfunc.scale.reupscale([clip, width, ...])

A quick 'n easy wrapper used to re-upscale a clip descaled with descale using znedi3.

lvsfunc.scale.ssim_downsample(clip[, width, ...])

muvsfunc.SSIM_downsample rewrite taken from a Vardë gist.

lvsfunc.scale.comparative_descale(clip, width=None, height=720, kernel=None, thr=5e-08)

Easy wrapper to descale to SharpBicubic and an additional kernel, compare them, and then pick one or the other.

The output clip has props that can be used to frameeval specific kernels by the user.

Parameters
  • clip (VideoNode) – Input clip

  • width (Optional[int]) – Width to descale to (if None, auto-calculated)

  • height (int) – Descale height

  • kernel (Union[Kernel, str, None]) – Kernel to compare BicubicSharp to. This can also be a string (Default: Spline36 if None).

  • thr (float) – Threshold for which kernel to pick

Return type

VideoNode

Returns

Descaled clip

lvsfunc.scale.comparative_restore(clip, width=None, height=720, kernel=None)

Companion function to go with comparative_descale to reupscale the clip for descale detail masking.

Parameters
  • clip (VideoNode) – Input clip

  • width (Optional[int]) – Width to upscale to (if None, auto-calculated)

  • height (int) – Upscale height

  • kernel (Union[Kernel, str, None]) – Kernel to compare BicubicSharp to (Default: Spline36 if None)

Return type

VideoNode

Returns

Reupscaled clip

lvsfunc.scale.descale(clip, upscaler=<function reupscale>, width=None, height=720, kernel=<lvsfunc.kernels.Bicubic object>, threshold=0.0, mask=<function descale_detail_mask>, src_left=0.0, src_top=0.0, show_mask=False)

A unified descaling function. Includes support for handling fractional resolutions (experimental), multiple resolutions, detail masking, and conditional scaling.

If you want to descale to a fractional resolution, set src_left and src_top and round up the target height.

If the source has multiple native resolutions, specify height as a list.

If you want to conditionally descale, specify a non-zero threshold.

Dependencies:

  • vapoursynth-descale

  • znedi3

Parameters
  • clip (VideoNode) – Clip to descale

  • upscaler (Optional[Callable[[VideoNode, int, int], VideoNode]]) – Callable function with signature upscaler(clip, width, height) -> vs.VideoNode to be used for reupscaling. Must be capable of handling variable res clips for multiple heights and conditional scaling. If a single height is given and upscaler is None, a constant resolution GRAY clip will be returned instead. Note that if upscaler is None, no upscaling will be performed and neither detail masking nor proper fractional descaling can be performed. (Default: lvsfunc.scale.reupscale())

  • width (Union[int, List[int], None]) – Width(s) to descale to (if None, auto-calculated)

  • height (Union[int, List[int]]) – Height(s) to descale to. List indicates multiple resolutions, the function will determine the best. (Default: 720)

  • kernel (Kernel | str) – Kernel used to descale the clip. This can also be a string. (see lvsfunc.kernels.Kernel, (Default: kernels.Bicubic(b=0, c=1/2))

  • threshold (float) – Error threshold for conditional descaling (Default: 0.0, always descale)

  • mask (Union[Callable[[VideoNode, VideoNode], VideoNode], VideoNode, None]) – Function or mask clip used to mask detail. If None, no masking. Function must accept a clip and a reupscaled clip and return a mask. (Default: lvsfunc.scale.descale_detail_mask())

  • src_left (float) – Horizontal shifting for fractional resolutions (Default: 0.0)

  • src_top (float) – Vertical shifting for fractional resolutions (Default: 0.0)

  • show_mask (bool) – Return detail mask

Return type

VideoNode

Returns

Descaled and re-upscaled clip with float bitdepth

lvsfunc.scale.descale_detail_mask(clip: vs.VideoNode = '__no__default__', rescaled_clip: vs.VideoNode = '__no__default__', threshold: float = 0.05) vs.VideoNode

Generate a detail mask given a clip and a clip rescaled with the same kernel.

Function is curried to allow parameter tuning when passing to lvsfunc.scale.descale()

Parameters
  • clip – Original clip

  • rescaled_clip – Clip downscaled and reupscaled using the same kernel

  • threshold – Binarization threshold for mask (Default: 0.05)

Returns

Mask of lost detail

lvsfunc.scale.gamma2linear(clip, curve, gcor=1.0, sigmoid=False, thr=0.5, cont=6.5, epsilon=1e-06)

Converts from gamma to linear.

Return type

VideoNode

lvsfunc.scale.linear2gamma(clip, curve, gcor=1.0, sigmoid=False, thr=0.5, cont=6.5)

Converts from linear to gamma.

Return type

VideoNode

lvsfunc.scale.mixed_rescale(clip, width=None, height=720, kernel=<lvsfunc.kernels.Bicubic object>, downscaler=<function ssim_downsample>, credit_mask=<function descale_detail_mask>, mask_thr=0.05, mix_strength=0.25, show_mask=False, nnedi3_args={}, eedi3_args={})

InsaneAA rewrite to be a much saner and easier to read function. Descales and downscales the given clip and merges them together with a set strength.

This can be useful for dealing with a source that you can’t accurately descale, but you still want to force it. Not recommended to use it on everything, however.

A string can be passed instead of a Kernel object if you want to use that. This gives you access to every kernel object in lvsfunc.kernels(). For more information on what every kernel does, please refer to their documentation.

This is still a work in progress at the time of writing. Please use with care.

Parameters
  • clip (VideoNode) – Input clip.

  • width (Optional[int]) – Upscale width. If None, determine from height (default: None).

  • height (int) – Upscale height (Default: 720).

  • kernel (Kernel | str) – Kernel used to descale the clip. This can also be a string. (see lvsfunc.kernels.Kernel, Default: kernels.Bicubic(b=0, c=1/2) (Catrom)).

  • downscaler (Union[Callable[[VideoNode, int, int], VideoNode], Kernel, str]) – Kernel or custom scaler used to downscale the clip. This can also be a string. (see lvsfunc.kernels.Kernel, Default: ssim_downsample).

  • credit_mask (Union[Callable[[VideoNode, VideoNode], VideoNode], VideoNode, None]) – Function or mask clip used to mask detail. If None, no masking. Function must accept a clip and a reupscaled clip and return a mask. (Default: lvsfunc.scale.descale_detail_mask()).

  • mask_thr (float) – Binarization threshold for lvsfunc.scale.descale_detail_mask() (Default: 0.05).

  • mix_strength (float) – Merging strength between the descaled and downscaled clip. Stronger values will make the lineart look closer to the downscaled clip. This can get pretty dangerous very quickly if you use a sharp downscaler!

  • show_mask (bool | int) – Return the credit_mask. If set to 2, it will return the lineart mask instead.

  • nnedi3_args (Dict[str, Any]) – Additional args to pass to nnedi3.

  • eedi3_args (Dict[str, Any]) – Additional args to pass to eedi3.

Return type

VideoNode

Returns

Rescaled clip with a downscaled clip merged with it and credits masked.

lvsfunc.scale.reupscale(clip: vs.VideoNode = '__no__default__', width: int | None = None, height: int = 1080, kernel: Kernel | str = <lvsfunc.kernels.Bicubic object>, **kwargs: Any) vs.VideoNode

A quick ‘n easy wrapper used to re-upscale a clip descaled with descale using znedi3.

Function is curried to allow parameter tuning when passing to lvsfunc.scale.descale()

Stolen from Varde with some adjustments made.

Dependencies:

  • znedi3

Parameters
  • clip – Input clip

  • width – Upscale width. If None, determine from height assuming 16:9 aspect ratio (Default: None)

  • height – Upscale height (Default: 1080)

  • kernel – Kernel used to downscale the doubled clip (see lvsfunc.kernels.Kernel, Default: kernels.Bicubic(b=0, c=1/2))

  • kwargs – Arguments passed to znedi3 (Default: nsize=4, nns=4, qual=2, pscrn=2)

Returns

Reupscaled clip

lvsfunc.scale.ssim_downsample(clip, width=None, height=720, smooth=0.816496580927726, kernel=<lvsfunc.kernels.Catrom object>, gamma=False, curve=None, sigmoid=False, epsilon=1e-06)

muvsfunc.SSIM_downsample rewrite taken from a Vardë gist. Unlike muvsfunc’s implementation, this function also works in float and does not use nnedi3_resample. Most of the documentation is taken from muvsfunc.

SSIM downsampler is an image downscaling technique that aims to optimize for the perceptual quality of the downscaled results. Image downscaling is considered as an optimization problem where the difference between the input and output images is measured using famous Structural SIMilarity (SSIM) index. The solution is derived in closed-form, which leads to the simple, efficient implementation. The downscaled images retain perceptually important features and details, resulting in an accurate and spatio-temporally consistent representation of the high resolution input.

Original gist

Parameters
  • clip (VideoNode) – Input clip.

  • width (Optional[int]) – Output width. If None, autocalculates using height.

  • height (int) – Output height (default: 720).

  • smooth (float | VSFunction) – Image smoothening method. If you pass an int, it specifies the “radius” of the internally-used boxfilter, i.e. the window has a size of (2*smooth+1)x(2*smooth+1). If you pass a float, it specifies the “sigma” of core.tcanny.TCanny, i.e. the standard deviation of gaussian blur. If you pass a function, it acts as a general smoother. Default uses a gaussian blur.

  • curve (Optional[Literal[vapoursynth.TransferCharacteristics.TRANSFER_IEC_61966_2_1, vapoursynth.TransferCharacteristics.TRANSFER_BT709, vapoursynth.TransferCharacteristics.TRANSFER_BT601, vapoursynth.TransferCharacteristics.TRANSFER_ST240_M, vapoursynth.TransferCharacteristics.TRANSFER_BT2020_10, vapoursynth.TransferCharacteristics.TRANSFER_BT2020_12]]) – Gamma mapping. Will auto-determine based on the input props or resolution. Can be forced with for example curve=vs.TransferCharacteristics.TRANSFER_BT709.

  • gamma (bool) – Perform a gamma conversion prior to scaling and after scaling. This function MUST be set to True for sigmoid and epsilon to function.

  • sigmoid (bool) – When True, applies a sigmoidal curve after the power-like curve (or before when converting from linear to gamma-corrected). This helps reduce the dark halo artefacts found around sharp edges caused by resizing in linear luminance. This parameter only works if gamma=True.

  • epsilon (float) – Machine epsilon. This parameter only works if gamma=True.

Return type

VideoNode

Returns

Downsampled clip.