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 15th January 2012, 23:55   #1  |  Link
mandarinka
Registered User
 
mandarinka's Avatar
 
Join Date: Jan 2007
Posts: 666
Improving compressibility of grainy sources with little to no visual impact - ideas?

As the subject says... I'd like to hear people's ideas or tricks you use for encoding grainy sources when the goal is to preserve the look. Something like denoising without the effects being visible.

I know that one can use encoding tools like psy trellis, but relying on x264's abilities means that the cost of keeping the grain (or something that looks similar to the original grain) is simply there, compromising the amount of bits we can spend on detail that is underneath that grain.
That's why I'd like to hear your ideas on how to actually make a grainy source compress (even just slightly) better, so that the overall quality can be improved. As said, I'm interested in such filtering that would have as little visual impact as possible.

Some approaches that come to my mind: for example, separating noise and softening it a little bit before adding back; or alternatively some denoising that would only remove faint noise while keeping the coarser, more rough part of it. I assume the latter might be able to preserve the look of film grain, but such effect might actually be partially realised by the encoding itself, so this might not be successful in practice.

P.S.:
Thanks for your input

Last edited by mandarinka; 16th January 2012 at 00:12.
mandarinka is offline   Reply With Quote
Old 16th January 2012, 00:38   #2  |  Link
Didée
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 5,390
Just these days, I plugged a little script with a similar goal: dampen the grain just a little, to keep the original look, and make it fast, because it's kind of PITA if a sluggish script does almost nothing. (And in particular when it's plenty of seasons with plenty of episodes...)

I took Spresso, and extended the spatial principle to temporal, using Fluxsmooth. The basic chain

Code:
Spresso(4,24,2).STpresso(4,24,4,12,3,49,1)
dropped the resulting bitrate (CRF19.5 on a quite grainy 720p stream) from ~6000kbps to ~5000kbps. That's -16%, depending on settings you can expect -5% to -20%.

The script is pretty fast, speed hit is neglectible. (Eventually, the encoding even may go faster, due to the grain becoming "easier".)


Code:
function STPresso(clip clp, int "limit", int "bias", int "RGmode", int "tthr", int "tlimit", int "tbias", int "back")
{
limit  = default( limit,   3 )  # spatial limit:  the spatial part won't change a pixel more than this
bias   = default( bias,   24 )  # bias: the percentage of the spatial filter that will apply
RGmode = default( RGmode,  4 )  # the spatial filter is RemoveGrain, this is its mode
tthr   = default( tthr,   12 )  # temporal threshold for fluxsmooth. Can be set "a good bit bigger" than usually
tlimit = default( tlimit,  3 )  # the temporal filter won't change a pixel more than this
tbias  = default( tbias,  49 )  # the percentage of the temporal filter that will apply
back   = default( back,    1 )  # after all changes have been calculated, reduce all pixel changes by this value. (Shift "back" towards original value)

LIM1  = (limit>0) ? string( round(limit*100.0/bias-1.0) ) : string( round(100.0/bias) )
LIM2  = (limit<0) ? "1" : string(limit)
BIA   = string(bias)
BK = string(back)
TLIM1  = (tlimit>0) ? string( round(tlimit*100.0/tbias-1.0) ) : string( round(100.0/tbias) )
TLIM2  = (tlimit<0) ? "1" : string(tlimit)
TBIA   = string(tbias)

expr  = (limit<0) ?   "x y - abs "+LIM1+" < x x 1 x y - x y - abs / * - ?"
 \                :   "x y - abs 1 < x x "+LIM1+" + y < x "+LIM2+" + x "+LIM1+" - y > x "+LIM2+" - " \ 
                    + "x 100 "+BIA+" - * y "+BIA+" * + 100 / ? ? ?"
texpr  = (limit<0) ?   "x y - abs "+TLIM1+" < x x 1 x y - x y - abs / * - ?"
 \                :   "x y - abs 1 < x x "+TLIM1+" + y < x "+TLIM2+" + x "+TLIM1+" - y > x "+TLIM2+" - " \ 
                    + "x 100 "+TBIA+" - * y "+TBIA+" * + 100 / ? ? ?"

bzz = clp.removegrain(RGmode)
mt_lutxy( clp, bzz, expr, U=3,V=3)
tthr==0 ? last : mt_lutxy(last,last.mt_makediff(mt_makediff(bzz,bzz.fluxsmootht(tthr),U=3,V=3),U=3,V=3),texpr,U=3,V=3)
back==0 ? last : mt_lutxy(last,clp,"x "+BK+" + y < x "+BK+" + x "+BK+" - y > x "+BK+" - y ? ?",U=2,V=2)
}
The spatial part might be a bit too narrow for 1080p encoding (since it's only a 3x3 kernel). It did fit quite well for 720p encoding, though.
__________________
- 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 16th January 2012, 01:13   #3  |  Link
mastrboy
Registered User
 
Join Date: Sep 2008
Posts: 288
I usually run TTempSmoothF(strength=1,maxr=7) to increase compression without noticeable quality loss, but I've seen this create some artifacts on the luma plane on high motion scenes, using motion compensation usually solves this, but the speed will slow down quite a bit...

But considering Didée's history on this forum, his solution is more likely a better option
mastrboy is offline   Reply With Quote
Old 16th January 2012, 15:11   #4  |  Link
pandy
Registered User
 
Join Date: Mar 2006
Posts: 1,038
http://registry.gimp.org/node/11742 - can be nice if someone will implement this as a Avisynth filter...
pandy is offline   Reply With Quote
Old 16th January 2012, 15:51   #5  |  Link
Didée
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 5,390
@pandy: For the time being, you can do with FFT3dFilter or dfttest. Or generally with any spatial filter - apply a lowpass, filter the lowpassed image, re-apply the hipass.

___
edit: found a free minute.

Code:
fft3dfilter(sigma=0.01,sigma2=1,sigma3=4,sigma4=8,bw=16,bh=16,ow=8,oh=8,bt=1,plane=0)
__________________
- 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; 16th January 2012 at 16:32.
Didée is offline   Reply With Quote
Old 16th January 2012, 17:22   #6  |  Link
mandarinka
Registered User
 
mandarinka's Avatar
 
Join Date: Jan 2007
Posts: 666
I'm testing the S(T)Presso functions and I'm pleasantly surprised For example, I was able to get reductions even with lowered settings that virtually 'did nothing' (which is nice, although I didn't have much time to check what it does with quality, with low differences in filesize it's hard to tell). Optimal settings would be higher, but so far I was trying it from the obsessive side of things.

What I noticed however is something I assume is fluxsmooth-related, because I recall it from DFMDerainbow: at the default tthr of 12, I was getting some artifacting in pans (can probably happen in other motion), where fluxsmooth got fooled and averaged stuff it shouldn't have (motion compensating prevented it); and also, in an animation source with colour flashes, the averages caused changes in hue in large areas (lowering tthr to about 6 made them mostly go away) or stuff like gun blazes. This probably wouldn't be issue in live action footage, and even here, the averaging shouldn't be that visible in motion, human eyes probably don't care much. I don't know what's the internal operation of fluxsmooth like, but it probably isn't very careful about chroma...

Last edited by mandarinka; 16th January 2012 at 17:25.
mandarinka is offline   Reply With Quote
Old 16th January 2012, 17:50   #7  |  Link
Boulder
Pig on the wing
 
Boulder's Avatar
 
Join Date: Mar 2002
Location: Hollola, Finland
Posts: 4,364
Didée, can you please come up with a modification for 1080p sources? That function looks just like what I was looking for some time ago - improve compressibility but retain much of the detail.
__________________
And if the band you're in starts playing different tunes
I'll see you on the dark side of the Moon...
Boulder is offline   Reply With Quote
Old 16th January 2012, 19:01   #8  |  Link
pandy
Registered User
 
Join Date: Mar 2006
Posts: 1,038
Quote:
Originally Posted by Didée View Post
@pandy: For the time being, you can do with FFT3dFilter or dfttest. Or generally with any spatial filter - apply a lowpass, filter the lowpassed image, re-apply the hipass.
As You know wavelets are bit different than FFT, also You need be very precise with FFT (describing filtered bins range) - they are always pros and cons for each method but IMHO wavelets seems to be more "analog".
pandy is offline   Reply With Quote
Old 16th January 2012, 19:15   #9  |  Link
librarian
Registered User
 
Join Date: Nov 2011
Posts: 63
Playing, like Didée posted (Or generally with any spatial filter - apply a lowpass, filter the lowpassed image, re-apply the hipass), with Vaguedenoiser, a wavelet-based spatial denoiser?
librarian is offline   Reply With Quote
Old 20th March 2012, 09:22   #10  |  Link
bmb
Registered User
 
Join Date: May 2009
Posts: 8
didee i cant seem to get stpresso to work ?
bmb is offline   Reply With Quote
Old 20th March 2012, 09:48   #11  |  Link
blvakillxxmc
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks,i am working
blvakillxxmc is offline   Reply With Quote
Old 20th March 2012, 11:47   #12  |  Link
Didée
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 5,390
Why not? What error message? What script?
__________________
- 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 20th March 2012, 13:39   #13  |  Link
bmb
Registered User
 
Join Date: May 2009
Posts: 8
scratch that, i sorted it, didee is their anyway of contacting you privately ?
bmb is offline   Reply With Quote
Old 20th March 2012, 13:54   #14  |  Link
Didée
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 5,390
Sure. This forum offers private message (PM) functionality.
__________________
- 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 20th March 2012, 14:11   #15  |  Link
bmb
Registered User
 
Join Date: May 2009
Posts: 8
sent u a message didee
bmb is offline   Reply With Quote
Old 23rd April 2012, 15:59   #16  |  Link
SilaSurfer
Registered User
 
SilaSurfer's Avatar
 
Join Date: Oct 2009
Posts: 212
Quote:
Originally Posted by Didée View Post
Just these days, I plugged a little script with a similar goal: dampen the grain just a little, to keep the original look, and make it fast, because it's kind of PITA if a sluggish script does almost nothing. (And in particular when it's plenty of seasons with plenty of episodes...)

I took Spresso, and extended the spatial principle to temporal, using Fluxsmooth. The basic chain

Code:
Spresso(4,24,2).STpresso(4,24,4,12,3,49,1)
dropped the resulting bitrate (CRF19.5 on a quite grainy 720p stream) from ~6000kbps to ~5000kbps. That's -16%, depending on settings you can expect -5% to -20%.

The script is pretty fast, speed hit is neglectible. (Eventually, the encoding even may go faster, due to the grain becoming "easier".)


Code:
function STPresso(clip clp, int "limit", int "bias", int "RGmode", int "tthr", int "tlimit", int "tbias", int "back")
{
limit  = default( limit,   3 )  # spatial limit:  the spatial part won't change a pixel more than this
bias   = default( bias,   24 )  # bias: the percentage of the spatial filter that will apply
RGmode = default( RGmode,  4 )  # the spatial filter is RemoveGrain, this is its mode
tthr   = default( tthr,   12 )  # temporal threshold for fluxsmooth. Can be set "a good bit bigger" than usually
tlimit = default( tlimit,  3 )  # the temporal filter won't change a pixel more than this
tbias  = default( tbias,  49 )  # the percentage of the temporal filter that will apply
back   = default( back,    1 )  # after all changes have been calculated, reduce all pixel changes by this value. (Shift "back" towards original value)

LIM1  = (limit>0) ? string( round(limit*100.0/bias-1.0) ) : string( round(100.0/bias) )
LIM2  = (limit<0) ? "1" : string(limit)
BIA   = string(bias)
BK = string(back)
TLIM1  = (tlimit>0) ? string( round(tlimit*100.0/tbias-1.0) ) : string( round(100.0/tbias) )
TLIM2  = (tlimit<0) ? "1" : string(tlimit)
TBIA   = string(tbias)

expr  = (limit<0) ?   "x y - abs "+LIM1+" < x x 1 x y - x y - abs / * - ?"
 \                :   "x y - abs 1 < x x "+LIM1+" + y < x "+LIM2+" + x "+LIM1+" - y > x "+LIM2+" - " \ 
                    + "x 100 "+BIA+" - * y "+BIA+" * + 100 / ? ? ?"
texpr  = (limit<0) ?   "x y - abs "+TLIM1+" < x x 1 x y - x y - abs / * - ?"
 \                :   "x y - abs 1 < x x "+TLIM1+" + y < x "+TLIM2+" + x "+TLIM1+" - y > x "+TLIM2+" - " \ 
                    + "x 100 "+TBIA+" - * y "+TBIA+" * + 100 / ? ? ?"

bzz = clp.removegrain(RGmode)
mt_lutxy( clp, bzz, expr, U=3,V=3)
tthr==0 ? last : mt_lutxy(last,last.mt_makediff(mt_makediff(bzz,bzz.fluxsmootht(tthr),U=3,V=3),U=3,V=3),texpr,U=3,V=3)
back==0 ? last : mt_lutxy(last,clp,"x "+BK+" + y < x "+BK+" + x "+BK+" - y > x "+BK+" - y ? ?",U=2,V=2)
}
The spatial part might be a bit too narrow for 1080p encoding (since it's only a 3x3 kernel). It did fit quite well for 720p encoding, though.
. Great to be back after some absence, and look at that, the first thing that I find going through the forum another very interesting Didée's function. This would definitely come in handy with those series like you said lots of seasons and episodes. I would just ask you Didée could you make modification to your function to Motion Compensate FluxSmooth, give it a pair of glasses , please. Thanks

Last edited by SilaSurfer; 23rd April 2012 at 16:39.
SilaSurfer is offline   Reply With Quote
Old 23rd April 2012, 23:11   #17  |  Link
TheProfileth
Leader of Dual-Duality
 
TheProfileth's Avatar
 
Join Date: Aug 2010
Location: America
Posts: 134
@SilaSurfer
From what I understand the whole point of using fluxsmooth is that it works very quickly so adding motion compensation atleast proper motion compensation would probably make it a lot less viable as a "quick guiltless compression aid" then again I could be wrong and maybe proper motion compensation could be achieved while not completely ruining the speed.
__________________
I'm Mr.Fixit and I feel good, fixin all the sources in the neighborhood
My New filter is in the works, and will be out soon
TheProfileth is offline   Reply With Quote
Old 25th April 2012, 15:59   #18  |  Link
SilaSurfer
Registered User
 
SilaSurfer's Avatar
 
Join Date: Oct 2009
Posts: 212
Quote:
Originally Posted by TheProfileth View Post
@SilaSurfer
From what I understand the whole point of using fluxsmooth is that it works very quickly so adding motion compensation atleast proper motion compensation would probably make it a lot less viable as a "quick guiltless compression aid" then again I could be wrong and maybe proper motion compensation could be achieved while not completely ruining the speed.
I know it would slow down this function, but IMHO Fluxsmooth doesn't need very slow settings when using Motion compensation of MVTools2. Where it could benefit fluxsmooth is only on scene changes. It would still be faster then Mdegrain2/3, which would be ideal for as I said above for seasons of series (more practical). I will try to modify this myself when I get time.

Last edited by SilaSurfer; 25th April 2012 at 16:04.
SilaSurfer is offline   Reply With Quote
Old 25th April 2012, 21:37   #19  |  Link
TheProfileth
Leader of Dual-Duality
 
TheProfileth's Avatar
 
Join Date: Aug 2010
Location: America
Posts: 134
Quote:
Originally Posted by SilaSurfer View Post
I know it would slow down this function, but IMHO Fluxsmooth doesn't need very slow settings when using Motion compensation of MVTools2. Where it could benefit fluxsmooth is only on scene changes. It would still be faster then Mdegrain2/3, which would be ideal for as I said above for seasons of series (more practical). I will try to modify this myself when I get time.
Hmm I was thinking, Fluxsmooth works on a 3 frame basis right? Just the previous current and next? Well if so then only minimal motion compensation is really necessary as you stated. I may consider putting together a rough demo of a mocomped fluxsmooth in the meantime if I get a chance.
__________________
I'm Mr.Fixit and I feel good, fixin all the sources in the neighborhood
My New filter is in the works, and will be out soon
TheProfileth is offline   Reply With Quote
Old 25th April 2012, 21:50   #20  |  Link
Didée
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 5,390
The rough demo is present, since many many years, in the documentation of MVTools. Section "Usage examples". Just replace DegrainMedian with FluxSmooth.
__________________
- 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
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 01:17.


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