Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # film restoring script by videoFred.
- # denoising, resizing, stabilising, sharpening, auto-levels and auto-white balance.
- # film restoration script by videoFred.
- # denoising, resizing, stabilising, sharpening, auto-levels and auto-white balance.
- #
- # Modified by John Meyer, June 8, 2010
- #
- # Changes made by Meyer:
- # 1. Replaced the MVDegrainMulti function, which was part of the original MVTools
- # with the equivalent function using the newer and faster MVTools2.
- # 2. Added RemoveDirt function prior to Degrain in order to eliminate large dust spots. This
- # substantially improves the dirt removal capabilities of this script. The user will need to
- # download this plugin at:
- # http://www.removedirt.de.tf/
- # 3. Eliminated a great deal of sharpening. The original script did sharpening in at least four places.
- # With grainy film stock, this sometimes created objectionable grain. Also, the limitedSharpenFaster
- # function, while excellent for video, is uncessarily slow, and isn't really needed for this low
- # resolution source. Too much sharpening can make the film look too much like video.
- # 4. Added multithreading. I was able to roughly triple the script performance. It may be possible to
- # substantially increase this, perhaps as much as 12x instead of 3x. However, the autolevels function
- # would have to be replaced.
- # 5. Fixed several small errors I found. The result3 option didn't have the manual color correction code, so I
- # added that back in. All the numbered "stab" variables (stab1, stab2, etc.) aren't needed except
- # for providing a test function for stabilization. I eliminated all of this to streamline the script.
- # 6. Deflicker seemed redundant, given all the averaging that takes place with MDegrain, and also the averaging
- # that is done when the autolevels outputs are selected, so I took it out.
- # 7. I reduced the number of frames used for averaging autolevels from the default (which is 5) to 2.
- # I probably should add a variable in the header so the user can change this. Something else to do
- # in the future ...
- # 8. I added yet another set of crop parameters. I did this because both my capture and my output are
- # done using NTSC DV AVI which is 720x480. However, 8mm film is almost exactly square, so the captured
- # 720x480 video has black bars on the side. These need to be cropped off prior to doing motion
- # stabilization, but then added back prior to the final output, which must still be 720x480 with the
- # black bars on the side.
- # 9. I reduced the default depan settings to 20, which is what was recommended in the original script. I
- # also reduced the post-depan cropping. I did this so I could keep as much of the original frame
- # as possible.
- # 10. I removed the second denoising and sharpening function. It just seemed to be too much, and made the
- # result too artificial
- # 11. I removed the MVFLowFPS interpolation. I did this during one of dozens and dozens of attempts
- # to improve the speed of the script. I should probably add this back, but if I do so, I also
- # need to make it work correctly for interlaced output. If the goal is to show this on an NTSC or PAL
- # television set, then it is not correct to convert from the film fps to 25 fps progressive (PAL) or
- # 29.97 fps progressive (NTSC). Instead, this should be done as follows (example given is NTSC):
- #
- # MFlowFPS(source,super,backward_vec, forward_vec, num=60000, den=1001,ml=200)
- # SeparateFields()
- # SelectEvery(4, 0, 3)
- # Weave()
- #
- # This yields interlaced 29.97, which has twice the temporal resolution as 29.97 progressive, and will
- # therefore look correct on a TV set. I have done a lot of this as part of my Kinescope to video
- # conversion scripts. For those scripts, the goal is to make the filmed version of a TV show look
- # like it was actually videotaped. However, for something that originated on film, this "does violence"
- # to the original feel of the media. It is true that it can make horizontal pans less "juddery," but
- # it won't feel like film anymore. Also, this technique does break down, espcially with fast motion
- # in the foreground.
- # 12. Removed the unecessary "coloryuv(off_U=blue,off_V=red)" statement from the denoising section.
- # 13. Added killaudio() statement to prevent lockups when using SetMTMode().
- #====================================================================================
- #Change the following line to point to your video file
- film="C:\Documents and Settings\Goran\Desktop\svadbadeinterlace.avi" # source clip, you must specify the full path here
- #====================================================================================
- #GENERAL PARAMETERS
- #----------------------------------------------------------------------------------------------------------------------------
- result="results3" #specify the wanted output here
- trim_begin=0 play_speed=50 numerator= 25 denumerator= 1 #trim frames and play speed (PAL: 16.6666 or 18.75)
- #COLOR AND LEVELS PARAMATERS
- #----------------------------------------------------------------------------------------------------------------------------
- saturation=1.2 #zasitenost
- gamma=1.2 #for all outputs
- blue= -0 red=-0 #manual color adjustment, when returning result3 & result4. Values can be positive or negative
- black_level=0 white_level=255 output_black=0 output_white=255 #manual levels, when returning result2 & result4
- #SIZE, CROP AND BORDERS PARAMETERS
- #----------------------------------------------------------------------------------------------------------------------------
- CLeft=16 CTop=16 CRight=16 CBottom=16 #crop values after Depan and before final resizing
- W=1024 H=768 #final size after cropping
- bord_left=0 bord_top=0 bord_right=0 bord_bot=0 #720p= borders 150
- in_bord_left=0 in_bord_top=0 in_bord_right=0 in_bord_bot=0 #Borders around input that must be removed
- #STABILISING PARAMETERS
- #----------------------------------------------------------------------------------------------------------------------------
- maxstabH=20 #maximum values for the stabiliser (in pixels) 20 is a good start value
- maxstabV=20
- est_left=40 est_top=40 est_right=40 est_bottom=40 est_cont=1.6 #crop and contast values for special Estimate clip
- #DENOISING PARAMETERS
- #----------------------------------------------------------------------------------------------------------------------------
- denoising_strength=800 #denoising level of first denoiser: MVDegrain()
- block_size= 16 #block size of MVDegrain
- block_size_v= 16
- block_over= 8 #block overlapping of MVDegrainMulti()
- dirt_strength=23 #sets amount of dirt removal (big spots)
- #FOUR STEP SHARPENING PARAMETERS
- #--------------------------------------------------------------------------------------------------------------------------------
- PRE_sharp_ness= 120 PRE_radi_us= 3 #presharpening (UnsharpMask) just after first denoising
- #AUTO LEVELS PARAMETER
- #--------------------------------------------------------------------------------------------------------------------------------
- X=4 #X is a special parameter for reducing the autolevels effect on the whites
- X2=2 #X2 is a special parameter for reducing the autolevels effect on the blacks
- # END VARIABLES, BEGIN SCRIPT
- #=================================================================================================================================
- SetMemoryMax(1024) #set this to 1/3 of the available memory
- Loadplugin("Depan.dll")
- LoadPlugin("DepanEstimate.dll")
- Loadplugin("removegrain.dll")
- LoadPlugin("MVTools2.dll")
- Loadplugin("mt_masktools.dll")
- Loadplugin("warpsharp.dll")
- LoadPlugIn("LimitedSupport_09Jan06B.dll")
- LoadPlugin("autolevels.dll")
- loadplugin("C:\Program Files\AviSynth 2.5\plugins\RemoveDirtSSE2.dll")
- #Remove all setmtmode statements (there are three in this script) if not using multi-threaded (MT) AVISynth
- setmtmode(5,4)
- source1= Avisource(film).killaudio().assumefps(play_speed).trim(trim_begin,0).converttoYV12()
- cropped_source=source1.crop(in_bord_left,in_bord_top,-in_bord_right,-in_bord_bot) #remove any black borders on input video
- setmtmode(2,0)
- #STABILIZING
- #....................................................................................................................................................................
- stab_reference= cropped_source.crop(est_left,est_top,-est_right,-est_bottom).tweak(cont=est_cont).MT_binarize(threshold=80).greyscale().invert()
- mdata=DePanEstimate(stab_reference,trust=1.0,dxmax=maxstabH,dymax=maxstabV)
- stab=DePanStabilize(cropped_source,data=mdata,cutoff=0.5,dxmax=maxstabH,dymax=maxstabV,method=1,mirror=15)
- #DENOISING
- #...................................................................................................................................................................
- input_to_removedirt=stab.crop(CLeft,CTop,-CRight,-CBottom)
- stabcrop=RemoveDirtMC(input_to_removedirt,dirt_strength,false)
- super = stabcrop.MSuper(pel=2)
- bvec1 = MAnalyse(super, isb = true, delta = 1, blksize=block_size, overlap=block_over)
- fvec1 = MAnalyse(super, isb = false, delta = 1, blksize=block_size, overlap=block_over)
- bvec2 = MAnalyse(super, isb = true, delta = 2, blksize=block_size, overlap=block_over)
- fvec2 = MAnalyse(super, isb = false, delta = 2, blksize=block_size, overlap=block_over)
- bvec3 = MAnalyse(super, isb = true, delta = 3, blksize=block_size, overlap=block_over)
- fvec3 = MAnalyse(super, isb = false, delta = 3, blksize=block_size, overlap=block_over)
- #Use the second line below, and un-comment two lines above if you want to average more frames. Doesn't seem necessary for most work IMHO.
- denoised=stabcrop.MDegrain2(super, bvec1,fvec1,bvec2,fvec2,thSAD=denoising_strength).levels(0,gamma,255,0,255).tweak(sat=saturation).unsharpmask(PRE_sharp_ness,PRE_radi_us,0)
- denoised=stabcrop.MDegrain3(super, bvec1,fvec1,bvec2,fvec2,bvec3,fvec3,thSAD=denoising_strength).levels(0,gamma,255,0,255).tweak(sat=saturation).unsharpmask(PRE_sharp_ness,PRE_radi_us,0)
- #SHARPENING
- #...................................................................................................................................................................
- sharp1=denoised.sharpen(0.2)
- PreBorderFrame = sharp1.Lanczos4Resize(W - bord_left - in_bord_left - bord_right - in_bord_right, H - bord_top - in_bord_top - bord_bot - in_bord_bot)
- #RESULT1: AUTOLEVELS,AUTOWHITE
- #......................................................................................................................................................................
- setmtmode(5,4)
- result1= PreBorderFrame.coloryuv(autowhite=true).addborders(X,0,0,0,$FFFFFF).addborders(0,0,X2,0,$000000).autolevels(filterRadius=2).crop(X,0,-X2,-0).addborders(bord_left+in_bord_left, bord_top+in_bord_top, bord_right+in_bord_right, bord_bot+in_bord_bot)
- #RESULT2: MANUAL LEVELS, AUTOWHITE
- #......................................................................................................................................................................
- result2= PreBorderFrame.levels(black_level,gamma,white_level,0,255).coloryuv(autowhite=true).addborders(bord_left+in_bord_left, bord_top+in_bord_top, bord_right+in_bord_right, bord_bot+in_bord_bot)
- #RESULT3: AUTOLEVELS, MANUAL COLOR CORRECTIONS
- #.....................................................................................................................................................................
- result3= PreBorderFrame.coloryuv(off_U=blue,off_V=red).addborders(X,0,0,0,$FFFFFF).addborders(0,0,X2,0,$000000).autolevels(filterRadius=2).crop(X,0,-X2,-0).addborders(bord_left+in_bord_left, bord_top+in_bord_top, bord_right+in_bord_right, bord_bot+in_bord_bot)
- #RESULT4: MANUAL LEVELS, MANUAL COLOR CORRECTIONS
- #.....................................................................................................................................................................
- result4= PreBorderFrame.coloryuv(off_U=blue,off_V=red).levels(black_level,gamma,white_level,0,255).addborders(bord_left+in_bord_left, bord_top+in_bord_top, bord_right+in_bord_right, bord_bot+in_bord_bot)
- #PARAMETERS FOR THE COMPARISONS
- #.....................................................................................................................................................................
- W2= W+bord_left+bord_right
- H2= H+bord_top+bord_bot
- source4=Lanczos4Resize(source1,W2,H2)
- #COMPARISONS: ORIGINAL VS RESULTS
- #......................................................................................................................................................................
- resultS1= stackhorizontal(subtitle(source4,"original",size=28,align=2),subtitle(result1,"autolevels, autowhite",size=28,align=2))
- resultS2= stackhorizontal(subtitle(source4,"original",size=28,align=2),subtitle(result2,"autowhite, manual levels correction",size=28,align=2))
- resultS3= stackhorizontal(subtitle(source4,"original",size=28,align=2),subtitle(result3,"autolevels, manual color correction",size=28,align=2))
- resultS4= stackhorizontal(subtitle(source4,"original",size=28,align=2),subtitle(result4,"manual colors and levels correction",size=28,align=2))
- Eval(result)
- # END SCRIPT, BEGIN FUNCTIONS
- #=================================================================================================================================
- #REMOVE DIRT FUNCTION
- #......................................................................................................................................................................
- function RemoveDirt(clip input, int limit, bool _grey)
- {
- clensed=input.Clense(grey=_grey, cache=4)
- alt=input.RemoveGrain(2)
- return RestoreMotionBlocks(clensed,input,alternative=alt,pthreshold=6,cthreshold=8, gmthreshold=40,dist=3, dmode=2,debug=false,noise=limit,noisy=4, grey=_grey)
- # Alternative settings
- # return RestoreMotionBlocks(clensed,input,alternative=alt,pthreshold=4,cthreshold=6, gmthreshold=40,dist=1,dmode=2,debug=false,noise=limit,noisy=12,grey=_grey,show=true)
- # return RestoreMotionBlocks(clensed,input,alternative=alt,pthreshold=6,cthreshold=8, gmthreshold=40,dist=3,tolerance= 12,dmode=2,debug=false,noise=limit,noisy=12,grey=_grey,show=false)
- }
- function RemoveDirtMC(clip,int limit, bool "_grey")
- {
- _grey=default(_grey, false)
- limit = default(limit,6)
- i=MSuper(clip,pel=2)
- bvec = MAnalyse(i,isb=false, blksize=8, delta=1, truemotion=true)
- fvec = MAnalyse(i,isb=true, blksize=8, delta=1, truemotion=true)
- backw = MFlow(clip,i,bvec)
- forw = MFlow(clip,i,fvec)
- clp=interleave(backw,clip,forw)
- clp=clp.RemoveDirt(limit,_grey)
- clp=clp.SelectEvery(3,1)
- return clp
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement