Log in

View Full Version : Add grain only to debanded areas of the picture


McCauley
30th May 2008, 21:37
Hi folks,

could someone tell me a way to add grain only to debanded areas of the picture?
I guess it could be done done with masktools, but i haven't found (or understood) the appropiate filter, nor did the search reveal something i found useful.

Regards and thanks in advance
McCauley

Dark Shikari
30th May 2008, 23:11
Are you trying to get the encoder to retain the debanding dither?

If the answer is yes, I have an experimental x264 patch which might be able to help you with that...

mahsah
31st May 2008, 02:58
I would also be interested in that patch. You think you could post a win32 binary?

Dark Shikari
31st May 2008, 03:11
Here's a simple unthreaded build (http://mirror05.x264.nl/Dark/force.php?file=./x264_new_metric.exe); note its my dev build so it contains a few unrelated patches related to b8x8 and p4x4 (minor RD improvements).

How to use: The more RD, the better. Subme6? Good. B-rdo? Better. Subme7? Even better.

Use deadzones (trellis 0) with it; I'm not sure how well trellis 1 would perform. Trellis 2 should be fine I guess.

The purpose of the algorithm used here is to attempt to weight higher the importance of retaining complexity in the output video; this generally results in better-kept dither and sometimes results like this (http://i27.tinypic.com/a4ldtj.png) (without new algorithm, same bitrate (http://i29.tinypic.com/344fpd3.png)).

mahsah
31st May 2008, 03:37
Neat, thanks.

Do I need to add any special command line switches like with FGO?

EDIT:
What deadzone settings would you recommend?

Dark Shikari
31st May 2008, 03:53
Neat, thanks.

Do I need to add any special command line switches like with FGO?No.EDIT:
What deadzone settings would you recommend?Anything, defaults are fine.

LaTo
31st May 2008, 07:12
### Udeband() by LaTo INV.
###
### Need : gradfun2db, addgrain, MT masktools
###
function Udeband(clip src, float "GFthr", int "AGstr") {
GFthr = default(GFthr,1.2)
AGstr = default(AGstr,4)
noband = src.addborders(16,16,16,16).gradfun2db(GFthr).crop(16,16,-16,-16).addgrain(AGstr,0,0)
mask = mt_luts(src,src,mode="range",pixels=mt_square(1),expr="y").mt_lut(expr="400 x 2.5 - abs 1.7 ^ /")
deband = mt_merge(src,noband,mask,luma=true)
return deband }
###
Udeband(src, GFthr, AGstr)
GFthr = gradfun2db threshold
AGstr = addgrain strength

ex: Udeband(last, 1.2, 4) (default)

McCauley
31st May 2008, 20:39
Merci, LaTo, that was exactly what i was looking for!
Dark Shikari, looking forward to test your build, thanks for all your patches!

Regards
McCauley

Adub
31st May 2008, 21:37
I have been looking for something like this. good work LaTo.

thetoof
1st June 2008, 10:42
Yeah, great job. Maybe this could be used with the patch your making Dark Shikari... iirc, it's only meant to optimize grain that already exists, so if you want to apply it on a source that already contains banding, this function could be very helpful!

Thanks LaTo!

Dark Shikari
1st June 2008, 17:10
Yeah, great job. Maybe this could be used with the patch your making Dark Shikari... iirc, it's only meant to optimize grain that already exists, so if you want to apply it on a source that already contains banding, this function could be very helpful!

Thanks LaTo!My idea was that you run gradfun2db and that should be sufficient. I did that for an encode of a concert video with a whole lot of banding in the background due to the spotlights/etc.

Akatsuker
19th September 2008, 23:31
About the gradfun2db, I know that it's made for POSTprocessing.
But, and this Udeband one? Is it about postprocessing too?

If i put it on the input avs to encode, as I do with my source, for example, could it work?

LaTo
20th September 2008, 06:47
About the gradfun2db, I know that it's made for POSTprocessing.
But, and this Udeband one? Is it about postprocessing too?

If i put it on the input avs to encode, as I do with my source, for example, could it work?

it's better for postprocessing, but you can use it to encode... (Use a x264 build with psyrdo to keep the effect)

R3Z
20th January 2009, 07:09
### Udeband() by LaTo INV.
###
### Need : gradfun2db, addgrain, MT masktools
###
function Udeband(clip src, float "GFthr", int "AGstr") {
GFthr = default(GFthr,1.2)
AGstr = default(AGstr,4)
noband = src.addborders(16,16,16,16).gradfun2db(GFthr).crop(16,16,-16,-16).addgrain(AGstr,0,0)
mask = mt_luts(src,src,mode="range",pixels=mt_square(1),expr="y").mt_lut(expr="400 x 2.5 - abs 1.7 ^ /")
deband = mt_merge(src,noband,mask,luma=true)
return deband }
###
Udeband(src, GFthr, AGstr)
GFthr = gradfun2db threshold
AGstr = addgrain strength

ex: Udeband(last, 1.2, 4) (default)

Sorry to bump this thread but i wish to understand what the bolded lines are actually doing ? I read the doco and it says that it does an mt_edge( mode = "min/max" ). What exactly is that ?

Trying to learn this stuff is pretty hard so i hoped you can help me out here LaTo :)

Cheers
R3Z

MadRat
22nd January 2009, 08:04
I recommend getting the gradfun2db filter and using the gradfunkmirror script. gradfunkmirror does exactally what you're requesting. You should be able to find what you need in this message. http://forum.doom9.org/showthread.php?p=812357#post812357

R3Z
23rd January 2009, 05:33
I recommend getting the gradfun2db filter and using the gradfunkmirror script. gradfunkmirror does exactally what you're requesting. You should be able to find what you need in this message. http://forum.doom9.org/showthread.php?p=812357#post812357

Sorry MadRat, what i want to do is somehow blur and sharpen the grain added to the debanded areas without having it actually blur and sharpen the source.

I dont know how to do this thats all.

Cheers,

R3Z

Sagekilla
23rd January 2009, 06:43
To do that, you would do something like:


source = last
.... (processing)
processed = source.SomeMaskingStuffHere()

diff = mt_makediff(source, processed, U=3, V=3)

diff = diff.Blur(1) # if you want to blur it, for example)

mt_adddiff(source,diff)



diff would give you the amount of grain that was added to the debanded areas, which you could then apply a Blur() or Sharpen() or whatever it is you'd want.

LaTo
23rd January 2009, 21:33
Udeband is outdated, try GradFun2DBmod (http://forum.doom9.org/showthread.php?t=144537) ;)

LaTo
24th January 2009, 08:04
Sorry to bump this thread but i wish to understand what the bolded lines are actually doing ?

mt_luts(src,src,mode="range",pixels=mt_square(1),expr="y")
It's a edgemask... The same as mt_edge("min/max",0,255) but with radius parameter (here 1).

mt_lut(expr="400 x 2.5 - abs 1.7 ^ /")
It allow to consider only areas where this min-max is low (here 1...3).
It's simply a mathematical curve, try with a calculator ;)



Sorry MadRat, what i want to do is somehow blur and sharpen the grain added to the debanded areas without having it actually blur and sharpen the source.
You should read GrainFactory3 function, it could help you...


As Sagekilla said, you can try this one:
source = last
processed = source.Addgrain(10,0,0)

diff = mt_makediff(source, processed, U=1, V=1)

diff = diff.Blur(1).Sharpen(1)

mt_makediff(source,diff,U=2,V=2)

R3Z
25th January 2009, 05:30
Many thanks LaTo and SageKilla. Sorry to ask such dumb questions :o

That being said i am very appreciative of your help :)

Sagekilla
25th January 2009, 07:32
Not really a dumb question :) I was oblivious to masks at one point (I still am, but have a rudimentary knowledge of them at least) so don't feel bad about asking questions.

Besides, it never hurts to ask a question!

@LaTo: Would that lut expression (400 x 2.5 - abs 1.7 ^ /) evaluate to "400 / (abs(x - 2.5) ^ 1.7)" ? If so, wouldn't that make it disproportionately huge if x was something small? (for x = 3, you'd get 1300)

LaTo
25th January 2009, 10:07
@LaTo: Would that lut expression (400 x 2.5 - abs 1.7 ^ /) evaluate to "400 / (abs(x - 2.5) ^ 1.7)" ? If so, wouldn't that make it disproportionately huge if x was something small? (for x = 3, you'd get 1300)
Yes, it's normal because it's automatically clamped to [0:255]
So in fact, it's more or less this: x=>0 && x<=3 ? 255 : 0

Look at this screenshot from GradFun2DBmod : Example (http://latoninf.free.fr/d9/GF/show.png)
Only the sky is debanded ;)

Sagekilla
26th January 2009, 00:21
So basically, if the value is < 3 then you're applying debanding..

LaTo
26th January 2009, 06:14
So basically, if the value is < 3 then you're applying debanding..

yes ;)

R3Z
27th January 2009, 07:48
Just want to say thanks again to Lato and Sagekilla. I ended up achieving the result i wanted with little processing power required;

http://www.users.on.net/~inet_s51/1.jpg (http://www.users.on.net/~inet_s51/2.jpg)

Sorry its a JPEG and a poor source but you get the idea (only video i could find at work). Just using blockbusters detail settings to decide where to put grain.The effect is pleasing to my eyes as i like grain. I would like to learn how to use edge masks and make soft transitions between areas of low and high detail.