Advertisement
Guest User

Untitled

a guest
Jun 1st, 2016
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.49 KB | None | 0 0
  1. ##################################################################################################################
  2. #
  3. # High bitdepth tools for Avisynth - GradFun3mod r6
  4. # based on Dither v1.27.1
  5. # Author: Firesledge, slightly modified by Gebbi
  6. #
  7. # What?
  8. # - This is a slightly modified version of the original GradFun3.
  9. # - It combines the usual color banding removal stuff with resizers during the process
  10. # for sexier results (less detail loss, especially for downscales of cartoons).
  11. # - This is a starter script, not everything is covered through parameters. Modify it to your needs.
  12. #
  13. # Requirements (in addition to the Dither requirements):
  14. # - AviSynth 2.6.x
  15. # - Debilinear, Debicubic, DebilinearM
  16. # - NNEDI3 + nnedi3_resize16
  17. #
  18. # Changes from the original GradFun3:
  19. # - yuv444 = true
  20. # (4:2:0 -> 4:4:4 colorspace conversion, needs 1920x1080 input)
  21. # - resizer = [ "none", "Debilinear", "DebilinearM", "Debicubic", "DebicubicM", "Spline16",
  22. # "Spline36", "Spline64", "lineart_rpow2", "lineart_rpow2_bicubic" ]
  23. # (use it only for downscales)
  24. # NOTE: As of r2 Debicubic doesn't have 16-bit precision, so a Y (luma) plane fix by torch is used here,
  25. # more info: https://mechaweaponsvidya.wordpress.com/2015/07/07/a-precise-debicubic/
  26. # Without yuv444=true Dither_resize16 is used with an inverse bicubic kernel.
  27. # - w = 1280, h = 720
  28. # (output width & height for the resizers; or production resolution for resizer="lineart_rpow2")
  29. # - smode = 4
  30. # (the old GradFun3mod behaviour for legacy reasons; based on smode = 1 (dfttest);
  31. # not useful anymore in most cases, use smode = 2 instead (less detail loss))
  32. # - deb = true
  33. # (legacy parameter; same as resizer = "DebilinearM")
  34. #
  35. # Usage examples:
  36. # - Source is bilinear 720p->1080p upscale (BD) with 1080p credits overlayed,
  37. # revert the upscale without fucking up the credits:
  38. # lwlibavvideosource("lol.m2ts")
  39. # GradFun3mod(smode=1, yuv444=true, resizer="DebilinearM")
  40. #
  41. # - You just want to get rid off the banding without changing the resolution:
  42. # GradFun3(smode=2)
  43. #
  44. # - Source is 1080p production (BD), downscale to 720p:
  45. # GradFun3mod(smode=2, yuv444=true, resizer="Spline36")
  46. #
  47. # - Source is a HDTV transportstream (or CR or whatever), downscale to 720p:
  48. # GradFun3mod(smode=2, resizer="Spline36")
  49. #
  50. # - Source is anime, 720p->1080p upscale, keep the resolution
  51. # but with smoother lineart instead of bilinear upscaled shit:
  52. # GradFun3mod(smode=2, resizer="lineart_rpow2")
  53. # This won't actually resize the video but instead mask the lineart and re-upscale it using
  54. # nnedi3_rpow2 which often results in much better looking lineart (script mostly by Daiz).
  55. #
  56. # Note: Those examples don't include parameters like thr, radius, elast, mode, ampo, ampn, staticnoise.
  57. # You probably don't want to use the default values.
  58. # For 16-bit output use:
  59. # GradFun3mod(lsb=true).Dither_out()
  60. #
  61. # What's the production resolution of my korean cartoon?
  62. # - Use your eyes combined with Debilinear(1280,720) - if it looks like oversharped shit,
  63. # it was probably produced in a higher resolution.
  64. # - Use Debilinear(1280,720).BilinearResize(1920,1080) for detail loss search.
  65. # - Alternatively you can lookup the (estimated) production resolution at
  66. # http://anibin.blogspot.com (but don't blindly trust those results)
  67. #
  68. # This program is free software. It comes without any warranty, to
  69. # the extent permitted by applicable law. You can redistribute it
  70. # and/or modify it under the terms of the Do What The Fuck You Want
  71. # To Public License, Version 2, as published by Sam Hocevar. See
  72. # http://sam.zoy.org/wtfpl/COPYING for more details.
  73. #
  74. ##################################################################################################################
  75.  
  76.  
  77. Function GradFun3mod (clip src, float "thr", int "radius", float "elast",
  78. \ int "mask", int "mode", float "ampo", float "ampn", int "pat",
  79. \ bool "dyn", float "dthr", int "smode", float "wmin", float "thr_det",
  80. \ float "thr_edg", int "debug", int "subspl", bool "lsb", bool "lsb_in",
  81. \ bool "staticnoise", float "thrc", int "radiusc", float "elastc",
  82. \ int "y", int "u", int "v", clip "ref", bool "slice", bool "yuv444", int "w", int "h", string "resizer", bool "deb")
  83. {
  84. thr = Default (thr, 0.35)
  85. thrc = Default (thrc, thr)
  86. radius = Default (radius, 12)
  87. radiusc = Default (radiusc, radius)
  88. elast = Default (elast, 3.0)
  89. elastc = Default (elastc, elast)
  90. mask = Default (mask, 2)
  91. smode = Default (smode, 2)
  92. wmin = Default (wmin, 1.0)
  93. thr_det = Default (thr_det, 2 + Round (Dither_max (thr - 0.35, 0) / 0.3))
  94. debug = Default (debug, 0)
  95. subspl = Default (subspl, 0)
  96. lsb = Default (lsb, false)
  97. lsb_in = Default (lsb_in, false)
  98. ref = Default (ref, src)
  99. yuv444 = Default (yuv444, false)
  100. w = Default (w, 1280)
  101. h = Default (h, 720)
  102. resizer = Default (resizer, "none")
  103. deb = Default (deb, false)
  104.  
  105.  
  106. # Do lineart smoothing first for sharper results
  107.  
  108. ow = src.Width()
  109. oh = src.Height()
  110.  
  111. src = (resizer == "lineart_rpow2") ? lineart_rpow2(src, w, h, ow, oh, false)
  112. \ : (resizer == "lineart_rpow2_bicubic") ? lineart_rpow2(src, w, h, ow, oh, true)
  113. \ : src
  114.  
  115. # Input range check. The other parameters are checked by the plugins.
  116. Assert (radius > 0, "GradFun3: "+chr(34)+"radius" +chr(34)+" must be strictly positive.")
  117. Assert (radiusc > 0, "GradFun3: "+chr(34)+"radiusc"+chr(34)+" must be strictly positive.")
  118. Assert (thr > 0, "GradFun3: "+chr(34)+"thr" +chr(34)+" must be strictly positive.")
  119. Assert (thrc > 0, "GradFun3: "+chr(34)+"thrc" +chr(34)+" must be strictly positive.")
  120. Assert (thr_det > 0, "GradFun3: "+chr(34)+"thr_det"+chr(34)+" must be strictly positive.")
  121. Assert (elast >= 1, "GradFun3: "+chr(34)+"elast" +chr(34)+" must be greater or equal to 1.")
  122. Assert (elastc >= 1, "GradFun3: "+chr(34)+"elastc" +chr(34)+" must be greater or equal to 1.")
  123. (yuv444 && resizer != "lineart_rpow2" && resizer != "lineart_rpow2_bicubic") ? Assert (w == 1280, "GradFun3mod: Output resolution must be 1280x720 when using 4:4:4 colorspace.") : true
  124. (yuv444 && resizer != "lineart_rpow2" && resizer != "lineart_rpow2_bicubic") ? Assert (h == 720, "GradFun3mod: Output resolution must be 1280x720 when using 4:4:4 colorspace.") : true
  125.  
  126. src_8 = (lsb_in) ? src.DitherPost (mode=-1) : src
  127. src_16 = (lsb_in) ? src : src.Dither_convert_8_to_16 ()
  128. ref_16 = (lsb_in) ? ref : ref.Dither_convert_8_to_16 ()
  129. yv411_flag = src.Dither_isyv411 ()
  130.  
  131. # Main debanding
  132.  
  133. yp = (! Defined (y) || (y == 3)) ? 3 : 1
  134. up = (! Defined (u) || (u == 3)) ? 3 : 1
  135. vp = (! Defined (v) || (v == 3)) ? 3 : 1
  136.  
  137. chroma_flag = ( (thrc != thr || radiusc != radius || elastc != elast)
  138. \ && yp == 3 && (up == 3 || vp == 3))
  139. up2 = (chroma_flag) ? 1 : up
  140. vp2 = (chroma_flag) ? 1 : vp
  141.  
  142. src_16
  143. flt_y = Dither_gf3_smooth_mod (last, src_8, ref_16, smode, radius, thr, elast, lsb_in, wmin, subspl, yp, up2, vp2)
  144. flt_c = (chroma_flag) ? Dither_gf3_smooth_mod (last, src_8, ref_16, smode, radiusc, thrc, elastc, lsb_in, wmin, subspl, 1, up, vp) : flt_y
  145. flt = (chroma_flag) ? flt_y.MergeChroma (flt_c) : flt_y
  146.  
  147. # Edge/detail mask
  148.  
  149. td_lo = Dither_max (thr_det * 0.75, 1)
  150. td_hi = Dither_max (thr_det, 1)
  151. mexpr = Dither_make_expr_gate (td_lo, td_hi)
  152.  
  153. dmask = (mask > 0 && yv411_flag) ? src_8.ConvertToY8 () : src_8
  154. dmask = (mask > 0) ? dmask.Dither_build_gf3_range_mask (mask) : dmask
  155. dmask = (mask > 0) ? dmask.mt_lut (expr=mexpr) : dmask
  156. dmask = (mask > 0) ? dmask.Dither_removegrain_emul (22, -1) : dmask
  157. dmask = (mask > 1) ? dmask.Dither_removegrain_emul (11, -1) : dmask
  158. dmask = (mask > 2) ? dmask.Dither_removegrain_emul (20, -1) : dmask
  159. dmask = (mask > 0 && yv411_flag) ? dmask.ConvertToYV411 () : dmask
  160.  
  161. res_16 = (mask > 0) ? Dither_merge16_8 (flt, src_16, dmask, luma=true, y=yp, u=up, v=vp) : flt
  162.  
  163. # Resizing / colorspace conversion (GradFun3mod)
  164.  
  165. resizer = (deb) ? "DebilinearM" : resizer
  166. resizer = (yuv444 && resizer == "none") ? "Spline36" : resizer
  167.  
  168. rkernel = (resizer == "Debilinear" && yuv444) ? res_16.DebilinearY(w,h,lsb_inout=true)
  169. \ : (resizer == "Debilinear") ? res_16.Debilinear(w,h,lsb_inout=true)
  170. \ : (resizer == "DebilinearM" && yuv444) ? res_16.DebilinearM(w,h,lsb_inout=true,chroma=false)
  171. \ : (resizer == "DebilinearM") ? res_16.DebilinearM(w,h,lsb_inout=true)
  172. \ : (resizer == "Debicubic" && yuv444) ? res_16.debicubicy_precise(w,h)
  173. \ : (resizer == "Debicubic") ? res_16.Dither_resize16(w,h,kernel="bicubic",invksh=true,fh=1.3,fv=1.3,invkstaps=7)
  174. \ : (resizer == "DebicubicM" && yuv444) ? res_16.DebicubicM_precise16(w,h,chroma=false)
  175. \ : (resizer == "DebicubicM") ? res_16.DebicubicM_precise16(w,h)
  176. \ : (resizer == "Spline16" && yuv444) ? res_16.Dither_resize16(w,h,kernel="spline16",fh=1.2,fv=1.2).ConvertToY8()
  177. \ : (resizer == "Spline16") ? res_16.Dither_resize16(w,h,kernel="spline16",fh=1.2,fv=1.2)
  178. \ : (resizer == "Spline36" && yuv444) ? res_16.Dither_resize16(w,h,kernel="spline36",fh=1.2,fv=1.2).ConvertToY8()
  179. \ : (resizer == "Spline36") ? res_16.Dither_resize16(w,h,kernel="spline36",fh=1.2,fv=1.2)
  180. \ : (resizer == "Spline64" && yuv444) ? res_16.Dither_resize16(w,h,kernel="spline64",fh=1.2,fv=1.2).ConvertToY8()
  181. \ : (resizer == "Spline64") ? res_16.Dither_resize16(w,h,kernel="spline64",fh=1.2,fv=1.2)
  182. \ : ((resizer == "lineart_rpow2" || resizer == "lineart_rpow2_bicubic") && yuv444) ? res_16.Dither_resize16(1280,720,kernel="spline36",fh=1.2,fv=1.2).ConvertToY8()
  183. \ : (resizer == "lineart_rpow2" || resizer == "lineart_rpow2_bicubic") ? res_16
  184. \ : (resizer == "none") ? res_16
  185. \ : Assert (false, "GradFun3mod: wrong resizer value.")
  186.  
  187. ly = rkernel
  188. lu = res_16.utoy8().Dither_resize16(1280, 720, src_left=0.25, kernel="blackman")
  189. lv = res_16.vtoy8().Dither_resize16(1280, 720, src_left=0.25, kernel="blackman")
  190. (yuv444) ? ytouv(lu,lv,ly) : rkernel
  191.  
  192. # Dithering
  193.  
  194. result = (lsb) ? last : last.DitherPost (
  195. \ mode=mode, ampo=ampo, ampn=ampn, pat=pat, dyn=dyn,
  196. \ prot=false, thr=dthr, staticnoise=staticnoise,
  197. \ y=yp, u=up, v=vp, slice=slice
  198. \ )
  199. result = (lsb)
  200. \ ? Dither_switch_planes16 (src_16, result, y=y, u=u, v=v)
  201. \ : Dither_switch_planes8 (src_8, result, y=y, u=u, v=v)
  202.  
  203. (debug == 1 ) ? dmask.GreyScale () : result
  204. (debug == 1 && lsb) ? Dither_convert_8_to_16 () : last
  205. }
  206.  
  207.  
  208.  
  209. Function Dither_gf3_smooth_mod (clip src_16, clip src_8, clip ref_16, int smode, int radius, float thr, float elast, bool lsb_in, float wmin, int subspl, int yp, int up, int vp)
  210. {
  211. src_16
  212. (smode == 0) ? Dither_gf3_smoothgrad_multistage (ref_16, radius, thr, elast, yp, up, vp)
  213. \ : (smode == 1) ? Dither_gf3_dfttest (src_8, ref_16, radius, thr, elast, lsb_in, yp, up, vp)
  214. \ : (smode == 2) ? Dither_gf3_bilateral_multistage (ref_16, radius, thr, elast, wmin, subspl, yp, up, vp)
  215. \ : (smode == 3) ? Dither_gf3_smoothgrad_multistage_3 (radius, thr, elast, yp, up, vp)
  216. \ : (smode == 4) ? Dither_gf3_dfttest_mod (src_8, ref_16, radius, thr, elast, lsb_in, yp, up, vp)
  217. \ : Assert (false, "GradFun3mod: wrong smode value.")
  218. }
  219.  
  220.  
  221.  
  222. # Valid values for y, u and v: 1 and 3
  223. Function Dither_gf3_dfttest_mod (clip src, clip src_8, clip ref,
  224. \ int radius, float thr, float elast, bool lsb_in,
  225. \ int y, int u, int v)
  226. {
  227. Assert (radius <= 128, "GradFun3: max "+chr(34)+"radius" +chr(34)+" value is 128 when smode = 1.")
  228.  
  229. hrad = Dither_max (radius * 3 / 4, 1)
  230.  
  231. (lsb_in) ? src : src_8
  232.  
  233. dfttest (
  234. \ sigma=thr*12, tbsize=1,
  235. \ sbsize=hrad*4, sosize=hrad*3,
  236. \ lsb=true, lsb_in=lsb_in,
  237. \ Y=(y==3), U=(u==3), V=(v==3)
  238. \ )
  239.  
  240. Dither_limit_dif16 (last, ref, thr=thr, elast=elast, y=y, u=u, v=v)
  241. }
  242.  
  243. Function lineart_rpow2 (clip src, int w, int h, int ow, int oh, bool bicubic)
  244. {
  245. w = default(w, 1280)
  246. h = default(h, 720)
  247. ow = default(ow, 1920)
  248. oh = default(oh, 1080)
  249. bicubic = default(bicubic, false)
  250.  
  251. orig = src
  252. (bicubic) ? DebicubicM(src, w, h) : DebilinearM(src, w, h)
  253. nnedi3_rpow2(2, cshift="Spline36Resize", fwidth=ow, fheight=oh)
  254. edges = last.mt_edge("prewitt", 4, 24, 4, 24, chroma="true").removegrain(20, 1)
  255. sharp = last
  256. mt_merge(orig, sharp, edges)
  257. }
  258.  
  259. Function debicubicy_precise(clip src, int dst_width, int dst_height, float "src_left", float "src_top", float "src_width", float "src_height")
  260. {
  261. # script by torch
  262.  
  263. sw = src.width()
  264. sh = src.height()/2
  265. src_left = default(src_left, 0.0)
  266. src_top = default(src_top, 0.0)
  267. src_width = default(src_width, sw)
  268. src_height = default(src_height, sh)
  269.  
  270. inv_left = -src_left * dst_width/src_width
  271. inv_top = -src_top * dst_height/src_height
  272. inv_width = (sw-src_left) * dst_width/src_width
  273. inv_height = (sh-src_top) * dst_height/src_height
  274.  
  275. seed = debicubicy(src, dst_width, dst_height, src_left, src_top, src_width+src_left, src_height+src_top, lsb_inout=true)
  276. up = seed.dither_resize16(sw, sh, inv_left, inv_top, inv_width, inv_height, kernel="bicubic", fh=-1.0, fv=-1.0)
  277. diff = dither_sub16(src, up, dif=true).dither_lut16("x 32768 - 99 * 32768 +")
  278. down = diff.debicubicy(dst_width, dst_height, src_left, src_top, src_width+src_left, src_height+src_top, lsb_inout=true)
  279. down = down.dither_lut16("x 32768 - 99 / 32768 + round")
  280.  
  281. return seed.dither_add16(down, dif=true)
  282. }
  283.  
  284. Function DebicubicM_precise16(clip input, int target_width, int target_height, bool "chroma")
  285. {
  286. # based on DebilinearM v1.3.1
  287.  
  288. Assert(target_width > 0, "GradFun3mod: target width must be greater than 0")
  289. Assert(target_height > 0, "GradFun3mod: target height must be greater than 0")
  290.  
  291. thr = 10
  292. expand = 1
  293. inflate = 2
  294. kernel = "Spline36"
  295. chroma = Default(chroma, true)
  296.  
  297. w = input.Width()
  298. h = input.Height()/2
  299. uvint = chroma ? 3 : 1
  300.  
  301. # Resizing
  302. input_8bit = input.DitherPost(mode=-1)
  303. dbi = chroma ? input.Dither_resize16(target_width,target_height,kernel="bicubic",invksh=true,fh=1.3,fv=1.3,invkstaps=7)
  304. \ : input.debicubicy_precise(target_width,target_height)
  305. dbi_8bit = chroma ? input_8bit.Debicubic(target_width,target_height)
  306. \ : input_8bit.DebicubicY(target_width,target_height)
  307. dbi2 = dbi_8bit.ResizeX(w,h, kernel="Bicubic", chroma=chroma)
  308. rs = input.ResizeX(target_width,target_height, kernel=kernel, chroma=chroma, lsb_in=true, lsb=true)
  309.  
  310. # Masking
  311. diffmask = mt_lutxy(input_8bit,dbi2, "x y - abs", U=uvint, V=uvint).mt_binarize(threshold=thr, U=uvint, V=uvint)
  312. diffmask = diffmask.ResizeX(target_width,target_height, kernel="Bilinear", chroma=chroma)
  313. \ .mt_binarize(threshold=3, U=uvint, V=uvint)
  314. \ .DebilinearM_expand(expand=expand, U=uvint, V=uvint)
  315. \ .DebilinearM_inflate(inflate=inflate, U=uvint, V=uvint)
  316. merged = Dither_merge16_8(dbi,rs,diffmask, u=uvint, v=uvint)
  317.  
  318. return merged
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement