Anime Upscale Script

This was a basic script created to upscale and filter anime footage for use with AMV editing and is not intended to be used on footage for archival purposes. Special thanks to BasharOfTheAges for suggestions and feedback on the script.

There are three strengths to the filter and it is recommend that you use Strength=2 or higher with cel animated anime while using Strength=1 for most newer digitally animated sources. Only extremely old grainy anime should need Strength=3. Another important setting is widesreen, which is set to either true or false, the default is true. The FullHD setting, which is true by default, controls whether the output is 1080p (when set to true) or 720p (when set to false). There is also the ability to crop through this script; cropSD crops at the original resolution, and cropHD crops at the new upscaled resolution. This filter will run very slow as the backbone of it is MDegrain2(), which you can disable by setting Degrain=0, however that is not recommended.

AnimeUpscale (AviSynth Script)

File Name: AnimeUpscale.avsi

Requirements:

  • aWarpSharp2
  • ColorMatrix
  • Flash3kyuu Deband
  • Hysteria
  • Mask Tools 2
  • MVTools2
  • RgTools
#################################################################################################
#
#  AnimeUpscale() by l33tmeatwad
#  v4.4.1 - 6-26-2017
#
#  Parameters:
#       Strength               (Default = 1)
#          -Use strength=1 for basic upscaling.
#          -Use strength=2 for videos with grain or noise.
#          -Use strength=3 for videos with lots of gain or noise.
#       Degrain                (Default = 0)
#       (NOTE: Default is 2 for Strength > 2)
#          -Use Degrain=0 to disable MDegrain2.
#          -Use Degrain=1 for standard degrain.
#          -Use Degrain=2 for double pass standard degrain.
#          -Use Degrain=3 for heavy degrain.
#          -Use Degrain=4 for double pass heavy degrain.
#       FullHD                 (Default = true) Use false for 720p.
#       Widescreen             (Default = true) Use false for fullscreen videos.
#       CropSD                 (Default = "0,0,0,0") Ex. "4,2,-4,-2"
#       CropHD                 (Default = "0,0,0,0") Ex. "4,2,-4,-2"
#       ConvertMatrix           (Default = true) Converts from Color Matrix Rec.601 to Rec.709.
#       ResizeMethod           (Default = "Spline36")
#
#################################################################################################


function mDegrainSimple(clip, int "frames", int "blksize") {
originalvideo = clip

blksize = Default(blksize, 8)
frames  = Default(frames, 1)
overlap = blksize/2

super = MSuper(originalvideo, pel=2, sharp=1)

backward_vec3 = (frames==3) ? MAnalyse(super, isb = true, delta = 3, blksize=blksize, overlap=overlap) : super
backward_vec2 = (frames>=2) ? MAnalyse(super, isb = true, delta = 2, blksize=blksize, overlap=overlap) : super
backward_vec1 = MAnalyse(super, isb = true, delta = 1, blksize=blksize, overlap=overlap)
forward_vec1 = MAnalyse(super, isb = false, delta = 1, blksize=blksize, overlap=overlap)
forward_vec2 = (frames>=2) ? MAnalyse(super, isb = false, delta = 2, blksize=blksize, overlap=overlap) : super
forward_vec3 = (frames==3) ? MAnalyse(super, isb = false, delta = 2, blksize=blksize, overlap=overlap) : super
mvvideo = (frames==3) ? MDegrain3(originalvideo, super, backward_vec1,forward_vec1,backward_vec2,forward_vec2,backward_vec3,forward_vec3,thSAD=400) : (frames==2) ? MDegrain2(originalvideo, super, backward_vec1,forward_vec1,backward_vec2,forward_vec2,thSAD=400) : MDegrain1(originalvideo, super, backward_vec1,forward_vec1,thSAD=400)

return mvvideo
}

function AnimeUpscale(clip videoSD, int "strength", int "degrain", bool "FullHD", bool "Widescreen", string "cropSD", string "cropHD", bool "ConvertMatrix", string "ResizeMethod") {

strength = Default(strength, 1)
FullHD = Default(FullHD, true)
widescreen = Default(widescreen, true)
cropSD = Default(cropSD, "0,0,0,0")
cropHD = Default(cropHD, "0,0,0,0")
ConvertMatrix = Default(ConvertMatrix, true)
ResizeMethod = Default(ResizeMethod, "Spline36")
ResizeMethod = ResizeMethod+"Resize"
degrain = (strength>=3) ? Default(degrain, 2) : (strength==2) ? Default(degrain, 1) : Default(degrain, 0)

#Cropping SD Footage
videoSD = (cropSD!="0,0,0,0") ? Eval( "videoSD.crop(" + cropSD + ")" ) : videoSD

#RemoveGrain
videoSD = (strength>=3) ? videoSD.removegrain(2).flash3kyuu_deband() : videoSD

#Grain and Noise Removal
videoSD = (degrain==1) ? videoSD.mDegrainSimple(frames=2) : (degrain==2) ? videoSD.mDegrainSimple(frames=2).mDegrainSimple(frames=2) : (degrain==3) ? videoSD.mDegrainSimple(frames=2,blksize=4) : (degrain>=4) ? videoSD.mDegrainSimple(frames=2,blksize=4).mDegrainSimple(frames=2,blksize=4) : videoSD

#Upscale the video to HD
Size = (FullHD==true && widescreen==true) ? "(1920,1080)" : (FullHD==true && widescreen==false) ? "(1440,1080)" : (FullHD==false && widescreen==true) ? "(1280,720)" : "(960,720)"
videoHD = Eval("videoSD." + ResizeMethod + Size)

#Sharpening HD
videoHD = (FullHD==true) ? VideoHD.Hysteria(7.0) : (FullHD==false) ? VideoHD.Hysteria(3.0) : VideoHD

#Edge Enhancement
videoHD = (FullHD==true) ? videoHD.awarpsharp(10,2) : videoHD.awarpsharp(8,1)

#Debanding
videoHD = videoHD.flash3kyuu_deband()

#Cropping HD Footage
videoHD = (cropHD!="0,0,0,0") ? Eval( "videoHD.crop(" + cropHD + ")." + ResizeMethod + Size ) : videoHD

#Convert ColorMatrix
VideoHD = (ConvertMatrix==true) ? VideoHD.ColorMatrix(mode="Rec.601->Rec.709") : VideoHD

return VideoHD
}

AnimeUpscale (VapourSynth Script)

File Name: animeupscale.py

Requirements:

  • aWarpSharp2
  • Flash3kyuu Deband
  • Hysteria
  • MVTools2
#################################################################################################
#
#  AnimeUpscale() by l33tmeatwad
#  v4.4.1 - 6-26-2017
#
#  Parameters:
#       Strength               (Default = 1)
#          -Use Strength=1 for basic upscaling.
#          -Use Strength=2 for videos with grain or noise.
#          -Use Strength=3 for videos with lots of gain or noise.
#       Degrain                (Default = 0)
#       (NOTE: Default is 2 for Strength > 2)
#          -Use Degrain=0 to disable MDegrain2.
#          -Use Degrain=1 for standard Degrain.
#          -Use Degrain=2 for double pass standard Degrain.
#          -Use Degrain=3 for heavy Degrain.
#          -Use Degrain=4 for double pass heavy Degrain.
#       FullHD                 (Default = true) Use false for 720p.
#       Widescreen             (Default = true) Use false for fullscreen videos.
#       CropSD                 (Default = "0,0,0,0") Ex. "4,2,-4,-2"
#       CropHD                 (Default = "0,0,0,0") Ex. "4,2,-4,-2"
#       ConvertMatrix           (Default = true) Converts from Color Matrix Rec.601 to Rec.709.
#       ResizeMethod           (Default = "Spline36")
#
#################################################################################################

import vapoursynth as vs

def MDegrainSimple(clip, frames=1, blksize=8):
    core = vs.get_core()

    if not isinstance(clip, vs.VideoNode):
        raise TypeError('MDegrainSimple: This is not a clip')
        
    overlap = int(blksize/2)

    super = core.mv.Super(clip, pel=2, sharp=1)
    if frames >= 3:
        bw3 = core.mv.Analyse(super, isb = True, delta = 3, blksize=blksize, overlap=overlap)
        fw3 = core.mv.Analyse(super, isb = False, delta = 2, blksize=blksize, overlap=overlap)
    if frames == 2:
        bw2 = core.mv.Analyse(super, isb = True, delta = 2, blksize=blksize, overlap=overlap)
        fw2 = core.mv.Analyse(super, isb = False, delta = 2, blksize=blksize, overlap=overlap)

    bw1 = core.mv.Analyse(super, isb = True, delta = 1, blksize=blksize, overlap=overlap)
    fw1 = core.mv.Analyse(super, isb = False, delta = 1, blksize=blksize, overlap=overlap)

    if frames >=3:
        clip = core.mv.Degrain3(clip, super, bw1, fw1, bw2, fw2, bw3, fw3, thsad=400)
    elif frames == 2:
        clip = core.mv.Degrain2(clip, super, bw1, fw1, bw2, fw2, thsad=400)
    else:
        clip = core.mv.Degrain1(clip, super, bw1, fw1, thsad=400)
    return clip


def AnimeUpscale(clip, Strength=1, Degrain=None, FullHD=True, Widescreen=True, CropSD=None, CropHD=None, ConvertMatrix=True, ResizeMethod="Spline36"):
    core = vs.get_core()
    import hysteria as hy
    if Degrain == None:
        if Strength>=3:
            Degrain=2
        elif Strength==2:
            Degrain=1
        else:
            Degrain=0
    
    if ConvertMatrix==True:
        matrix=', matrix_in_s="170m", matrix_s="709"'
    else:
        matrix=''    
    
    #Cropping SD Footage
    if CropSD != None:
        clip = eval('core.std.CropRel(clip,'+CropSD+')')

    #RemoveGrain
    if Strength==3:
        clip = core.rgvs.RemoveGrain(clip,2)
        clip = core.f3kdb.Deband(clip)

    #Grain and Noise Removal    
    if Degrain==1:
        clip = MDegrainSimple(clip,frames=2)
    elif Degrain==2:
        clip = MDegrainSimple(clip,frames=2)
        clip = MDegrainSimple(clip,frames=2)
    elif Degrain==3:
        clip = MDegrainSimple(clip,frames=2,blksize=4)
    elif Degrain==4:
        clip = MDegrainSimple(clip,frames=2,blksize=4)
        clip = MDegrainSimple(clip,frames=2,blksize=4)
    
    #Upscale the video to HD
    if FullHD==True:
        height="1080"
        if Widescreen==True:
            width="1920"
        else:
            width="1440"
    else:
        height="720"
        if Widescreen==True:
            width="1280"
        else:
            width="960"
    clip = eval('core.resize.'+ResizeMethod+'(clip,width='+width+', height='+height+matrix+')')
    
    #Sharpening HD
    if FullHD==True:
        clip = hy.Hysteria(clip,strength=7.0)
    else:
        clip = hy.Hysteria(clip,strength=3.0)
    
    #Edge Enhancement
    if FullHD==True:
        clip = core.warp.AWarpSharp2(clip,depth=10,blur=2)
    else:
        clip = core.warp.AWarpSharp2(clip,depth=8,blur=1)

    #Debanding
    clip = core.f3kdb.Deband(clip)
    
    #Cropping HD Footage
    if CropHD != None:
        clip = eval('core.std.CropRel(clip,'+CropHD+')')
        clip = eval('core.resize.'+ResizeMethod+'(clip,width='+width+', height='+height+')')
    

    return clip




Anime Upscale Classic (Depreciated, 32-bit AviSynth Only)

File Name: AUclassic_v3.5.avsi

WARNING: THIS VERSION IS OUTDATED AND USES SPATIAL SMOOTHING WHICH CAN HEAVILY DEGRADE THE VISUAL QUALITY OF YOUR VIDEO, USE WITH CAUTION!

This was a basic script created to upscale and filter anime footage for use with AMV editing and is not intended to be used on footage for archival purposes. There are four strengths to the filter and it is recommend that you use Strength=2 or higher with cel animated anime while using Strength=1 for most newer digitally animated sources. Only extremely old grainy anime should need Strength=3. Another important setting is widesreen, which is set to either true or false, the default is true. The FullHD setting, which is true by default, controls whether the output is 1080p (when set to true) or 720p (when set to false). There is also the ability to crop through this script; cropSD crops at the original resolution, and cropHD crops at the new upscaled resolution. This filter will run very slow as the backbone of it is MDegrain2(), which you can disable by setting Degrain=0, however that is not recommended.

Requirements:

  • aWarpSharp
  • Deathray
  • Dehalo Alpha
  • eDeen
  • FastLineDarkenMod
  • Flash3kyuu Deband
  • MT Mask Tools
  • MVTools2
  • Repair

Known Issues

  • Unstable and does not work with AviSynth+
  • Deathray is not compatible with most GPUs.
#################################################################################################
#
#  AUclassic() by l33tmeatwad
#  v3.5 - 10-8-2014
#
#  Parameters:
#       Strength               (Default = 2)
#          -Use strength=1 for basic upscaling.
#          -Use strength=2 for videos with mild grain or noise.
#          -Use strength=3 for videos with lots of gain or noise.
#       Degrain                (Default = 1)
#       (NOTE: Default is 3 for Strength=3)
#          -Use Degrain=0 to disable MDegrain2.
#          -Use Degrain=1 for standard degrain.
#          -Use Degrain=2 for double pass standard degrain.
#          -Use Degrain=3 for heavy degrain.
#          -Use Degrain=4 for double pass heavy degrain.
#       FullHD                 (Default = true) Use false for 720p.
#       Widescreen             (Default = true) Use false for fullscreen videos.
#       CropSD                 (Default = "0,0,0,0") Ex. "2,2,-2,-2"
#       CropHD                 (Default = "0,0,0,0") Ex. "2,2,-2,-2"
#       ResizeMethod           (Default = "lanczos4resize")
#       GPU                    (Default = false)
#          -Use eDeen for smoothing instead of Deathray.
#
#################################################################################################


function mdegrain2default(clip) {
originalvideo = clip

super = MSuper(originalvideo, pel=2, sharp=1)
backward_vec2 = MAnalyse(super, isb = true, delta = 2, overlap=4)
backward_vec1 = MAnalyse(super, isb = true, delta = 1, overlap=4)
forward_vec1 = MAnalyse(super, isb = false, delta = 1, overlap=4)
forward_vec2 = MAnalyse(super, isb = false, delta = 2, overlap=4)
mvvideo = MDegrain2(originalvideo, super, backward_vec1,forward_vec1,backward_vec2,forward_vec2,thSAD=400)

return mvvideo
}

function mdegrain2strong(clip) {
originalvideo = clip

super = MSuper(originalvideo, pel=2, sharp=1)
backward_vec2 = MAnalyse(super, isb = true, delta = 2, blksize=4, overlap=2)
backward_vec1 = MAnalyse(super, isb = true, delta = 1, blksize=4, overlap=2)
forward_vec1 = MAnalyse(super, isb = false, delta = 1, blksize=4, overlap=2)
forward_vec2 = MAnalyse(super, isb = false, delta = 2, blksize=4, overlap=2)
mvvideo = MDegrain2(originalvideo, super, backward_vec1,forward_vec1,backward_vec2,forward_vec2,thSAD=400)

return mvvideo
}

function AUclassic(clip, int "strength", int "degrain", bool "FullHD", bool "widescreen", string "cropSD", string "cropHD", string "ResizeMethod", bool "GPU") {

strength = Default(strength, 2)
FullHD = Default(FullHD, true)
widescreen = Default(widescreen, true)
cropSD = Default(cropSD, "0,0,0,0")
cropHD = Default(cropHD, "0,0,0,0")
ResizeMethod = Default(ResizeMethod, "lanczos4resize")
GPU = Default(GPU, false)
degrain = (strength==3) ? Default(degrain, 2) : Default(degrain, 1)

#Legacy Line Enhancement
videoSD = (strength<3 && GPU==false) ? clip.fastlinedarkenmod() : clip

#Cropping SD Footage
videoSD = (cropSD!="0,0,0,0") ? Eval( "videoSD.crop(" + cropSD + ")" ) : videoSD

#Grain and Noise Removal
videoSD = (degrain==1) ? videoSD.mdegrain2default() : (degrain==2) ? videoSD.mdegrain2default().mdegrain2default() : (degrain==3) ? videoSD.mdegrain2strong() : (degrain==4) ? videoSD.mdegrain2strong().mdegrain2strong() : videoSD

#Spatial Smoothing with Deathray
videoSD = (strength==2 && GPU==true) ? videoSD.Deathray() : videoSD
videoSD = (strength==3 && GPU==true) ? videoSD.dehalo_alpha().Deathray() : videoSD

#Sharpening
videoSD = videoSD.fastlinedarkenmod()

#Upscale the video to HD
videoHD = (FullHD==true && widescreen==true) ? Eval("videoSD." + ResizeMethod + "(1920,1080)") : (FullHD==true && widescreen==false) ? Eval("videoSD." + ResizeMethod + "(1440,1080)") : (FullHD==false && widescreen==true) ? Eval("videoSD." + ResizeMethod + "(1280,720)") : Eval("videoSD." + ResizeMethod + "(960,720)")

#Spatial Smoothing with eDeen
videoHD = (strength==2 && FullHD==true && GPU==false) ? videoHD.eDeen(3,9,10,2,3) : (strength==2 && FullHD==false && GPU==false) ? videoHD.eDeen(2,6,7,2,3) : videoHD
videoHD = (strength==3 && FullHD==true && GPU==false) ? videoHD.eDeen(7,14,21,2,3) : (strength==3 && FullHD==false && GPU==false) ? videoHD.eDeen(5,9,14,2,3) : videoHD

#Edge Enhancement
videoHD = (FullHD==true) ? videoHD.awarpsharp(10,2).flash3kyuu_deband() : videoHD.awarpsharp(8,2).flash3kyuu_deband()

#Cropping HD Footage
videoHD = (cropHD!="0,0,0,0") ? Eval( "videoHD.crop(" + cropHD + ")" ) : videoHD
videoHD = (FullHD==false && widescreen==true && cropHD!="0,0,0,0") ? Eval("videoHD." + ResizeMethod + "(1280,720)") : (FullHD==false && widescreen==false && cropHD!="0,0,0,0") ? Eval("videoHD." + ResizeMethod + "(960,720)") : videoHD
videoHD = (FullHD==true && widescreen==true && cropHD!="0,0,0,0") ? Eval("videoHD." + ResizeMethod + "(1920,1080)") : (FullHD==true && widescreen==false && cropHD!="0,0,0,0") ? Eval("videoHD." + ResizeMethod + "(1440,1080)") : videoHD

return videoHD
}

Release Notes

Version 4.4.1 (6-26-2017)

  • Added VapourSynth Version.
  • Changed ResizeMethod variable to not require resize.
  • Removed Levels().

Version 4.4 (6-16-2017)

  • Added Hysteria.
  • Removed FastLineDarkenMod() & LSFMod().

Version 4.3 (5-5-2015)

  • Added ConvertMatrix to allow for Rec.601 to Rec.709 conversion.
  • Added mDegrainSimple() function.
  • Removed mdegrain2default() and mdegrain2strong() functions.

Version 4.2.1 (10-13-2014)

  • Fixed an error in the script that prevented sharpening at Strength=1.

Version 4.2 (10-11-2014)

  • Added a RemoveGrain back for Strength=3.
  • Removed Strength=4.
  • Removed DeHalo Alpha.
  • Adjusted what Strength settings control and the default.

Version 4.1 (10-8-2014)

  • Removed DAA, LSFMod, and RemoveGrain.
  • Added a LSFModLight function to the main script.
  • Added line sharpening before DeHalo for Strength=4.
  • Improved stability.

Version 4.0 (8-28-2014)

  • Complete rewrite of the script that removes all spatial smoothing.
  • Added LSFMod for additional sharpening.
  • Improved stability and speed when using multi-threaded AviSynth.

Version 3.5 (10-8-2014)

  • Renamed function to avoid conflicting with the AnimeUpscale rewrite.
  • Removed DAA.
  • Slightly reduced aWarpSharp depth setting.

Version 3.4 (8-25-2014)

  • Replaced Gradfun2DBMod with flash3kyuu_deband.

Version 3.3.3 (6-8-2014)

  • Changed the Classic option to GPU.
  • GPU option uses Deathray instead of eDeen when set to true.
  • Removed Edge option for improved stability.

Version 3.3.2 (10-26-2013)

  • Minor fixes.

Version 3.3.1 (10-17-2013)

  • Adjusted the filter order so that sharpening happens after spatial smoothing.

Version 3.3 (10-15-2013)

  • Fixed issues where spatial smoothing did not run when FullHD = false.
  • Strength 3 changed to use Dehalo Alpha.
  • Strength 3 runs the sharpening filter after smoothing filters are run.

Version 3.2 (9-22-2013)

  • Edge disabled by default for all settings.
  • AA setting renamed to FixComb.

Version 3.1 (9-20-2013)

  • eDeen replaced by Deathray for spatial smoothing.
  • eDeen still usable by setting classic to true.

Version 3.0 (6-1-2013)

  • Complete rewrite that changed the entire way this script functions.
  • Strength now only controls the eDeen settings, but does affect other default values.
  • Added the ability to control the degrain strength.
  • Degrain default is 1, but when strength is set to 3 the default changes to 2.
  • MAA is now optional via the Edge setting.
  • Edge is on by default for FullHD when Strength is greater than 1.
  • Version 2.2 (5-12-2013)
  • Added cropSD() and cropHD() for internal cropping of the video.
  • Optimized filters for 720p upscales.

Version 2.1 (5-8-2013)

  • Added FullHD option that outputs 720p when set to false, the default is true.
  • Fixed error in the script for strength level 1.

Version 2.0 (5-3-2013)

  • Renamed internal function l33tdegrain() to mdegrain2default()
  • Added mdegrain2strong() which runs MDegrain2 with overlapped blocks (blksize=4, overlap=2) and subpixel precision.
  • Added 3 strength levels.
  • Strength levels 2, 3, & 4 are now 3, 4, & 6 respectively.
  • Strength levels 2, 5, & 7 use mdegrain2strong() for more aggressive noise reduction.
  • Added parameter that enables anti-aliasing using DAA()

Version 1.0 (8-23-2012)

  • First public release of animeupscale()
  • Internal function l33tdegrain() runs MDegrain2 with overlapped blocks (blksize=8, overlap=4) and subpixel precision.
  • MDegrain2 is used one all strength levels for grain removal and noise reduction.
  • Gradfun2db() is used on all strength levels to reduce banding after noise reduction.
  • aWarpSharp() is used on all strength levels to smooth out edges and lines.
  • Strength levels 2, 3, & 4 use eDeen for aggressive spatial smoothing.
  • Strength levels 3 & 4 use MAA() for additional smoothing on edges.