Log in

View Full Version : Old SMDegrain won't work with Avisynth+


hello_hello
27th March 2015, 22:03
Not that it matters but I can't work out why myself (I'm not Avisynth smart enough) and I'm a bit curious. Can anybody explain why the following script works fine with Avisynth 2.6 RC1 but produces the following error when using Avisynth+ (2.6.0.5 portable via MeGUI).

Script error. Expected '('
((null), line 340, column 4)

Thanks.

Edit: I had to split the script between two posts.

################################################################################################
### ###
### Simple MDegrain - SMDegrain() ###
### ###
### By Caroliano ###
### ###
### Special Thanks: Didée, Jawed, onesloth, rkalwaitis, Sagekilla and MVtools people ###
### ###
### v0.4 - 03 Sep 2011 ###
### ###
################################################################################################
###
### General purpose pure temporal denoiser, not targeted to a specific source. It is a
### simple wrapper for MDegrain1/2/3, to facilitate using and experimenting with it, with
### minimal dependencies. But it also offers many optional optimizations, plus an internal
### contra-sharpener to make things easier.
###
### Doom9 topic: http://forum.doom9.org/showthread.php?p=1523412
###
### +-----------+
### | CHANGELOG |
### +-----------+
###
###
### v0.4: - Merged changes by Dogway:
### Added parameters "lsb" and "lsb_out", for MVtools2 mod version from cretindesalpes' Dither
### (http://forum.doom9.org/showthread.php?p=1386559#post1386559)
### - Added parameters "hpad" and "vpad"
### - Misc: Fully updated the documentation, copying part of Dogway's updated documentation.
### - Misc: Cosmetics changes on the source code
###
### v0.3 : - Added external prefilted clip option for motion-search (thanks onesloth)
### - Added contrasharpening option (thanks rkalwaitis) <-- new dependencies
### - Shamelesly copied some parts from Jawed's Killer.avsi:
### Added "Refine Motion" (MRecalculate), for possibly improved speed/quality
### Added experimental fake "MDegrain5" with tr = 5
### - Added chroma option for motion-search
### - Experimental SETMOD() support (copied from onesloth mod on fastdegrain())
### - Fix: Plane parameter now works
### - Attention: Changed the defaults for thSAD and overlap.
### - Misc: Cosmetics changes on the source code
### - Misc: A bit more fool-proof
###
### v0.2 : - Added this introductory section
### - Added more parameters
###
### v0.1 : - Basic working version
### - Not released
###
###
### +--------------+
### | DEPENDENCIES |
### +--------------+
###
### Required:
### ------------
### -> MVtools2 (v2.5.11.2 or higher)
### (http://avisynth.org.ru/mvtools/mvtools2.html)
### *or*
###
### -> Dither (1.9.5 or higher) (MVtools2 v2.5.11.2mod included)
### (http://forum.doom9.org/showthread.php?p=1386559#post1386559)
### Optional:
### ------------
### -> MaskTools (v2a48 or higher for lsb=true and/or contrasharp=true) (http://manao4.free.fr/)
### -> Repair (only for contrasharpening=true)
###
###
###
### +------------------+
### | BASIC PARAMETERS |
### +------------------+
###
###
### tr [int: 1, "2", 3, 5]
### --------------
### Temporal radius. Select between MDegrain 1, 2 or 3. Higher is better, but also much slower.
### tr = 5 is an "fake MDegrain5": experimental and incompatible (?) with "LSB = true" and pel > 1.
###
###
### thSAD [int: "200"]
### --------------
### Strenght of denoising. Low values can result in staggered denoising,
### large values can result in ghosting and artefacts.
###
###
### blksize [int: 4, "8", 16]
### --------------
### Size of a block (horizontal). Larger blocks are less sensitive to noise, are faster, but also
### less accurate, leading to ghosting and artefacts. You may use 16 for more speed, when using
### RefineMotion, or for HD resolutions like 1080p.
###
###
### RefineMotion [bool: true, "false"]
### --------------
### Use MRecalculate to refine motion estimation, improving quality at a given blksize (8 or 16 only).
### Using blksize=16 should give quality very similar to blksize=8 but with performance that's somewhat faster.
### Turn it on for better motion vectors, specially when dealing with ghosting issues and whatnot.
### You can think about it like half-steps in quality/speed between 4 and 8 and 16 block sizes.
###
###
### Contrasharp [bool= true, "false"]
### --------------
### "Sharpens the denoised clip, but doesn't add more to any pixel than what was removed previously"
### In the practice you will get a slightly sharper result than the source, which is welcome, but may reintroduce
### artfacts in the source, decreasing the denoising effectiveness, and haloing. Requires MaskTools2.
###
###
###
###
### +---------------------------------+
### | 16 BIT PROCESSING AND DITHERING |
### +---------------------------------+
###
###
### LSB [bool= true, "false"]
### --------------
### This enables 16 bit depth precision for denoising, avoiding banding thanks to a dither algorithm friendly for optimum encodings.
### For this to be used you will need the Dither package and its version of mvtools2 plus MaskTools2a48 or above.
###
###
### LSB_OUT [bool= true, "false"]
### --------------
### If you set LSB to true, you can also choose to output in 16 bits so you can keep filtering in 16 bits or/and use your own ditherpost.
### For this to be used you will need the Dither package and its version of mvtools2 plus MaskTools2a48 or above.
###
###
###
###
### +---------------------+
### | ADVANCED PARAMETERS |
### +---------------------+
###
###
### limit [int: 1 - "255"]
### --------------
### Maximal change of pixel luma (post-process like DeGrainMedian plugin
### and LimitChange function of SSETools plugin, to prevent some artefacts).
###
###
### limitc [int: 1 - "255"]
### --------------
### Maximal change of pixel chroma.
###
###
### pel [int: 1, "2", 4]
### --------------
### Accuracy of the motion estimation. 1 means a precision to the pixel.
### 2 means a precision to half a pixel. 4 means a precision to quarter a pixel, produced by
### spatial interpolation (more accurate but slower and not always better due to big level scale step).
###
###
### overlap [int: "blksize/2"]
### --------------
### Must be *even* and *less* than block size. Try use overlap value from blksize/4 to blksize/2.
### The greater overlap, the more blocks number, and the lesser the processing speed.
###
###
### sharp [int: 0, 1, "2"]
### --------------
### Subpixel interpolation method for pel=2,4.
### Use 0 for soft interpolation (bilinear), 1 for bicubic interpolation (4 tap Catmull-Rom),
### 2 for sharper Wiener interpolation (6 tap, similar to Lanczos.
###
###
### plane [int: 1, 2, 3, "4"]
### --------------
### Select the planes you wish to denoise:
### 0 - luma only, 1 - chroma U, 2 - chroma V, 3 - both chromas, 4 - all.
### Keep in mind that plane=4 can sometimes create chroma smearing.
###
###
### Chroma [bool= "true", false]
### --------------
### Takes chroma planes into consideration for calculating the motion vectors.
### Turn it off for more speed with little cost in quality.
###
###
### Search [int: 0, 1, 2, 3, "4", 5]
### --------------
### 0= 'OneTimeSearch', 1 = 'NStepSearch', 2= Logarithmic, 3= Exhaustive, 4= Hexagon, 5= Uneven Multi Hexagon
### See details at MVtools documentation: http://avisynth.org.ru/mvtools/mvtools2.html
###
###
### Hpad, Vpad [int= "blksize"]
### --------------
### It is horizontal/vertical padding added to source frame (both left and right). Small padding is added for more correct
### motion estimation near frame borders. Turn it to 0 if you are running out of resources in your system.
###
###
### pre [clip: - ]
### --------------
### Sets a different clip that will be used ONLY to obtain the motion vectors. Useful for very damaged/grainy sources.
### You should define the new clip in a variable and reference it here. For example:
###
### blured = last.Blur(1.58) # or a strong and fast denoiser of your preference
### SMDegrain(pre=blured)
###
###
### Dark [bool: true, "false"]
### --------------
### Tweaks scene change detection, to be more sensitive, but can erroneously trigger scene change during
### bright/complex scenery with lots of motion.
###
###
###
###
###
### +-------------+
### | FINAL NOTES |
### +-------------+
###
### If there is an important parameter not implemented, please ask.
### Or better yet, implement it yourself and post in the Doom9 thread.
###
### Many paremeters will likely never be implemented,
### and you are better off using the original MVtools way.
###
################################################################################################



function SMDegrain ( clip input, clip "pre", int "tr", int "thSAD", int "plane", int "limit", int "limitc", int "pel",
\ int "sharp", int "blksize", int "overlap", int "search", bool "RefineMotion", bool "dark", bool "chroma",
\ int "hpad", int "vpad", bool "lsb", bool "lsb_out", bool "contrasharp", bool "preout")
{
o = input
tr = default( tr, 2 )
thSAD = default( thSAD, 200 )
plane = default( plane, 4 )
limit = default( limit, 255 )
limitc = default( limitc, limit)

pel = default( pel, 2 )
sharp = default( sharp, 2 )
chroma = default( chroma, true )

blksize = default( blksize, 8 )
overlap = default( overlap, blksize/2 )
search = default( search, 4 )
hpad = default( hpad, blksize)
vpad = default( vpad, blksize)

lsb_out = default( lsb_out, false)
lsb = default( lsb , lsb_out)

contrasharp = default( contrasharp, false) # contrasharpening off by default.

preout = default( preout , false )
dark = default( dark , false )
RefineMotion = default( RefineMotion, false) # true means MRecalculate will be used to improve motion vectors


# Invalid input handling and setting adtional internal variables.

lsb_out ? Assert (lsb == true, "lsb_out requires: lsb=true") : NOP
lsb ? Assert (tr <= 3, "tr > 3 isn't supported when lsb=true") : NOP

pel = (tr > 3) ? 1 : pel
o = o.assumeframebased() # MSuper pel=2 is faster with this

RefineMotion ? Assert (blksize >= 8 , "For RefineMotion you need a blksize of 8 or 16") : NOP

halfblksize = blksize/2 # MRecalculate works with half block size
halfoverlap = (overlap == 2) ? overlap : overlap/2 # Halve the overlap to suit the halved block size
halfthSAD = thSAD/2 # MRecalculate uses a more strict thSAD, which defaults to 150 (half of function's default of 300)


# The default values thSCD1=400, thSCD2=130 often do not reckognize some scenechanges,
# especially in dimmed/dark scenery. lowering to sth like 350,90 is more safe in reckognizing scenechanges ...
# however, the more you lower these values, the more there is danger that the detection
# erroneously triggers during bright/complex scenery with lots of motion.
thSCD1 = (dark == true) ? 350 : 400
thSCD2 = (dark == true) ? 90 : 130


# See if SetMTMode is used. If yes, change it to the most adequate (?) for the script.
try {
SMTMdefaultmode = GetMTMode()
SMTM = (SMTMdefaultmode!=0)
} catch(err_msg) {SMTM = false}

hello_hello
27th March 2015, 22:05
# Motion vector search

(SMTM) ? SetMTMode(2) : NOP()

super = o.MSuper(pel=pel, hpad=hpad, vpad=vpad, sharp=sharp, chroma=chroma)
search_super = (defined(pre)) ? pre.MSuper(pel=pel, hpad=hpad, vpad=vpad, sharp=sharp, chroma=chroma) : super

recalculate = (defined(pre)) ? pre.MSuper(pel=pel, hpad=hpad, vpad=vpad, sharp=sharp, chroma=chroma, levels = 1) :
\ o.MSuper(pel=pel, hpad=hpad, vpad=vpad, sharp=sharp, chroma=chroma, levels = 1)

bv5 = search_super.MAnalyse(isb = true, delta = 5, overlap=overlap, blksize=blksize, search=search, chroma=chroma)
bv4 = search_super.MAnalyse(isb = true, delta = 4, overlap=overlap, blksize=blksize, search=search, chroma=chroma)
bv3 = search_super.MAnalyse(isb = true, delta = 3, overlap=overlap, blksize=blksize, search=search, chroma=chroma)
bv2 = search_super.MAnalyse(isb = true, delta = 2, overlap=overlap, blksize=blksize, search=search, chroma=chroma)
bv1 = search_super.MAnalyse(isb = true, delta = 1, overlap=overlap, blksize=blksize, search=search, chroma=chroma)

fv1 = search_super.MAnalyse(isb = false, delta = 1, overlap=overlap, blksize=blksize, search=search, chroma=chroma)
fv2 = search_super.MAnalyse(isb = false, delta = 2, overlap=overlap, blksize=blksize, search=search, chroma=chroma)
fv3 = search_super.MAnalyse(isb = false, delta = 3, overlap=overlap, blksize=blksize, search=search, chroma=chroma)
fv4 = search_super.MAnalyse(isb = false, delta = 4, overlap=overlap, blksize=blksize, search=search, chroma=chroma)
fv5 = search_super.MAnalyse(isb = false, delta = 5, overlap=overlap, blksize=blksize, search=search, chroma=chroma)

# MRecalculate, if higher quality motion estimation is wanted. It can also be used to speedup, by using higher blksizes

bv5 = RefineMotion ? MRecalculate(recalculate, bv5, blksize=halfblksize, overlap=halfoverlap, thSAD=halfthSAD, search=search, chroma=chroma) : bv5
bv4 = RefineMotion ? MRecalculate(recalculate, bv4, blksize=halfblksize, overlap=halfoverlap, thSAD=halfthSAD, search=search, chroma=chroma) : bv4
bv3 = RefineMotion ? MRecalculate(recalculate, bv3, blksize=halfblksize, overlap=halfoverlap, thSAD=halfthSAD, search=search, chroma=chroma) : bv3
bv2 = RefineMotion ? MRecalculate(recalculate, bv2, blksize=halfblksize, overlap=halfoverlap, thSAD=halfthSAD, search=search, chroma=chroma) : bv2
bv1 = RefineMotion ? MRecalculate(recalculate, bv1, blksize=halfblksize, overlap=halfoverlap, thSAD=halfthSAD, search=search, chroma=chroma) : bv1

fv1 = RefineMotion ? MRecalculate(recalculate, fv1, blksize=halfblksize, overlap=halfoverlap, thSAD=halfthSAD, search=search, chroma=chroma) : fv1
fv2 = RefineMotion ? MRecalculate(recalculate, fv2, blksize=halfblksize, overlap=halfoverlap, thSAD=halfthSAD, search=search, chroma=chroma) : fv2
fv3 = RefineMotion ? MRecalculate(recalculate, fv3, blksize=halfblksize, overlap=halfoverlap, thSAD=halfthSAD, search=search, chroma=chroma) : fv3
fv4 = RefineMotion ? MRecalculate(recalculate, fv4, blksize=halfblksize, overlap=halfoverlap, thSAD=halfthSAD, search=search, chroma=chroma) : fv4
fv5 = RefineMotion ? MRecalculate(recalculate, fv5, blksize=halfblksize, overlap=halfoverlap, thSAD=halfthSAD, search=search, chroma=chroma) : fv5



# Finally, MDegrain

output = lsb ? ((tr == 3) ? o.MDegrain3(super, bv1, fv1, bv2, fv2, bv3, fv3, thSAD=thSAD, limit=limit, limitc=limitc, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2,lsb=lsb) :
\ (tr == 2) ? o.MDegrain2(super, bv1, fv1, bv2, fv2, thSAD=thSAD, limit=limit, limitc=limitc, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2,lsb=lsb) :
\ o.MDegrain1(super, bv1, fv1, thSAD=thSAD, limit=limit, limitc=limitc, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2,lsb=lsb)):
\ ((tr == 5) ? Eval("""
mdg3 = o.MDegrain3(super, bv1, fv1, bv2, fv2, bv3, fv3, thSAD=thSAD, limit=limit, limitc=limitc, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2)
mdg3.MDegrain2(super,bv4,fv4,bv5,fv5,thSAD=thSAD, limit=limit, limitc=limitc, plane= plane, thSCD1=thSCD1, thSCD2=thSCD2)
\ .Merge(mdg3, 0.455) # correct weightings (hopefully)
""") :
\ (tr == 3) ? o.MDegrain3(super, bv1, fv1, bv2, fv2, bv3, fv3, thSAD=thSAD, limit=limit, limitc=limitc, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2) :
\ (tr == 2) ? o.MDegrain2(super, bv1, fv1, bv2, fv2, thSAD=thSAD, limit=limit, limitc=limitc, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2) :
\ o.MDegrain1(super, bv1, fv1, thSAD=thSAD, limit=limit, limitc=limitc, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2) )

(SMTM) ? SetMTMode(SMTMdefaultmode) : NOP()


# Contrasharp with LSB
if = contrasharp && lsb ? true : false
output1= if ? output.DitherPost(mode=-1) : nop
sharp8 = if ? Contrasharpening (output1, o) : nop
lsbctr = if ? Dither_merge16_8 (output,sharp8.Dither_convert_8_to_16(), \
DitherBuildMask (sharp8, output1) ) : nop

# Return is implicit
lsb_out ? (contrasharp ? lsbctr : output) : (contrasharp ? (lsb ? lsbctr.DitherPost() : ContraSharpening(output, o)) : (lsb ? output.DitherPost() : output))

}


#### Helper function

FUNCTION ContraSharpening(denoised, clip)
{

# contra-sharpening: sharpen the denoised clip, but don't add more to any pixel than what was removed previously.
# script function from Didee from the VERY GRAINY thread
s = denoised.minblur(1,1) # Damp down remaining spots of the denoised clip.
allD = mt_makediff(clip,denoised) # The difference achieved by the denoising.
ssD = mt_makediff(s,s.removegrain(11,-1)) # The difference of a simple kernel blur.
ssDD = ssD.repair(allD,1) # Limit the difference to the max of what the denoising removed locally.
ssDD = SSDD.mt_lutxy(ssD,"x 128 - abs y 128 - abs < x y ?") # abs(diff) after limiting may not be bigger than before.

denoised.mt_adddiff(ssDD,U=2,V=2) # Apply the limited difference. (Sharpening is just inverse blurring.)

RETURN (last)
}

hello_hello
6th May 2015, 21:38
Bump?

vdcrim
6th May 2015, 22:18
Avisynth+ features for users
- Script language extensions, with support for multiline conditionals and loops.


if is a reserved keyword in Avisynth+:
if = contrasharp && lsb ? true : false

StainlessS
7th May 2015, 10:38
Try gimport with gscript

Groucho2004
7th May 2015, 17:38
Try gimport with gscriptWhy not just change the variable name to something that isn't a reserved keyword? Seems to be the obvious choice.

StainlessS
8th May 2015, 01:57
Why not just change the variable name to something that isn't a reserved keyword? Seems to be the obvious choice.

Ooops, yes of course, guess I did not even read the problem line.

hello_hello
9th May 2015, 16:09
Thanks.

tormento
10th May 2015, 14:05
Look here (http://forum.videohelp.com/threads/369142-Simple-MDegrain-Mod-v3-0d-A-Quality-Denoising-Solution)