digitalFAQ.com Forum

digitalFAQ.com Forum (https://www.digitalfaq.com/forum/)
-   Restore, Filter, Improve Quality (https://www.digitalfaq.com/forum/video-restore/)
-   -   Various AVISynth scripts to clean noise and convert PAL to NTSC (https://www.digitalfaq.com/forum/video-restore/3790-avisynth-scripts-clean.html)

metaleonid 01-01-2012 05:52 PM

Various AVISynth scripts to clean noise and convert PAL to NTSC
 
Hello,

I have a few AVISynth script file presets that I use to clean/sharpen VHS and sometimes convert PAL to NTSC. I'd like to share them.

The footage is assumed interlaced and Huffyuv losslessly compressed.

Function names speak for themselves. First one just cleans and sharpens the image. I am sure I will get criticized for splitting into fields and treating fields like frames.

Code:

LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\2.0\LoadPluginEx2.dll")
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\2.0\DustV5.dll")
Import("..\LimitedSharpen\LimitedSharpen.avsi")

function vhs_interlaced(clip myclip, int start, int end, int "dest_width", int "dest_height", int "pixiedust_limit", float "sigma", int "plane", float "ss_x", float "ss_y")
{
myclip = AssumeTFF(myclip) #top field first
dest_width = default(dest_width, myclip.width)
dest_height = default(dest_height, myclip.height)
pixiedust_limit = default(pixiedust_limit, 4)
fft3dfilter_sigma = default(sigma, 2)
fft3dfilter_plane = default(plane, 0)
limitedsharpen_ss_x = default(ss_x, 1)
limitedsharpen_ss_y = default(ss_y, 1)

myclip = SeparateFields(myclip)
myclip = PixieDust(myclip, limit = pixiedust_limit) #spatial noise removal
myclip = fft3dfilter(myclip, sigma = fft3dfilter_sigma, bt = 5, plane = fft3dfilter_plane, sharpen = 0, svr = 0) #temporal noise removing
myclip = LimitedSharpen(myclip, ss_x = limitedsharpen_ss_x, ss_y = limitedsharpen_ss_y, Smode = 3, dest_x = dest_width)
myclip = Weave(myclip)

return myclip
}

Code:

function vhs_pal_interlaced_to_ntsc(clip myclip, int start, int end, int "dest_width", int "dest_height", int "pixiedust_limit", float "sigma", float "ss_x", float "ss_y")
{
myclip = AssumeTFF(myclip) #top field first
dest_width = default(dest_width, myclip.width)
dest_height = default(dest_height, myclip.height)
pixiedust_limit = default(pixiedust_limit, 4)
fft3dfilter_sigma = default(sigma, 1)
limitedsharpen_ss_x = default(ss_x, 1)
limitedsharpen_ss_y = default(ss_y, 1)

myclip = SeparateFields(myclip)
myclip = PixieDust(myclip, limit = pixiedust_limit)
myclip = fft3dfilter(myclip, sigma = fft3dfilter_sigma, bt = 5, sharpen = 0, svr = 0)
myclip = Weave(myclip)
myclip = TDeint(myclip, mode = 1, order = 1, type = 2) # Order = 0 for bff, 1 for tff => 50p fps
myclip = ConvertFPS(myclip, 59.94) # use interpolation to change to 59.94p fps
myclip = AssumeFrameBased(myclip) # Just in case
myclip = AssumeTFF(myclip) # Makes frame 0 become even field below
myclip = SeparateFields(myclip) # We will be throwing out 1 of each pair
myclip = SelectEvery(myclip, 4, 0, 3) # Choose even then odd, or TFF (use AssumeBFF()...
 # ...or SelectEvery(4,1,2) here for BFF
myclip = LimitedSharpen(myclip, ss_x = limitedsharpen_ss_x, ss_y = limitedsharpen_ss_y, Smode = 3, dest_x = dest_width, dest_y = 240)
myclip = Weave(myclip) # Combine fields for encoder

return myclip
}

The below 2 functions assume the footage is progressive PAL:

Code:

function vhs_pal_progressive(clip myclip, int start, int end, int "dest_width", int "dest_height", int "pixiedust_limit", float "sigma", float "ss_x", float "ss_y")
{
myclip = AssumeTFF(myclip) #top field first
dest_width = default(dest_width, myclip.width)
dest_height = default(dest_height, myclip.height)
pixiedust_limit = default(pixiedust_limit, 4)
fft3dfilter_sigma = default(sigma, 1)
limitedsharpen_ss_x = default(ss_x, 1)
limitedsharpen_ss_y = default(ss_y, 1)

myclip = PixieDust(myclip, limit = pixiedust_limit)
myclip = fft3dfilter(myclip, sigma = fft3dfilter_sigma, bt = 5, sharpen = 0, svr = 0)
myclip = SeparateFields(myclip) # I'd like to sharpen the field
myclip = LimitedSharpen(myclip, ss_x = limitedsharpen_ss_x, ss_y = limitedsharpen_ss_y, Smode = 3, dest_x = dest_width, dest_y = dest_height/2)
myclip = Weave(myclip)

return myclip
}

Code:

function vhs_pal_progressive_to_ntsc(clip myclip, int start, int end, int "dest_width", int "dest_height", int "pixiedust_limit", float "sigma", float "ss_x", float "ss_y")
{
myclip = AssumeTFF(myclip) #top field first
dest_width = default(dest_width, myclip.width)
dest_height = default(dest_height, myclip.height)
pixiedust_limit = default(pixiedust_limit, 4)
sigma = default(sigma, 1)
ss_x = default(ss_x, 1)
ss_y = default(ss_y, 1)

myclip = vhs_pal_progressive(myclip, start, end, dest_width, 480, pixiedust_limit, sigma, ss_x, ss_y)
myclip = TDeint(myclip, mode = 1, order = 1, type = 2) # Order = 0 for bff, 1 for tff => 50p fps
myclip = ChangeFPS(myclip, 59.94) # use interpolation to change to 59.94p fps
myclip = AssumeFrameBased(myclip) # Just in case
myclip = AssumeTFF(myclip) # Makes frame 0 become even field below
myclip = SeparateFields(myclip) # We will be throwing out 1 of each pair
myclip = SelectEvery(myclip, 4, 0, 3) # Choose even then odd, or TFF (use AssumeBFF()...
 # ...or SelectEvery(4,1,2) here for BFF
myclip = Weave(myclip) # Combine fields for encoder

return myclip
}

Discuss.

References:
FFfft3dfilter: http://avisynth.org.ru/fft3dfilter/fft3dfilter.html
LimitedSharpen: http://avisynth.org/mediawiki/LimitedSharpen

I can provide the before and after screen shots if anyone is interested.

kpmedia 01-01-2012 10:05 PM

I'll reply with my own scripting later....

For now, I edited your post to clean up the Avisynth scripting, and put it between [code][/code] tags.
Take no offense, I'm just trying to make it easy for others to follow along.

The excessive use of / makes it hard for newbies to know what's going on. Some of the devs at Doom9 and Avisynth.org use far too many / marks in order to put code on separate lines. With a GUI like AvsPmod, breaking the code up like that doesn't make much sense anymore. The GUI dumps all the variables into option boxes, and then the lines wrap in the code view.

As far as your code goes...

My preferred method is to convert PAL to progressive with a high quality deinterlacer, and then convert it to 480p24, and then alter the audio within a few ms via Sound Forge 9 Pro. In the era of 120hz HDTVs and progressive-scan DVD/Blu-ray players, it just makes more sense.

From my quick glance at the script, you're split fields, denoising it, padding it with dupe frames, and then re-interlacing it as NTSC. Is that correct? That does work, but it's forever juddered.

Definitely post some stills and clips. :)

metaleonid 01-04-2012 10:35 PM

5 Attachment(s)
Quote:

Originally Posted by kpmedia (Post 18743)
My preferred method is to convert PAL to progressive with a high quality deinterlacer, and then convert it to 480p24, and then alter the audio within a few ms via Sound Forge 9 Pro. In the era of 120hz HDTVs and progressive-scan DVD/Blu-ray players, it just makes more sense.

First off, the sound is noticeably slow. I've tried this with progressive scan PAL as source. Converted to NTSC film (23.97fps) and slowed down the soundtrack. I was very unhappy with the result and decided to create interlaced NTSC instead.

Quote:

Originally Posted by kpmedia (Post 18743)
As far as your code goes...From my quick glance at the script, you're split fields, denoising it, padding it with dupe frames, and then re-interlacing it as NTSC. Is that correct? That does work, but it's forever juddered.

It is correct although I am not sure about duping part. I don't think I do it, don't I? Again, I don't understand that deinterlacing part in the code. I "stole" the code from the guy who posted it on doom forum. I wanted to create my own version without deinterlacing. Why can't I just separate into fields, and just use ConvertFPS without deinterlacing? I haven't tried it, but I will give it a shot some day.

Now screen shots. The footage was interlaced PAL VHS that I captured with my LifeView FlyVideo 3000FM via S-Video cable from JVC VCR in Edit mode. The format is PAL 25fps 704x576, Huffyuv. I chose the frame where the motion takes place so that you can see interlaced artifacts. And you can see some chroma noise. The 1st image is the original unfiltered. The 2nd image is denoised/sharpened (see the script above) also in PAL and is in cropped D1 (i.e. 704x576). It's very interesting to compare this to the original. Take a closer look and let me know what you think as far as noise and sharpening goes. The 3rd image is denoised, converted to NTSC and then sharpened. The resolution is cropped D1 (704x480) for NTSC. The 2nd and the 3rd image are for illustration purposes. When I go for DVD, I use half D1, not cropped D1. So the last 2 images (4th and 5th) are half D1 PAL and half D1 NTSC respectively.

--Leonid

kpmedia 01-04-2012 11:25 PM

To quickly address the audio comment, you have to alter pitch by almost 4%. I forget the exact % down to thousandths.
Otherwise it goes chipmunk

juhok 01-06-2012 07:23 PM

To be blunt.. if stealing code, steal something better, this don't make sense to me.. (only commenting the denoising part)

Why PixieDust? It's legacy, you need tricks even to load the plugin in modern Avisynth, IIRC it's not that well suited for VHS even way back then. Also PixieDust is spatio-temporal filter, not just spatial (http://avisynth.org/mediawiki/Dust/PixieDust), FFT3DFilter is spatio-temporal also. I have no idea about PixieDust but the default settings passed to FFT3DFilter are too high, bt=5 can cause ghosting. Separating fields and then feeding them to temporal denoiser as progressive causes filter to work with wrong temporal information because you're missing the field inbetween. I don't have the know-how to explain this better than this. There's no reason to separate fields for denoising because FFT3DFilter supports interlaced processing (and PixieDust can be forgotten..). If you really want to use PixieDust, use something like lossless bob -> filter -> re-interlace. And finally a matter of taste, to my eyes the samples are oversharpened and cause artifacts from denoising to be visible.

I'd suggest taking a look at MVDeGrain[1-3] http://avisynth.org.ru/mvtools/mvtools2.html

About audio processing; http://avisynth.org/mediawiki/TimeStretch

PS. Post samples of the video, still images don't tell the full story.

metaleonid 01-06-2012 09:16 PM

Quote:

Originally Posted by juhok (Post 18801)
Why PixieDust? It's legacy, you need tricks even to load the plugin in modern Avisynth, IIRC it's not that well suited for VHS even way back then. Also PixieDust is spatio-temporal filter, not just spatial (http://avisynth.org/mediawiki/Dust/PixieDust), FFT3DFilter is spatio-temporal also.

I've tried one without another and honestly I thought that doing Pixie followed by FFT3DFilter give the best result. FFT3DFilter alone still leaves VHS noise. Pixie alone doesn't smooth out the noise. It clears VHS noise, but there are still some leftovers.

Quote:

Originally Posted by juhok (Post 18801)
but the default settings passed to FFT3DFilter are too high, bt=5 can cause ghosting.

bt = 5 is a mode. I think you've confused it with sigma. bt = 5 means that it looks 2 frames back and 2 frames forward. bt = 4 means 2 frames back, 1 frame forward. bt = 3 means 2 previous frames.

Quote:

Originally Posted by juhok (Post 18801)
Separating fields and then feeding them to temporal denoiser as progressive causes filter to work with wrong temporal information because you're missing the field inbetween.

You're right about FFT3DFilter. Although, I don't understand why I am missing the field in between. The fields now are progressive frame. What I should've done is to move even fields quarter scan line down and odd fields quarter scan line up and then feed them to the filters. Separating fields gives better result for PixieDust.

What should I have done is this:
SeparateFields
Pixie
Weave
FFT3D(bt = 5)
SeparateFields
LimitedSharpen
Weave

It will take forever to process though. :)

Quote:

Originally Posted by juhok (Post 18801)
I'd suggest taking a look at MVDeGrain[1-3] http://avisynth.org.ru/mvtools/mvtools2.html

Is it DegrainMedian? If so, I use it when I record from LaserDisc. For LDs I use DegrainMedian and then LimitedSharpen. No Pixie, no FFT. I'm not happy with LD results though.

Quote:

Originally Posted by juhok (Post 18801)
PS. Post samples of the video, still images don't tell the full story.

In Huffyuv? How long should the clip be? I mean they weigh a lot. :eek:

juhok 01-06-2012 09:47 PM

Quote:

Originally Posted by metaleonid (Post 18803)
bt = 5 is a mode. I think you've confused it with sigma. bt = 5 means that it looks 2 frames back and 2 frames forward. bt = 4 means 2 frames back, 1 frame forward. bt = 3 means 2 previous frames.

I'm not confused, temporal radius of 5 frames can cause ghosting in FFT3DFilter. Sigma affects this too so it's codependent.

Quote:

Originally Posted by metaleonid (Post 18803)
You're right about FFT3DFilter. Although, I don't understand why I am missing the field in between. The fields now are progressive frame. What I should've done is to move even fields quarter scan line down and odd fields quarter scan line up and then feed them to the filters. Separating fields gives better result for PixieDust.

My bad, I was thinking separate even / odd field processing which I just did in my own script yesterday. With Separatefields() they are in correct order but you must move even or odd fields one line up/down (not quarter). This can be done and some filters like FFT3Dfilter will do it internally. If you don't align the fields you're going to get bad temporal match which I guess results in more denoising/softening of the video.

Quote:

Originally Posted by metaleonid (Post 18803)
What should I have done is this:
SeparateFields
Pixie
Weave
FFT3D(bt = 5)
SeparateFields
LimitedSharpen
Weave

It will take forever to process though. :)

You need to add "interlaced=true" to FFT3Dfilter. Also, aligment issues..

Quote:

Originally Posted by metaleonid (Post 18803)
Is it DegrainMedian? If so, I use it when I record from LaserDisc. For LDs I use DegrainMedian and then LimitedSharpen. No Pixie, no FFT. I'm not happy with LD results though.

No it is not, it's MVDegrain from the MVTools2 package.

Quote:

Originally Posted by metaleonid (Post 18803)
In Huffyuv? How long should the clip be? I mean they weigh a lot. :eek:

5-10 frames should be enough.

I understand that things may look ok to you even if they're not done correctly. But passing bad workflow as advice to people who may use it to damage their videos without knowing any better is not nice. This is the danger with Avisynth, it's easy to mess up.

kpmedia 01-06-2012 10:03 PM

Quote:

Originally Posted by metaleonid (Post 18803)
In Huffyuv? How long should the clip be? I mean they weigh a lot.

You can upload up to 8MB (7.99MB) as a Free Member. That should cover at least 1 second (30 frames), as Huffyuv. That also easily allows for longer clips encoded down to MPEG-4, either as H.264 of Xvid in MP4 container, and with decent bitrates -- at least 60-90 seconds worth. That's plenty for samples.

Or use a Dropbox free account.

Remember that Site Staff don't visit "download sharing" sites like Megaupload: http://www.digitalfaq.com/forum/news...embership.html
Countdown clocks, ad-filled pages, etc -- wastes time, and those sites are often sources of malware exploits.

metaleonid 01-06-2012 10:45 PM

Quote:

Originally Posted by juhok (Post 18804)
I'm not confused, temporal radius of 5 frames can cause ghosting in FFT3DFilter. Sigma affects this too so it's codependent.

Actually I was always using 4 until I got the newer version where I can use 5. Now what would you suggest?

Quote:

Originally Posted by juhok (Post 18804)
With Separatefields() they are in correct order but you must move even or odd fields one line up/down (not quarter). This can be done and some filters like FFT3Dfilter will do it internally. If you don't align the fields you're going to get bad temporal match which I guess results in more denoising/softening of the video.

You're correct and I have realized that I have screwed some of my videos up over the years which now can't be undone.

Do you happen to know off the top of your head the AVISynth function that moves the field up and down?

By the way, why is that one line up and one line down? Don't they have to meet in the middle to be aligned? Shouldn't it be half line up and half line down? 100fps.com suggested quarter scan line in deinterlacing. Please, advice.

So I guess, the more correct way should be:
Code:

SeparateFields
If even move down, if odd move up
Pixie
If even move up, if odd move down
Weave
FFT3D(bt = ?, interlaced = true)
SeparateFields
LimitedSharpen
Weave

LimitedSharpen is spatial as far as I know so no need to move up and down.

Quote:

Originally Posted by juhok (Post 18804)
I understand that things may look ok to you even if they're not done correctly. But passing bad workflow as advice to people who may use it to damage their videos without knowing any better is not nice. This is the danger with Avisynth, it's easy to mess up.

I totally agree. I just didn't know better. I made this script up using trial and failure.

juhok 01-06-2012 11:04 PM

I'm way past my bedtime and I'll comment more later. For now I wanted to say that you only need to move even OR odd fields, not both. If you think about what an interlaced frame is, you'll figure out why. :) No idea why 100fps talks about quarter. I don't remember how properly to do this in code, check doom9.

My way has been (slower than your way because of double height):
Code:

Bob(0.0, 1.0) # lossless bob
Progressive filter here
Assume?FF.SeparateFields().SelectEvery(4,0,3).Weave() # AssumeTFF / AssumeBFF per source & re-interlace


metaleonid 01-09-2012 10:23 AM

Quote:

Originally Posted by juhok (Post 18807)
I'm way past my bedtime and I'll comment more later. For now I wanted to say that you only need to move even OR odd fields, not both. If you think about what an interlaced frame is, you'll figure out why. :) No idea why 100fps talks about quarter. I don't remember how properly to do this in code, check doom9.

My way has been (slower than your way because of double height):
Code:

Bob(0.0, 1.0) # lossless bob
Progressive filter here
Assume?FF.SeparateFields().SelectEvery(4,0,3).Weave() # AssumeTFF / AssumeBFF per source & re-interlace


What does the lossless bob do? Does it separate fields and moves top one down one line? If so, before you weave it, don't you need to move the top one back up? Thanks.

Now do you suggest I do this:

Code:

Bob(0.0, 1.0) # lossless bob
Pixie
FFT3D(bt = 5, interlaced = false)
LimitedSharpen
AssumeTFF.SeparateFields().SelectEvery(4,0,3).Weave()

I'm a bit confused how bob works. I thought it separates fields and doubles the rate thus each field is a progressive frame.

--Leonid

kpmedia 01-09-2012 02:57 PM

Code:

SetMemoryMax(512)
SetMTMode(3)

avisource("c:\video.avi")
# ffvideosource("c:\video.m2v")
SetMTMode(2)
ConvertToYV12()
QTGMC(Preset="Slow")
SelectEven()
Deen() # remove edge noise
# FFT3DFilter(sigma=4, bt=3, plane=3)
# TTempSmooth(maxr=5, lthresh=16, cthresh=4, strength=4)
Cnr2("xoo",4,2,64) # remove chroma banding noise, wide UV setting
RemoveGrain(mode=2, modeU=2, modeV=2)
# DeGrainMedian(limitY=15,limitUV=7,mode=0)
Spline36Resize(720,480)
AssumeFPS(23.976)

SetMTMode(1)
GetMTMode(false) > 0 ? distributor() : last

This is from Avisynth 2.6 MT.

Some of the commented out NR settings can be activated, though never all of them together. I snagged some quick settings from the main processing system just now. These are some common basics amongst the various scripts written for specific types of errors.

All of the numbers are fungible, change as needed.

Avisynth is not a monkey-see/monkey-do type software. It's easier to make a mess than to improve video, if you don't fully understand what's happening in the scripts. Only use as many filters as needed. Never unnecessarily use extra filters, as it mostly just makes quality worse (not better).

AvsPmod is helpful, so you can preview what's going on by changing settings, or activating various filters.

Do note that some of these can be created in VirtualDub, too. Vdub has several NR filters. The Median filter, for example. Or changing the framerate to 23.976. Some things are only possible in Avisynth, some are only possible in VirtualDub, and some can be done in both.

juhok 01-09-2012 07:49 PM

Quote:

Originally Posted by metaleonid (Post 18824)
What does the lossless bob do? Does it separate fields and moves top one down one line? If so, before you weave it, don't you need to move the top one back up? Thanks.

Now do you suggest I do this:

Code:

Bob(0.0, 1.0) # lossless bob
Pixie
FFT3D(bt = 5, interlaced = false)
LimitedSharpen
AssumeTFF.SeparateFields().SelectEvery(4,0,3).Weave()

I'm a bit confused how bob works. I thought it separates fields and doubles the rate thus each field is a progressive frame.

--Leonid

Read http://avisynth.org/mediawiki/Bob (I remembered wrong, only luma was lossless). Your code is correct.

PS. Instead of LimitedSharpen try http://avisynth.org/mediawiki/LSFmod
PPS. http://avisynth.org/ is a good reading :)


All times are GMT -5. The time now is 02:51 PM

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