wizoomer95

AVISynth Script for Improving Quality of TATMR Deleted Storylines Blu-ray rip

Oct 24th, 2020
2,161
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #################################
  2. # Macro_Deinterlace.avs, Correct incorrectly resized interlaced video.
  3. #  YUY2.
  4. #
  5. # (c) John H. Meyer - July 8, 2014
  6. #
  7. # Required:-
  8. #    MVTools      (c) Manao, Fizick, Tsp, TSchniede, SEt
  9. #    GScript      (c) Gavino.
  10. #    Grunt        (c) Gavino.
  11. #    s_ExLogo.avs (c) StainlessS.
  12. #    RT_Stats     (c) StainlessS.
  13. #################################
  14. #
  15. Function Macro_Deinterlace(clip clp,int RowStart,int RowStep,Bool "TopRowFirst",Float "dFact",Float "dMin",Int "Amount",Int "Amount2",Bool "Show") {
  16. # Create two half-height clips from groups of 'ScanStep' rows of pixels
  17. # Shift one of these clips in time (using motion estimation) halfway between previous frame and current frame
  18. # Then, reassemble the two half height clips.
  19. # Finally fix thin lines using S_ExLogo with Amount2 arg and then 2nd pass S_Exlogo with Amount arg.
  20. # First clip uses 'RowStart' rows from top of original clip, plus rows of height='RowStep' in between.
  21. # Args:
  22. #   RowStart, Offset to bottom row in scanlines
  23. #   RowStep, size of rows in scanlines
  24. #   TopRowFirst, default False = was originally bottom field first.
  25. #   dFact, default 4.0. (Greater than 1.0).  For scene change (1st frame) condition @ n to succeed: dFact*Diff(n-2 -> n-1)+dMin  < Diff(n-1 -> n)
  26. #   dMin, default 3.0. (Greater than 0.1).   dMin avoids silly numbers (very small) at static scenes detecting false scene changes.
  27. #   Amount, Default 256, 0 -> 256. 0=Switched Off. DeLogo thin lines.
  28. #   Amount2, Default 0,  0 -> 256. 0=Switched Off. Mild greater diameter delogo BEFORE Primary Amount Delogo.
  29. #   Show, Default False. Show metrics.
  30. #
  31. #   In Metrics (Show=True) shows 'Hint=x.x', is a hint for choosing dFact, valid on frame after scene change.
  32. ####
  33.     clp
  34.     myName="Macro_Deinterlace: "
  35.     TopRowFirst = Default(TopRowFirst,False)            # Was bottom field first (before bad resize).
  36.     dFact = Float(Default(dFact,4.0))                   # Scene Change, dFact*Diff(n-2 -> n-1)+dMin  < Diff(n-1 -> n)
  37.     dMin = Float(Default(dMin,3.0))                     # Scene Change, avoid silly numbers on static scenes
  38.     Amount = Default(Amount,256)                        # Strength of S_Exlogo mixing, greater more opaque delogo (256 max), 0 = none.
  39.     Amount2= Default(Amount2,0)                         # Stringth of first mild initial S_ExLogo, 0 = none
  40.     Show=Default(Show,False)                            # Metrics
  41.     Assert(RowStep < Height/2 && RowStep >= 2,myName+"Illegal RowStep")
  42.     Assert(RowStart >=0 && RowStart <= RowStep,myName+"Illegal RowStart")
  43.     Assert(dFact > 1.0,myName+"Illegal dFact")
  44.     Assert(dMin > 0.1,myName+"Illegal dMin")
  45.     Assert(Amount  >= 0 && Amount  <= 256,myName+"Illegal Amount")
  46.     Assert(Amount2 >= 0 && Amount2 <= 256,myName+"Illegal Amount2")
  47.     RowStart= (RowStart % RowStep)
  48.     FuncS="""
  49.        Function Fn@@@(clip clp,clip Fix,Float dFact,Float dMin,Amount,Amount2,Bool Show) {
  50.            clp
  51.            n = current_frame                                                           # Local variable, quicker
  52.            NotNext = (PrevFrame@@@ + 1 != n)                                           # Init OR, user jumped about ?
  53.            # We detect relative to previous frame (n-1), ie when we are 1st frame AFTER Scene Change
  54.            prevdif = (NotNext) ? RT_YDifference(Last,n=n - 1, delta=-1) : PrevDif@@@   # Diff between current_frame-2 and current_frame - 1
  55.            currdif = RT_YDifference(Last,n=n - 1, delta= 1)                            # Diff between current_frame-1 and current_frame
  56.            SC = (dFact*prevdif + dMin) < currdif                                       # Scene Change if dFact*prevdiff+dMin  < currdif
  57.            dHint = (currdif - dMin) / max(prevdif,0.0001)
  58.            dHint = (dHint < 0.0) ? 0.0 : dHint
  59.            Last = (SC) ? Last : Fix                                                    # Conditional Select frame
  60.            (Show) ? RT_Subtitle("%d ]  %-5.2f : %-5.2f : \a%c%s\a-: Hint=%-5.2f\ndFact =%-5.2f: dMin=%-5.2f\nAmount=%4d : Amount2=%-3d",
  61.                    \ n,prevdif,currdif, (SC)?33:45,(SC) ? "Yes" : "No ",dHint,dFact,dMin,Amount,Amount2) : NOP
  62.            Global PrevDif@@@   = currdif
  63.            Global PrevFrame@@@ = n
  64.            Last
  65.        }
  66.        #######################################
  67.        # Unique Global Variables Initialization
  68.        #######################################
  69.        Global PrevFrame@@@ = -42                                                   # Force Init
  70.        Global PrevDif@@@   = 0.0
  71.        #######################################
  72.        # Init
  73.        #######################################
  74.        BotRow = clp.Crop(0,(RowStart>0)?RowStart:RowStep,-0,RowStep)
  75.        for(y=((RowStart>0)?RowStart:RowStep)+RowStep*2,clp.Height-RowStart,RowStep*2) {
  76.            botRow = BotRow.StackVertical(clp.Crop(0,y,-0,Min(clp.Height-y,RowStep)))
  77.        }
  78.        if(TopRowFirst) { Offset_BotRow = BotRow}                                                       # midway between current and next frames
  79.        Else            { Offset_BotRow = BotRow.DeleteFrame(BotRow.FrameCount-1).DuplicateFrame(0) }   # midway between Previous and current frames
  80.        super_BotRow=MSuper(Offset_BotRow,pel=2)
  81.        vfe_BotRow=manalyse(super_BotRow,truemotion=true,isb=false,delta=1,blksize=8,overlap=4,search=6)
  82.        vbe_BotRow=manalyse(super_BotRow,truemotion=true,isb=true,delta=1,blksize=8,overlap=4,search=6)
  83.        est_BotRow=mflowinter(Offset_BotRow,super_BotRow,vbe_BotRow,vfe_BotRow,time=50,blend=false,mL=100)
  84.        newClip = clp
  85.        for(y=0,est_BotRow.Height-RowStep,RowStep) {    # Re-assemble clip with motion compensated bottom row
  86.            newClip=newClip.Overlay(est_BotRow.Crop(0,y,-0,Min(est_BotRow.Height-y,RowStep)),x=0,y=((RowStart>0)?RowStart:RowStep)+y*2)
  87.        }
  88.        Fix=newClip
  89.        if(Amount2 > 0) {   # Preparatory Delogo, used before PRIMARY Delogo (More Mild Amount2 setting OR none)
  90.            for(y=RowStart,Fix.height-1,RowStep) {
  91.                Fix = Fix.S_Exlogo(0,y-2,-0,4,BlurMode=5,amount=Amount2)
  92.            }
  93.        }
  94.        if(Amount > 0) {    # PRIMARY Delogo of thin lines (best use strong ie max Amount setting)
  95.            for(y=RowStart,Fix.height-1,RowStep) {
  96.                Fix = Fix.S_Exlogo(0,y-1,-0,2,BlurMode=5,amount=Amount)
  97.            }
  98.        }
  99.        #######################################
  100.        # Unique Runtime Call, GScriptClip must be a one-liner:
  101.        #######################################
  102.        ARGS = "Fix,dFact,dMin,Amount,Amount2,Show"
  103.        Last.GScriptClip("Fn@@@(last, "+ARGS+")", local=true, args=ARGS)
  104.    """
  105.     #######################################
  106.     # Unique Identifier Definition
  107.     #######################################
  108.     RT_IncrGlobal("MacroD_InstanceNumber")
  109.     InstS = RT_StrReplace(FuncS,"@@@","_MacroD_"+String(MacroD_InstanceNumber))
  110. #    RT_WriteFile("DEBUG_"+String(MacroD_InstanceNumber)+".TXT","%s",InstS)
  111.     Return GScript(InstS)
  112. }
  113.  
  114.  
  115. #Import("C:\Program Files\AviSynth 2.5\plugins\s_exlogo.avs")
  116. #source = AVISource("E:\fs.avi").KillAudio()
  117.  
  118. #This is for the deleted storylines featurette on the 20th anniversary Thomas and the Magic Railroad Blu-ray.  The encode on the disc was horrible for Shout! Factory's usual standards.
  119. #This AVISynth script is the best way I've come to fixing it.  There's still a few blended frames at scene changes, but it's light-years ahead compared to the original video.
  120. #This script is designed to work with the featurette ripped directly without re-encoding using MakeMKV.  I cannot gaurantee another rip will work with this script.
  121. #The way I load this into AVISynth is I open the original MKV ripped from the Blu-ray in VirtualDub2.
  122. #From there, I start the frameserver and load the frameserved video into AVISynth.  There may be another more elegant way of getting the file in directly, but I have NOT tested this.  Your mileage may vary.
  123. #This also does IVTC on the clip, so you'll be getting a 1080p video at 23.976 frames per second.  You won't be getting true 1080p quality, though.  After all, this was initially sourced from an analog video source (likely a VHS Tape) from Britt Allcroft's personal collection.
  124. #Special thanks to johnmeyer on the Doom9 Forums for pointing me in the right direction for fixing this badly encoded video!  This full script was put together by Rico Robbins, aka wizoomer95.
  125.  
  126. Import("F:\avs\s_ExLogo.AVS")
  127. LoadPlugin("F:\avs\GScript_26_32.dll")    #Even if you are using AVISynth+, you MUST load the plugin and not rely on the GScript integration.  Otherwise, the function will break and this script will result in an error.
  128. AVIFileSource("C:\Users\Rico Robbins\Videos\video.vdr.avi").KillAudio()    #Yes, you want to kill the audio.  This is just for processing.  Don't worry, we'll dub it back in later in this very script!
  129. ConvertToYUY2()
  130. Crop(60,54,-60,-54)    #Cropping out the black borders on the original video.  Next, we do perform inverse telecine.
  131. Telecide()
  132. Decimate()
  133.  
  134. #Next comes the magic in this script only akin to gold dust itself...
  135.  
  136. ROWSTART= 2
  137. RowStep = 3
  138. TOP_ROW_FIRST=False
  139. AMOUNT = 256
  140. AMOUNT2= 200
  141. SHOW=True
  142. DFACT1=4.0
  143. DMIN1=3.0
  144.  
  145. L=Macro_Deinterlace(ROWSTART,ROWSTEP,TopRowFirst=TOP_ROW_FIRST,dFact=DFACT1,dMin=DMIN1,Amount=AMOUNT,Amount2=AMOUNT2,Show=FALSE)    #Originally Show was set to SHOW with StackHorizontal not commented out below for debugging details
  146. R=Macro_Deinterlace(ROWSTART,ROWSTEP,TopRowFirst=TOP_ROW_FIRST,dFact=4.0,dMin=3.0,Amount=256,Amount2=0,Show=SHOW)        #StackHorizontal(L,R)
  147. FixedVideo = AddBorders(L,60,54,60,54)
  148. OrigAudio = AVIFileSource("C:\Users\Rico Robbins\Videos\video.vdr.avi").KillVideo()
  149. AudioDub(FixedVideo,OrigAudio)
  150. #This will add the borders we cropped up earlier before processing, to restore it to 1920x1080 in resolution.  To leave them cropped, comment out the above line and uncomment the line below.
  151. #AudioDub(L,OrigAudio)
  152.  
RAW Paste Data