Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 14th May 2008, 18:27   #1  |  Link
g-force
Guest
 
Posts: n/a
GTDegrain (function for faster temporal degraining)

Comments/suggestions welcome

-G

Code:
# GTDegrain ver.1.07 28May08
# Function by G-force, heavily borrows from Didée's TemporalDegrain which removes a bit more noise, but may be slower
# Requires mvtools.dll, RemoveGrain.dll, mt_masktools-26.dll
# Changelog:
# 1.07 Added Changelog
# 1.06 TemporalSoften(prefilt,255,255,24,2) was TemporalSoften(prefilt,255,255,25,2)
#      Interleave(fw3,fw2,fw1,bw3,bw2,bw1,input,fw1,fw2,fw3,bw1,bw2,bw3) was Interleave(bw3,bw2,bw1,input,fw1,fw2,fw3)
#      Interleave(fw2,fw1,bw2,bw1,input,fw1,fw2,bw1,bw2) was Interleave(bw2,bw1,input,fw1,fw2)
#      Interleave(fw1,bw1,input,fw1,bw1) was Interleave(bw1,input,fw1)
#      TemporalSoften(degrain,255,255,24,2) was TemporalSoften(degrain,255,255,255,2), 2 places
#      SelectEvery(1+degrain*4,degrain*2) was SelectEvery(1+degrain*2,degrain)
# 1.05 Replaced TTempsmoothF with TemporalSoften, 3 places 
#      Added .MT_Lutxy(last,"x 128 - abs y 128 - abs < x y ?")
# 1.04 Switched from MVDegrain to MVCompensate/TemporalSoften
# 1.03 Release

function GTDegrain (clip input, clip "denoise", int "degrain", int "prefilt", int "pel", int "sharp",  
    \     int "blksize", int "overlap", int "dct", int "search", int "idx", int "thSAD")

{                                    # Denoise is a pre-filtered external clip for use instead of internal pre-filter
degrain = default(degrain,2)         # Degrain temporal radius (1 to 3)
prefilt = default(prefilt,4)         # prefilter strength (1 to 7)
pel     = default(pel,2)             # Mvector subpixel interpolation precision (1, 2, or 4)
sharp   = default(sharp,1)           # Mvector subpixel interpolation method (0, 1, or 2(best))
blksize = default(blksize,8)         # Mvector blocksize (use 16 for more speed or HD resolution)
overlap = default(overlap,blksize/2) # Mvector block overlap amount (Max is blksize/2)
dct     = default(dct,0)             # Mvector using of DCT for blocks difference calculation (0 to 4)
search  = default(search,2)          # Mvector type of search at every level (0 to 3)
idx     = default(idx,1)             # MV initial idx value
thSAD   = default(thSAD,400)         # MV thSAD value (use 300 for additional artifact protection)

########## prefilter (for obtaining motion vectors)
defined(denoise) ? denoise : input.TemporalSoften(prefilt,255,255,24,2).Repair(input,9)

########## get motion vectors
bw1 = last.MVAnalyse(isb=true, delta=1,pel=pel,sharp=sharp,blksize=blksize,overlap=overlap,dct=dct,search=search,idx=idx)
fw1 = last.MVAnalyse(isb=false,delta=1,pel=pel,sharp=sharp,blksize=blksize,overlap=overlap,dct=dct,search=search,idx=idx)
bw2 = (degrain>=2) ? 
    \ last.MVAnalyse(isb=true, delta=2,pel=pel,sharp=sharp,blksize=blksize,overlap=overlap,dct=dct,search=search,idx=idx)
    \ : NOP()
fw2 = (degrain>=2) ? 
    \ last.MVAnalyse(isb=false,delta=2,pel=pel,sharp=sharp,blksize=blksize,overlap=overlap,dct=dct,search=search,idx=idx)
    \ : NOP()
bw3 = (degrain>=3) ? 
    \ last.MVAnalyse(isb=true, delta=3,pel=pel,sharp=sharp,blksize=blksize,overlap=overlap,dct=dct,search=search,idx=idx)
    \ : NOP()
fw3 = (degrain>=3) ? 
    \ last.MVAnalyse(isb=false,delta=3,pel=pel,sharp=sharp,blksize=blksize,overlap=overlap,dct=dct,search=search,idx=idx)
    \ : NOP()

########## apply motion vectors
bw1 = input.MVCompensate(bw1,thSAD=thSAD,idx=idx+1)
fw1 = input.MVCompensate(fw1,thSAD=thSAD,idx=idx+1)
bw2 = (degrain>=2) ? input.MVCompensate(bw2,thSAD=thSAD,idx=idx+1) : NOP()
fw2 = (degrain>=2) ? input.MVCompensate(fw2,thSAD=thSAD,idx=idx+1) : NOP()
bw3 = (degrain>=3) ? input.MVCompensate(bw3,thSAD=thSAD,idx=idx+1) : NOP()
fw3 = (degrain>=3) ? input.MVCompensate(fw3,thSAD=thSAD,idx=idx+1) : NOP()

########## 1st degrain stage
inter = (degrain>=3) ? Interleave(fw3,fw2,fw1,bw3,bw2,bw1,input,fw1,fw2,fw3,bw1,bw2,bw3) 
    \ : (degrain==2) ? Interleave(fw2,fw1,bw2,bw1,input,fw1,fw2,bw1,bw2) 
    \ : Interleave(fw1,bw1,input,fw1,bw1)
inter.TemporalSoften(degrain,255,255,24,2)

########## limit 1st degrain stage
last.Repair(inter,9)

########## 2nd degrain stage
TemporalSoften(degrain,255,255,24,2)
SelectEvery(1+degrain*4,degrain*2)

########## contra-sharpening (sharpen, but don't add more than what denoising removed)
nr = last
nr.RemoveGrain(4,-1)                      # remove some spots before estimating sharpening
MT_MakeDiff(last,last.RemoveGrain(12,-1)) # difference of a simple kernel blur (overestimate sharpening)
last.Repair(MT_MakeDiff(input,nr),1,-1).MT_Lutxy(last,"x 128 - abs y 128 - abs < x y ?")
    # limit the difference to only the sharpness that the denoising removed 
    # and make sure that the abs limited difference is less than the abs difference
nr.MT_AddDiff(last,U=2,V=2)               # apply the limited difference (add the sharpness back in)

output = last
return(output)
}

Last edited by g-force; 28th May 2008 at 20:25.
  Reply With Quote
Old 15th May 2008, 05:48   #2  |  Link
thetoof
Sleepy overworked fellow
 
Join Date: Feb 2008
Location: Maple syrup's homeland
Posts: 933
Oh I remember where that came from

Will do some tests soon.
__________________
AnimeIVTC() - v2.00
-http://boinc.berkeley.edu/-
Let all geeks use their incredibly powerful comps for the greater good (no, no, it won't slow your filtering/encoding :p)
thetoof is offline   Reply With Quote
Old 22nd May 2008, 16:27   #3  |  Link
g-force
Guest
 
Posts: n/a
Updated the code. Now even faster!
  Reply With Quote
Old 22nd May 2008, 16:47   #4  |  Link
Didée
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 5,394
g-force, is there a certain reason why you did remove the mt_lutxy - line in the contrasharpen section?

I had a certain reason to put it in, and now I'm curious about your reason to remove it again ...
__________________
- We´re at the beginning of the end of mankind´s childhood -

My little flickr gallery. (Yes indeed, I do have hobbies other than digital video!)
Didée is offline   Reply With Quote
Old 22nd May 2008, 17:42   #5  |  Link
thetoof
Sleepy overworked fellow
 
Join Date: Feb 2008
Location: Maple syrup's homeland
Posts: 933
Hmmm... first test with GTDegrain (on the sample where TD removed the dirt removedirt couldn't)
TD = 2,2 fps
GT 1.03 = 2 fps
GT 1.04 = 2,2 fps
GTemporalDegrainFaster = 4,5 fps

Where did all your speed improvement go?
__________________
AnimeIVTC() - v2.00
-http://boinc.berkeley.edu/-
Let all geeks use their incredibly powerful comps for the greater good (no, no, it won't slow your filtering/encoding :p)
thetoof is offline   Reply With Quote
Old 22nd May 2008, 18:13   #6  |  Link
g-force
Guest
 
Posts: n/a
Quote:
Originally Posted by Didée View Post
g-force, is there a certain reason why you did remove the mt_lutxy - line in the contrasharpen section?

I had a certain reason to put it in, and now I'm curious about your reason to remove it again ...
I couldn't see that it did much visually. So your comparing (denoised-blurred denoise) with the same thing lightly repaired by (original-denoised), and taking the one closest to grey (I think). What does this do exactly? It looks like some sort of edge detector. Are there some sources that this helps?

If you feel pretty strongly that it's better with it in, I'll put it back!

-G
  Reply With Quote
Old 22nd May 2008, 18:23   #7  |  Link
g-force
Guest
 
Posts: n/a
Quote:
Originally Posted by thetoof View Post
Hmmm... first test with GTDegrain (on the sample where TD removed the dirt removedirt couldn't)
TD = 2,2 fps
GT 1.03 = 2 fps
GT 1.04 = 2,2 fps
GTemporalDegrainFaster = 4,5 fps

Where did all your speed improvement go?
Oh no! Really? I just can't see how that could be...

-G
  Reply With Quote
Old 22nd May 2008, 18:44   #8  |  Link
g-force
Guest
 
Posts: n/a
thetoof,

make sure you're using TTempSmooth v0.9.4.

-G
  Reply With Quote
Old 22nd May 2008, 19:09   #9  |  Link
Didée
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 5,394
Regarding speed, with the way you're using ttempsmoothF, you can as well replace it with plain temporalsoften. (You're actually not using any features that make a difference, and for plain averaging, temporalsoften is faster.)

Regarding contra-sharpen, look at THIS.

(Before changing things, better make sure you've *understood* what they're doing.)
__________________
- We´re at the beginning of the end of mankind´s childhood -

My little flickr gallery. (Yes indeed, I do have hobbies other than digital video!)
Didée is offline   Reply With Quote
Old 22nd May 2008, 20:35   #10  |  Link
g-force
Guest
 
Posts: n/a
Quote:
Originally Posted by Didée View Post
Regarding speed, with the way you're using ttempsmoothF, you can as well replace it with plain temporalsoften. (You're actually not using any features that make a difference, and for plain averaging, temporalsoften is faster.)

Regarding contra-sharpen, look at THIS.

(Before changing things, better make sure you've *understood* what they're doing.)
Ugh, I am blocked from Mediafire here at work, but it must be good to deserve such a scolding! It's going back in. Thanks for the suggestion on TemporalSoften!

-G
  Reply With Quote
Old 22nd May 2008, 20:41   #11  |  Link
Didée
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 5,394
Try this link.

The point is that the mentioned mt_lutxy prevents that unrelated noise gets "restored" (like e.g. block flicker, which the synthetic example shows). Without that line, you might get back some of the crud that already was successfully removed ...

BTW, the scolding was a late punishment for the previous versions of GTDegrain where you had included the impaired contrasharp version as being mine, without noting that you had randomly removed some stuff.
__________________
- We´re at the beginning of the end of mankind´s childhood -

My little flickr gallery. (Yes indeed, I do have hobbies other than digital video!)

Last edited by Didée; 22nd May 2008 at 21:03.
Didée is offline   Reply With Quote
Old 22nd May 2008, 22:30   #12  |  Link
g-force
Guest
 
Posts: n/a
Quote:
Originally Posted by Didée View Post
BTW, the scolding was a late punishment for the previous versions of GTDegrain where you had included the impaired contrasharp version as being mine, without noting that you had randomly removed some stuff.
Sorry Didée, I like to give credit where it's due, but I realize that it's frustrating when people say that something is yours when they have messed with it too much. I hope something like "inspired by" or "borrowed from" is more tasteful for things that get changed. If there is an even more appropriate etiquette, please let me know so that I can be respectful.

-G
  Reply With Quote
Old 23rd May 2008, 00:44   #13  |  Link
Nikos
Registered User
 
Join Date: Jun 2002
Location: Greece
Posts: 236
Why the scenechange parameter value is 255?
In Scripted_MVDegrain3 function the value is 24.
long.temporalsoften(3,255,255,24,2) # instead of TTempSmooth - if and only IF alt-clip is used
__________________
Greece PAL User...
Nikos is offline   Reply With Quote
Old 23rd May 2008, 01:00   #14  |  Link
Terranigma
*Space Reserved*
 
Terranigma's Avatar
 
Join Date: May 2006
Posts: 953
Quote:
Originally Posted by Nikos View Post
Why the scenechange parameter value is 255?
In Scripted_MVDegrain3 function the value is 24.
long.temporalsoften(3,255,255,24,2) # instead of TTempSmooth - if and only IF alt-clip is used
What're you talking about Nikos? It wasn't 24 either, but 99.9
Terranigma is offline   Reply With Quote
Old 23rd May 2008, 01:02   #15  |  Link
ChrisW77
Registered User
 
Join Date: Sep 2006
Posts: 72
I tried this on 640x480 VHS, and it's not even close to Didée's TemporalDegrain.

Using

Code:
source = video
denoised = source.MedianBlur(2,2,2).FluxSmoothT()

SetMTMode(2,6)
TemporalDegrain(source,denoised,degrain=2)
Looks great, runs great (around 12fps), a little soft, but overall great.

Using the same script, but replacing the above with GTDegrain(), results in half the speed, and half the quality. Blurry, not enough noise removal, and slow.
Whats the point, when you can just use the superior TemporalDegrain ?
ChrisW77 is offline   Reply With Quote
Old 23rd May 2008, 01:05   #16  |  Link
Terranigma
*Space Reserved*
 
Terranigma's Avatar
 
Join Date: May 2006
Posts: 953
Quote:
Originally Posted by ChrisW77 View Post
Whats the point, when you can just use the superior TemporalDegrain ?
Or mc_spuds, which doesn't get nearly half the credit it deserves. I guess I could add my reply for mc_spuds whenever I see someone recommending temporaldegrain.
Terranigma is offline   Reply With Quote
Old 23rd May 2008, 01:33   #17  |  Link
ChrisW77
Registered User
 
Join Date: Sep 2006
Posts: 72
Quote:
Originally Posted by Terranigma View Post
Or mc_spuds, which doesn't get nearly half the credit it deserves. I guess I could add my reply for mc_spuds whenever I see someone recommending temporaldegrain.

Yep, I agree, it's rather good. Thing is, it's also very slow. Until I can buy a 10Ghz QuadCore, I will have to do with alternatives.
ChrisW77 is offline   Reply With Quote
Old 23rd May 2008, 02:03   #18  |  Link
Nikos
Registered User
 
Join Date: Jun 2002
Location: Greece
Posts: 236
Terranigma read again the Scripted_MVDegrain3 function post:
Code:
function Scripted_MVDegrain3(clip c, clip "mvbw", clip "mvfw", clip "mvbw2", clip "mvfw2", clip "mvbw3", clip "mvfw3",
 \                        int "thSAD", int "plane", int "limit", clip "pelclip", int "idx")
{
thSAD = default(thSAD, 400)
plane = default(plane, 4)
limit = default(limit, 255)
_idx = default(idx, -11)

thSAD = thSAD / 8

# alt = c.FFT3DFilter(sigma=10,sigma2=6,sigma3=4,sigma4=2,bw=16,bh=16,ow=8,oh=8,bt=1)

SAD_fw3 = c.MVMask(mvfw3, kind=1, ml=thSAD, gamma=0.999, Ysc=255)
SAD_fw2 = c.MVMask(mvfw2, kind=1, ml=thSAD, gamma=0.999, Ysc=255)
SAD_fw1 = c.MVMask(mvfw,  kind=1, ml=thSAD, gamma=0.999, Ysc=255)
SAD_bw1 = c.MVMask(mvbw,  kind=1, ml=thSAD, gamma=0.999, Ysc=255)
SAD_bw2 = c.MVMask(mvbw2, kind=1, ml=thSAD, gamma=0.999, Ysc=255)
SAD_bw3 = c.MVMask(mvbw3, kind=1, ml=thSAD, gamma=0.999, Ysc=255)

comp_fw3 = c.MVCompensate(mvfw3, idx=_idx) # .mt_merge(alt,SAD_fw3,U=3,V=3)
comp_fw2 = c.MVCompensate(mvfw2, idx=_idx) # .mt_merge(alt,SAD_fw2,U=3,V=3)
comp_fw1 = c.MVCompensate(mvfw,  idx=_idx) # .mt_merge(alt,SAD_fw1,U=3,V=3)
comp_bw1 = c.MVCompensate(mvbw,  idx=_idx) # .mt_merge(alt,SAD_bw1,U=3,V=3)
comp_bw2 = c.MVCompensate(mvbw2, idx=_idx) # .mt_merge(alt,SAD_bw2,U=3,V=3)
comp_bw3 = c.MVCompensate(mvbw3, idx=_idx) # .mt_merge(alt,SAD_bw3,U=3,V=3)

black    = blankclip(c,color_yuv=$008080)
long     = interleave( comp_fw3,comp_fw2,comp_fw1,  c,   comp_bw1,comp_bw2,comp_bw3 )
long_SAD = interleave( SAD_fw3, SAD_fw2, SAD_fw1, black, SAD_bw1, SAD_bw2, SAD_bw3  )

long.TTempSmooth(3,255,255,1,1,strength=4,pfclip=long_SAD,fp=false,scthresh=99.9)
# long.temporalsoften(3,255,255,24,2)  # instead of TTempSmooth - if and only IF alt-clip is used
SelectEvery(7,3)
}
__________________
Greece PAL User...
Nikos is offline   Reply With Quote
Old 23rd May 2008, 02:05   #19  |  Link
Terranigma
*Space Reserved*
 
Terranigma's Avatar
 
Join Date: May 2006
Posts: 953
Quote:
Originally Posted by Nikos View Post
Terranigma read again
Code:
long.TTempSmooth(3,255,255,1,1,strength=4,pfclip=long_SAD,fp=false,scthresh=99.9)
# long.temporalsoften(3,255,255,24,2)  # instead of TTempSmooth - if and only IF alt-clip is used
SelectEvery(7,3)
No no, we're both right, but we were talking about different things. I could've sworn that temporalsoften read ttempsmooth.
Terranigma is offline   Reply With Quote
Old 23rd May 2008, 13:40   #20  |  Link
Nikos
Registered User
 
Join Date: Jun 2002
Location: Greece
Posts: 236
The same question:
Why the scenechange parameter in value in temporalsoften is 255?
__________________
Greece PAL User...
Nikos is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:21.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.