Log in

View Full Version : Maa2


Overdrive80
14th October 2015, 16:59
import vapoursynth as vs

##MAA2 supports Y8, YV12 and YV24 input.
##
##Requirements:
##
## * AviSynth 2.6a4
## * SangNom2 0.3+
## * FTurn
## * Masktools 2.0a48
##
##Parameters:
##
## + [int] mask (1)
## * 0: Disable masking
## * 1: Enable masking
## * -i: Enable masking with custom treshold (sensible values are between 0 and 30)
## + [bool] chroma (false)
## * false: Don't process chroma channels (copy UV from the source clip if present)
## * true: Process chroma channels
## + [float] ss (2.0)
## * Supersampling factor (sensible values are between 2.0 and 4.0 for HD content)
## + [int] aa (48)
## * Sangnom2 luma antialiasing strength
## + [int] aac (aa-8)
## * Sangnom2 chroma antialiasing strength
## + [int] threads (4)
## * Number of threads used by every Sangnom2 instance
## + [int] show (0)
## * 0: Don't overlay mask
## * 1: Overlay mask only
## * 2: Overlay mask and run antialiasing on the luma plane

def maa2(clip, mask=1, chroma=False, ss=2.0, aa=48, aac = None, threads = 4, show = 0):

core = vs.get_core()

#Control of errors
if not isinstance(clip, vs.VideoNode):
raise ValueError('This is not a clip')

if show < 0 or show > 2:
raise ValueError("MAA2: Parameter 'show' must be between 0 and 2")

if clip.format.color_family != vs.YUV:
raise ValueError("MAA2: Input must be Y8, YV12 or YV24")

#Scaling
maxi = 2 ** clip.format.bits_per_sample
mid = (2 ** clip.format.bits_per_sample) // 2

# Default values
mtresh = -mask if mask < 0 else 7
aac = aa-8 if (aac == None or aac >= 0) else 0

if mask != 0:
m = core.std.Sobel(clip, mtresh, mtresh, [0,1,2], 0)
m = core.std.Inflate(m, [0,1,2])

if (chroma== 0 and show == 0):
c_aa = sangnom2aa(clip, ss=ss, aa=aa, aac=aac, threads=threads)
else:
c_aa = core.std.ShufflePlanes(clip, planes=[0], colorfamily=vs.GRAY)
c_aa = sangnom2aa(c_aa, ss=ss, aa=aa, aac=aac, threads=threads)

if show == 1:
if clip.format == vs.GRAY8:
c_aa = core.resize.Bicubic(c_aa, format=vs.YUV420P8)
c_aa = core.std.Lut(c_aa, planes=[1,2], function= lambda x: x - maxi)
else:
c_aa = core.std.Lut(clip, planes=[1,2], function= lambda x: x / 2)

elif show == 2:
if clip.format == vs.GRAY8:
c_aa = core.resize.Bicubic(c_aa, format=vs.YUV420P8)
c_aa = core.std.Lut(c_aa, planes=[1,2], function= lambda x: x - maxi)
else:
c_aa = core.std.ShufflePlanes([clip.std.ShufflePlanes(planes=1, colorfamily=vs.GRAY).resize.Bicubic(format=vs.GRAY8), clip.std.ShufflePlanes(planes=2, colorfamily=vs.GRAY).resize.Bicubic(format=vs.GRAY8), c_aa], planes=[0,0,0])
c_aa = core.std.Lut(c_aa, planes=[1,2], function= lambda x: x / 2)

if mask!=0:
if show > 0:
if clip.format.color_family == vs.YUV and clip.format.bits_per_sample == 24:
return core.std.MaskedMerge(clip, c_aa, core.std.ShufflePlanes(clips=m, planes=[0,0,0]), planes=[0,1,2])
else:
return core.std.MaskedMerge(core.resize.Bicubic(clip, format=vs.YUV420P8), c_aa, m, planes=[0,1,2], colorfamily=vs.YUV)
elif chroma:
return core.std.MaskedMerge(clip, c_aa, m, planes=[0,1,2])
else:
return core.std.MaskedMerge(clip, c_aa, m, planes=[0,1,2])
else:
return core.std.Expr([clip, c_aa], expr=['x y {op}'.format(op="and")])


############
# #
#Sangnom2AA#
# #
############
def sangnom2aa(clip, ss=2.0, aa=48, aac=None, threads=4):

core = vs.get_core()

if ss <= 0:
raise ValueError("MAA2: Supersampling factor must be > 0")

aac = aa-8 if (aac == None or aac >= 0) else 0

width = clip.width
height = clip.height
ss_w = int(round(width*ss/4.0)*4)
ss_h = int(round(height*ss/4.0)*4)

last = core.resize.Spline(clip, ss_w, ss_h)
last = core.std.Transpose(last)
last = core.sangnom.SangNomMod(last, 1, aa, aac)
last = core.std.Transpose(last)
last = core.sangnom.SangNomMod(last, 1, aa, aac)
last = core.resize.Spline(last, width, height)

return last



If you detect errors, please do not hesitate to tell me

original: http://avisynth.nl/index.php/MAA2