digitalFAQ.com Forums [Archives]

digitalFAQ.com Forums [Archives] (http://www.digitalfaq.com/archives/)
-   Avisynth Scripting (http://www.digitalfaq.com/archives/avisynth/)
-   -   Avisynth: Motion adaptive filtering now possible? (http://www.digitalfaq.com/archives/avisynth/3594-avisynth-motion-adaptive.html)

kwag 06-03-2003 05:28 PM

So far, so good :!:

kwag 06-03-2003 07:33 PM

I should have RTFM 8O
I took a sample routine from AviSynth's manual, and added my scene change detection. Now it should be bullet proof :!: But again, test it, test it, and test it again to be sure :!:


Code:

## DLL Section ##
#
LoadPlugin("C:\Filters25\MPEG2Dec3.dll")
LoadPlugin("C:\Filters25\STMedianFilter.dll")
LoadPlugin("C:\Filters25\UnFilter.dll")
#
#
## Defined Variables and Constants ##
#
MaxTreshold = 1.58
scd_trigger = 15 # Scene change trigger value.
nf =  0 # Next frame.
#
#
## Main section and static filters ###
#
Mpeg2Source("Your_D2V_Source_Here")
Limiter()
UnFilter(50, 50)
BicubicResize( Your_Resize_Values_Here )
STMedianFilter(8, 32, 0, 0 )
MergeChroma(blur(1.50))
#
#
## Dynamic Linear Adaptive filtering and Scene Change Detection ##
#
# (From AviSynth's Manual) - This will apply temporalsoften to very static # scenes, and apply a
# _variable_ blur on moving scenes. Blur is now capped properly. We 
# also assign a variable - and this is why a line break is inserted:

ScriptClip("nf = YDifferenceToNext()"+chr(13)+ "nf > 2.5 ? Blur(fmin(nf/20,1.5)) : TemporalSoften(2,7,7,3,2)")

#
# Scene change detection (kwag) - If a scene change is detected, we
# double blur. This affects the scene before and the one after the
# scene change, thus providing a softer transition for the encoder instead
# of a sharp "spike".
# If it's not a scene change, then we apply linear value from diff to next
# frame, scaled to MergeLuma's range.

ScriptClip("nf > scd_trigger ? blur(MaxTreshold).blur(MaxTreshold) : MergeLuma(Blur(fmin(nf/20,1.5)))")

#
#
#
#LetterBox( Your_Values_Here ) # Depends on situation. Use MovieStacker!
#AddBorders( Your_Values_Here ) # Depends on situation. Use MovieStacker!
Limiter()
#
#
### Function Calls #####
#
function fmin(float f1, float f2) {
  return (f1<f2) ? f1 : f2
}
#
#
####

-kwag

DKruskie 06-03-2003 07:46 PM

Kwag
This green bar shows up in tmpgenc in the video window along the right side with new script like I posted last night. I have never seen this before..I just tried it out again and I'm still getting the green bar


David

kwag 06-03-2003 07:51 PM

Quote:

Originally Posted by DKruskie
Kwag
This green bar shows up in tmpgenc in the video window along the right side with new script like I posted last night. I have never seen this before..I just tried it out again and I'm still getting the green bar


David

I think you have some CODEC conflict with AviSynth 2.52. I haven't seen this problem, and apparently nobody else has experienced this problem :!:
All my encodes are as clear as the samples I posted. Do you have installed the Nimo CODEC pack or something like that :?:

-kwag

DKruskie 06-03-2003 07:54 PM

I just tried your latest script and all is working great again :D ..I dont know what was going on, but I'm glad it's gone :!: :!: Nope no nimo.


David

kwag 06-03-2003 08:20 PM

Quote:

Originally Posted by DKruskie
I just tried your latest script and all is working great again :D ..I dont know what was going on, but I'm glad it's gone :!: :!: Nope no nimo.


David

Weird behaviours 8O

@All,
Change the line:
ScriptClip("nf > scd_trigger ? blur(MaxTreshold).blur(MaxTreshold) : MergeLuma(Blur(fmin(nf/20,1.5)))")

To read:
ScriptClip("nf > scd_trigger ? blur(MaxTreshold).blur(MaxTreshold) : Blur(0)")

That MergeLuma is screwing up!

-kwag

ovg64 06-03-2003 10:04 PM

This Script works 4 me, But the last one you post, its a no no cause it blurs just about anything that move, and i mean BLUR at least thats what happen when i tried it twice.

Code:
## DLL Section ##
#
LoadPlugin("C:\Filters25\MPEG2Dec3.dll")
LoadPlugin("C:\Filters25\STMedianFilter.dll")
LoadPlugin("C:\Filters25\UnFilter.dll")
#
####

## Defined Variables and Constants ##
#
MaxTreshold = 1.58
scd_trigger = 15 # Scene change trigger value.
nf = 0 # Current frame.
lf = 0 # Last frame
val = 0 # Dynamic value applied to filters
#
####

## Main section and static filters ###
#
Mpeg2Source("Your_D2V_Source_Here")
Limiter()
UnFilter(50, 50)
BicubicResize( Your_Resize_Values_Here )
STMedianFilter(8, 32, 0, 0 )
TemporalSoften(2,7,7,3,2) # Experimental!
MergeChroma(blur(1.50))
#
####

## Dynamic Linear Adaptive filtering and Scene Change Detection ##
#
FrameEvaluate("lf = YDifferenceToNext()")
FrameEvaluate("nf = YDifferenceToNext()")
FrameEvaluate("val = (lf / 16) + 0.05")
ScriptClip("(abs(nf - lf) < scd_trigger) ? (val < MaxTreshold) ? \
mergeluma(blur(val)) : mergeluma(blur(MaxTreshold)) : \
( (sign(nf-lf)) != -1) && (val < MaxTreshold) ? mergeluma(blur(val)) : mergeluma(blur(MaxTreshold)).mergeluma(blur(MaxTre shold))")
#
####

#LetterBox( Your_Values_Here ) # Depends on situation. Use MovieStacker!
#AddBorders( Your_Values_Here ) # Depends on situation. Use MovieStacker!
Limiter()
#
####

kwag 06-03-2003 10:19 PM

The excessive blur would be caused by this line:

ScriptClip("nf = YDifferenceToNext()"+chr(13)+ "nf > 2.5 ? Blur(fmin(nf/20,1.5)) : TemporalSoften(2,7,7,3,2)")

Increase the value 20 in: Blur(fmin(nf/20,1.5))
to something like: Blur(fmin(nf/35,1.5))

Now it should blurr less. Thanks for the info :!:
I see that "blur" is heavier than the blur on the luma (mergeluma), so that default value of 20 that was on the script seems too low, and causes too high values for blur.

Tell me the result with 35 :wink:

-kwag

ovg64 06-03-2003 10:42 PM

No still the same as before way too much Blur this is my script you tell me if ok.

LoadPlugin("C:\Documents and Settings\Osvaldo\My Documents\Avi Synth 2.51\MPEG2Dec3.dll")
LoadPlugin("C:\Documents and Settings\Osvaldo\My Documents\Avi Synth 2.51\asharp.dll")
LoadPlugin("C:\Documents and Settings\Osvaldo\My Documents\Avi Synth 2.51\Deen.dll")
LoadPlugin("C:\Documents and Settings\Osvaldo\My Documents\Avi Synth 2.51\STmedianFilter.dll")

MaxTreshold = 1.58
scd_trigger = 15 # Scene change trigger value.
nf = 0 # Next frame.
Mpeg2Source("C:\Documents and Settings\Osvaldo\Desktop\DVD\THC.d2v")
Limiter()
asharp(2,2)
Tweak(bright=-25)
BicubicResize(480, 366, 0, 0.6, 6, 0, 708, 480)
deen("a2d",2,10,12)
TemporalSoften(2,7,7,3,2)
MergeChroma(blur(1.50))
MergeLuma(blur( 0.2))
ScriptClip("nf = YDifferenceToNext()"+chr(13)+ "nf > 2.5 ? Blur(fmin(nf/35,1.5)) : TemporalSoften(2,7,7,3,2)")
ScriptClip("nf > scd_trigger ? blur(MaxTreshold).blur(MaxTreshold) : Blur(0)")
AddBorders(0, 57, 0, 57)
LetterBox(0, 0, 16, 16)
function fmin(float f1, float f2) {
return (f1<f2) ? f1 : f2
}

kwag 06-03-2003 11:06 PM

Check your PM :!:

kwag 06-03-2003 11:25 PM

I got'cha now :!: :!:

Here's the problem with the adaptive filtering. The way the filters work'("ed" :!: ) , was that for low moving tresholds, temporalsoften was applied, and then after a certain treshold (boundary), it would switch to another filter (Blur in this case).
And that's exactly where the problem was You can't switch filters in and out of an encode, because they're messing up the color space somehow :!:
What I did is that now the filters switch, but I keep mergeluma ON with a very very low bleeding value (0.01).
This way, the filters is still on (when it's not supposed to), by generating a very low value, so there's no turn on/turn off "switching glitch", which is what was causing the "blinks" or "flashes" at sporadic points. I just finished a 10 minute encode, and with the previous script, there were CLEARLY visible blinking points. Not so now :!:
Here's the updated script portion. Just change this section and paste it to the script I posted previously in this thread.:

Code:

## Dynamic Linear Adaptive filtering and Scene Change Detection ##
#
# This will apply temporalsoften to very static scenes, and apply a
# _variable_ blur on moving scenes. Blur is now capped properly. We
# also assign a variable - and this is why a line break is inserted:

ScriptClip("nf = YDifferenceToNext()"+chr(13)+ "nf > 2.5 ? mergeluma(Blur(fmin(nf/16,1.5))) : TemporalSoften(2,7,7,3,2).MergeLuma(Blur(0.01))")

#
# Scene change detection (kwag) - If a scene change is detected, we
# double blur. This affects the scene before and the one after the #
# scene change, thus providing a softer transition for the encoder instead # of a sharp "spike".
# If it's not a scene change, then we apply linear value from diff to next
# frame, scaled to MergeLuma's range.

ScriptClip("nf > scd_trigger ? MergeLuma(blur(MaxTreshold)).MergeLuma((blur(MaxTreshold)) : MergeLuma(blur(0.01))")

#
####

DAMN :!:, I hope this is really the end of the nightmares 8O :? :twisted:

-kwag

kwag 06-03-2003 11:36 PM

Yesssss :!: :!: :!:.
Another test encode where I had flashes. They're gone :mrgreen:

@sh0dan,
Can someone make a note in the AviSynth manual, related to "switching" filters dynamically :idea:
If it's going to be done, like we're doing here, the mergeluma must be kept on, even in conditional fails (by generating a minimal value ) and avoiding the "overshoot/undershoot" (sort of) effect when the filters are turned on/off.

-kwag

DKruskie 06-03-2003 11:40 PM

Is there a chance you could show a full script for this :?: , I'm having problems editing my script.



David

kwag 06-03-2003 11:54 PM

Quote:

Originally Posted by DKruskie
Is there a chance you could show a full script for this :?: , I'm having problems editing my script.



David

Hi David,

Check the "Latest Script" here: http://www.kvcd.net/forum/viewtopic.php?t=3483
I just updated it :)

-kwag

DKruskie 06-03-2003 11:58 PM

Thanks :D ..I'll go get it :D

David

DKruskie 06-04-2003 12:10 AM

I see something at the top of the movie in tiny letters that says:
script error: there is no function named "fmin"
scripterror line 2..here is script from tok, Is there something wrong with mine? The movie did look great though.

## DLL Section ##
#
LoadPlugin("C:\Filters25\MPEG2Dec3.dll")
LoadPlugin("C:\Filters25\STMedianFilter.dll")
LoadPlugin("C:\Filters25\UnFilter.dll")
#
####

## Defined Variables and Constants ##
#
MaxTreshold = 1.58
scd_trigger = 15 # Scene change trigger value.
nf = 0 # Current frame.
lf = 0 # Last frame
val = 0 # Dynamic value applied to filters
#
####

## Main section and static filters ###
#
Mpeg2Source("C:\DVD\matrix.d2v")
Limiter()
UnFilter(50, 50)
BicubicResize( 352,240,0,0.6,8,0,704,480 )
STMedianFilter(8, 32, 0, 0 )
TemporalSoften(2,7,7,3,2) # Experimental!
MergeChroma(blur(1.50))
#
####

## Dynamic Linear Adaptive filtering and Scene Change Detection ##
#
FrameEvaluate("lf = YDifferenceToNext()")
FrameEvaluate("nf = YDifferenceToNext()")
FrameEvaluate("val = (lf / 16) + 0.2")
ScriptClip("nf = YDifferenceToNext()"+chr(13)+ "nf > 2.5 ? mergeluma(Blur(fmin(nf/16,1.5))) : TemporalSoften(2,7,7,3,2).MergeLuma(Blur(0.01))")
#
####

#LetterBox( 0,24,0,24) # Depends on situation. Use MovieStacker!
#AddBorders( 0,0,0,0 ) # Depends on situation. Use MovieStacker!
Limiter()
#
####
AssumeFPS(23.976)
LoadPlugin("C:\TOK\ToK_EXTRAS\Sampler\Sampler-2.5.dll")
oldfps = framerate
interval = round((FrameCount/24)/59.940)/10
nFrames = round(24)
SelectRangeEvery( (round(framecount/interval)),nFrames)


David

kwag 06-04-2003 12:15 AM

Quote:

Originally Posted by DKruskie
I see something at the top of the movie in tiny letters that says:
script error: there is no function named "fmin"

sheit :!: 8O
Sorry, :oops: get the script again. I forgot to add that function. It's there now :!:

BlueBeard 06-04-2003 02:43 AM

Quote:

Originally Posted by kwag

We'll find a fix or a workaround in a few "centons" :mrgreen:

That's alot better that a few "yaren" :mrgreen:

sh0dan 06-04-2003 02:57 AM

Quote:

Originally Posted by kwag
@sh0dan,
Can someone make a note in the AviSynth manual, related to "switching" filters dynamically :idea:
If it's going to be done, like we're doing here, the mergeluma must be kept on, even in conditional fails (by generating a minimal value ) and avoiding the "overshoot/undershoot" (sort of) effect when the filters are turned on/off.

-kwag

I'm afraid I'm not quite following you there.... :?:

boeddha 06-04-2003 04:00 AM

Yesterday I posted a message about colors being blue. But this had nothing to do whit the script. I was using an old version of divx.

Jellygoose 06-04-2003 04:19 AM

Currently testing the Latest Script... It seems that CQ goes down for me, however, I can's talk about the picture quality yet... Is that about right? :o

girv 06-04-2003 07:02 AM

From the latest script:

Code:

ScriptClip("nf > scd_trigger ? MergeLuma(blur(MaxTreshold)).MergeLuma(blur(MaxTreshold)) : MergeLuma(blur(0.01))")
Surely "nf > scd_trigger" will only be true for the frame before the scene change, so the frame after the change will only have the 0.01 blur applied?

girv 06-04-2003 07:14 AM

@kwag: I did some tests with blurring different numbers of frames around scene changes and it seemed to increase filesize in all cases :?:

DorvalCS 06-04-2003 07:17 AM

Testing the new script as well... I too have noticed a decrease in CQ... I went from 70.21 with previous script to 65.11 with the new one!

Not sure about quality at this time either.

kwag 06-04-2003 08:03 AM

Quote:

Originally Posted by sh0dan
Quote:

Originally Posted by kwag
@sh0dan,
Can someone make a note in the AviSynth manual, related to "switching" filters dynamically :idea:
If it's going to be done, like we're doing here, the mergeluma must be kept on, even in conditional fails (by generating a minimal value ) and avoiding the "overshoot/undershoot" (sort of) effect when the filters are turned on/off.

-kwag

I'm afraid I'm not quite following you there.... :?:

Hi sh0dan,

What I mean is that if applying conditional filters, specially blur or mergeluma, they must be applied continuously throughout every frame. Not applied on some frames, and removed on other frames like in an adaptive script. Look at this line:

ScriptClip("nf = YDifferenceToNext()"+chr(13)+ "nf > 2.5 ? mergeluma(Blur(fmin(nf/16,1.5))) : TemporalSoften(2,7,7,3,2)")

If activity is below the treshold of 2.5, TemporalSoften is applied (mergeluma is cut off). If it's above 2.5 mergeluma is applied (TemporalSoften is cut off).

That's why I added this on the false condition:
TemporalSoften(2,7,7,3,2).mergeluma(Blur(0.01))
So now mergeluma is always on, even on the "false" condition, but with a very low value.
I guess mergeluma(Blur(0.01)) could be added at the beginning of the script instead of here, but for illustration, I added it here.
The first line, which is identical to the sample posted at http://www.avisynth.org/index.php?pa...ditionalFilter except for the value 20 which I changed to 16, shows the "blinking" flaw on several scenes throughout a movie.

-kwag

kwag 06-04-2003 08:12 AM

Quote:

Originally Posted by girv
From the latest script:

Code:

ScriptClip("nf > scd_trigger ? MergeLuma(blur(MaxTreshold)).MergeLuma(blur(MaxTreshold)) : MergeLuma(blur(0.01))")
Surely "nf > scd_trigger" will only be true for the frame before the scene change, so the frame after the change will only have the 0.01 blur applied?

Put this line at the end of your script:
ScriptClip("Subtitle(String(nf),1,45)") and slowly move frame by frame with Vdub. There you can see what's happening on the frame before and after scene changes.

-kwag

kwag 06-04-2003 08:27 AM

Quote:

Originally Posted by girv
@kwag: I did some tests with blurring different numbers of frames around scene changes and it seemed to increase filesize in all cases :?:

8O I'm getting a consistent average of ~30KB less per minute with the scene change line.
Are you benchmarking "exactly" the same amount of frames on the same clip, with and without this line:

ScriptClip("nf > scd_trigger ? MergeLuma(blur(MaxTreshold)).MergeLuma(blur(MaxTre shold)) : MergeLuma(blur(0.01))")

-kwag

kwag 06-04-2003 08:31 AM

Quote:

Originally Posted by DorvalCS
Testing the new script as well... I too have noticed a decrease in CQ... I went from 70.21 with previous script to 65.11 with the new one!

Not sure about quality at this time either.

Probably because the minumum value for mergeluma is 0.01 instead of 0.1 as it was in the previous script. Change both instances of 0.01 to 0.1 on the mergeluma functions, and that should bring the CQ back up again :wink:

Edit: Actually, let's make it better:

ScriptClip("nf = YDifferenceToNext()"+chr(13)+ "nf > 2.5 ? mergeluma(Blur(fmin(nf/16,1.5))) : TemporalSoften(2,7,7,3,2).mergeluma(Blur(fmin(nf/16,1.5)))")

That just made my file size go down ~150KB per 30 seconds clip :!:

-kwag

audioslave 06-04-2003 09:00 AM

Oh, man... I started encoding "A Knight's Tale" yesterday, before the flashing was removed from the script. This morning I restarted the process with the new script. And now... it's changed again...
Don't get me wrong I think it's GREAT that the script is tested and trimmed :D . Looks like I'm gonna have to start all over again with my encode :wink: .

@kwag
Have you updated the script with the last change you posted? Would be great since I'm not a programmer like yourself, and I have some trouble understanding how the new script works...

girv 06-04-2003 09:17 AM

Quote:

Originally Posted by kwag
ScriptClip("Subtitle(String(nf),1,45)") and slowly move frame by frame with Vdub. There you can see what's happening on the frame before and after scene changes.

Well now here's a curious one.

Code:

FrameEvaluate("nf = YDifferenceToNext()")
ScriptClip("Subtitle(String(nf))")

...and...
Code:

ScriptClip("nf = YDifferenceToNext()" + chr(13) + "Subtitle(String(nf))")
...produce different results :!: In the first snippet (which is modeled on the current optimal script), nf goes large (>15) after the scene change but in the second nf goes large before the change. I think the second snippet is more correct as the Y Difference to the Next frame should be largest before a scene change, right?

@sh0dan: is this the way its supposed to be ?

The current script only max-blurs the frame either side of a scene change because of this "feature". In the first ScriptClip nf is large before the change so the Blur is clamped to 1.5 and applied to the frame before the change; in the second ScriptClip nf is large after the change so the 2x Blur is applied to the frame after the change. I don't think this is quite what you intended, unless you're considerably cleverer than me (always possible of course :)

I'd been using a single ScriptClip for my own experiments so thats why I was getting different results from the optimal script.

kwag 06-04-2003 09:22 AM

Hi audioslave,

Yes, I just updated the script with the changes above.
I'm going to encode the complete movie, and watch for any "blinks". But I think that the problem is over :)

Here, I might be pushing the limits a little for a 704x480 on one disk, but here's a "Red Planet" sample, now at 704x480 with CQ of 63.38 to fit on one disk: www.kvcd.net/red-planet-704x480.mpg

-kwag

kwag 06-04-2003 09:26 AM

Quote:

Originally Posted by girv
@sh0dan: is this the way its supposed to be ?

Maybe it's a "Feature" :mrgreen:
Quote:


The current script only max-blurs the frame either side of a scene change because of this "feature".
If it's a bug, let's keep it, as I like that bug (feature) :mrgreen:

-kwag

audioslave 06-04-2003 09:43 AM

@kwag
WOW! Amazing 8O ! How long is that move?
Here comes the question-nuke once again;
Do you recommend using 112kbps or 128kbps for the audio?
Do you think I should be able to squeeze "A Knight's Tale" onto one CD? It's about 127 min long...
Sorry for being a pain in the ***. :wink:

kwag 06-04-2003 09:51 AM

Quote:

Originally Posted by audioslave
@kwag
WOW! Amazing 8O ! How long is that move?

One hour, fourty seven minutes :mrgreen:
Quote:

Here comes the question-nuke once again;
Do you recommend using 112kbps or 128kbps for the audio?
I always use 112Kbps, and ALWAYS encode with HeadAC3he with DS2.
Quote:

Do you think I should be able to squeeze "A Knight's Tale" onto one CD? It's about 127 min long...
With the new script, probably yes, but at 528x480. I think that's a little too long for 704x480. Maybe not :!:, it really depends on the action of the movie. Do a small clip and test it out.

And here's another sample that I think speaks for itself :wink:
www.kvcd.net/red-planed-action-704x480.mpg

-kwag

audioslave 06-04-2003 09:55 AM

Hi kwag, and thank you for your fast replies (as always!).
What do you mean by "encode with HeadAC3he with DS2"? Downsampling perhaps...? :roll:

kwag 06-04-2003 09:59 AM

Quote:

Originally Posted by audioslave
What do you mean by "encode with HeadAC3he with DS2"? Downsampling perhaps...? :roll:

"Dolby Surround II" :)

audioslave 06-04-2003 10:02 AM

Yes, of course! :D
I usually downsample to 44.1 kHz for maximum compatibility. Is that a stupid thing to do? I'll try to squeeze the movie onto ONE CD-R when I come home from work! With your excellent new script of course! :wink:
Thanks again!

Jellygoose 06-04-2003 10:49 AM

So for encoding at a lower resolution than 704x576/480, will the Scene Change Trigger value has to be changed?
Or is it working with the current script with all other resolutions too?

kwag 06-04-2003 10:53 AM

Quote:

Originally Posted by Jellygoose
So for encoding at a lower resolution than 704x576/480, will the Scene Change Trigger value has to be changed?
Or is it working with the current script with all other resolutions too?

I don't know Jellygoose :!:, I haven't tried it. I only tried the script at 528x480 and 704x480. The current "trigger" value should work for all resolutions. Give it a try at 352x240(288) and see what you get, then let us know 8)

-kwag

Jellygoose 06-04-2003 10:58 AM

I'm actually thinking about backing up my LOTR Special Extended Edition on one 99 min. CD-R, with the current script at that resolution... I'll do that later and tell you about the results!! :wink:
So at KVCDx3 resolution, the current script should also work with no blinks or flashes right?


All times are GMT -5. The time now is 06:38 PM  —  vBulletin © Jelsoft Enterprises Ltd

Site design, images and content © 2002-2024 The Digital FAQ, www.digitalFAQ.com
Forum Software by vBulletin · Copyright © 2024 Jelsoft Enterprises Ltd.