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 1st May 2021, 10:27   #1  |  Link
Dogway
Registered User
 
Join Date: Nov 2009
Posts: 1,918
Grade Pack v1.2

EDIT: Previously titled Understanding Expr()

Grade Pack on GitHub.

#####################

I'm trying to port a contrast function to internal function Expr(), it works but I'm not sure I'm coding it efficiently.
At first glance, does the code look fine? Am I supposed to do "x range_max /" and "range_max *" to scale source to float (and back), or is there something I can do with scale_inputs? Same with my bdpth variable workflow.
And is there a way to limit it to TV levels within Expr() or is a range conversion with ColorYUV() preferred (faster)?


Code:
    cont = pow(cont + 1., 3.);

    float knee = 1. / (1. + exp(cont * pivot));
    float shldr = 1. / (1. + exp(cont * (pivot - 1.)));

    color = (1. / (1. + exp(cont * (pivot - color))) - knee) / (shldr - knee);

Code:
function SmoothContrast2(clip c, float "cont", int "pivot"){

    cont    = Default(cont, 0.0)
    pivot   = Default(pivot, 127)

    sisphbd = AvsPlusVersionNumber > 2294
    bdpth   = sisphbd ? pow(256., BitsPerComponent(c)/8.)/256. : 1.
    rmax    = 255.0*bdpth
    pivot   = float(pivot*bdpth)/rmax

    cont = pow(cont + 1.0, 3.0)

    knee  = "1.0 1.0 "+string(cont)+" "+string(pivot)+" * exp + /"
    shldr = "1.0 1.0 "+string(cont)+" "+string(pivot)+" 1.0 - * exp + /"
    expr(c,"1.0 1.0 "+string(cont)+" "+string(pivot)+" x range_max / - * exp + / "+knee+" - "+shldr+" "+knee+" - / range_max *", "x", "x", scale_inputs = "none" )
}

Last edited by Dogway; 21st May 2021 at 17:27.
Dogway is offline   Reply With Quote
Old 1st May 2021, 18:05   #2  |  Link
real.finder
Registered User
 
Join Date: Jan 2012
Location: Mesopotamia
Posts: 2,532
range_max and others, and scale_inputs are just helpers for making RPN expression work with 8-16 bit and float, you can make code without them just like original expr in vs

anyway, in general scale_inputs is slower theoretically

Quote:
And is there a way to limit it to TV levels within Expr() or is a range conversion with ColorYUV() preferred (faster)?
you can but I think you will add more codes to RPN expression to clamp it to TV range (with clip function same as MaskTools2 lut) and it may become slower than using ColorYUV or maybe faster I didn't check but if it in lut it will be faster than additional ColorYUV (theoretically in this case)
__________________
See My Avisynth Stuff
real.finder is offline   Reply With Quote
Old 1st May 2021, 18:25   #3  |  Link
Dogway
Registered User
 
Join Date: Nov 2009
Posts: 1,918
Thanks, yes, I looked through some old code and checked about range limiting manually, just though there could be some automation available.

So optimization and grammar wise the next looks fine for all bitdepths, right? (not worried about old avs versions):

Code:
function SmoothContrast2(clip c, float "cont", int "pivot", float "shoulder"){

    cont    = Default(cont, 0.0)
    pivot   = Default(pivot, 127)
    sh      = Default(shoulder, 1.0)

    sisphbd = AvsPlusVersionNumber > 2294
    bdpth   = sisphbd ? pow(256., BitsPerComponent(c)/8.)/256. : 1.
    rmax    = 255.0*bdpth
    pivot   = float(pivot*bdpth)/rmax

    csign = cont < 0.0 ? -1.0 : 1.0
    cont  = pow(cont + csign, 3.0)

    str  = "1.0 1.0 "+string(cont)+" "+string(pivot)+" "
    knee  = str + " * exp + /"
    shldr = str + " 1.0 - * exp + /"

    cont < 0.0 ? Eval("""
    expr(c," "+string(pivot)+" 1.0 x range_max / "+shldr+" "+knee+" - * "+knee+" + / 1.0 - log "+string(cont)+" / - range_max *", "", "", scale_inputs = "none" )""") : \
    \
    Eval("""
    expr(c,"1.0 1.0 "+string(cont)+" "+string(pivot)+" x range_max / - * exp + / "+knee+" - "+shldr+" "+knee+" - / range_max *", "", "", scale_inputs = "none" )""") }
This is sigmoidal contrast, I'm not sure there's a function like this available.

Am I really supposed to do?
Code:
bdpth   = sisphbd ? pow(256., BitsPerComponent(c)/8.)/256. : 1.

I've been testing the speed of various functions, apparently expr() is for the most part faster than masktools2 (4790K CPU here), except for mt_makediff().
It's even faster than Overlay for blending 2 clips through a mask.

Code:
expr(a,b,msk,"x range_max z - * y z * + range_size / ",scale_inputs="none" )

Tried to test it against ColorYUV(levels="TV->PC") and it's slower though.

Code:
toPC="x ymin - ymax ymin - / range_max *"
toTV="x ymax ymin - * range_max / ymin + "
toPCc="x range_half - cmax cmin - / range_max * range_half +"
toTVc="x range_half - cmax cmin - * range_max / range_half + "
expr(c,toPC,toPCc,toPCc, scale_inputs = "none" )
expr(toTV,toTVc,toTVc, scale_inputs = "none" )

Last edited by Dogway; 1st May 2021 at 18:31.
Dogway is offline   Reply With Quote
Old 1st May 2021, 18:37   #4  |  Link
real.finder
Registered User
 
Join Date: Jan 2012
Location: Mesopotamia
Posts: 2,532
didn't check the code yet but if you don't care about non plus avs then you can remove Eval and use if{} else{} http://avisynth.nl/index.php/GScript#AviSynth.2B
__________________
See My Avisynth Stuff
real.finder is offline   Reply With Quote
Old 1st May 2021, 18:49   #5  |  Link
Dogway
Registered User
 
Join Date: Nov 2009
Posts: 1,918
Great, I'm more worried about all the code to make the vars in float range, probably should use the scalef decorator or something but didn't have much success while trying it.

I found out that indeed ColorYUV(cont_y=125.0) is sigmoidal contrast, in any case you can't tweak the pivot point and it's 7% slower than my function above (probably because I'm not processing chroma planes).
Dogway is offline   Reply With Quote
Old 1st May 2021, 19:07   #6  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,155
Warning, ColorYUV TV->PC and PC->TV mod chroma too to avoid colorshift. [TV->PC.Y Does NOT]
[EDIT: Above "colorshift", think is saturation shift, not sure]

Also, below seems a bit complicated
Code:
bdpth   = sisphbd ? pow(256., BitsPerComponent(c)/8.)/256. : 1.
Code:
bdpth   = sisphbd ? pow(2, BitsPerComponent(c)-8)       : 1.

# OR

bdpth   = sisphbd ? BitLShift(1, BitsPerComponent(c)-8) : 1.
EDIT:
Quote:
probably because I'm not processing chroma planes
Doggy had not posted that when I wrote above.
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 1st May 2021 at 19:24.
StainlessS is offline   Reply With Quote
Old 1st May 2021, 19:09   #7  |  Link
real.finder
Registered User
 
Join Date: Jan 2012
Location: Mesopotamia
Posts: 2,532
also http://avisynth.nl/index.php/Internal_functions#Format

"1.0 1.0 "+string(cont)+" "+string(pivot)+" * exp + /"

can be

"1.0 1.0 {} {} * exp + /".Format(cont,pivot)

or even "1.0 1.0 {cont} {pivot} * exp + /".Format()
__________________
See My Avisynth Stuff

Last edited by real.finder; 1st May 2021 at 19:16.
real.finder is offline   Reply With Quote
Old 2nd May 2021, 12:43   #8  |  Link
Dogway
Registered User
 
Join Date: Nov 2009
Posts: 1,918
Thanks for the help, had no idea of most of them, basically because no one is coding in this style. I plan to modernize all my scripts to squeeze performance to max as filtering HD content is very slow even on my CPU.
If you still find something iffy, please let me know.

Code:
###
###                    v3.0 (19-01-2014)
### SmoothContrast() - v4.0 (02-05-2021)
###                  By Dogway
###
### Applies contrast in the "S" (sigmoidal) curve fashion.
###
###
### No dependencies
### 
### Performs better in gamma encoded space
###
### Example: SmoothContrast(0.7, 128, 0.0, "PC")
###
####################################


function SmoothContrast(clip c, float "cont", int "pivot", float "sat", string "range"){


    cont    = Default(cont, 0.0)
    sat     = Default(sat,  cont/1.5)
    range   = Default(range,  "TV")
    pivot   = Defined(pivot) ? range == "TV" ? (pivot-16.)/219. : pivot/255. : range == "TV" ? 0.50913 : 0.5

    range = range == "TV"
    Assert(AvsPlusVersionNumber > 2294, "Update Avisynth+ version")

    cont    = pow(cont + sign(cont), 3.)
    sat     = sat != 0. ? (1.-max(min(sat, 1.), -1.))/2. - 0.5 : 0.

    rangePC  = range ? "x ymin - ymax ymin - /" : "x range_max /"
    rangeTV  = range ? "ymax ymin - * range_max / ymin +" : ""
    str    = "1. 1. {cont} {pivot}"
    knee   = str + " * exp + /"
    shldr  = str + " 1. - * exp + /"
    ycont  = ""+knee+" A^ "+str+" "+rangePC+" - * exp + / A - "+shldr+" A - / range_max * "+rangeTV+""
    yconti = ""+knee+" A^ {pivot} 1. "+rangePC+" "+shldr+" A - * A + / 1. - log {cont} / - range_max * "+rangeTV+""
    cntrst = cont != 0. ? cont > 0. ? Format(ycont) : Format(yconti) : ""

    rangePCc  = range ? "x range_half - cmax cmin - / range_max * range_half +" : "x"
    rangeTVc  = range ?   "range_half - cmax cmin - * range_max / range_half +" : ""

    strtn  = IsRGB(c) && IsPlanar(c) ? cntrst : sat != 0. ? Format(""+rangePCc+" A^ 1. {sat} - A * {sat} range_max A - * + "+rangeTVc+"") : ""

    expr(c, cntrst, strtn, strtn, scale_inputs = "none" ) }
Dogway is offline   Reply With Quote
Old 2nd May 2021, 13:49   #9  |  Link
gispos
Registered User
 
Join Date: Oct 2018
Location: Germany
Posts: 759
Quote:
Originally Posted by Dogway View Post
Thanks for the help, had no idea of most of them, basically because no one is coding in this style. I plan to modernize all my scripts to squeeze performance to max as filtering HD content is very slow even on my CPU.
If you still find something iffy, please let me know.

Code:
###
###                    v3.0 (19-01-2014)
### SmoothContrast() - v4.0 (02-05-2021)
###                  By Dogway
###
### Applies contrast in the "S" (sigmoidal) curve fashion.
###
###
### No dependencies
### 
### Performs better in gamma encoded space
###
### Example: SmoothContrast(0.7, 128, 0.0, "PC")
###
####################################


function SmoothContrast(clip c, float "cont", int "pivot", float "sat", string "range"){


    cont    = Default(cont, 0.0)
    sat     = Default(sat,  cont/1.5)
    range   = Default(range,  "TV")
    pivot   = Defined(pivot) ? range == "TV" ? (pivot-16.)/219. : pivot/255. : range == "TV" ? 0.50913 : 0.5

    range = range == "TV"
    Assert(AvsPlusVersionNumber > 2294, "Update Avisynth+ version")

    cont    = pow(cont + sign(cont), 3.)
    sat     = sat != 0. ? (1.-max(min(sat, 1.), -1.))/2. - 0.5 : 0.

    rangePC  = range ? "x ymin - ymax ymin - /" : "x range_max /"
    rangeTV  = range ? "ymax ymin - * range_max / ymin +" : ""
    str    = "1. 1. {cont} {pivot}"
    knee   = str + " * exp + /"
    shldr  = str + " 1. - * exp + /"
    ycont  = ""+knee+" A^ "+str+" "+rangePC+" - * exp + / A - "+shldr+" A - / range_max * "+rangeTV+""
    yconti = ""+knee+" A^ {pivot} 1. "+rangePC+" "+shldr+" A - * A + / 1. - log {cont} / - range_max * "+rangeTV+""
    cntrst = cont != 0. ? cont > 0. ? Format(ycont) : Format(yconti) : ""

    rangePCc  = range ? "x range_half - cmax cmin - / range_max * range_half +" : "x"
    rangeTVc  = range ?   "range_half - cmax cmin - * range_max / range_half +" : ""

    strtn  = IsRGB(c) && IsPlanar(c) ? cntrst : sat != 0. ? Format(""+rangePCc+" A^ 1. {sat} - A * {sat} range_max A - * + "+rangeTVc+"") : ""

    expr(c, cntrst, strtn, strtn, scale_inputs = "none" ) }
Just tested and found to be good. Any more of it?
__________________
Live and let live
gispos is offline   Reply With Quote
Old 2nd May 2021, 17:08   #10  |  Link
poisondeathray
Registered User
 
Join Date: Sep 2007
Posts: 4,969
Thanks, it's a handy function, and I love that it can work in RGB too

If someone has time, please consider writing a expr based "hdragc" or "shadow boost" function; or even a shadow/highlight filter (like adobe). Those are common use scenarios like adjusting contrast.
poisondeathray is offline   Reply With Quote
Old 2nd May 2021, 23:07   #11  |  Link
Dogway
Registered User
 
Join Date: Nov 2009
Posts: 1,918
I compared it against ColorYUV contrast and it's slower, but it doesn't seem to do the same. I think ColorYUV is simply a linear contrast (Lift + Gain) and it clips to TV Range using "coring" instead of working within TV range.

I'm currently using the function as a prefilter for SMDegrain because for some reason in HBD it denoises too much destroying low contrast details. Tested also using local contrast but didn't work as expected and it might also enhance noise, so no good.

I'm uploading to my github, check there for future changes and more stuff.
https://github.com/Dogway/Avisynth-S...rast.v4.0.avsi
Dogway is offline   Reply With Quote
Old 3rd May 2021, 00:49   #12  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,155
Doggy,
See Here, ColorYUV2(8 bit only):- https://forum.doom9.org/showthread.p...13#post1443313

Get the ColorYUV2 Graffer,
RPow is same as ColorYUV Gamma,
SPow, not part of standard ColorYUV.



ColorYUV2 Graffer
http://www.mediafire.com/file/wq4f9y...b_21120614.zip

Leave SPow, and Pord at defaults and behaves same as Standard 8 bit ColorYUV.

EDIT:
1_Cont_512


2_Cont_256.jpeg


3_Cont_0.jpg


4_Cont_Minus_256.jpeg [ie flat horizontal]


5_Cont_Minus_512.jpeg


6_Cont_Minus_1024.jpeg
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 3rd May 2021 at 02:42.
StainlessS is offline   Reply With Quote
Old 13th May 2021, 14:53   #13  |  Link
Dogway
Registered User
 
Join Date: Nov 2009
Posts: 1,918
I'm updating all my functions to modern code so finally put SmoothContrast into a "Grade Pack" file with other functions I made and just updated.

Updated Vignette, now it should be faster.

Updated SatMask, now not anymore in MasksPack. Still needs to fix an issue in "Dullness" mode, but "Vibrance" works fine.

Included new function SmoothLevels, behaves exactly like Internal Levels() but with value autoscaling, so you can use SmoothLevels(16, 1.200, 255, 0, 255) and it will work over any bitdepth.
Dogway is offline   Reply With Quote
Old 13th May 2021, 15:36   #14  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,155
Good Dog
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???
StainlessS is offline   Reply With Quote
Old 13th May 2021, 16:27   #15  |  Link
Dogway
Registered User
 
Join Date: Nov 2009
Posts: 1,918
Quote:
Originally Posted by StainlessS View Post
Good Dog



By the way, also updated Logo() and MasksPack.

I'm also almost done with "Stabilization Tools Pack", Stab() was in a really mess condition but also touched FixBorders(). I want to do FilmGateFix() as well but code is a bit convoluted and I want to improve it at the same time so will do later.

Want to update MatteCrop() with your suggestion too, and then I will resume TransformsPack which is already in RC4 internally but need to clean it up.

EDIT: Topic moved to Filters Packs thread.

Last edited by Dogway; 19th August 2021 at 23:47.
Dogway is offline   Reply With Quote
Old 13th May 2021, 16:51   #16  |  Link
kedautinh12
Registered User
 
Join Date: Jan 2018
Posts: 1,137
Thanks
kedautinh12 is online now   Reply With Quote
Reply

Tags
bitdepth, expr, expression, scale_inputs

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 00:50.


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