Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function MDG (clip clp, int "blksize", int "overlap", int "thsad", int "thsadc", float "limit", float "limitc", int "debug", int "search", bool "chroma", int "thscd1", \
- int "thscd2", int "scalecsad", bool "recalc", bool "multirecalc", bool "multirecalc2", bool "cs", int "prefilter", float "blurlimit", int "bias", \
- bool "truemotion", int "dct", bool "hdr", float "sstrength", int "radius", int "thsad2", int "pel")
- {
- original = clp
- hd = clp.width() > 720 ? true : false
- hdr = default(hdr, clp.width() > 1920 ? true : false)
- blksize = default(blksize, clp.width() > 1920 ? 32 : 16)
- blksize2 = blksize/2
- blksize3 = blksize/4
- blksize4 = blksize/8
- overlap = default(overlap, blksize/2)
- overlap2 = overlap/2
- overlap3 = overlap/4
- overlap4 = overlap/8
- limit = default(limit, 0.1)
- limitc = default(limitc, limit + 0.25)
- debug = default(debug, 0)
- radius = default(radius, 1)
- pel = default(pel, 4)
- search = default(search, 4)
- searchparam = 2
- searchparam2 = 2
- searchparam3 = 2
- searchparam4 = 2
- pelsearch = 2
- scalecsad = default(scalecsad, clp.is420 ? 0 : clp.is422 ? 1 : clp.is444 ? 2 : 0)
- recalc = default(recalc, true)
- multirecalc = default(multirecalc, blksize3 >= 4 ? true : false)
- multirecalc2 = default(multirecalc2, blksize4 >= 4 ? clp.width() > 1920 || !multirecalc ? false : multirecalc ? true : false : false)
- thscd1 = default(thscd1, 400)
- thscd2 = default(thscd2, 85)
- cs = default(cs, false)
- thsad = default(thsad, 100)
- thsadc = default(thsadc, thsad)
- thsad = thsad < 100 ? 100 : thsad
- thsadc = thsadc < 100 ? 100 : thsadc
- thsad2 = default(thsad2, thsad)
- sstrength = default(sstrength, 2.0)
- blurlimit = default(blurlimit, 0.15)
- bias = default(bias, 20)
- chroma = default(chroma, true)
- truemotion = default(truemotion, false)
- lda = truemotion ? (1000 * blksize * blksize) / 64 : 4
- lda2 = truemotion ? (1000 * blksize2 * blksize2) / 64 : 4
- lda3 = truemotion ? (1000 * blksize3 * blksize3) / 64 : 4
- lda4 = truemotion ? (1000 * blksize4 * blksize4) / 64 : 4
- lsad = truemotion ? 8000 : 6000
- pnew = truemotion ? 150 : 50
- orgbits = clp.bitspercomponent()
- prefiltbits = orgbits == 16 ? 10 : 8
- dct = default(dct, prefiltbits == 10 ? 0 : 5)
- prefilter = default(prefilter, 1)
- c2 = clp.convertbits(bits=16)
- c2blur = blurlimit > 0 ? c2.spresso(bias=bias, biasc=bias*2, rgmodec=chroma?4:0, limit=blurlimit, limitc=blurlimit*2) : c2
- prefilt = prefiltbits < orgbits ? clp.convertbits(bits=prefiltbits) : clp
- clp = c2
- Lthres = ex_bs(75, 8, prefiltbits, tv_in=true, tv_out=true, fulls=false)
- 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)
- prefilt = prefilt.ex_luma_rebuild(s0=3.0, uv=chroma?3:1)
- sharp = sstrength > 0 ? clp.detailsharpen(sstr=sstrength) : clp
- superanalyse = prefilt.msuper(pel=pel, sharp=1, rfilter=2, chroma=chroma)
- supermdg = sharp.msuper(pel=pel, levels=1, sharp=1, rfilter=2, chroma=chroma)
- 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)
- 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)
- 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()
- 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()
- 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()
- 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()
- 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
- 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
- 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()
- 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()
- 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()
- 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()
- 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
- 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
- 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()
- 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()
- 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()
- 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()
- 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
- 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
- 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()
- 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()
- 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()
- 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()
- 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)
- finalclip = cs ? cshd(finalclip, clp, fv1, bv1, supermdg, hd) : finalclip
- prefilt = debug == 1 ? prefilt.convertbits(orgbits) : prefilt
- original = debug == 1 ? chroma ? original.ex_luma_rebuild(s0=3.0) : original.ex_luma_rebuild(s0=3.0, uv=1) : original
- original = debug > 1 ? original.convertbits(bits=16) : original
- output = \
- debug == 1 ? interleave(original, prefilt) \
- : 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) \
- : debug == 3 ? interleave(original, subtract(clp, finalclip.mergechroma(clp)).levels(127*256, 1, 132*256, 0*256, 255*256)) \
- : debug == 4 ? interleave(original, subtract(clp, finalclip.mergeluma(clp)).levels(127*256, 1, 132*256, 0*256, 255*256)) \
- : 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()) \
- : 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) \
- : debug == 7 ? interleave(original, sharp).subtitle("Effect of sharpening", x=0, y=20) \
- : debug == 8 ? interleave(original, c2blur).subtitle("Effect of spatial blur", x=0, y=20) \
- : finalclip
- output = debug == 0 ? chroma ? output : output.greyscale() : output
- return output
- }
- function CSHD(clip denoised, clip original, clip cf1, clip cb1, clip super, bool "HD", bool "planar", int "overshoot"){
- HD = default(HD ,false)
- planar = default(planar,false)
- overshoot = default(overshoot,0)
- avs26 = VersionNumber() < 2.60 ? false : true
- yuy26 = avs26 && Isyuy2(denoised) && planar
- HD ? eval("""
- cb1=original.MCompensate(Super, cb1, planar=planar)
- cf1=original.MCompensate(Super, cf1, planar=planar)
- 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")
- 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()
- s = denoised.MinBlur(HD?2:1,1,planar=planar) # Damp down remaining spots of the denoised clip.
- s = yuy26 ? s.Planar2Interleaved().ConvertToYV16() : s
- original = yuy26 ? original.Planar2Interleaved().ConvertToYV16() : original
- denoised = yuy26 ? denoised.Planar2Interleaved().ConvertToYV16() : denoised
- allD = mt_makediff(original,denoised) # The difference achieved by the denoising.
- ssD = mt_makediff(s,HD?s.removegrain(20,-1,planar=planar).\
- removegrain(20,-1,planar=planar):\
- s.removegrain(11,-1,planar=planar)) # The difference of a simple kernel blur.
- allD = yuy26 ? allD.ConvertToYUY2().Interleaved2planar(true) : allD
- ssD = yuy26 ? ssD.ConvertToYUY2().Interleaved2planar(true) : ssD
- 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.
- ssDD = yuy26 ? ssDD.Planar2Interleaved().ConvertToYV16() : ssDD
- ssD = yuy26 ? ssD.Planar2Interleaved().ConvertToYV16() : ssD
- ssDD = avs26 ? SSDD.mt_lutxy(ssD,"x range_half - abs y range_half - abs < x y ?",use_expr=1)
- \ : SSDD.mt_lutxy(ssD,"x 128 - abs y 128 - abs < x y ?") # abs(diff) after limiting may not be bigger than before.
- denoised.mt_adddiff(ssDD,U=2,V=2) # Apply the limited difference. (Sharpening is just inverse blurring)
- HD ? mt_clamp(last,pmax,pmin,overshoot,overshoot,chroma="copy first") : last
- yuy26 ? last.ConvertToYUY2().Interleaved2planar() : last
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement