View Full Version : Grade Pack v1.2
Dogway
1st May 2021, 10:27
EDIT: Previously titled Understanding Expr()
Grade Pack (https://github.com/Dogway/Avisynth-Scripts) 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)?
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);
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" )
}
real.finder
1st May 2021, 18:05
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
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)
Dogway
1st May 2021, 18:25
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):
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? 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.
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.
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" )
real.finder
1st May 2021, 18:37
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
Dogway
1st May 2021, 18:49
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).
StainlessS
1st May 2021, 19:07
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
bdpth = sisphbd ? pow(256., BitsPerComponent(c)/8.)/256. : 1.
bdpth = sisphbd ? pow(2, BitsPerComponent(c)-8) : 1.
# OR
bdpth = sisphbd ? BitLShift(1, BitsPerComponent(c)-8) : 1.
EDIT:
probably because I'm not processing chroma planes
Doggy had not posted that when I wrote above.
real.finder
1st May 2021, 19:09
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()
Dogway
2nd May 2021, 12:43
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.
###
### 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" ) }
gispos
2nd May 2021, 13:49
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.
###
### 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?
poisondeathray
2nd May 2021, 17:08
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.
Dogway
2nd May 2021, 23:07
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-Scripts/blob/master/SmoothContrast.v4.0.avsi
StainlessS
3rd May 2021, 00:49
Doggy,
See Here, ColorYUV2(8 bit only):- https://forum.doom9.org/showthread.php?p=1443313#post1443313
Get the ColorYUV2 Graffer,
RPow is same as ColorYUV Gamma,
SPow, not part of standard ColorYUV.
https://s20.postimg.cc/e05bqbhq5/Coloryuv2_Graf.jpg (https://postimg.cc/image/r4aw309rt/)
ColorYUV2 Graffer
http://www.mediafire.com/file/wq4f9yy1kmp6753/ColorYUV2_Graf_v1-0b_21120614.zip
Leave SPow, and Pord at defaults and behaves same as Standard 8 bit ColorYUV.
EDIT:
1_Cont_512
https://i.postimg.cc/zfHTtxnd/1-Cont-512.jpg (https://postimages.org/)
2_Cont_256.jpeg
https://i.postimg.cc/156tjbTV/2-Cont-256.jpg (https://postimages.org/)
3_Cont_0.jpg
https://i.postimg.cc/xdgqPHZs/3-Cont-0.jpg (https://postimages.org/)
4_Cont_Minus_256.jpeg [ie flat horizontal]
https://i.postimg.cc/KvnTMKmW/4-Cont-Minus-256.jpg (https://postimages.org/)
5_Cont_Minus_512.jpeg
https://i.postimg.cc/CxSqHF1x/5-Cont-Minus-512.jpg (https://postimages.org/)
6_Cont_Minus_1024.jpeg
https://i.postimg.cc/90yDZSH9/6-Cont-Minus-1024.jpg (https://postimages.org/)
Dogway
13th May 2021, 14:53
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.
StainlessS
13th May 2021, 15:36
Good Dog :)
Dogway
13th May 2021, 16:27
Good Dog :)
:D
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 (https://forum.doom9.org/showthread.php?t=182881).
kedautinh12
13th May 2021, 16:51
Thanks
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.