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. |
9th February 2022, 22:14 | #1 | Link |
Registered User
Join Date: Dec 2005
Location: Sweden
Posts: 703
|
ScriptClip AverageLuma Value in Function
Im trying to make a function that will detect an average luma of the image and use the resulting number into levels approximately like this:
(but without printing the result with Subtitle) Code:
ScriptClip(Last, """ threshold = 55 luma = AverageLuma ## gives the average luma of the current frame luma < threshold \ ? Levels(0, 1.0+0.5*(threshold-luma)/threshold, 255, 0, 255) \ : last Subtitle("luma=" + String(luma), align=2) """) function AutoL(clip c) { c.ex_levels((luma/340),(luma/6100),237,0,170) return last } Thanks! EDIT: Thought I figured out the "rounding" thing Code:
ex_levels((luma/340%.0f),(luma/6100%.2f),237,0,170) Anyway I got it to work sort of but its really slow: Code:
function AutoL(clip c) { current_frame = 0 luma=Averageluma(c) c.ex_levels((luma/340),(luma/6100),237,0,170) return last } EDIT2: Changing to: Code:
function AutoL(clip c) { current_frame = -1 luma=Averageluma(c) c.ex_levels((luma/340),(luma/6100),237,0,170) return last } Last edited by anton_foy; 10th February 2022 at 00:53. |
10th February 2022, 00:52 | #2 | Link |
Registered User
Join Date: Nov 2009
Posts: 2,361
|
What about this:
Code:
function AutoL(clip c, int "thr") { thr = Default(thr, 55) ScriptClip(c,function[thr] () { luma = AverageLuma() ## gives the average luma of the current frame luma < thr \ ? ex_levels((luma/340),1.0+0.5*(thr-luma)/thr,237,0,170) \ : last } ) }
__________________
i7-4790K@Stock::GTX 1070] AviSynth+ filters and mods on GitHub + Discussion thread |
10th February 2022, 01:06 | #3 | Link | |
Registered User
Join Date: Dec 2005
Location: Sweden
Posts: 703
|
Quote:
BTW looking forward to SceneStats! |
|
10th February 2022, 01:43 | #4 | Link |
Registered User
Join Date: Nov 2009
Posts: 2,361
|
You can use Eval() or use a pointer, I prefer the second.
Code:
function AutoL(clip c, int "thr") { thr = Default(thr, 55) ScriptClip(c,function[thr] () { luma = AverageLuma() orig = last ex_levels((luma/340),1.0+0.5*(thr-luma)/thr,237,0,170) blabla() luma < thr : last : orig } ) }
__________________
i7-4790K@Stock::GTX 1070] AviSynth+ filters and mods on GitHub + Discussion thread |
10th February 2022, 10:20 | #5 | Link | |
Registered User
Join Date: Dec 2005
Location: Sweden
Posts: 703
|
Quote:
Code:
function AutoNR(clip c, string "Preset", int "TR", int "BLK", int "THSAD", float "SHARP", \ int "WARP") { # Select presets: Preset = default( Preset, "Medium" ) pNum = (Preset == "Low" ) ? 0 : \ (Preset == "Medium" ) ? 1 : \ (Preset == "High" ) ? 2 : \ Assert( FALSE , "'Preset' choice is invalid" ) # Preset defaults: Low Medium High TR = default( TR, Select( pNum, 1 , 3 , 4 ) ) BLK = default( BLK, Select( pNum, 16, 16, 16 ) ) THSAD = default( THSAD, Select( pNum, 220, 340, 470 ) ) SHARP = default( SHARP, Select( pNum, 0.16, 0.20, 0.24 ) ) WARP = default( WARP, Select( pNum, 4 , 6 , 7 ) ) ### Temporal color NR v=c.vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false).vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) ### Spatial NR s=v.fft3dgpu(sigma=0.3,sigma2=0.4,sigma3=1.4,sigma4=1,plane=3,bt=3). \ fft3dgpu(sigma=0.3,sigma2=0.4,sigma3=1.6,sigma4=2.7,plane=0,bt=3).ex_vibrance(1.2) ### Dynamic NR mask emask=v.ex_levels((luma/340),(luma/6100),237,0,170) m=mt_merge(v,s,emask.mt_invert().mt_inpand(),Y=3,U=3,V=3, luma=true) ### Dynamic prefilter p=m.coloryuv(autogain=true).vdmc() ### Temporal NR sm=m.SmDegrain(prefilter=p,tr=tr,thsad=thsad,blksize=blk).tweak(sat=1.06) sh=sm.awarpsharp2(depth=warp) sh = sh.sharpen(sharp) ### Debanding sh.neo_f3kdb(grainY=40,grainC=3,mt=true) return last } Code:
function AutoNR3(clip c, string "Preset", int "TR", int "BLK", int "THSAD", float "SHARP", \ int "WARP") { # Select presets: Preset = default( Preset, "Medium" ) pNum = (Preset == "Low" ) ? 0 : \ (Preset == "Medium" ) ? 1 : \ (Preset == "High" ) ? 2 : \ Assert( FALSE , "'Preset' choice is invalid" ) # Preset defaults: Low Medium High TR = default( TR, Select( pNum, 1 , 3 , 4 ) ) BLK = default( BLK, Select( pNum, 16, 16, 16 ) ) THSAD = default( THSAD, Select( pNum, 220, 340, 470 ) ) SHARP = default( SHARP, Select( pNum, 0.16, 0.20, 0.24 ) ) WARP = default( WARP, Select( pNum, 4 , 6 , 7 ) ) ScriptClip(c,function[tr,blk,thsad,sharp,warp] () { luma = AverageLuma() orig = last ### Temporal color NR vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) ### Spatial NR s=fft3dgpu(sigma=0.3,sigma2=0.4,sigma3=1.4,sigma4=1,plane=3,bt=3). \ fft3dgpu(sigma=0.3,sigma2=0.4,sigma3=1.6,sigma4=2.7,plane=0,bt=3).ex_vibrance(1.2) f = last ### Dynamic NR mask emask=ex_levels((luma/340),(luma/6100),237,0,170) mt_merge(f,s,emask.mt_invert().mt_inpand(),Y=3,U=3,V=3, luma=true) ### Dynamic prefilter p=coloryuv(autogain=true).vdmc() ### Temporal NR SmDegrain(prefilter=p,tr=tr,thsad=thsad,blksize=blk).tweak(sat=1.06) ### Sharpen awarpsharp2(depth=warp) sharpen(sharp) ### Debanding neo_f3kdb(grainY=40,grainC=3,mt=true) #return last luma < last } ) } This line looks good: Code:
ex_levels((luma/340),(luma/6100),237,0,170) Code:
ex_levels((luma/340),1.0+0.5*(thr-luma)/thr,237,0,170) |
|
11th February 2022, 22:23 | #8 | Link | |
Registered User
Join Date: Dec 2005
Location: Sweden
Posts: 703
|
Quote:
As I read about ScriptClip and how it works I find: Code:
Calling a lot of filters / functions inside the runtime script will slow down your encoding speed. Those filters will be created and destroyed on every frame; thus you pay initialisation/cleanup costs at every frame. If you can, put outside the runtime environment those filter calls not essential for the runtime processing; break the runtime script into more scripts if you have to. For example, instead of doing this: I tested out my new function and found that I need to use "RDifferenceFromPrevious()" instead of AverageLuma. Still need to get scriptclip to work inside my function and for more than one line. Like this: Code:
function AutoNR(clip c, int "TR", int "BLK", float "SHARP", int "WARP") { tr = Default(tr, 4) blk = Default(blk, 16) sharp = Default(sharp, 0.24) warp = Default(warp, 7) ###ScriptClip lines in BOLD rex = ex_levels(0,0.48,48).converttorgb24().ExtractR() red = RDifferenceFromPrevious(rex) low = red/4.6 mid = low/1.7 ths = int(red)*36 c.fft3dgpu(sigma=0.3,sigma2=0.4,sigma3=(MID),sigma4=(LOW),plane=4,bt=3) FluxSmoothT(temporal_threshold=10, luma=false, chroma=true) vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) p=coloryuv(autogain=true).vdmc() SmDegrain(prefilter=p,tr=tr,thsad=ths,blksize=blk).tweak(sat=1.06) awarpsharp2(depth=warp) sharpen(sharp) neo_f3kdb(grainY=40,grainC=3,mt=true) return last } Code:
fft3dgpu(sigma=0.3,sigma2=0.4,sigma3=(MID),sigma4=(LOW),plane=4,bt=3) SmDegrain(prefilter=p,tr=tr,thsad=ths,blksize=blk) Last edited by anton_foy; 11th February 2022 at 22:27. |
|
18th February 2022, 00:21 | #9 | Link |
Registered User
Join Date: Dec 2005
Location: Sweden
Posts: 703
|
This test is working how I want it resulting in a number:
Code:
ScriptClip(last, """ red = ex_levels(0,0.48,48).converttorgb().ExtractR().invert() redavg = RDifferenceFromPrevious(red) Subtitle("luma=" + String(redavg), align=2) """) Code:
function ANR(clip c) s=ScriptClip(last, """ red = ex_levels(0,0.48,48).converttorgb().ExtractR().invert() redavg = RDifferenceFromPrevious(red) #return last """) sig = s/4.6 sig2 = s/1.6 c.fft3dgpu(sigma=sig,sigma2=sig2) return last Last edited by anton_foy; 18th February 2022 at 00:45. |
18th February 2022, 00:53 | #10 | Link |
Registered User
Join Date: Nov 2009
Posts: 2,361
|
You have to put everything that's dynamic inside ScriptClip, that's the basis.
Sometimes you can do some tricks/cheats like writing to frameprops and then retrieving them but it's not standard practice.
__________________
i7-4790K@Stock::GTX 1070] AviSynth+ filters and mods on GitHub + Discussion thread |
18th February 2022, 10:49 | #12 | Link |
Registered User
Join Date: Nov 2009
Posts: 2,361
|
This? also I really don't like fft3dgpu, unless you are doing some dirty job (very damaged clips) and it gets the job done I would rather use the CPU version which is totally different (more correct?)
Code:
ScriptClip(last, """ red = ex_levels(0,0.48,48).converttorgb().ExtractR().invert() redavg = RDifferenceFromPrevious(red) sig = redavg/4.6 sig2 = redavg/1.6 fft3dgpu(sigma=sig,sigma2=sig2) """)
__________________
i7-4790K@Stock::GTX 1070] AviSynth+ filters and mods on GitHub + Discussion thread |
18th February 2022, 15:08 | #13 | Link | |
Registered User
Join Date: Dec 2005
Location: Sweden
Posts: 703
|
Quote:
Btw. your new smdegrain version is so fast and pleasant looking at default. The reason I dumped fastdegrain. Last edited by anton_foy; 18th February 2022 at 15:12. |
|
18th February 2022, 22:16 | #14 | Link | |
Registered User
Join Date: Dec 2005
Location: Sweden
Posts: 703
|
Quote:
Code:
function anr(clip c){ ScriptClip(last, """ red = ex_levels(0,0.48,48).converttorgb().ExtractR().invert() redavg = RDifferenceFromPrevious(red) sig = redavg/4.6 sig2 = redavg/1.6 fft3dgpu(sigma=sig,sigma2=sig2) """) #<--- error on this line vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) return last } |
|
18th February 2022, 23:14 | #15 | Link | |
Registered User
Join Date: Nov 2009
Posts: 2,361
|
Maybe this:
Quote:
__________________
i7-4790K@Stock::GTX 1070] AviSynth+ filters and mods on GitHub + Discussion thread |
|
19th February 2022, 03:35 | #16 | Link |
Registered User
Join Date: Dec 2005
Location: Sweden
Posts: 703
|
Thanks it works now but the number from RDifferenceFromPrevious
seems to get wrong when fed into the sigma's. For example on frame 1 on my test clip I got 12.524107 according to Code:
Subtitle("redavg=" + String(redavg), align=2) Code:
csig4 = redavg/6 When I try the script below sigma's seem to be super high like 40 or something, totally smeared out the chroma. Do I need to convert into string(redavg, "%0.1f") and then to float or something to get correct values? Code:
function anr(clip c){ ScriptClip(c, """ red = ex_levels(0,0.48,48).converttorgb().ExtractR() redavg = RDifferenceFromPrevious(red) csig3 = redavg/30 csig4 = redavg/6 mergechroma(temporalsoften(4,0,255,0,2). \ neo_fft3d(sigma=0,sigma2=0,sigma3=csig3,sigma4=csig4,bt=1,y=1,u=3,v=3))""") vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) return last } Code:
function anr(clip c){ ScriptClip(c, """ red = ex_levels(0,0.48,48).converttorgb().ExtractR() redavg = RDifferenceFromPrevious(red) csig3 = redavg/8400 csig4 = redavg/1700 sig3 = redavg/1700 sig4 = redavg/1260 ths_ = redavg*0.138 ths = int(ths_) shr = redavg*0.00006 mergechroma(temporalsoften(4,0,255,0,2). \ neo_fft3d(sigma=0,sigma2=0,sigma3=csig3,sigma4=csig4,bt=1,y=1,u=3,v=3)) vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) neo_fft3d(sigma=0.4,sigma2=0.6,sigma3=sig3,sigma4=sig4,y=3,u=2,v=2,bt=3) p=convertbits(8).coloryuv(autogain=true).TemporalSoften(3,4,8,scenechange=0,mode=2) SmDegrain(prefilter=p,tr=4,thsad=ths) sharpen(shr)""") awarpsharp2(depth=7) neo_f3kdb(grainY=40,grainC=3,mt=true) return last } Look here at "mdiff2" by raffriff42: https://forum.doom9.org/showthread.p...52#post1756852 This shows difference from the previous frame without the use of scriptclip. Is there no way of getting resulting numbers instead of visual difference by using this technique? Last edited by anton_foy; 20th February 2022 at 01:29. |
5th March 2022, 14:12 | #17 | Link |
Registered User
Join Date: Dec 2005
Location: Sweden
Posts: 703
|
At it again...this code below gives me error
Code:
"MAnalyse: Clip must be YUV or YUY2 ...SmDegrain.avsi, line 294, line 345, line 634" Code:
function anv(clip c){ converttoPlanarRGB(c) fft3dfilter(sigma=0.3,sigma2=0.4,sigma3=1.7,sigma4=2.1,plane=4,bt=4) S=ScriptClip(""" lum = converttorgb().invert().averageG rgb = converttorgb().RGBDifferenceFromPrevious luma = int(lum + rgb) ltr = int(luma*0.00009) ths = int(luma*0.01) p = convertbits(8).TemporalSoften(3,4,6,0,2).ex_levels(10,0.95,126) SmDegrain(prefilter=p,tr=ltr,thsad=ths,DCT=1) #Subtitle("luma=" + String(ths) +","+ String(ltr), align=2) """) n = s.converttoyuv444() n = n.vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) n = n.neo_f3kdb(grainY=40,grainC=3,mt=true).tweak(sat=1.16) return n } EDIT: Aaah I missed the "c," Now it is working but damn it is slow (I feared it would be) and only because of scriptclip since without it is pretty fast New code: Code:
function anv(clip c){ converttoPlanarRGB(c) # S=ScriptClip(c,""" lum = converttorgb().invert().averageG rgb = converttorgb().RGBDifferenceFromPrevious luma = int(lum + rgb) ltr = int(luma*0.00009) ths = int(luma*0.01) p = convertbits(8).TemporalSoften(3,4,6,0,2).ex_levels(10,0.95,126) SmDegrain(prefilter=p,tr=ltr,thsad=ths,DCT=1) #Subtitle("luma=" + String(ths) +","+ String(ltr), align=2) """) n = s.fft3dfilter(sigma=0.3,sigma2=0.4,sigma3=1.7,sigma4=2.1,plane=4,bt=1) n = n.converttoyuv444() n = n.vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) n = n.neo_f3kdb(grainY=40,grainC=3,mt=true).tweak(sat=1.16) return n } Last edited by anton_foy; 5th March 2022 at 14:44. |
5th March 2022, 15:41 | #19 | Link |
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
|
Totally untested. [No idea what its supposed to be doing - my best guess at intended use]
[EDIT: Ignore this, methinks the later EDIT is as intended.] Code:
Function anv(clip c){ converttoPlanarRGB(c) # Last is planar RGB, input bitdepth : ## c clip NOT used after here. rgbInvertC = invert() # Inverted rgb, input depth Rgb8 = convertbits(8) # planar RGB, 8 bit p = Rgb8.TemporalSoften(3,4,6,0,2).ex_levels(10,0.95,126) # SmDegrain Prefilter clip SSS = """ lum = rgbInvertC.averageG rgb = RGBDifferenceFromPrevious # on last, ie planar RGB, input bitdepth luma = int(lum + rgb) ltr = int(luma*0.00009) ths = int(luma*0.01) SmDegrain(prefilter=p,tr=ltr,thsad=ths,DCT=1) # Horrible, real slow call to SmDegrain at every frame. #Subtitle("luma=" + String(ths) +","+ String(ltr), align=2) return last """ # ScriptClip: Input Last, ie PlanarRgb at input depth ScriptClip(Last,SSS,args="rgbInvertC,p",local=true) # NEEDS Grunt Plugin for Args. fft3dfilter(sigma=0.3,sigma2=0.4,sigma3=1.7,sigma4=2.1,plane=4,bt=1) converttoyuv444() vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) neo_f3kdb(grainY=40,grainC=3,mt=true).tweak(sat=1.16) return Last } __END__ # HERE old script. After above __END__ is not executed. Function anv(clip c){ converttoPlanarRGB(c) # # *** NOTE, NOT calling below scriptclip with planarRGB, is input clip c *** S=ScriptClip(c,""" lum = converttorgb().invert().averageG rgb = converttorgb().RGBDifferenceFromPrevious luma = int(lum + rgb) ltr = int(luma*0.00009) ths = int(luma*0.01) p = convertbits(8).TemporalSoften(3,4,6,0,2).ex_levels(10,0.95,126) SmDegrain(prefilter=p,tr=ltr,thsad=ths,DCT=1) #Subtitle("luma=" + String(ths) +","+ String(ltr), align=2) """) n = s.fft3dfilter(sigma=0.3,sigma2=0.4,sigma3=1.7,sigma4=2.1,plane=4,bt=1) n = n.converttoyuv444() n = n.vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) n = n.neo_f3kdb(grainY=40,grainC=3,mt=true).tweak(sat=1.16) return n } dont know if intended nor if works properley, I dont use SmDegrain. EDIT: On 2nd thoughts, maybe this as intended [again untested at all] But p clip is 8 bits and function input clip c may not be, dont know if smdegrain will handle correctly. [maybe below in RED needs removal] Code:
Function anv(clip c){ InputPlanarRGB = converttoPlanarRGB(c) rgbInvertC = InputPlanarRGB.invert() # Inverted rgb, input depth p = c.convertbits(8).TemporalSoften(3,4,6,0,2).ex_levels(10,0.95,126) # SmDegrain Prefilter clip SSS = """ lum = rgbInvertC.averageG rgb = InputPlanarRGB.RGBDifferenceFromPrevious # on planar RGB, input bitdepth luma = int(lum + rgb) ltr = int(luma*0.00009) ths = int(luma*0.01) SmDegrain(prefilter=p,tr=ltr,thsad=ths,DCT=1) # Horrible, real slow call to SmDegrain at every frame. #Subtitle("luma=" + String(ths) +","+ String(ltr), align=2) return last """ # ScriptClip: Input c, ie Input clip ScriptClip(c,SSS,args="rgbInvertC,p,InputPlanarRGB",local=true) # NEEDS Grunt Plugin for Args. fft3dfilter(sigma=0.3,sigma2=0.4,sigma3=1.7,sigma4=2.1,plane=4,bt=1) converttoyuv444() vsCnr2(mode="ooo",ln=5,un=40,vn=40,sceneChroma=false) neo_f3kdb(grainY=40,grainC=3,mt=true).tweak(sat=1.16) return Last }
__________________
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; 5th March 2022 at 16:15. |
5th March 2022, 16:16 | #20 | Link | |
Registered User
Join Date: Dec 2005
Location: Sweden
Posts: 703
|
@StailessS
Thank you! I have to change to Code:
rgb = converttorgb().RGBDifferenceFromPrevious SmDegrain get this again: Code:
"MAnalyse: Clip must be YUV or YUY2 ...SmDegrain.avsi, line 294, line 345, line 634" Quote:
@kedautinh12 Thanks but for me neo_fft3d is a bit slower than fft3dfilter at the same ncpu setting. Last edited by anton_foy; 5th March 2022 at 16:19. |
|
|
|