Advertisement
Zastin

adachi to shimamura 01.vpy

Oct 22nd, 2020 (edited)
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.04 KB | None | 0 0
  1. import vapoursynth as vs
  2. core=vs.core
  3. import kagefunc as kgf
  4. import zzfunc as zz
  5. import fvsfunc as fvf
  6. import mvsfunc as mvf
  7. import muvsfunc as muf
  8. import havsfunc as haf
  9. import debandshit as db
  10. import mvmulti as mv
  11. import vsTAAmbk as taa
  12. import dhh
  13. from vsutil import *
  14. from functools import partial
  15. import zzfunc_beta as zzt
  16. from G41Fun import EdgeDetect
  17.  
  18. def autogain(clip): return core.avsw.Eval('clip.coloryuv(autogain=true)', [clip], ['clip'])
  19.  
  20. def all_lines(clip, radius=3, **mode):
  21. refa = haf.mt_inpand_multi(haf.mt_expand_multi(clip, sw=radius, sh=radius, **mode), sw=radius, sh=radius, **mode)
  22. refb = haf.mt_expand_multi(haf.mt_inpand_multi(clip, sw=radius, sh=radius, **mode), sw=radius, sh=radius, **mode)
  23. return core.std.Expr([clip, refa, refb], 'x y - abs x z - abs max')
  24.  
  25. def dark_lines(clip, radius=3, show=False, **mode):
  26. ref = haf.mt_inpand_multi(haf.mt_expand_multi(clip, sw=radius, sh=radius, **mode), sw=radius, sh=radius, **mode)
  27. if show:
  28. return ref
  29. return core.std.Expr([clip, ref], 'x y - abs')
  30.  
  31.  
  32. def MMFilter(clips, mode='min', planes=None):
  33. planes = list(range(clips[0].format.num_planes)) if planes is None else [planes] if isinstance(planes, int) else planes
  34. expr = 'x y - x z - * 0 < {} x y - abs x z - abs < {} ? ?'.format('z' if mode=='max' else 'x', 'z y' if mode=='max' else 'y z')
  35. expr = [expr if i in planes else '' for i in range(clips[0].format.num_planes)]
  36. return core.std.Expr(clips, expr)
  37.  
  38. both = dict(coordinates=[0,1,0,1,1,0,1,0])
  39.  
  40. def Dither_gf3_bilateral_multistage(src, ref=None, radius=None, thr=1/3, elast=3.0, wmin=1.0, subspl=0, planes=[0,1,2]):
  41. if radius is None:
  42. # 16 @ 1080p, 12 at 720p and 9 at 480p
  43. radius = max( (src.width - 1280)/160 + 12, (src.height - 720)/ 90 + 12 )
  44. y, u, v = [3 if x in planes else 1 for x in range(3)]
  45. thr_1 = round(max(thr * 4.5, 1.25), 1)
  46. thr_2 = round(max(thr * 9, 5), 1)
  47. subspl_2 = subspl if subspl in (0,1) else subspl / 2
  48. r4 = round(max(radius * 4 / 3, 4))
  49. r2 = round(max(radius * 2 / 3, 3))
  50. r1 = round(max(radius / 3, 2))
  51. clips = [src.fmtc.nativetostack16()]
  52. clip_names = ["c"]
  53. ref_t = "c"
  54. if ref is not None:
  55. ref_s = ref.fmtc.nativetostack16()
  56. clips += [ref_s]
  57. clip_names += ["ref"]
  58. ref_t = "ref"
  59. avs_stuff = "c.Dither_bilateral16(radius={r4}, thr={thr_1}, flat=0.75, wmin={wmin}, ref={ref_t}, subspl={subspl}, y={y}, u={u}, v={v})"
  60. avs_stuff += ".Dither_bilateral16(radius={r2}, thr={thr_2}, flat=0.25, wmin={wmin}, ref={ref_t}, subspl={subspl_2}, y={y}, u={u}, v={v})"
  61. avs_stuff += ".Dither_bilateral16(radius={r1}, thr={thr_2}, flat=0.50, wmin={wmin}, ref={ref_t}, subspl={subspl_2}, y={y}, u={u}, v={v})"
  62. avs_stuff = avs_stuff.format(ref_t=ref_t, r4=r4, r2=r2, r1=r1, thr_1=thr_1, thr_2=thr_2, wmin=wmin, subspl=subspl, subspl_2=subspl_2, y=y, u=u, v=v)
  63. return core.avsw.Eval(avs_stuff, clips=clips, clip_names=clip_names).fmtc.stack16tonative()
  64. def lumadouble(clip):
  65. clip = core.std.Transpose(clip)
  66. clip = core.nnedi3.nnedi3(clip, 0, 1, 0, 0, 4, 2).nnedi3.nnedi3(1, 0, 0, 0, 4, 2)
  67. clip = core.std.Transpose(clip)
  68. clip = core.nnedi3.nnedi3(clip, 0, 1, 0, 0, 4, 2).nnedi3.nnedi3(1, 0, 0, 0, 4, 2)
  69. return core.resize.Spline36(clip, src_left=.5, src_top=.5)
  70. def show(*clips): return core.std.Interleave(clips, mismatch=1).resize.Spline36(format=vs.RGB24, dither_type='error_diffusion',matrix_in=1)
  71. ssf = 2.
  72.  
  73. def aa(base, clip, mask):
  74. aa = clip.std.Transpose().resize.Spline16(1980, 3520)
  75. mclip = mask.std.Maximum().resize.Bilinear(3520, 1980).std.Binarize(1111)
  76. aa = aa.eedi3m.EEDI3(0, 0, gamma=0, sclip=aa.znedi3.nnedi3(0,0,0,0,4,2), mclip=mclip.std.Transpose()).std.Transpose()
  77. aa = aa.eedi3m.EEDI3(0, 0, gamma=0, sclip=aa.znedi3.nnedi3(0,0,0,0,4,2), mclip=mclip).resize.Spline36(1920, 1080)
  78. return base.std.MaskedMerge(aa, mask)
  79.  
  80. def RepairMod(clip, repairclip, **planes):
  81. rmax = repairclip.std.Maximum(**planes, coordinates=[0,1,0,1,1,0,1,0]).std.Maximum(**planes)
  82. rmin = repairclip.std.Minimum(**planes, coordinates=[0,1,0,1,1,0,1,0]).std.Minimum(**planes)
  83. return core.std.Expr([clip, rmax, rmin], 'x y min z max')
  84.  
  85.  
  86. def FSRCNNX(clip):
  87. return get_y(join([clip]*3).placebo.Shader(shader=r'C:\Users\Zastin\AppData\Roaming\mpv\shaders\FSRCNNX_x2_16-0-4-1.glsl', filter='box', width=clip.width*2, height=clip.height*2))
  88. def nnedi3_rpow2(clip):
  89. clip = clip.std.Transpose().znedi3.nnedi3(0, 1, 0, 0, 4, 2)
  90. return clip.std.Transpose().znedi3.nnedi3(0, 1, 0, 0, 4, 2).resize.Spline36(src_left=.5, src_top=.5)
  91.  
  92.  
  93.  
  94.  
  95.  
  96. m_none = "[0 75][655 755][2026 2127][2314 2365][3167 3219][3889 3956][4935 5042][6114 6257][7865 8012][8194 8223][9452 9577][10218 10307][10724 10765][16359 16424][20252 20313][23254 23337][24545 24628][26926 26973][27032 27233][29498 29549][30618 30809][31953 31975][31976 32521]"
  97. m_strong = "[318 389][3220 3285][3957 4004][5527 5641][8488 8523][8974 9105][10640 10723][11298 11387][14552 14587][15297 15314][15513 15520][18556 18663][21111 21218]"
  98. m_strong_masked = "[6446 6506][7580 7645][7786 7864][8013 8101][9152 9259][9578 9677][10980 11111][11472 11531][11532 11627][11628 11709][13282 13413][14408 14491][14794 14885][15801 15926][16689 17565][17938 18009][20096 20203][22701 22772][24239 24310][28094 28164][29306 29401][29814 29869][30378 30497][30498 30617][32968 33039][33736 33855][34234 34283][34313 34336][34448 34486][34515 34542][34580 34643]"
  99. m_grain = "[318 389][7208 7315][8974 9105][9152 9259][9578 9843][10640 10723][10980 11111][11298 11387][18556 18663]"
  100. #op="[3957 6113]"
  101.  
  102.  
  103.  
  104. waka = core.ffms2.Source(r'C:\Users\Zastin\Desktop\WIP\scissorgirls\Adachi to Shimamura - 01 (Wakanim FR 1080p).mkv')[23:]
  105. funi = core.ffms2.Source(r'C:\Users\Zastin\Desktop\cartoons\[Erai-raws] Adachi to Shimamura - 01 [1080p].mkv')[192:]
  106.  
  107. hsm = kgf.hardsubmask(waka, funi)
  108. diff = core.std.Expr([waka, funi], 'x y - abs').resize.Point(format=vs.YUV444P8)
  109. diff = core.std.Expr(split(diff), 'x y z max max')
  110. main = diff.std.Binarize(10)
  111. upper = diff.std.Binarize(30)
  112. middle= diff.std.Binarize(20)
  113. lower = main.std.Minimum()
  114. rad = 3
  115. diffh = iterate(lower, partial(core.std.Minimum, coordinates=[0,0,0,1,1,0,0,0]), rad-1)
  116. diffv = iterate(lower, partial(core.std.Minimum, coordinates=[0,1,0,0,0,0,1,0]), rad-1)
  117. diffhv= core.std.Expr([diffh,diffv], 'x y max').misc.Hysteresis(main)
  118. diffb = middle.std.Minimum().misc.Hysteresis(main)
  119. diffc = upper.rgvs.RemoveGrain(2).misc.Hysteresis(main)
  120. hsm_ts = iterate(core.std.Expr([diffhv, diffb, diffc, hsm], 'x y z max max'), core.std.Maximum, 5).std.Convolution([1]*25)
  121. hsm = core.std.Expr([hsm, hsm_ts], 'x y max')
  122.  
  123. clipyuv = waka.std.MaskedMerge(funi, hsm)
  124.  
  125. y = zz.y(clipyuv)
  126. desl = depth(depth(y, 32).descale.Debilinear(1504, 846), 16)
  127. clip = depth(y, 16)
  128.  
  129. bars = core.std.Expr(split(clipyuv.std.Expr(['x 17 <= 255 0 ?','x 128 - abs 1 <= 255 0 ?']).resize.Point(format=vs.YUV444P8)), 'x y z min min')
  130. bars = iterate(bars, partial(core.std.Maximum, coordinates=[0,0,0,1,1,0,0,0]), 10).std.Median().std.BoxBlur(hradius=100).std.Binarize(255).std.Maximum(coordinates=[0,1,0,0,0,0,1,0]).std.Maximum(coordinates=[0,1,0,0,0,0,1,0]).std.Maximum(coordinates=[0,1,0,0,0,0,1,0]).std.Convolution([1]*5,mode='v')
  131. borders = y.std.BlankClip(1920-8,1080-8)
  132. for x in range(1, 5):
  133. borders = borders.std.AddBorders(1,1,1,1,min(256 * (x/4), 255))
  134.  
  135. creds = core.std.Expr([clip, desl.resize.Bilinear(1920, 1080)], 'x y - abs')
  136. creds = creds.std.Binarize(12<<8).misc.Hysteresis(creds.std.Maximum().std.Binarize(4<<8))
  137. creds = creds.std.Maximum(coordinates=[0,1,0,1,1,0,1,0]).std.Maximum().std.Maximum(coordinates=[0,1,0,1,1,0,1,0]).std.Convolution([1]*25)
  138.  
  139. creds = core.std.Expr([creds,borders,bars], 'x y z max 257 * max')
  140.  
  141. fsr = FSRCNNX(desl)
  142. fsrref = fsr.resize.Spline36(1920,1080)
  143. desl = core.std.Merge(nnedi3_rpow2(desl), fsr, 0.5).resize.Spline36(1920,1080)
  144.  
  145. mask = clip.std.Prewitt()
  146. maskm = mask.std.Maximum()
  147.  
  148. aamask = maskm.std.Binarize(40<<8).std.Convolution([1]*9)
  149.  
  150. maska = mask.std.Binarize(30<<8)
  151. maskb = mask.std.Binarize(60<<8)
  152. mask = core.std.Expr([maska, maskb.std.Maximum().std.Maximum(), maskb], 'x y - z max')
  153. mask = mask.std.Maximum().std.Maximum().std.Minimum().std.Minimum()
  154. maskmin = mask.std.Minimum()
  155. mask = core.std.Expr([mask, maskmin.std.Maximum(), maskmin], 'x y 0 = and x x z + 2 / ?')
  156. imask = iterate(mask, core.std.Maximum, 4).std.Invert()
  157. mask = core.std.Expr([mask, imask], 'x y +')
  158. desl = aa(desl, fsr, aamask)
  159. desl = desl.rgvs.Repair(clip.std.MaskedMerge(desl, mask), 1)
  160.  
  161. edges = EdgeDetect(fsrref, "FDOG").std.Maximum().std.Minimum().std.Expr(f'x {20<<8} < 0 x {50<<8} > 65535 x {20<<8} - {65535/(30<<8)} / ? ?')
  162. lines = all_lines(fsrref, 2).std.Binarize(2 * 256)
  163. lines = core.std.Expr([lines.rgvs.RemoveGrain(1), lines.rgvs.RemoveGrain(3).std.Maximum(threshold=1<<15)], 'x y max')
  164. det = zzt.combine([edges, lines]).std.Convolution([1]*9)
  165.  
  166. desl = clip.std.MaskedMerge(desl, det)
  167. clip = desl.std.MaskedMerge(clip, creds)
  168.  
  169. mask_normal = db.rangemask(clipyuv, 2, 2).std.Binarize(2)
  170.  
  171. mask_big = db.rangemask(clipyuv, 12, 6).std.Binarize(3)
  172.  
  173. rad = 16
  174. thr = 2
  175. mi0 = y.std.Minimum()
  176. ma0 = y.std.Maximum()
  177. mi1 = y.std.Minimum(**both)
  178. ma1 = y.std.Maximum(**both)
  179. mi = iterate(mi0, partial(core.std.Minimum, coordinates=[0,0,0,1,1,0,0,0]), rad-1)
  180. ma = iterate(ma0, partial(core.std.Maximum, coordinates=[0,0,0,1,1,0,0,0]), rad-1)
  181. maskx = core.std.Expr([ma, mi], 'x y - abs').std.Binarize(thr)
  182. mi = iterate(mi0, partial(core.std.Minimum, coordinates=[0,1,0,0,0,0,1,0]), rad-1)
  183. ma = iterate(ma0, partial(core.std.Maximum, coordinates=[0,1,0,0,0,0,1,0]), rad-1)
  184. masky = core.std.Expr([ma, mi], 'x y - abs').std.Binarize(thr)
  185. mi = iterate(mi1, partial(core.std.Minimum, coordinates=[0,0,1,0,0,1,0,0]), rad-2)
  186. ma = iterate(ma1, partial(core.std.Maximum, coordinates=[0,0,1,0,0,1,0,0]), rad-2)
  187. maskz = core.std.Expr([ma, mi], 'x y - abs').std.Binarize(thr)
  188. mi = iterate(mi1, partial(core.std.Minimum, coordinates=[1,0,0,0,0,0,0,1]), rad-2)
  189. ma = iterate(ma1, partial(core.std.Maximum, coordinates=[1,0,0,0,0,0,0,1]), rad-2)
  190. maska = core.std.Expr([ma, mi], 'x y - abs').std.Binarize(thr)
  191. mask_directional = zzt.std.CombineClips([maskx, masky, maskz, maska], 'min')
  192. mask_directional = join([mask_directional]*3).resize.Bilinear(format=vs.YUV420P8)
  193.  
  194. rad = 15
  195. a = iterate(iterate(y, core.std.Minimum, rad), core.std.Maximum, rad)
  196. b = iterate(iterate(y, core.std.Maximum, rad), core.std.Minimum, rad)
  197. mask_lines = core.std.Expr([y,a,b], 'x y - abs x z - abs max 1 > 255 0 ?').std.Maximum().std.Maximum(threshold=128)
  198. mask_lines = join([mask_lines]*3).resize.Bilinear(format=vs.YUV420P8)
  199.  
  200. mask_weak = zzt.combine([mask_normal, mask_big, mask_directional], 'min').std.Deflate()
  201. mask_weak = depth(mask_weak, 16, range=1, range_in=1)
  202.  
  203. mask_strong = zzt.combine([mask_normal, mask_big, mask_directional]).std.BoxBlur(hradius=3, vradius=3)
  204. mask_strong = zzt.combine([mask_strong, mask_lines])
  205. mask_strong = depth(mask_strong, 16, range=1, range_in=1)
  206.  
  207. gmask = db.rangemask(y, 6, 1).std.Binarize(4).std.BoxBlur(hradius=10, vradius=10)
  208. gmask = core.std.Expr([gmask,gmask.std.BoxBlur(hradius=10, vradius=10)], 'x y min 257 *', vs.GRAY16)
  209.  
  210. clip = zz.mergechroma(clip, depth(clipyuv, 16))
  211.  
  212. bil = mvf.LimitFilter(db.Dither_bilateral(clip, thr=0.35), clip, thr=0.35, thrc=0.25, elast=3)
  213. verystrong = clip.placebo.Deband(7,2,5,16,0)
  214. verystrong = MMFilter([clip,bil,verystrong],'max')
  215. strong = clip.placebo.Deband(7,1,4,16,0)
  216. strong = MMFilter([clip,bil,strong],'max')
  217. weak = MMFilter([clip,bil,clip.placebo.Deband(7,1,2,16,0)])
  218.  
  219. deband = strong.std.MaskedMerge(weak, mask_strong).std.MaskedMerge(clip, mask_weak)
  220. deband = fvf.rfs(deband, verystrong, m_strong)
  221. deband = fvf.rfs(deband, verystrong.std.MaskedMerge(strong, mask_strong).std.MaskedMerge(db.f3kdb(clip, 15, 60, 40, 40), mask_weak), m_strong_masked)
  222. deband = fvf.rfs(kgf.adaptive_grain(deband, luma_scaling=3).std.MaskedMerge(deband, gmask, 0), core.grain.Add(deband, var=1, constant=True), m_grain)
  223. clip = fvf.rfs(deband, clip, m_none)
  224.  
  225. depth(clip[:34644], 10).set_output()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement