Log in

View Full Version : RemoveDirtVS


handaimaoh
18th November 2013, 19:22
So I've finally finished porting RemoveDirt to Vapoursynth. I've tested it a little bit but it could definitely use more extensive testing.

To use it in Vapoursynth here is also the ported version of the Avisynth script as a function:


#removedirtvs.py

import vapoursynth as vs

def RemoveDirt(input, repmode=16):
core = vs.get_core()
cleansed = core.rgvs.Clense(input)
sbegin = core.rgvs.ForwardClense(input)
send = core.rgvs.BackwardClense(input)
scenechange = core.rdvs.SCSelect(input, sbegin, send, cleansed)
alt = core.rgvs.Repair(scenechange, input, mode=[repmode,repmode,1])
restore = core.rgvs.Repair(cleansed, input, mode=[repmode,repmode,1])
corrected = core.rdvs.RestoreMotionBlocks(cleansed, restore, neighbour=input, alternative=alt, gmthreshold=70, dist=1, dmode=2, noise=10, noisy=12, grey=0)
return core.rgvs.RemoveGrain(corrected, mode=[17,17,1])


Copy and paste that into a text file and save as removedirtvs.py and then put it into the Lib/site-packages folder in your Python install. And then you can simply use the function as in this example:


import vapoursynth as vs
import removedirtvs

core = vs.get_core()
core.std.LoadPlugin(plugins_vs + r'ffms2.dll')
core.std.LoadPlugin(plugins_vs + r'RemoveDirt.dll')
clip = core.ffms2.Source(src)
clip = removedirtvs.RemoveDirt(clip)
clip.set_output()


Windows 32-bit build (https://www.dropbox.com/s/r00l64nb8lbp4rx/RemoveDirtVS.dll). Windows 64-bit build (https://www.dropbox.com/s/98hsfusuvkjh1c0/RemoveDirtVS_x64.dll).

Source code is here (https://github.com/handaimaoh/removedirtvs).

handaimaoh
18th November 2013, 19:51
I forgot to mention, if you are compiling it yourself make sure that "VS_TARGET_CPU_X86" is defined because there is no fallback code in case it isn't right now. I will also eventually create a makefile.

Edited to add:
Just added an #error case in case that is not defined.

Myrsloik
18th November 2013, 20:40
I forgot to mention, if you are compiling it yourself make sure that "VS_TARGET_CPU_X86" is defined because there is no fallback code in case it isn't right now. I will also eventually create a makefile.

Edited to add:
Just added an #error case in case that is not defined.

Uh, why is the function in a class you have to instantiate and not a simple function in a python module?

handaimaoh
18th November 2013, 20:41
Uh, why is the function in a class you have to instantiate and not a simple function in a python module?

I simply copied how Chikuzen did his ImageReader script here (http://forum.doom9.org/showthread.php?t=166088). I don't really know more than a passing amount of Python.

Myrsloik
18th November 2013, 20:44
I simply copied how Chikuzen did his ImageReader script here (http://forum.doom9.org/showthread.php?t=166088). I don't really know more than a passing amount of Python.

This should work:

#removedirtvs.py

import vapoursynth as vs

def RemoveDirt(input, repmode=16):
core = vs.get_core()
cleansed = core.rgvs.Clense(input)
sbegin = core.rgvs.ForwardClense(input)
send = core.rgvs.BackwardClense(input)
scenechange = core.rdvs.SCSelect(input, sbegin, send, cleansed)
alt = core.rgvs.Repair(scenechange, input, mode=[repmode,repmode,1])
restore = core.rgvs.Repair(cleansed, input, mode=[repmode,repmode,1])
corrected = core.rdvs.RestoreMotionBlocks(cleansed, restore, neighbour=input, alternative=alt, gmthreshold=70, dist=1, dmode=2, noise=10, noisy=12, grey=0)
return core.rgvs.RemoveGrain(corrected, mode=[17,17,1])

handaimaoh
19th November 2013, 17:16
This should work:

#removedirtvs.py

import vapoursynth as vs

def RemoveDirt(input, repmode=16):
core = vs.get_core()
cleansed = core.rgvs.Clense(input)
sbegin = core.rgvs.ForwardClense(input)
send = core.rgvs.BackwardClense(input)
scenechange = core.rdvs.SCSelect(input, sbegin, send, cleansed)
alt = core.rgvs.Repair(scenechange, input, mode=[repmode,repmode,1])
restore = core.rgvs.Repair(cleansed, input, mode=[repmode,repmode,1])
corrected = core.rdvs.RestoreMotionBlocks(cleansed, restore, neighbour=input, alternative=alt, gmthreshold=70, dist=1, dmode=2, noise=10, noisy=12, grey=0)
return core.rgvs.RemoveGrain(corrected, mode=[17,17,1])

Ok, I'll update the first post.

handaimaoh
20th November 2013, 00:30
So I guess no one has actually tested this yet but myself since there was a crash bug introduced where I forgot to add back in an EMMS call that was removed at one point from a method that would call into the few functions using MMX intrinsics. Sometimes it would manifest at the very beginning of a video and sometimes after a few hundred to a few thousand frames. So that has now been fixed and a new binary is posted here (https://www.dropbox.com/s/r00l64nb8lbp4rx/RemoveDirtVS.dll).

I knew that stupid MMX code would haunt me in some way...

Myrsloik
20th November 2013, 00:40
So I guess no one has actually tested this yet but myself since there was a crash bug introduced where I forgot to add back in an EMMS call that was removed at one point from a method that would call into the few functions using MMX intrinsics. Sometimes it would manifest at the very beginning of a video and sometimes after a few hundred to a few thousand frames. So that has now been fixed and a new binary is posted here (https://www.dropbox.com/s/r00l64nb8lbp4rx/RemoveDirtVS.dll).

I knew that stupid MMX code would haunt me in some way...

VapourSynth requires sse2 so it's a mystery why you kept the mmx code path as well. Just delete the mmx and have a better life.

handaimaoh
20th November 2013, 00:44
I realize that. It's not a seperate code path, though. It's a couple of functions that were only written in MMX. I was already going to rewrite them since the MMX intrinsics prevents compiling for x64. It's just that I hadn't done it yet and I noticed that it was causing the plugin to instantly crash in some cases because of the lack of emms.

handaimaoh
20th November 2013, 02:00
So converted and purged all the MMX code so I can now make x64 Windows builds. Will build new binaries tomorrow morning.

Reel.Deel
20th November 2013, 02:16
So I guess no one has actually tested this yet but myself since there was a crash bug introduced where I forgot to add back in an EMMS call that was removed at one point from a method that would call into the few functions using MMX intrinsics. Sometimes it would manifest at the very beginning of a video and sometimes after a few hundred to a few thousand frames. So that has now been fixed and a new binary is posted here (https://www.dropbox.com/s/r00l64nb8lbp4rx/RemoveDirtVS.dll).


Ok now I have some time to test, but when I try to load your recently uploaded binary I get this error: (does not happen with the previous binary)
---------------------------
VirtualDub Error
---------------------------
Avisynth open failure:
Python exception: 'Failed to load C:\\Vapoursynth\\RemoveDirtVS\\RemoveDirtVS.dll'
Traceback (most recent call last):
File "vapoursynth.pyx", line 1082, in vapoursynth.vpy_evaluateScript (src\cython\vapoursynth.c:16736)
File "C:\VapourSynth\RemoveDirtVS\test.vpy", line 4, in <module>
core.std.LoadPlugin(path=r'C:\Vapoursynth\RemoveDirtVS\RemoveDirtVS.dll')
File "vapoursynth.pyx", line 1005, in vapoursynth.Function.__call__ (src\cython\vapoursynth.c:15793)
vapoursynth.Error: 'Failed to load C:\\Vapoursynth\\RemoveDirtVS\\RemoveDirtVS.dll'

handaimaoh
20th November 2013, 02:18
What's your script? Loaded just fine for me after I made the fix to test.

Reel.Deel
20th November 2013, 02:19
import vapoursynth as vs
core = vs.get_core()
core.avs.LoadPlugin(path=r'C:\Program Files\AviSynth 2.5\plugins\DGDecodeNV.dll')
core.std.LoadPlugin(path=r'C:\Vapoursynth\RemoveDirtVS\RemoveDirtVS.dll')

# Source
src = core.avs.DGSource(dgi=r'C:\Vapoursynth\RemoveDirtVS\00110.dgi')

# Proccesing
src = core.vivtc.VFM(clip=src, order=1, field=1)
src = core.vivtc.VDecimate(clip=src)

# RemoveDirt
clip = src
cleansed = core.rgvs.Clense(clip)
sbegin = core.rgvs.ForwardClense(clip)
send = core.rgvs.BackwardClense(clip)
scenechange = core.rdvs.SCSelect(clip, sbegin, send, cleansed)
alt = core.rgvs.Repair(scenechange, clip, mode=[16,16,1])
restore = core.rgvs.Repair(cleansed, clip, mode=[16,16,1])
corrected = core.rdvs.RestoreMotionBlocks(cleansed, restore, neighbour=clip, alternative=alt, gmthreshold=70, dist=1, dmode=2, noise=10, noisy=12, grey=0)
clip = core.rgvs.RemoveGrain(clip, mode=[17,17,1])
clip.set_output()

handaimaoh
20th November 2013, 02:21
Just pulled down the binary on another computer and ran the same script. Loaded just fine in VirtualDub for me. What version of Vapoursynth you running?

Reel.Deel
20th November 2013, 02:24
Hmm that's odd. If I replace the binary with the older one, it loads without errors. :confused:

Im using VS R21 on 32-bit Windows XP sp3.

Going to update to R22 test version to see if that does anything.

Update:
With R22 test 2, I can't even load a script. It just freezes VDub. I also removed RemoveDirt and tried with a simpler script and still froze.
Will investigate a little more.

handaimaoh
20th November 2013, 02:38
I'm running R22 test 2 on both machines. Loads just fine. *shrug*

handaimaoh
20th November 2013, 16:52
First post has been updated with both 32-bit and 64-bit builds for Windows.

Reel.Deel
15th December 2014, 20:52
The links in the OP are dead so just in case someone is looking for them here they are: RemoveDirtVS.zip (https://www.dropbox.com/s/wuv5a3ufqrerra6/RemoveDirtVS.zip?dl=1)

Selur
20th February 2022, 16:47
I'm using a script called 'killerspots' (https://github.com/Selur/VapoursynthScriptsInHybrid/blob/master/killerspots.py) which works find unless I use:it with advanced = True (the default) in which case:
def RemoveDirtMod(clip: vs.VideoNode, limit: int =10):
core = vs.core
clensed = core.rgvs.Clense(clip)
alt = core.rgvs.RemoveGrain(clip,mode=1)
clip = core.rdvs.RestoreMotionBlocks(clensed, clip, alternative=alt, pthreshold=4, cthreshold=6, gmthreshold=40, dist=3, dmode=2, noise=limit, noisy=12)
return clip
gets used and then the script crashes after some time.
The script I use is:
# Imports
import os
import sys
import ctypes
# Loading Support Files
Dllref = ctypes.windll.LoadLibrary("I:/Hybrid/64bit/vsfilters/Support/libfftw3f-3.dll")
import vapoursynth as vs
# getting Vapoursynth core
core = vs.core
# Import scripts folder
scriptPath = 'I:/Hybrid/64bit/vsscripts'
sys.path.insert(0, os.path.abspath(scriptPath))
# Loading Plugins
core.std.LoadPlugin(path="I:/Hybrid/64bit/vsfilters/MiscFilter/MiscFilters/MiscFilters.dll")
core.std.LoadPlugin(path="I:/Hybrid/64bit/vsfilters/DenoiseFilter/TTempSmooth/TTempSmooth.dll")
core.std.LoadPlugin(path="I:/Hybrid/64bit/vsfilters/DenoiseFilter/DFTTest/DFTTest.dll")
core.std.LoadPlugin(path="I:/Hybrid/64bit/vsfilters/DenoiseFilter/NEO_FFT3DFilter/neo-fft3d.dll")
core.std.LoadPlugin(path="I:/Hybrid/64bit/vsfilters/Support/DCTFilter.dll")
core.std.LoadPlugin(path="I:/Hybrid/64bit/vsfilters/DeblockFilter/Deblock/Deblock.dll")
core.std.LoadPlugin(path="I:/Hybrid/64bit/vsfilters/DenoiseFilter/RemoveDirt/RemoveDirtVS.dll")
core.std.LoadPlugin(path="I:/Hybrid/64bit/vsfilters/Support/libmvtools.dll")
core.std.LoadPlugin(path="I:/Hybrid/64bit/vsfilters/GrainFilter/RemoveGrain/RemoveGrainVS.dll")
core.std.LoadPlugin(path="I:/Hybrid/64bit/vsfilters/SourceFilter/LSmashSource/vslsmashsource.dll")
# Import scripts
import havsfunc
import killerspots
# source: 'G:\TestClips&Co\files\noisy cartoons examples\1080p_v2.mp4'
# current color space: YUV420P8, bit depth: 8, resolution: 1440x1080, fps: 25, color matrix: 709, yuv luminance scale: limited, scanorder: progressive
# Loading G:\TestClips&Co\files\noisy cartoons examples\1080p_v2.mp4 using LibavSMASHSource
clip = core.lsmas.LibavSMASHSource(source="G:/TestClips&Co/files/noisy cartoons examples/1080p_v2.mp4")
# Setting color matrix to 709.
clip = core.std.SetFrameProps(clip, _Matrix=1)
clip = clip if not core.text.FrameProps(clip,'_Transfer') else core.std.SetFrameProps(clip, _Transfer=1)
clip = clip if not core.text.FrameProps(clip,'_Primaries') else core.std.SetFrameProps(clip, _Primaries=1)
# Setting color range to TV (limited) range.
clip = core.std.SetFrameProp(clip=clip, prop="_ColorRange", intval=1)
# making sure frame rate is set to 25
clip = core.std.AssumeFPS(clip=clip, fpsnum=25, fpsden=1)
clip = killerspots.KillerSpots(clip=clip)
# denoising using MCTemporalDenoise
clip = havsfunc.MCTemporalDenoise(i=clip, settings="very high", ncpu=1)
# set output frame rate to 25.000fps
clip = core.std.AssumeFPS(clip=clip, fpsnum=25, fpsden=1)
# Output
clip.set_output()
(Tried different source filters and settings Vapoursynth core count to 1, none did help.)

Calling:
VSPipe.exe --progress E:\Output\encodingTempSynthSkript_2022-02-20@16_37_30_9010.vpy -c y4m NUL]
If get:

[mov,mp4,m4a,3gp,3g2,mj2 @ 000001fe31031d00] stream 0, timescale not set
Script evaluation done in 1.18 seconds
Frame: 209/1992 (11.73 fps)
In each call it ends at different frame numbers. (another Hybrid user has the same issue so I suspect that it's not related to my system itself)

When not using advanced=True, only 'clip = core.rgvs.Clense(clip)' will be called and the scripts runs fine.
So I suspect the issue is with 'rdvs.RestoreMotionBlocks'. since the RemoveDirt version I use is the 64bit one from above which from 2013 I was wondering whether this could be due to some incompatibility with the current Vapoursynth version or if anyone has an idea how to fix this.

Cu Selur

Ps.: read over at https://forum.doom9.org/showthread.php?t=176199 that the Vapoursynth port seems to be known to be buggy,.. :/

Selur
5th June 2022, 15:10
In case someone has the knowledge and motivation to look into RemoveDirts RestoreMotionBlocks randomly crashing:

When I change
clip = core.rdvs.RestoreMotionBlocks(clensed, clip, alternative=alt, pthreshold=4, cthreshold=6, gmthreshold=40, dist=3, dmode=2, noise=limit, noisy=12)
to
clip = core.rdvs.RestoreMotionBlocks(clensed, clip, alternative=alt, pthreshold=4, cthreshold=6, gmthreshold=40, dist=3, dmode=2, noise=limit, noisy=0)
by setting noisy to zero I get no random crash.
So the problem probably lies somehwere in the noise handling during motion detection.


Cu Selur