HQDegrain

This forum is for questions and discussion of all the aspects of handling and cleaning up your footage with Avisynth.
Locked
Alek93j
Joined: Wed Feb 25, 2009 4:36 am
Org Profile

HQDegrain

Post by Alek93j » Mon Jun 20, 2011 10:28 pm

Hi all, I made this little degrain filter. I hope some of you will find it useful.

Code: Select all

 
 ####################################################################################################################
#                                                                                                                                                                                                       
#  ~HQDegrain v0.9 by Alek93j~   19/06/2011                                                                                                                                           
#                                                                                                                                                                                                        
# Required MVtools2.dll    http://avisynth.org.ru/mvtools/mvtools2.html                                                                                                                           
#       ttempsmooth.dll    http://avisynth.org.ru/docs/english/externalfilters/ttempSmooth.htm                           
#   mt_masktools-25.dll    http://manao4.free.fr/masktools-v2.0a48.zip
#   blockbuster.dll   http://kvcd.net/sansgrip/avisynth/                                   
#                                                                                                          ~Syntax~ 
#                                                                                                                  
 #################################################################################################################### 
#                                                                                                                   
# # It doesn't work perfectely with sudden luma changes such as flashes and fades, so it                            
# # is best not to filter those parts.                                                                              
#                                                                                                                  
# -Example of a Backup of a NTSC DvD-                                                                             
#                                                                                                                   
# MPEG2Source("C:\blabla.d2v")                                                                                    
# ivtc() (your prefer ivtc ectr)                                                                                 
# A=hqdegrain() (Clip normally processed)                                                                            
# B=ttempsmoothf(maxr=7,strength=5,lthresh=3,cthresh=4) (or your prefer denoise for clean fade)                 
# B.Trim(0,215)+A.Trim(216,31464)                                                                                
#                                                                                                                  
 ####################################################################################################################

Function HQDegrain(clip clp,int"Degrain",int"limit",bool"HQ",\
                   int"maxr",int"strength",int"lthresh",int"cthresh",\
                   clip"PreNoise",int"BkSize",bool"noise",\
                   int"Blk",int"PEL",int"DCT",int"Overlap",\
                   int "thSAD", int "thSADC",int "thSCD1", int "thSCD2")
{

#MDegrain Strength

Degrain = Default( Degrain , 2     ) # Means how many frame should be analyzed for a better result, 3 doesn't help much.
limit   = Default( limit   , 255   ) # limit of mvdegrain. (255 mean no limits)

#ttempsmoothf Strength

HQ      = Default( HQ      , true  ) # Repair artefact.
maxr    = Default( maxr    , 5     )
strength= Default( strength, 4     )
lthresh = Default( lthresh , 3     )
cthresh = Default( cthresh , 4     )

#Noise Filter

Noise   = Default( Noise   , true  )
BkSize  = Default( BkSize  , 8     )

#Motion Vector Analyse

Blk     = Default( Blk     , 8     ) # Use 16/for 1080p - 8/for 720p - 4/for 480p to avoid unnecessary slowdowns.
PEL     = Default( PEL     , 2     )
DCT     = Default( DCT     , 0     )
OPS     = Blk/2
Overlap = Default( Overlap , OPS   ) 

#Threshold Setting

thSAD   = Default( thSAD   , 180   ) # Lower values will reduce artefacts for bad motion vectors, but they will also have a lighter denoising effect. For animated sources, 120 seems to be a good setting to start with.
thSADC  = Default( thSADC  , thSAD )
thSCD1  = Default( thSCD1  , 400   )
thSCD2  = Default( thSCD2  , 102   )

#Processing

input = clp

clp = defined(PreNoise) ? PreNoise 
       \: (Noise==true) ? clp.blockbuster(method="noise",Block_size=BkSize) : clp

sup = clp.Msuper(hpad=Blk,vpad=Blk,PEL=PEL,levels=0)
sup2= clp.Msuper(hpad=Blk,vpad=Blk,PEL=PEL,levels=1)

BV1 = MAnalyse(sup,isb = true , truemotion=true,delta=1,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap)
FV1 = MAnalyse(sup,isb = false, truemotion=true,delta=1,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap)

BV2 = (Degrain>=2) ? MAnalyse(sup,isb = true , truemotion=true,delta=2,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 
FV2 = (Degrain>=2) ? MAnalyse(sup,isb = false, truemotion=true,delta=2,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 

BV3 = (Degrain==3) ? MAnalyse(sup,isb = true , truemotion=true,delta=3,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 
FV3 = (Degrain==3) ? MAnalyse(sup,isb = false, truemotion=true,delta=3,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 

clp = (degrain==1) ? clp.MDegrain1(sup2, BV1, FV1,                     thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit): 
 \    (degrain==2) ? clp.MDegrain2(sup2, BV1, FV1, BV2, FV2,           thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit):
 \                   clp.MDegrain3(sup2, BV1, FV1, BV2, FV2, BV3, FV3, thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit)

Diff1 = mt_makediff(input,clp)

clp2  = clp.ttempsmoothf(maxr=maxr,strength=strength,lthresh=lthresh,cthresh=cthresh)

Diff2 = mt_makediff(input,clp2)

lutxy = mt_lutxy(Diff1,Diff2,"x 128 - abs y 128 - abs < x y ?")

output = (HQ==true) ? Input.mt_makediff(lutxy,U=2,V=2) : clp2

output }
hqdegrain(thsad=120)
Image
Image
Image
Image

hqdegrain()
Image
Image
Image
Image
good work and crf 14 for overkill quality \ò/

Mister Hatt
Joined: Tue Dec 25, 2007 8:26 am
Status: better than you
Contact:
Org Profile

Re: HQDegrain

Post by Mister Hatt » Wed Jun 22, 2011 11:17 pm

This seems like a rather walkabout way to get lower quality and/or slower processing than most of the other accepted standards for degraining? The benefit over straight ttempsmooth doesn't seem all that large, and the lack of effect on luma spikes kinda kills any use for it I can think of. Should probably fix that.

Alek93j
Joined: Wed Feb 25, 2009 4:36 am
Org Profile

Re: HQDegrain

Post by Alek93j » Fri Jun 24, 2011 9:58 am

new Version

Code: Select all

 ######################################################################
#                                                                                                                                                                                                       
#  ~HQDegrain v0.9.1 by Alek93j~  24/06/2011                                                                                                                                          
#                                                                                                                                                                                                        
# Required MVtools2.dll    http://avisynth.org.ru/mvtools/mvtools2.html                                                                                                                            
#       ttempsmooth.dll    http://avisynth.org.ru/docs/english/...tempSmooth.htm                           
#   mt_masktools-25.dll    http://manao4.free.fr/masktools-v2.0a48.zip                                  
#       blockbuster.dll    http://kvcd.net/sansgrip/avisynth/                             
#                                                                           ~Syntax~       
 ##################################################################################
#                                                                                                                   
# # It doesn't work perfectely with sudden luma changes such as flashes and fades, so it                            
# # is best not to filter those parts.                                                                              
#                                                                                                                  
# -Example of a Backup of a NTSC DvD-                                                                             
#                                                                                                                   
# MPEG2Source("C:\blabla.d2v")                                                                                    
# ivtc() (your prefer ivtc ectr)                                                                                 
# A=hqdegrain() (Clip normally processed)                                                                            
# B=ttempsmoothf(maxr=7,strength=5,lthresh=3,cthresh=4) (or your prefer denoise for clean fade)                 
# B.Trim(0,215)+A.Trim(216,31464)                                                                                
#                                                                                                                  
 ###########################################################################

Function HQDegrain(clip clp,int"Degrain",int"limit",bool"HQ",\
                   int"maxr",int"strength",int"lthresh",int"cthresh",\
                   clip"PreNoise",int"BkSize",bool"noise",\
                   int"Blk",int"PEL",int"DCT",int"Overlap",\
                   int "thSAD", int "thSADC",int "thSCD1", int "thSCD2")
{

#MDegrain Strength

Degrain = Default( Degrain , 2     ) # Means how many frame should be analyzed for a better result, 3 doesn't help much.
limit   = Default( limit   , 255   ) # limit of mvdegrain. (255 mean no limits)

#ttempsmoothf Strength

HQ      = Default( HQ      , true  ) # Reduce artefact...
maxr    = Default( maxr    , 5     )
strength= Default( strength, 4     )
lthresh = Default( lthresh , 3     )
cthresh = Default( cthresh , 4     )

#Noise Filter

Noise   = Default( Noise   , true  )
BkSize  = Default( BkSize  , 8     )

#Motion Vector Analyse

Blk     = Default( Blk     , 8     ) # Use 16/for 1080p - 8/for 720p - 4/for 480p to avoid unnecessary slowdowns.
PEL     = Default( PEL     , 2     )
DCT     = Default( DCT     , 0     )
OPS     = Blk/2
Overlap = Default( Overlap , OPS   ) 

#Threshold Setting

thSAD   = Default( thSAD   , 180   ) # Lower values will reduce artefacts for bad motion vectors, but they will also have a lighter denoising effect. For animated sources, 120 seems to be a good setting to start with.
thSADC  = Default( thSADC  , thSAD )
thSCD1  = Default( thSCD1  , 400   )
thSCD2  = Default( thSCD2  , 102   )

#Processing

input = clp

clp = defined(PreNoise) ? PreNoise 
       \: (Noise==true) ? clp.blockbuster(method="noise",Block_size=BkSize) : clp

sup = clp.Msuper(hpad=Blk,vpad=Blk,PEL=PEL,levels=0)

BV1 = MAnalyse(sup,isb = true , truemotion=true,delta=1,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap)
FV1 = MAnalyse(sup,isb = false, truemotion=true,delta=1,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap)

BV2 = (Degrain>=2) ? MAnalyse(sup,isb = true , truemotion=true,delta=2,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 
FV2 = (Degrain>=2) ? MAnalyse(sup,isb = false, truemotion=true,delta=2,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 

BV3 = (Degrain==3) ? MAnalyse(sup,isb = true , truemotion=true,delta=3,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 
FV3 = (Degrain==3) ? MAnalyse(sup,isb = false, truemotion=true,delta=3,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 

clp = (degrain==1) ? clp.MDegrain1(sup, BV1, FV1,                     thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit): 
 \    (degrain==2) ? clp.MDegrain2(sup, BV1, FV1, BV2, FV2,           thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit):
 \                   clp.MDegrain3(sup, BV1, FV1, BV2, FV2, BV3, FV3, thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit)

Diff1 = mt_makediff(input,clp,U=3,V=3)

clp2  = clp.ttempsmoothf(maxr=maxr,strength=strength,lthresh=lthresh,cthresh=cthresh)

Diff2 = mt_makediff(input,clp2,U=3,V=3)

lutxy = mt_lutxy(Diff1,Diff2,"x 128 - abs y 128 - abs < x y ?")

output = (HQ==true) ? Input.mt_makediff(lutxy,U=3,V=3) : clp2

output }

jakkor
Joined: Wed Feb 25, 2009 5:07 pm
Org Profile

Re: HQDegrain

Post by jakkor » Mon Jun 27, 2011 7:09 am

Error
there is no function named "blockbuster"
hqdegrain.avsi, line 70

User avatar
mirkosp
The Absolute Mudman
Joined: Mon Apr 24, 2006 6:24 am
Status: (」・ワ・)」(⊃・ワ・)⊃
Location: Gallarate (VA), Italy
Contact:
Org Profile

Re: HQDegrain

Post by mirkosp » Mon Jun 27, 2011 7:51 am

Blockbuster is one of the required functions and is linked in the script...
Image

jakkor
Joined: Wed Feb 25, 2009 5:07 pm
Org Profile

Re: HQDegrain

Post by jakkor » Mon Jun 27, 2011 8:25 am

I have blockbuster.dll in avisynth plugins so don't know what is wrong

User avatar
Scintilla
(for EXTREME)
Joined: Mon Mar 31, 2003 8:47 pm
Status: Quo
Location: New Jersey
Contact:
Org Profile

Re: HQDegrain

Post by Scintilla » Mon Jun 27, 2011 5:44 pm

Load it manually with a LoadPlugin line at the top of your script. If you have too many plugins in your plugins directory, AVISynth sometimes fails to load some of them.
ImageImage
:pizza: :pizza: Image :pizza: :pizza:

Alek93j
Joined: Wed Feb 25, 2009 4:36 am
Org Profile

Re: HQDegrain

Post by Alek93j » Wed Jun 29, 2011 3:50 pm

new version, added presets "fast" "slow" and lmask

Code: Select all

 ######################################################################
#                                                                                                                                                                                                       
#  ~HQDegrain v0.9.2 by Alek93j~  29/06/2011                                                                                                                                          
#                                                                                                                                                                                                        
# Required MVtools2.dll    http://avisynth.org.ru/mvtools/mvtools2.html                                                                                                                            
#       ttempsmooth.dll    http://avisynth.org.ru/docs/english/...tempSmooth.htm                           
#   mt_masktools-25.dll    http://manao4.free.fr/masktools-v2.0a48.zip                                  
#       blockbuster.dll    http://kvcd.net/sansgrip/avisynth/                             
#       removegrain.dll    http://avisynth.org/mediawiki/Removegrain 
#                                               
# ~Syntax~       
 ##################################################################################
#                                                                                                                   
# # It doesn't work perfectely with sudden luma changes such as fades, so it                            
# # is best not to filter those parts.                                                                              
#                                                                                                                  
# -Example of a Backup of a NTSC DvD-                                                                             
#                                                                                                                   
# MPEG2Source("C:\blabla.d2v")                                                                                    
# ivtc() (your prefer ivtc ectr)                                                                                 
# A=hqdegrain() (Clip normally processed)                                                                            
# B=ttempsmoothf(maxr=7,strength=5,lthresh=3,cthresh=4) (or your prefer denoise for clean fade)                 
# B.Trim(0,215)+A.Trim(216,31464)                                                                                
#                                                                                                                  
 ###########################################################################

Function HQDegrain(clip clp,int"Degrain",int"limit",string"preset",bool"HQ",\
                   int"maxr",int"strength",int"lthresh",int"cthresh",\
                   clip"PreNoise",int"BkSize",bool"noise",bool"lmask",\
                   int"Blk",int"PEL",int"DCT",int"Overlap",\
                   int "thSAD", int "thSADC",int "thSCD1", int "thSCD2")
{

#Presets avaiable "Fast" - "Slow"

Preset=Default(Preset,"Slow")
pset=(Preset=="Fast") ? 0 : 1
	
#MDegrain Strength

Degrain = Default( Degrain , Select(pset,1,2)) # Means how many frame should be analyzed for a better result, 3 doesn't help much.
limit   = Default( limit   , 255   ) # limit of mvdegrain. (255 mean no limits)

#ttempsmoothf Strength

HQ      = Default( HQ      , Select(pset,false,true)) # Reduce artefact...
maxr    = Default( maxr    , Select(pset,3,5))
strength= Default( strength, Select(pset,3,4))
lthresh = Default( lthresh , Select(pset,2,3))
cthresh = Default( cthresh , Select(pset,3,4))

#Noise Filter

Noise   = Default( Noise   , true  )
BkSize  = Default( BkSize  , 8     )
lmask   = Default( lmask   , Select(pset,false,true)) #if lmask is true, noise will not be added in the parts of the image with high luma values, because generally there is no need to filter them.

#Motion Vector Analyse

Blk     = Default( Blk     , 8     ) # Use 16/for 1080p - 8/for 720p - 4/for 480p to avoid unnecessary slowdowns.
PEL     = Default( PEL     , 2     )
DCT     = Default( DCT     , 0     )
OPS     = Blk/2
Overlap = Default( Overlap , Select(pset,0,OPS))

#Threshold Setting

thSAD   = Default( thSAD   , 180   ) # Lower values will reduce artefacts for bad motion vectors, but they will also have a lighter denoising effect. For animated sources, 120 seems to be a good setting to start with.
thSADC  = Default( thSADC  , thSAD )
thSCD1  = Default( thSCD1  , 400   )
thSCD2  = Default( thSCD2  , 102   )

#Processing

input = clp

mask = clp.invert().levels(82,1,155,0,255,coring=false).Removegrain(12)

nse  = defined(PreNoise) ? PreNoise 
       \: (Noise==true) ? clp.blockbuster(method="noise",Block_size=BkSize) : clp

clp = (Noise==false) ? clp : (lmask==true) ? mt_merge(clp,nse,mask) : nse

sup = clp.Msuper(hpad=Blk,vpad=Blk,PEL=PEL,levels=0)

BV1 = MAnalyse(sup,isb=true ,truemotion=true,delta=1,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap)
FV1 = MAnalyse(sup,isb=false,truemotion=true,delta=1,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap)

BV2 = (Degrain>=2) ? MAnalyse(sup,isb=true ,truemotion=true,delta=2,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 
FV2 = (Degrain>=2) ? MAnalyse(sup,isb=false,truemotion=true,delta=2,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 

BV3 = (Degrain==3) ? MAnalyse(sup,isb=true ,truemotion=true,delta=3,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 
FV3 = (Degrain==3) ? MAnalyse(sup,isb=false,truemotion=true,delta=3,blksize=Blk,blksizeV=Blk,DCT=DCT,overlap=Overlap) : Blackness () 

clp = (degrain==1) ? clp.MDegrain1(sup,BV1,FV1,                thSAD=thSAD,thSADC=thSADC,thSCD1=thSCD1,thSCD2=thSCD2,limit=limit): 
 \    (degrain==2) ? clp.MDegrain2(sup,BV1,FV1,BV2,FV2,        thSAD=thSAD,thSADC=thSADC,thSCD1=thSCD1,thSCD2=thSCD2,limit=limit):
 \                   clp.MDegrain3(sup,BV1,FV1,BV2,FV2,BV3,FV3,thSAD=thSAD,thSADC=thSADC,thSCD1=thSCD1,thSCD2=thSCD2,limit=limit)

Diff1 = mt_makediff(input,clp,U=3,V=3)

clp2  = clp.ttempsmoothf(maxr=maxr,strength=strength,lthresh=lthresh,cthresh=cthresh)

Diff2 = mt_makediff(input,clp2,U=3,V=3)

lutxy = mt_lutxy(Diff1,Diff2,"x 128 - abs y 128 - abs < x y ?")

output = (HQ==true) ? Input.mt_makediff(lutxy,U=3,V=3) : clp2

output }

Locked

Return to “AviSynth Help”