Advertisement
Guest User

Untitled

a guest
Feb 13th, 2011
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.23 KB | None | 0 0
  1. # film restoring script by videoFred.
  2. # denoising, resizing, stabilising, sharpening, auto-levels and auto-white balance.
  3.  
  4.  
  5.  
  6.  
  7. # film restoration script by videoFred.
  8. # denoising, resizing, stabilising, sharpening, auto-levels and auto-white balance.
  9. #
  10. # Modified by John Meyer, June 8, 2010
  11. #
  12. # Changes made by Meyer:
  13. # 1. Replaced the MVDegrainMulti function, which was part of the original MVTools
  14. # with the equivalent function using the newer and faster MVTools2.
  15. # 2. Added RemoveDirt function prior to Degrain in order to eliminate large dust spots. This
  16. # substantially improves the dirt removal capabilities of this script. The user will need to
  17. # download this plugin at:
  18. # http://www.removedirt.de.tf/
  19. # 3. Eliminated a great deal of sharpening. The original script did sharpening in at least four places.
  20. # With grainy film stock, this sometimes created objectionable grain. Also, the limitedSharpenFaster
  21. # function, while excellent for video, is uncessarily slow, and isn't really needed for this low
  22. # resolution source. Too much sharpening can make the film look too much like video.
  23. # 4. Added multithreading. I was able to roughly triple the script performance. It may be possible to
  24. # substantially increase this, perhaps as much as 12x instead of 3x. However, the autolevels function
  25. # would have to be replaced.
  26. # 5. Fixed several small errors I found. The result3 option didn't have the manual color correction code, so I
  27. # added that back in. All the numbered "stab" variables (stab1, stab2, etc.) aren't needed except
  28. # for providing a test function for stabilization. I eliminated all of this to streamline the script.
  29. # 6. Deflicker seemed redundant, given all the averaging that takes place with MDegrain, and also the averaging
  30. # that is done when the autolevels outputs are selected, so I took it out.
  31. # 7. I reduced the number of frames used for averaging autolevels from the default (which is 5) to 2.
  32. # I probably should add a variable in the header so the user can change this. Something else to do
  33. # in the future ...
  34. # 8. I added yet another set of crop parameters. I did this because both my capture and my output are
  35. # done using NTSC DV AVI which is 720x480. However, 8mm film is almost exactly square, so the captured
  36. # 720x480 video has black bars on the side. These need to be cropped off prior to doing motion
  37. # stabilization, but then added back prior to the final output, which must still be 720x480 with the
  38. # black bars on the side.
  39. # 9. I reduced the default depan settings to 20, which is what was recommended in the original script. I
  40. # also reduced the post-depan cropping. I did this so I could keep as much of the original frame
  41. # as possible.
  42. # 10. I removed the second denoising and sharpening function. It just seemed to be too much, and made the
  43. # result too artificial
  44. # 11. I removed the MVFLowFPS interpolation. I did this during one of dozens and dozens of attempts
  45. # to improve the speed of the script. I should probably add this back, but if I do so, I also
  46. # need to make it work correctly for interlaced output. If the goal is to show this on an NTSC or PAL
  47. # television set, then it is not correct to convert from the film fps to 25 fps progressive (PAL) or
  48. # 29.97 fps progressive (NTSC). Instead, this should be done as follows (example given is NTSC):
  49. #
  50. # MFlowFPS(source,super,backward_vec, forward_vec, num=60000, den=1001,ml=200)
  51. # SeparateFields()
  52. # SelectEvery(4, 0, 3)
  53. # Weave()
  54. #
  55. # This yields interlaced 29.97, which has twice the temporal resolution as 29.97 progressive, and will
  56. # therefore look correct on a TV set. I have done a lot of this as part of my Kinescope to video
  57. # conversion scripts. For those scripts, the goal is to make the filmed version of a TV show look
  58. # like it was actually videotaped. However, for something that originated on film, this "does violence"
  59. # to the original feel of the media. It is true that it can make horizontal pans less "juddery," but
  60. # it won't feel like film anymore. Also, this technique does break down, espcially with fast motion
  61. # in the foreground.
  62. # 12. Removed the unecessary "coloryuv(off_U=blue,off_V=red)" statement from the denoising section.
  63. # 13. Added killaudio() statement to prevent lockups when using SetMTMode().
  64. #====================================================================================
  65.  
  66. #Change the following line to point to your video file
  67.  
  68.  
  69. film="C:\Documents and Settings\Goran\Desktop\svadbadeinterlace.avi" # source clip, you must specify the full path here
  70.  
  71.  
  72. #====================================================================================
  73.  
  74.  
  75. #GENERAL PARAMETERS
  76. #----------------------------------------------------------------------------------------------------------------------------
  77. result="results3" #specify the wanted output here
  78. trim_begin=0 play_speed=50 numerator= 25 denumerator= 1 #trim frames and play speed (PAL: 16.6666 or 18.75)
  79.  
  80. #COLOR AND LEVELS PARAMATERS
  81. #----------------------------------------------------------------------------------------------------------------------------
  82. saturation=1.2 #zasitenost
  83. gamma=1.2 #for all outputs
  84. blue= -0 red=-0 #manual color adjustment, when returning result3 & result4. Values can be positive or negative
  85. black_level=0 white_level=255 output_black=0 output_white=255 #manual levels, when returning result2 & result4
  86.  
  87.  
  88. #SIZE, CROP AND BORDERS PARAMETERS
  89. #----------------------------------------------------------------------------------------------------------------------------
  90. CLeft=16 CTop=16 CRight=16 CBottom=16 #crop values after Depan and before final resizing
  91. W=1024 H=768 #final size after cropping
  92. bord_left=0 bord_top=0 bord_right=0 bord_bot=0 #720p= borders 150
  93. in_bord_left=0 in_bord_top=0 in_bord_right=0 in_bord_bot=0 #Borders around input that must be removed
  94.  
  95.  
  96. #STABILISING PARAMETERS
  97. #----------------------------------------------------------------------------------------------------------------------------
  98. maxstabH=20 #maximum values for the stabiliser (in pixels) 20 is a good start value
  99. maxstabV=20
  100. est_left=40 est_top=40 est_right=40 est_bottom=40 est_cont=1.6 #crop and contast values for special Estimate clip
  101.  
  102.  
  103. #DENOISING PARAMETERS
  104. #----------------------------------------------------------------------------------------------------------------------------
  105. denoising_strength=800 #denoising level of first denoiser: MVDegrain()
  106. block_size= 16 #block size of MVDegrain
  107. block_size_v= 16
  108. block_over= 8 #block overlapping of MVDegrainMulti()
  109. dirt_strength=23 #sets amount of dirt removal (big spots)
  110.  
  111.  
  112. #FOUR STEP SHARPENING PARAMETERS
  113. #--------------------------------------------------------------------------------------------------------------------------------
  114. PRE_sharp_ness= 120 PRE_radi_us= 3 #presharpening (UnsharpMask) just after first denoising
  115.  
  116.  
  117. #AUTO LEVELS PARAMETER
  118. #--------------------------------------------------------------------------------------------------------------------------------
  119. X=4 #X is a special parameter for reducing the autolevels effect on the whites
  120. X2=2 #X2 is a special parameter for reducing the autolevels effect on the blacks
  121.  
  122.  
  123. # END VARIABLES, BEGIN SCRIPT
  124. #=================================================================================================================================
  125.  
  126.  
  127. SetMemoryMax(1024) #set this to 1/3 of the available memory
  128.  
  129. Loadplugin("Depan.dll")
  130. LoadPlugin("DepanEstimate.dll")
  131. Loadplugin("removegrain.dll")
  132. LoadPlugin("MVTools2.dll")
  133. Loadplugin("mt_masktools.dll")
  134. Loadplugin("warpsharp.dll")
  135. LoadPlugIn("LimitedSupport_09Jan06B.dll")
  136. LoadPlugin("autolevels.dll")
  137. loadplugin("C:\Program Files\AviSynth 2.5\plugins\RemoveDirtSSE2.dll")
  138.  
  139. #Remove all setmtmode statements (there are three in this script) if not using multi-threaded (MT) AVISynth
  140. setmtmode(5,4)
  141. source1= Avisource(film).killaudio().assumefps(play_speed).trim(trim_begin,0).converttoYV12()
  142. cropped_source=source1.crop(in_bord_left,in_bord_top,-in_bord_right,-in_bord_bot) #remove any black borders on input video
  143. setmtmode(2,0)
  144.  
  145.  
  146. #STABILIZING
  147. #....................................................................................................................................................................
  148. stab_reference= cropped_source.crop(est_left,est_top,-est_right,-est_bottom).tweak(cont=est_cont).MT_binarize(threshold=80).greyscale().invert()
  149. mdata=DePanEstimate(stab_reference,trust=1.0,dxmax=maxstabH,dymax=maxstabV)
  150. stab=DePanStabilize(cropped_source,data=mdata,cutoff=0.5,dxmax=maxstabH,dymax=maxstabV,method=1,mirror=15)
  151.  
  152.  
  153. #DENOISING
  154. #...................................................................................................................................................................
  155. input_to_removedirt=stab.crop(CLeft,CTop,-CRight,-CBottom)
  156. stabcrop=RemoveDirtMC(input_to_removedirt,dirt_strength,false)
  157. super = stabcrop.MSuper(pel=2)
  158. bvec1 = MAnalyse(super, isb = true, delta = 1, blksize=block_size, overlap=block_over)
  159. fvec1 = MAnalyse(super, isb = false, delta = 1, blksize=block_size, overlap=block_over)
  160. bvec2 = MAnalyse(super, isb = true, delta = 2, blksize=block_size, overlap=block_over)
  161. fvec2 = MAnalyse(super, isb = false, delta = 2, blksize=block_size, overlap=block_over)
  162. bvec3 = MAnalyse(super, isb = true, delta = 3, blksize=block_size, overlap=block_over)
  163. fvec3 = MAnalyse(super, isb = false, delta = 3, blksize=block_size, overlap=block_over)
  164.  
  165.  
  166. #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.
  167. 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)
  168. 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)
  169.  
  170.  
  171. #SHARPENING
  172. #...................................................................................................................................................................
  173. sharp1=denoised.sharpen(0.2)
  174. 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)
  175.  
  176.  
  177. #RESULT1: AUTOLEVELS,AUTOWHITE
  178. #......................................................................................................................................................................
  179. setmtmode(5,4)
  180.  
  181. 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)
  182.  
  183. #RESULT2: MANUAL LEVELS, AUTOWHITE
  184. #......................................................................................................................................................................
  185. 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)
  186.  
  187. #RESULT3: AUTOLEVELS, MANUAL COLOR CORRECTIONS
  188. #.....................................................................................................................................................................
  189. 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)
  190.  
  191. #RESULT4: MANUAL LEVELS, MANUAL COLOR CORRECTIONS
  192. #.....................................................................................................................................................................
  193. 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)
  194.  
  195.  
  196. #PARAMETERS FOR THE COMPARISONS
  197. #.....................................................................................................................................................................
  198. W2= W+bord_left+bord_right
  199. H2= H+bord_top+bord_bot
  200. source4=Lanczos4Resize(source1,W2,H2)
  201.  
  202.  
  203. #COMPARISONS: ORIGINAL VS RESULTS
  204. #......................................................................................................................................................................
  205. resultS1= stackhorizontal(subtitle(source4,"original",size=28,align=2),subtitle(result1,"autolevels, autowhite",size=28,align=2))
  206. resultS2= stackhorizontal(subtitle(source4,"original",size=28,align=2),subtitle(result2,"autowhite, manual levels correction",size=28,align=2))
  207. resultS3= stackhorizontal(subtitle(source4,"original",size=28,align=2),subtitle(result3,"autolevels, manual color correction",size=28,align=2))
  208. resultS4= stackhorizontal(subtitle(source4,"original",size=28,align=2),subtitle(result4,"manual colors and levels correction",size=28,align=2))
  209.  
  210.  
  211. Eval(result)
  212.  
  213. # END SCRIPT, BEGIN FUNCTIONS
  214. #=================================================================================================================================
  215.  
  216.  
  217. #REMOVE DIRT FUNCTION
  218. #......................................................................................................................................................................
  219. function RemoveDirt(clip input, int limit, bool _grey)
  220. {
  221. clensed=input.Clense(grey=_grey, cache=4)
  222. alt=input.RemoveGrain(2)
  223. return RestoreMotionBlocks(clensed,input,alternative=alt,pthreshold=6,cthreshold=8, gmthreshold=40,dist=3, dmode=2,debug=false,noise=limit,noisy=4, grey=_grey)
  224.  
  225. # Alternative settings
  226. # 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)
  227. # 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)
  228. }
  229.  
  230. function RemoveDirtMC(clip,int limit, bool "_grey")
  231. {
  232. _grey=default(_grey, false)
  233. limit = default(limit,6)
  234. i=MSuper(clip,pel=2)
  235. bvec = MAnalyse(i,isb=false, blksize=8, delta=1, truemotion=true)
  236. fvec = MAnalyse(i,isb=true, blksize=8, delta=1, truemotion=true)
  237. backw = MFlow(clip,i,bvec)
  238. forw = MFlow(clip,i,fvec)
  239. clp=interleave(backw,clip,forw)
  240. clp=clp.RemoveDirt(limit,_grey)
  241. clp=clp.SelectEvery(3,1)
  242. return clp
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement