Advertisement
Guest User

MDG

a guest
Dec 17th, 2021
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 15.73 KB | None | 0 0
  1. function MDG (clip clp, int "blksize", int "overlap", int "thsad", int "thsadc", float "limit", float "limitc", int "debug", int "search", bool "chroma", int "thscd1", \
  2. int "thscd2", int "scalecsad", bool "recalc", bool "multirecalc", bool "multirecalc2", bool "cs", int "prefilter", float "blurlimit", int "bias", \
  3. bool "truemotion", int "dct", bool "hdr", float "sstrength", int "radius", int "thsad2", int "pel")
  4. {
  5. original = clp
  6. hd = clp.width() > 720 ? true : false
  7. hdr = default(hdr, clp.width() > 1920 ? true : false)
  8. blksize = default(blksize, clp.width() > 1920 ? 32 : 16)
  9. blksize2 = blksize/2
  10. blksize3 = blksize/4
  11. blksize4 = blksize/8
  12. overlap = default(overlap, blksize/2)
  13. overlap2 = overlap/2
  14. overlap3 = overlap/4
  15. overlap4 = overlap/8
  16. limit = default(limit, 0.1)
  17. limitc = default(limitc, limit + 0.25)
  18. debug = default(debug, 0)
  19. radius = default(radius, 1)
  20. pel = default(pel, 4)
  21. search = default(search, 4)
  22. searchparam = 2
  23. searchparam2 = 2
  24. searchparam3 = 2
  25. searchparam4 = 2
  26. pelsearch = 2
  27. scalecsad = default(scalecsad, clp.is420 ? 0 : clp.is422 ? 1 : clp.is444 ? 2 : 0)
  28. recalc = default(recalc, true)
  29. multirecalc = default(multirecalc, blksize3 >= 4 ? true : false)
  30. multirecalc2 = default(multirecalc2, blksize4 >= 4 ? clp.width() > 1920 || !multirecalc ? false : multirecalc ? true : false : false)
  31. thscd1 = default(thscd1, 400)
  32. thscd2 = default(thscd2, 85)
  33. cs = default(cs, false)
  34. thsad = default(thsad, 100)
  35. thsadc = default(thsadc, thsad)
  36. thsad = thsad < 100 ? 100 : thsad
  37. thsadc = thsadc < 100 ? 100 : thsadc
  38. thsad2 = default(thsad2, thsad)
  39. sstrength = default(sstrength, 2.0)
  40. blurlimit = default(blurlimit, 0.15)
  41. bias = default(bias, 20)
  42. chroma = default(chroma, true)
  43. truemotion = default(truemotion, false)
  44. lda = truemotion ? (1000 * blksize * blksize) / 64 : 4
  45. lda2 = truemotion ? (1000 * blksize2 * blksize2) / 64 : 4
  46. lda3 = truemotion ? (1000 * blksize3 * blksize3) / 64 : 4
  47. lda4 = truemotion ? (1000 * blksize4 * blksize4) / 64 : 4
  48. lsad = truemotion ? 8000 : 6000
  49. pnew = truemotion ? 150 : 50
  50. orgbits = clp.bitspercomponent()
  51. prefiltbits = orgbits == 16 ? 10 : 8
  52. dct = default(dct, prefiltbits == 10 ? 0 : 5)
  53. prefilter = default(prefilter, 1)
  54.  
  55. c2 = clp.convertbits(bits=16)
  56. c2blur = blurlimit > 0 ? c2.spresso(bias=bias, biasc=bias*2, rgmodec=chroma?4:0, limit=blurlimit, limitc=blurlimit*2) : c2
  57. prefilt = prefiltbits < orgbits ? clp.convertbits(bits=prefiltbits) : clp
  58. clp = c2
  59.  
  60. Lthres  = ex_bs(75, 8, prefiltbits, tv_in=true, tv_out=true, fulls=false)
  61.  
  62. prefilt = prefilter == 1 ? prefilt.ex_minblur(r=1, uv=chroma?3:1) : prefilter == 2 ? prefilt.ex_minblur(r=2, uv=chroma?3:1) : ex_merge(dfttest(prefilt,sstring="0.0:4.0 0.2:9.0 1.0:15.0",tbsize=1,U=chroma,V=chroma,dither=1,threads=1),prefilt,prefilt.ex_lut(Format("x ymin < range_max x {Lthres} > 0 range_max x ymin - range_max {Lthres} ymin - / * - ? ?"),UV=1,fulls=false), luma=chroma, UV=chroma?3:1, fulls=false)
  63. prefilt = prefilt.ex_luma_rebuild(s0=3.0, uv=chroma?3:1)
  64.  
  65. sharp = sstrength > 0 ? clp.detailsharpen(sstr=sstrength) : clp
  66.  
  67. superanalyse = prefilt.msuper(pel=pel, sharp=1, rfilter=2, chroma=chroma)
  68. supermdg = sharp.msuper(pel=pel, levels=1, sharp=1, rfilter=2, chroma=chroma)
  69.  
  70. fv1 = manalyse(superanalyse, isb=false, delta=1, blksize=blksize, overlap=overlap, search=search, searchparam=searchparam, pelsearch=pelsearch, truemotion=truemotion, lambda=lda, chroma=chroma, lsad=lsad, pnew=pnew, dct=dct, badsad=1500, badrange=search==3?-4:4, scalecsad=scalecsad, trymany=false, global=true)
  71. bv1 = manalyse(superanalyse, isb=true, delta=1, blksize=blksize, overlap=overlap, search=search, searchparam=searchparam, pelsearch=pelsearch, truemotion=truemotion, lambda=lda, chroma=chroma, lsad=lsad, pnew=pnew, dct=dct, badsad=1500, badrange=search==3?-4:4, scalecsad=scalecsad, trymany=false, global=true)
  72. fv2 = radius > 1 ? manalyse(superanalyse, isb=false, delta=2, blksize=blksize, overlap=overlap, search=search, searchparam=searchparam, pelsearch=pelsearch, truemotion=truemotion, lambda=lda, chroma=chroma, lsad=lsad, pnew=pnew, dct=dct, badsad=1500, badrange=search==3?-4:4, scalecsad=scalecsad, trymany=false, global=true) : NOP()
  73. bv2 = radius > 1 ? manalyse(superanalyse, isb=true, delta=2, blksize=blksize, overlap=overlap, search=search, searchparam=searchparam, pelsearch=pelsearch, truemotion=truemotion, lambda=lda, chroma=chroma, lsad=lsad, pnew=pnew, dct=dct, badsad=1500, badrange=search==3?-4:4, scalecsad=scalecsad, trymany=false, global=true) : NOP()
  74. fv3 = radius == 3 ? manalyse(superanalyse, isb=false, delta=3, blksize=blksize, overlap=overlap, search=search, searchparam=searchparam, pelsearch=pelsearch, truemotion=truemotion, lambda=lda, chroma=chroma, lsad=lsad, pnew=pnew, dct=dct, badsad=1500, badrange=search==3?-4:4, scalecsad=scalecsad, trymany=false, global=true) : NOP()
  75. bv3 = radius == 3 ? manalyse(superanalyse, isb=true, delta=3, blksize=blksize, overlap=overlap, search=search, searchparam=searchparam, pelsearch=pelsearch, truemotion=truemotion, lambda=lda, chroma=chroma, lsad=lsad, pnew=pnew, dct=dct, badsad=1500, badrange=search==3?-4:4, scalecsad=scalecsad, trymany=false, global=true) : NOP()
  76.  
  77. fv1 = recalc ? mrecalculate(superanalyse, fv1, thsad=thsad2, blksize=blksize2, overlap=overlap2, search=search, searchparam=searchparam2, truemotion=truemotion, lambda=lda2, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : fv1
  78. bv1 = recalc ? mrecalculate(superanalyse, bv1, thsad=thsad2, blksize=blksize2, overlap=overlap2, search=search, searchparam=searchparam2, truemotion=truemotion, lambda=lda2, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : bv1
  79. fv2 = recalc && isclip(fv2) ? mrecalculate(superanalyse, fv2, thsad=thsad2, blksize=blksize2, overlap=overlap2, search=search, searchparam=searchparam2, truemotion=truemotion, lambda=lda2, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(fv2) ? fv2 : NOP()
  80. bv2 = recalc && isclip(bv2) ? mrecalculate(superanalyse, bv2, thsad=thsad2, blksize=blksize2, overlap=overlap2, search=search, searchparam=searchparam2, truemotion=truemotion, lambda=lda2, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(bv2) ? bv2 : NOP()
  81. fv3 = recalc && isclip(fv3) ? mrecalculate(superanalyse, fv3, thsad=thsad2, blksize=blksize2, overlap=overlap2, search=search, searchparam=searchparam2, truemotion=truemotion, lambda=lda2, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(fv3) ? fv3 : NOP()
  82. bv3 = recalc && isclip(bv3) ? mrecalculate(superanalyse, bv3, thsad=thsad2, blksize=blksize2, overlap=overlap2, search=search, searchparam=searchparam2, truemotion=truemotion, lambda=lda2, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(bv3) ? bv3 : NOP()
  83.  
  84. fv1 = multirecalc ? mrecalculate(superanalyse, fv1, thsad=thsad2, blksize=blksize3, overlap=overlap3, search=search, searchparam=searchparam3, truemotion=truemotion, lambda=lda3, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : fv1
  85. bv1 = multirecalc ? mrecalculate(superanalyse, bv1, thsad=thsad2, blksize=blksize3, overlap=overlap3, search=search, searchparam=searchparam3, truemotion=truemotion, lambda=lda3, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : bv1
  86. fv2 = multirecalc && isclip(fv2) ? mrecalculate(superanalyse, fv2, thsad=thsad2, blksize=blksize3, overlap=overlap3, search=search, searchparam=searchparam3, truemotion=truemotion, lambda=lda3, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(fv2) ? fv2 : NOP()
  87. bv2 = multirecalc && isclip(bv2) ? mrecalculate(superanalyse, bv2, thsad=thsad2, blksize=blksize3, overlap=overlap3, search=search, searchparam=searchparam3, truemotion=truemotion, lambda=lda3, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(bv2) ? bv2 : NOP()
  88. fv3 = multirecalc && isclip(fv3) ? mrecalculate(superanalyse, fv3, thsad=thsad2, blksize=blksize3, overlap=overlap3, search=search, searchparam=searchparam3, truemotion=truemotion, lambda=lda3, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(fv3) ? fv3 : NOP()
  89. bv3 = multirecalc && isclip(bv3) ? mrecalculate(superanalyse, bv3, thsad=thsad2, blksize=blksize3, overlap=overlap3, search=search, searchparam=searchparam3, truemotion=truemotion, lambda=lda3, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(bv3) ? bv3 : NOP()
  90.  
  91. fv1 = multirecalc2 ? mrecalculate(superanalyse, fv1, thsad=thsad2, blksize=blksize4, overlap=overlap4, search=search, searchparam=searchparam4, truemotion=truemotion, lambda=lda4, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : fv1
  92. bv1 = multirecalc2 ? mrecalculate(superanalyse, bv1, thsad=thsad2, blksize=blksize4, overlap=overlap4, search=search, searchparam=searchparam4, truemotion=truemotion, lambda=lda4, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : bv1
  93. fv2 = multirecalc2 && isclip(fv2) ? mrecalculate(superanalyse, fv2, thsad=thsad2, blksize=blksize4, overlap=overlap4, search=search, searchparam=searchparam4, truemotion=truemotion, lambda=lda4, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(fv2) ? fv2 : NOP()
  94. bv2 = multirecalc2 && isclip(bv2) ? mrecalculate(superanalyse, bv2, thsad=thsad2, blksize=blksize4, overlap=overlap4, search=search, searchparam=searchparam4, truemotion=truemotion, lambda=lda4, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(bv2) ? bv2 : NOP()
  95. fv3 = multirecalc2 && isclip(fv3) ? mrecalculate(superanalyse, fv3, thsad=thsad2, blksize=blksize4, overlap=overlap4, search=search, searchparam=searchparam4, truemotion=truemotion, lambda=lda4, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(fv3) ? fv3 : NOP()
  96. bv3 = multirecalc2 && isclip(bv3) ? mrecalculate(superanalyse, bv3, thsad=thsad2, blksize=blksize4, overlap=overlap4, search=search, searchparam=searchparam4, truemotion=truemotion, lambda=lda4, chroma=chroma, pnew=pnew, dct=dct, scalecsad=scalecsad) : isclip(bv3) ? bv3 : NOP()
  97.  
  98. finalclip = radius == 1 ? c2blur.mdegrain1(supermdg, bv1, fv1, thsad=thsad, thsadc=thsadc, plane=chroma?4:0, limit=limit, limitc=limitc, thscd1=thscd1, thscd2=thscd2) : radius == 2 ? c2blur.mdegrain2(supermdg, bv1, fv1, bv2, fv2, thsad=thsad, thsadc=thsadc, plane=chroma?4:0, limit=limit, limitc=limitc, thscd1=thscd1, thscd2=thscd2) : c2blur.mdegrain3(supermdg, bv1, fv1, bv2, fv2, bv3, fv3, thsad=thsad, thsadc=thsadc, plane=chroma?4:0, limit=limit, limitc=limitc, thscd1=thscd1, thscd2=thscd2)
  99. finalclip = cs ? cshd(finalclip, clp, fv1, bv1, supermdg, hd) : finalclip
  100.  
  101. prefilt = debug == 1 ? prefilt.convertbits(orgbits) : prefilt
  102. original = debug == 1 ? chroma ? original.ex_luma_rebuild(s0=3.0) : original.ex_luma_rebuild(s0=3.0, uv=1) : original
  103.  
  104. original = debug > 1 ? original.convertbits(bits=16) : original
  105.  
  106. output = \
  107.   debug == 1 ? interleave(original, prefilt) \
  108. : debug == 2 ? interleave(original, finalclip).subtitle("search: " + string(search),x=0,y=20).subtitle("thsad: " + string(thsad),x=0,y=40).subtitle("thsadc: " + string(thsadc),x=0,y=60).subtitle("limit: " + string(limit),x=0,y=80).subtitle("limitc: " + string(limitc),x=0,y=100).subtitle("blksize: " + string(blksize),x=0,y=120).subtitle("blksize2: " + string(blksize2),x=0,y=140).subtitle("blksize3: " + string(blksize3),x=0,y=160).subtitle("blksize4: " + string(blksize4),x=0,y=180).subtitle("multirecalc: " + string(multirecalc),x=0,y=200).subtitle("multirecalc2: " + string(multirecalc2),x=0,y=220).subtitle("dct: " + string(dct),x=0,y=240).subtitle("orgbits: " + string(orgbits),x=0,y=260).subtitle("prefiltbits: " + string(prefiltbits),x=0,y=280).subtitle("searchparam: " + string(searchparam),x=0,y=300).subtitle("searchparam2: " + string(searchparam2),x=0,y=320).subtitle("searchparam3: " + string(searchparam3),x=0,y=340).subtitle("searchparam4: " + string(searchparam4),x=0,y=360) \
  109. : debug == 3 ? interleave(original, subtract(clp, finalclip.mergechroma(clp)).levels(127*256, 1, 132*256, 0*256, 255*256)) \
  110. : debug == 4 ? interleave(original, subtract(clp, finalclip.mergeluma(clp)).levels(127*256, 1, 132*256, 0*256, 255*256)) \
  111. : debug == 5 ? interleave(original.mt_lut(expr="x 16 &u 17 x 15 &u - 1 x 15 &u + ? 14 *", u=-128, v=-128, scale_inputs="allf").Greyscale(), finalclip.mt_lut(expr="x 16 &u 17 x 15 &u - 1 x 15 &u + ? 14 *", u=-128, v=-128, scale_inputs="allf").Greyscale()) \
  112. : debug == 6 ? mshow(superanalyse, fv1, thscd1=thscd1, thscd2=thscd2, showsad=true).subtitle("search: " + string(search),x=0,y=60).subtitle("thsad: " + string(thsad),x=0,y=80).subtitle("thsadc: " + string(thsadc),x=0,y=100).subtitle("blksize: " + string(blksize),x=0,y=120).subtitle("blksize2: " + string(blksize2),x=0,y=140).subtitle("blksize3: " + string(blksize3),x=0,y=160).subtitle("blksize4: " + string(blksize4),x=0,y=180).subtitle("multirecalc: " + string(multirecalc),x=0,y=200).subtitle("multirecalc2: " + string(multirecalc2),x=0,y=220).subtitle("dct: " + string(dct),x=0,y=240).subtitle("orgbits: " + string(orgbits),x=0,y=260).subtitle("prefiltbits: " + string(prefiltbits),x=0,y=280).subtitle("scalecsad: " + string(scalecsad),x=0,y=300).subtitle("thscd1: " + string(thscd1),x=0,y=320).subtitle("thscd2: " + string(thscd2),x=0,y=340) \
  113. : debug == 7 ? interleave(original, sharp).subtitle("Effect of sharpening", x=0, y=20) \
  114. : debug == 8 ? interleave(original, c2blur).subtitle("Effect of spatial blur", x=0, y=20) \
  115. : finalclip
  116.  
  117. output = debug == 0 ? chroma ? output : output.greyscale() : output
  118.  
  119. return output
  120.  
  121. }
  122.  
  123. function CSHD(clip denoised, clip original, clip cf1, clip cb1, clip super, bool "HD", bool "planar", int "overshoot"){
  124. HD        = default(HD    ,false)
  125. planar    = default(planar,false)
  126. overshoot = default(overshoot,0)
  127. avs26 = VersionNumber() < 2.60 ? false : true
  128. yuy26 = avs26 && Isyuy2(denoised) && planar
  129.  
  130. HD ? eval("""
  131. cb1=original.MCompensate(Super, cb1, planar=planar)
  132. cf1=original.MCompensate(Super, cf1, planar=planar)
  133. pmax = yuy26 ? original.Planar2Interleaved().ConvertToYV16().mt_logic(cb1.Planar2Interleaved().ConvertToYV16(), "max").mt_logic(cf1.Planar2Interleaved().ConvertToYV16(), "max") : original.mt_logic(cb1, "max").mt_logic(cf1, "max")
  134. pmin = yuy26 ? original.Planar2Interleaved().ConvertToYV16().mt_logic(cb1.Planar2Interleaved().ConvertToYV16(), "min").mt_logic(cf1.Planar2Interleaved().ConvertToYV16(), "min") : original.mt_logic(cb1, "min").mt_logic(cf1, "min")""") : nop()
  135.  
  136. s    = denoised.MinBlur(HD?2:1,1,planar=planar)                                   # Damp down remaining spots of the denoised clip.
  137. s    = yuy26 ? s.Planar2Interleaved().ConvertToYV16() : s
  138. original = yuy26 ? original.Planar2Interleaved().ConvertToYV16() : original
  139. denoised = yuy26 ? denoised.Planar2Interleaved().ConvertToYV16() : denoised
  140. allD = mt_makediff(original,denoised)                                             # The difference achieved by the denoising.
  141. ssD  = mt_makediff(s,HD?s.removegrain(20,-1,planar=planar).\
  142.                           removegrain(20,-1,planar=planar):\
  143.                         s.removegrain(11,-1,planar=planar))                       # The difference of a simple kernel blur.
  144. allD = yuy26 ? allD.ConvertToYUY2().Interleaved2planar(true) : allD
  145. ssD  = yuy26 ? ssD.ConvertToYUY2().Interleaved2planar(true) : ssD
  146. ssDD = ssD.repair(HD?ssD.repair(allD,1,planar=planar):allD,HD?12:1,planar=planar) # Limit the difference to the max of what the denoising removed locally.
  147. ssDD = yuy26 ? ssDD.Planar2Interleaved().ConvertToYV16() : ssDD
  148. ssD  = yuy26 ? ssD.Planar2Interleaved().ConvertToYV16() : ssD
  149. ssDD = avs26 ? SSDD.mt_lutxy(ssD,"x range_half - abs y range_half - abs < x y ?",use_expr=1)
  150.        \     : SSDD.mt_lutxy(ssD,"x 128 - abs y 128 - abs < x y ?")                       # abs(diff) after limiting may not be bigger than before.
  151.  
  152. denoised.mt_adddiff(ssDD,U=2,V=2)                                                 # Apply the limited difference. (Sharpening is just inverse blurring)
  153. HD ? mt_clamp(last,pmax,pmin,overshoot,overshoot,chroma="copy first") : last
  154. yuy26 ? last.ConvertToYUY2().Interleaved2planar() : last
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement