Advertisement
Guest User

Untitled

a guest
Jan 27th, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.98 KB | None | 0 0
  1. #################################################################################################
  2. ###### edi_resize16 v3.1 ###### by mawen1250 ###### 2013.12.06 ######
  3. #################################################################################################
  4. ###### from v2.8 on, edi_resize16 only supports AviSynth v2.6+ ######
  5. ###### Requirements: masktools v2.0a48, dither v1.24.0, nnedi3 v0.9.4, eedi3 v0.9.2, ######
  6. ###### RemoveGrain v1.0pre, Repair v1.0pre, SmoothAdjust v2.95, Contra-Sharpen mod 3.4 ######
  7. ###### FTurn v1.4 (not necessarily required but will improve speed) ######
  8. #################################################################################################
  9. ###### accept Y8, YV12, YV16, YV24 input, use edi and Dither_resize16(nr) for scaling ######
  10. ###### output format can be 8bit/16bit Y8/YV12/YV16/YV24 or RGB32/RGB24/RGB48YV12/RGB48Y ######
  11. #################################################################################################
  12. ###### "lsb_in"/"lsb" defines if input/output clip is stacked-16bit, default is False ######
  13. ###### "output" defines colorspace of output clip, for RGB output "lsb" is ignored ######
  14. #################################################################################################
  15. ###### when horizontal/vertical scale ratio > "ratiothr", we assume it's upscaling ######
  16. ###### when horizontal/vertical scale ratio <= "ratiothr", we assume it's downscaling ######
  17. ###### by default ratiothr=1.000 ######
  18. ###### edi is only applied when upscaling ######
  19. ###### Dither_resize16 is used after edi upscaling to fix shift and scale to target res ######
  20. ###### we call the edi+Dither_resize16 scaling method "edge area resize" ######
  21. ###### for downscaling, Dither_resize16 is applied instead, we call it "flat area resize" ######
  22. ###### when mixed=True, edi (edge area) is combined with Dither_resize16 (flat area) ######
  23. #################################################################################################
  24.  
  25.  
  26. Function edi_resize16(clip input, int "target_width", int "target_height", float "src_left", float "src_top",
  27. \ float "src_width", float "src_height",
  28. \ string "kernel_d", string "kernel_u", float "f_d", float "f_u", int "taps",
  29. \ float "a1", float "a2", float "a3", bool "invks_d", bool "invks_u", int "invkstaps", bool "noring",
  30. \ float "alpha", float "beta", float "gamma", int "nrad", int "mdis",
  31. \ float "ratiothr", bool "mixed", float "thr", float "elast", float "sharp",
  32. \ string "output", bool "tv_range", string "cplace", string "matrix", string "curve", float "gcor",
  33. \ int "Y", int "U", int "V", bool "lsb_in", bool "lsb", int "dither")
  34. {
  35. ###############################################################################################################################
  36. ###############################################################################################################################
  37. # Parameters for merging edge&flat upscaled clip
  38.  
  39. mixed = Default(mixed, True ) # edi_resize16 uses edi+Dither_resize16 for edge upscaling, this parameter defines whether to combine edi+Dither_resize16(edge area) and Dither_resize16(flat area) in upscaling, which achieves higher precision upscaling result(mainly for flat area).
  40. thr = Default(thr, 1.0 ) # the same with "thr" in Dither_limit_dif16, valid value range is (0, 10.0].
  41. elast = Default(elast, 1.5 ) # the same with "elast" in Dither_limit_dif16, valid value range is [1, 10.0].
  42. # PDiff: pixel value diff between flat clip and edge clip (edge clip as reference)
  43. # ODiff: pixel value diff between merged clip and edge clip (edge clip as reference)
  44. # PDiff, thr and elast is used to calculate ODiff:
  45. # ODiff = PDiff when [PDiff <= thr]
  46. # ODiff gradually smooths from thr to 0 when [thr <= PDiff <= thr * elast]
  47. # for elast>2.0, ODiff reaches maximum when [PDiff == thr * elast / 2]
  48. # ODiff = 0 when [PDiff >= thr * elast]
  49. #
  50. # Larger "thr" will result in more pixels being taken from flat area upscaled clip (Dither_resize16)
  51. # Larger "thr" will result in less pixels being taken from edge area upscaled clip (edi+Dither_resize16)
  52. # Larger "elast" will result in more pixels being blended from edge&flat area upscaled clip, for smoother merging
  53.  
  54. ###############################################################################################################################
  55. ###############################################################################################################################
  56. # Parameters for edi
  57.  
  58. alpha = Default(alpha, 0.2 )
  59. beta = Default(beta, 0.125 )
  60. gamma = Default(gamma, 20.0 )
  61. nrad = Default(nrad, 3 )
  62. mdis = Default(mdis, 40 )
  63.  
  64. ###############################################################################################################################
  65. ###############################################################################################################################
  66. # Parameters for Dither_resize16
  67.  
  68. kernel_d = Default(kernel_d, "Spline" ) # "kernelh","kernelv" of Dither_resize16 used in downscaling
  69. kernel_u = Default(kernel_u, "Spline" ) # "kernelh","kernelv" of Dither_resize16 used in upscaling
  70. f_d = Default(f_d, 1.0 ) # "fh","fv" of Dither_resize16 used in downscaling
  71. f_u = Default(f_u, 1.0 ) # "fh","fv" of Dither_resize16 used in upscaling
  72. taps = Default(taps, 32 ) # "taps" of Dither_resize16
  73. # a1, a2, a3 # "a1","a2","a3" of Dither_resize16
  74. invks_d = Default(invks_d, False ) # "invksh","invksv" of Dither_resize16 used in downscaling
  75. invks_u = Default(invks_u, False ) # "invksh","invksv" of Dither_resize16 used in upscaling
  76. invkstaps= Default(invkstaps,120 ) # "invkstaps" of Dither_resize16
  77.  
  78. noring = Default(noring, True )
  79.  
  80. resste = "Dither_resize16"
  81. resstf = noring ? "Dither_resize16nr" : "Dither_resize16"
  82.  
  83. ###############################################################################################################################
  84. ###############################################################################################################################
  85. # Post-Process
  86.  
  87. sharp = Default(sharp, 0 ) # Strength of Contra-Sharpen mod, for sharper edge. 0 means no sharpening, common value is about 100.
  88. # *Only* when {horrizontal or vertical}{scale ratio}>{ratiothr} will sharpening take effect (when edi is used for upscaling).
  89.  
  90. ###############################################################################################################################
  91. ###############################################################################################################################
  92. # Parameters for input/output
  93.  
  94. Y = Default(Y, 3 )
  95. U = Default(U, 3 )
  96. V = Default(V, 3 )
  97. lsb_in = Default(lsb_in, False ) # input clip is 16-bit stacked or not
  98. lsb = Default(lsb, False ) # output clip is 16-bit stacked or not
  99. dither = Default(dither, -1 ) # dither mode for conversion from 16-bit to 8-bit (same as "mode" in DitherPost)
  100.  
  101. sCSP = input.edi_resize16_GetCSP()
  102. Assert(sCSP=="Y8" || sCSP=="YV12" || sCSP=="YV16" || sCSP=="YV24", """edi_resize16: only accept Y8, YV12, YV16, YV24 input""")
  103.  
  104. output = Default(output, sCSP )
  105. # Output format. Possible values are:
  106. # "Y8" : Regular Y8 colorspace. Parameter "lsb" works on this output mode.
  107. # "YV12" : Regular YV12 colorspace. Parameter "lsb" works on this output mode.
  108. # "YV16" : Regular YV16 colorspace. Parameter "lsb" works on this output mode.
  109. # "YV24" : Regular YV24 colorspace. Parameter "lsb" works on this output mode.
  110. # "RGB32" : Regular RGB32 colorspace.
  111. # "RGB24" : Regular RGB24 colorspace.
  112. # "RGB48YV12": 48-bit RGB conveyed on YV12. Use it for rawvideo export only. Not suitable for display or further processing (it will look like garbage).
  113. # "RGB48Y" : 48-bit RGB. The components R, G and B are conveyed on three YV12 or Y8 (if supported) stack16 clips interleaved on a frame basis.
  114.  
  115. ###############################################################################################################################
  116.  
  117. IsY8 = sCSP == "Y8" || output == "Y8"
  118. sCSP = IsY8 ? "YV24" : sCSP
  119.  
  120. IsRGB = LeftStr(output, 3) == "RGB"
  121. oCSP = IsRGB || IsY8 ? "YV24" : output
  122.  
  123. Y = min(Y, 4)
  124. U = IsY8 ? 1 : min(U, 4)
  125. V = IsY8 ? 1 : min(V, 4)
  126. Yt = Y == 3 || Y == 4
  127. Ut = U == 3 || U == 4
  128. Vt = V == 3 || V == 4
  129. Y31 = Yt ? 3 : 1
  130. U31 = Ut ? 3 : 1
  131. V31 = Vt ? 3 : 1
  132. Y32 = Yt ? 3 : Y
  133. U32 = Ut ? 3 : U
  134. V32 = Vt ? 3 : V
  135. Y21 = Yt ? 2 : Y
  136. U21 = Ut ? 2 : U
  137. V21 = Vt ? 2 : V
  138. Y321 = Y > 1 ? 3 : Y
  139. U321 = U > 1 ? 3 : U
  140. V321 = V > 1 ? 3 : V
  141.  
  142. sw = input.Width ()
  143. sh = input.Height()
  144. sh = lsb_in ? sh/2 : sh
  145. swc = sCSP=="YV24" ? sw : sw/2
  146. shc = sCSP=="YV12" ? sh/2 : sh
  147. HD = (sw > 1024 || sh > 576) ? True : False
  148.  
  149. ###############################################################################################################################
  150.  
  151. tv_range = Default(tv_range, True ) # define source is TV Range or PC Range
  152.  
  153. cplace = Default(cplace, "MPEG2")
  154. # Placement of the chroma subsamples. Can be one of these strings:
  155. # "MPEG1": 4:2:0 subsampling used in MPEG-1. Chroma samples are located on the center of each group of 4 pixels.
  156. # "MPEG2": Subsampling used in MPEG-2 4:2:x. Chroma samples are located on the left pixel column of the group.
  157.  
  158. matrix = Default(matrix, HD ? "709" : "601")
  159. # The matrix used to convert the YUV pixels to computer RGB. Possible values are:
  160. # "601" : ITU-R BT.601 / ITU-R BT.470-2 / SMPTE 170M. For Standard Definition content.
  161. # "709" : ITU-R BT.709. For High Definition content.
  162. # "240" : SMPTE 240M
  163. # "FCC" : FCC
  164. # "YCgCo": YCgCo
  165. # When the parameter is not defined, ITU-R BT.601 and ITU-R BT.709 are automatically selected depending on the clip definition.
  166.  
  167. curve = Default(curve, "709" )
  168. # type of gamma mapping(transfer characteristic) for gamma-aware resize(only take effects for Dither_resize16 processing parts)
  169. # possible values:
  170. # "709" - ITU-R BT.709 transfer curve for digital video
  171. # "601" - ITU-R BT.601 transfer curve, same as "709"
  172. # "170" - SMPTE 170M, same as "709"
  173. # "240" - SMPTE 240M (1987)
  174. # "srgb" - sRGB curve
  175. # "2020" - ITU-R BT.2020 transfer curve, for 12-bit content. For sources of lower bitdepth, use the "709" curve.
  176. # "linear" - linear curve without gamma-aware processing
  177. curve = Yt ? curve : "linear"
  178. gcor = Default(gcor, 1.0 ) # Gamma correction, applied on the linear part.
  179.  
  180. ###############################################################################################################################
  181. ###############################################################################################################################
  182. # Parameters for scaling
  183.  
  184. ow = Default(target_width, sw)
  185. oh = Default(target_height, sh)
  186. owc = oCSP=="YV24" ? ow : ow/2
  187. ohc = oCSP=="YV12" ? oh/2 : oh
  188. Assert(!(output=="YV16" && ow!=owc*2), """edi_resize16: width of YV16 output clip must be MOD2!""")
  189. Assert(!(output=="YV12" && ow!=owc*2), """edi_resize16: width of YV12 output clip must be MOD2!""")
  190. Assert(!(output=="YV12" && oh!=ohc*2), """edi_resize16: height of YV12 output clip must be MOD2!""")
  191.  
  192. src_left = Default(src_left, 0 )
  193. src_top = Default(src_top, 0 )
  194. src_width = Default(src_width, sw)
  195. src_height = Default(src_height, sh)
  196.  
  197. ###############################################################################################################################
  198. ###############################################################################################################################
  199. # Pre-Cropping/Padding Calculation
  200.  
  201. prel = int(src_left/2) * 2
  202. pret = int(src_top /2) * 2
  203. prer = int((src_width > 0 ? -sw+src_left+src_width : src_width )/2) * 2
  204. preb = int((src_height> 0 ? -sh+src_top+src_height : src_height)/2) * 2
  205. prew = sw - prel + prer
  206. preh = sh - pret + preb
  207.  
  208. sCSP=="YV24" ? \
  209. Eval("""
  210. swmod2 = sw /2*2 == sw
  211. pwmod2 = prew/2*2 == prew
  212. wpre = prew < sw
  213. prel = wpre ? prel : 0
  214. prer = wpre ? pwmod2 ? prer : prer + 1 : swmod2 ? 0 : 1
  215. prew = sw - prel + prer
  216. wpre = prew < sw || !swmod2
  217. """) : \
  218. Eval("""
  219. swmod4 = sw /4*4 == sw
  220. pwmod4 = prew/4*4 == prew
  221. wpre = prew < sw
  222. prel = wpre ? prel : 0
  223. prer = wpre ? pwmod4 ? prer : prer + 2 : swmod4 ? 0 : 2
  224. prew = sw - prel + prer
  225. wpre = prew < sw || !swmod4
  226. """)
  227.  
  228. sCSP=="YV12" ? \
  229. Eval("""
  230. shmod4 = sh /4*4 == sh
  231. phmod4 = preh/4*4 == preh
  232. hpre = preh < sh
  233. pret = hpre ? pret : 0
  234. preb = hpre ? phmod4 ? preb : preb + 2 : shmod4 ? 0 : 2
  235. preh = sh - pret + preb
  236. hpre = preh < sh || !shmod4
  237. """) : \
  238. Eval("""
  239. shmod2 = sh /2*2 == sh
  240. phmod2 = preh/2*2 == preh
  241. hpre = preh < sh
  242. pret = hpre ? pret : 0
  243. preb = hpre ? phmod2 ? preb : preb + 1 : shmod2 ? 0 : 1
  244. preh = sh - pret + preb
  245. hpre = preh < sh || !shmod2
  246. """)
  247.  
  248. src_width = src_width <=0 ? +sw-src_left+src_width : src_width
  249. src_height = src_height<=0 ? +sh-src_top+src_height : src_height
  250. src_left = wpre ? src_left-prel : src_left
  251. src_top = hpre ? src_top -pret : src_top
  252.  
  253. src_leftc = sCSP=="YV24" ? src_left : src_left /2.
  254. src_topc = sCSP=="YV12" ? src_top /2. : src_top
  255. src_widthc = sCSP=="YV24" ? src_width : src_width /2.
  256. src_heightc = sCSP=="YV12" ? src_height/2. : src_height
  257.  
  258. ###############################################################################################################################
  259. ###############################################################################################################################
  260. # Scaling Ratio Calculation
  261.  
  262. ratiothr = Default(ratiothr, 1.000) # When scale ratio is larger than ratiothr, use edi+Dither_resize16 upscale method instead of pure Dither_resize16.
  263. # when horizontal/vertical scale ratio > "ratiothr", we assume it's upscaling
  264. # when horizontal/vertical scale ratio <= "ratiothr", we assume it's downscaling
  265.  
  266. ###############################################################################################################################
  267.  
  268. yhratio = float(ow ) / float(src_width )
  269. yvratio = float(oh ) / float(src_height )
  270. chratio = float(owc) / float(src_widthc )
  271. cvratio = float(ohc) / float(src_heightc)
  272.  
  273. enable = yhratio!=1 || yvratio!=1 || chratio!=1 || cvratio!=1 ||
  274. \ src_width !=int(src_width ) || src_height !=int(src_height ) || src_widthc !=int(src_widthc ) || src_heightc!=int(src_heightc) ||
  275. \ src_left !=int(src_left ) || src_top !=int(src_top ) || src_leftc !=int(src_leftc ) || src_topc !=int(src_topc )
  276.  
  277. yhct = yhratio>ratiothr ? Ceil( log(yhratio/ratiothr) / log(2) ) : 0
  278. yhrf = int(Pow(2, yhct))
  279. yrhratio = yhratio/yhrf
  280. yvct = yvratio>ratiothr ? Ceil( log(yvratio/ratiothr) / log(2) ) : 0
  281. yvrf = int(Pow(2, yvct))
  282. yrvratio = yvratio/yvrf
  283.  
  284. chct = chratio>ratiothr ? Ceil( log(chratio/ratiothr) / log(2) ) : 0
  285. chrf = int(Pow(2, chct))
  286. crhratio = chratio/chrf
  287. cvct = cvratio>ratiothr ? Ceil( log(cvratio/ratiothr) / log(2) ) : 0
  288. cvrf = int(Pow(2, cvct))
  289. crvratio = cvratio/cvrf
  290.  
  291. nonenny = yhct<=0 && yvct<=0
  292. nonennc = chct<=0 && cvct<=0
  293. nonenn = nonenny || nonennc
  294.  
  295. Ynnt = Yt&&!nonenny
  296. Unnt = Ut&&!nonennc
  297. Vnnt = Vt&&!nonennc
  298. Ynn31 = Ynnt ? 3 : 1
  299. Unn31 = Unnt ? 3 : 1
  300. Vnn31 = Vnnt ? 3 : 1
  301. Ynn = Yt&&nonenny ? 2 : Y
  302. Unn = Ut&&nonennc ? 2 : U
  303. Vnn = Vt&&nonennc ? 2 : V
  304.  
  305. nnt = Ynnt || Unnt || Vnnt
  306. mixed = !nnt || !enable ? False : mixed
  307.  
  308. ###############################################################################################################################
  309. ###############################################################################################################################
  310. # Shift Calculation
  311.  
  312. yhshift = yhrf>=2 ? 0.5 : 0 # luma horizontal shift introduced by edi_resize16_rpow2 (edge area resize)
  313. yvshift = yvrf>=2 ? 0.5 : 0 # luma vertical shift introduced by edi_resize16_rpow2 (edge area resize)
  314. yhfix = -yhshift # value to fix luma horizontal shift introduced by edi_resize16_rpow2 (edge area resize)
  315. yvfix = -yvshift # value to fix luma vertical shift introduced by edi_resize16_rpow2 (edge area resize)
  316.  
  317. chshift = oCSP=="YV24" ? sCSP=="YV24" ? chrf>=2 ? 0.50 : 0
  318. \ : cplace=="MPEG1" ? chrf>=2 ? 0.50 : 0
  319. \ : chrf>=2 ? 0.50-chrf/4. : -0.25
  320. \ : sCSP=="YV24" ? cplace=="MPEG1" ? chrf>=2 ? 0.50 : 0
  321. \ : chrf>=2 ? 0.75 : 0.25
  322. \ : cplace=="MPEG1" ? chrf>=2 ? 0.50 : 0
  323. \ : chrf>=2 ? 0.75-chrf/4. : 0
  324. # (chrf/2-0.5)/2-(chrf/2-1)
  325. # chroma horizontal shift introduced by edi_resize16_rpow2 (edge area resize)
  326. cvshift = cvrf>=2 ? 0.5 : 0 # chroma vertical shift introduced by edi_resize16_rpow2 (edge area resize)
  327. chfix = -chshift # value to fix chroma horizontal shift introduced by edi_resize16_rpow2 (edge area resize)
  328. cvfix = -cvshift # value to fix chroma vertical shift introduced by edi_resize16_rpow2 (edge area resize)
  329.  
  330. cphfixe = oCSP=="YV24" ? 0
  331. \ : cplace=="MPEG1" ? 0
  332. \ : 0.25-0.25/crhratio
  333. # value to fix chroma horizontal shift introduced by Dither_resize16 after edi_resize16_rpow2 processing (edge area resize)
  334.  
  335. cphfix = oCSP=="YV24" ? sCSP=="YV24" ? 0
  336. \ : cplace=="MPEG1" ? 0
  337. \ : 0.25
  338. \ : sCSP=="YV24" ? cplace=="MPEG1" ? 0
  339. \ : -0.5
  340. \ : cplace=="MPEG1" ? 0
  341. \ : 0.25-0.25/chratio
  342. # value to fix chroma horizontal shift introduced by Dither_resize16 (flat area resize)
  343. # this fixing only take effects when Y/U/V is processed separately(as Y8) in flat area scaling (colorspace transferring)
  344. # Dither_resize16 accepts "cplace" and correctly processes chroma placement for YUV input
  345.  
  346. ###############################################################################################################################
  347. ###############################################################################################################################
  348. # Pre-Process
  349.  
  350. input = wpre || hpre ? lsb_in ? input.Dither_resize16(wpre?prew:sw, hpre?preh:sh, wpre?prel:0, hpre?pret:0,
  351. \ wpre?prew:sw, hpre?preh:sh, kernel="point")
  352. \ : input.PointResize(wpre?prew:sw, hpre?preh:sh, wpre?prel:0, hpre?pret:0, wpre?prew:sw, hpre?preh:sh)
  353. \ : input
  354. input8 = lsb_in ? input.edi_resize16_Down8(tv_range, Yt, Ut, Vt, mixed ? -1 : dither) : input
  355. input16 = lsb_in ? input : input.edi_resize16_U16(tv_range, True, True, True)
  356.  
  357. ###############################################################################################################################
  358. ###############################################################################################################################
  359. # edi upscale for edge area
  360.  
  361. !(enable && nnt) ? NOP() : \
  362. yhct==chct && yvct==cvct && sCSP=="YV12" ? \
  363. Eval("""
  364. edgenn = input8.edi_resize16_rpow2(yvct, yhct, 1, 1, Yt, Ut, Vt, alpha, beta, gamma, nrad, mdis)
  365. edgennY = sharp>0 ? edgenn.CSmod(chroma=False, ss_w=1.00, ss_h=1.00, preblur=-4, Smethod=1, kernel=6, strength=sharp, Soothe=-1)
  366. \ .edi_resize16_U16(tv_range, Yt, Ut, Vt)
  367. \ : edgenn.edi_resize16_U16(tv_range, Yt, Ut, Vt)
  368. edgennU = edgennY.UToY8()
  369. edgennV = edgennY.VToY8()
  370. edgennY = edgennY.ConvertToY8()
  371. """) : \
  372. Eval("""
  373. edgennY = !Ynnt ? NOP()
  374. \ : sharp>0 ? input8.ConvertToYV12().edi_resize16_rpow2(yvct, yhct, 1, 1, Yt, False, False,
  375. \ alpha, beta, gamma, nrad, mdis)
  376. \ .CSmod(chroma=False, ss_w=1.00, ss_h=1.00, preblur=-4, Smethod=1, kernel=6, strength=sharp, Soothe=-1)
  377. \ .ConvertToY8 ().edi_resize16_U16(tv_range, Yt, False, False)
  378. \ : input8.ConvertToYV12().edi_resize16_rpow2(yvct, yhct, 1, 1, Yt, False, False,
  379. \ alpha, beta, gamma, nrad, mdis)
  380. \ .ConvertToY8 ().edi_resize16_U16(tv_range, Yt, False, False)
  381. edgennU = !Unnt ? NOP()
  382. \ : input8.UToY ().edi_resize16_rpow2(cvct, chct, 1, 1, Ut, False, False,
  383. \ alpha, beta, gamma, nrad, mdis)
  384. \ .ConvertToY8().edi_resize16_U16(tv_range, Ut, False, False)
  385. edgennV = !Vnnt ? NOP()
  386. \ : input8.VToY ().edi_resize16_rpow2(cvct, chct, 1, 1, Vt, False, False,
  387. \ alpha, beta, gamma, nrad, mdis)
  388. \ .ConvertToY8().edi_resize16_U16(tv_range, Vt, False, False)
  389. """)
  390.  
  391. ###############################################################################################################################
  392. ###############################################################################################################################
  393. # edge area resize & fix center shift
  394.  
  395. yrh = yrhratio>ratiothr
  396. yrv = yrvratio>ratiothr
  397. crh = crhratio>ratiothr
  398. crv = crvratio>ratiothr
  399.  
  400. !(enable && nnt) ? NOP() : \
  401. Eval("""
  402. edgennY = !Ynnt ? NOP()
  403. \ : curve=="linear" ? edgennY
  404. \ : edgennY.Dither_y_gamma_to_linear(tv_range, tv_range, curve, u=1, v=1, gcor=gcor)
  405. edgeY = !Ynnt ? input16.BlankClip(width=ow , height=oh *2, pixel_type="Y8", color_yuv=$008080)
  406. \ : edgennY.""" + resste + """(ow , oh , src_left *yhrf+yhfix , src_top *yvrf+yvfix,
  407. \ src_width *yhrf, src_height *yvrf, y=Y31, u=1 , v=1 ,
  408. \ kernelh=yrh?kernel_u:kernel_d, kernelv=yrv?kernel_u:kernel_d,
  409. \ fh=yrh?f_u:f_d, fv=yrv?f_u:f_d,
  410. \ invksh=yrh?invks_u:invks_d, invksv=yrv?invks_u:invks_d,
  411. \ taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
  412. \ )
  413. edgeY = !Ynnt ? edgeY
  414. \ : curve=="linear" ? edgeY
  415. \ : edgeY .Dither_y_linear_to_gamma(tv_range, tv_range, curve, u=1, v=1, gcor=gcor)
  416.  
  417. edgeU = !Unnt ? input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080)
  418. \ : edgennU.""" + resste + """(owc, ohc, src_leftc*chrf+chfix+cphfixe, src_topc*cvrf+cvfix,
  419. \ src_widthc*chrf, src_heightc*cvrf, y=U31, u=1 , v=1 ,
  420. \ kernelh=crh?kernel_u:kernel_d, kernelv=crv?kernel_u:kernel_d,
  421. \ fh=crh?f_u:f_d, fv=crv?f_u:f_d,
  422. \ invksh=crh?invks_u:invks_d, invksv=crv?invks_u:invks_d,
  423. \ taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
  424. \ )
  425.  
  426. edgeV = !Vnnt ? input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080)
  427. \ : edgennV.""" + resste + """(owc, ohc, src_leftc*chrf+chfix+cphfixe, src_topc*cvrf+cvfix,
  428. \ src_widthc*chrf, src_heightc*cvrf, y=V31, u=1 , v=1 ,
  429. \ kernelh=crh?kernel_u:kernel_d, kernelv=crv?kernel_u:kernel_d,
  430. \ fh=crh?f_u:f_d, fv=crv?f_u:f_d,
  431. \ invksh=crh?invks_u:invks_d, invksv=crv?invks_u:invks_d,
  432. \ taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
  433. \ )
  434.  
  435. edge16 = IsY8 ? edgeY : YToUV(edgeU, edgeV, edgeY)
  436. """)
  437.  
  438. ###############################################################################################################################
  439. ###############################################################################################################################
  440. # flat area resize
  441.  
  442. yh = yhratio>ratiothr
  443. yv = yvratio>ratiothr
  444. ch = chratio>ratiothr
  445. cv = cvratio>ratiothr
  446.  
  447. !(enable && (mixed || !(Ynnt && Unnt && Vnnt))) ? NOP() : \
  448. yhratio==chratio && yvratio==cvratio && (!mixed || (Ynnt && Unnt && Vnnt)) ? \
  449. Eval("""
  450. flat16 = curve=="linear" ? input16
  451. \ : input16.Dither_y_gamma_to_linear(tv_range, tv_range, curve, u=U21, v=V21, gcor=gcor)
  452. flat16 = flat16. """ + resstf + """
  453. \ (ow , oh , src_left , src_top ,
  454. \ src_width , src_height , y=Y32, u=U32, v=V32, cplace=cplace,
  455. \ kernelh=yh?kernel_u:kernel_d, kernelv=yv?kernel_u:kernel_d,
  456. \ fh=yh?f_u:f_d, fv=yv?f_u:f_d,
  457. \ invksh=yh?invks_u:invks_d, invksv=yv?invks_u:invks_d,
  458. \ taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
  459. \ )
  460. flat16 = curve=="linear" ? flat16
  461. \ : flat16 .Dither_y_linear_to_gamma(tv_range, tv_range, curve, u=U21, v=V21, gcor=gcor)
  462. """) : \
  463. Eval("""
  464. flatY = curve=="linear" ? input16.ConvertToY8() :
  465. \ (mixed || !Ynnt) && Yt ? input16.ConvertToY8().Dither_y_gamma_to_linear(tv_range, tv_range, curve, u=1 , v=1 , gcor=gcor)
  466. \ : input16.ConvertToY8()
  467. flatY = (mixed || !Ynnt) && Yt ? flatY .""" + resstf + """
  468. \ (ow , oh , src_left , src_top ,
  469. \ src_width , src_height , y=Y32, u=1 , v=1 ,
  470. \ kernelh=yh?kernel_u:kernel_d, kernelv=yv?kernel_u:kernel_d,
  471. \ fh=yh?f_u:f_d, fv=yv?f_u:f_d,
  472. \ invksh=yh?invks_u:invks_d, invksv=yv?invks_u:invks_d,
  473. \ taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
  474. \ )
  475. \ : input16.BlankClip(width=ow , height=oh *2, pixel_type="Y8", color_yuv=$008080)
  476. flatY = curve=="linear" ? flatY :
  477. \ (mixed || !Ynnt) && Yt ? flatY .Dither_y_linear_to_gamma(tv_range, tv_range, curve, u=1 , v=1 , gcor=gcor)
  478. \ : flatY
  479.  
  480. flatU = (mixed || !Unnt) && Ut ? input16.UToY8().""" + resstf + """
  481. \ (owc, ohc, src_leftc +cphfix , src_topc ,
  482. \ src_widthc , src_heightc , y=U32, u=1 , v=1 ,
  483. \ kernelh=ch?kernel_u:kernel_d, kernelv=cv?kernel_u:kernel_d,
  484. \ fh=ch?f_u:f_d, fv=cv?f_u:f_d,
  485. \ invksh=ch?invks_u:invks_d, invksv=cv?invks_u:invks_d,
  486. \ taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
  487. \ )
  488. \ : input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080)
  489.  
  490. flatV = (mixed || !Vnnt) && Vt ? input16.VToY8().""" + resstf + """
  491. \ (owc, ohc, src_leftc +cphfix , src_topc ,
  492. \ src_widthc , src_heightc , y=V32, u=1 , v=1 ,
  493. \ kernelh=ch?kernel_u:kernel_d, kernelv=cv?kernel_u:kernel_d,
  494. \ fh=ch?f_u:f_d, fv=cv?f_u:f_d,
  495. \ invksh=ch?invks_u:invks_d, invksv=cv?invks_u:invks_d,
  496. \ taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
  497. \ )
  498. \ : input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080)
  499.  
  500. flat16 = IsY8 ? flatY : YToUV(flatU, flatV, flatY)
  501. """)
  502.  
  503. ###############################################################################################################################
  504. ###############################################################################################################################
  505. # Threshold Merging & Output
  506.  
  507. enable ? \
  508. Eval("""
  509. merge16 = !nnt ? flat16
  510. \ : mixed ? Dither_limit_dif16(flat16, edge16, thr=thr, elast=elast, y=Ynn, u=Unn, v=Vnn)
  511. \ : Ynnt==Unnt && Unnt==Vnnt || IsY8 ? edge16
  512. \ : mt_lutxy(edge16, flat16, Y=Yt?Ynnt?2:4:1, U=Ut?Unnt?2:4:1, V=Vt?Vnnt?2:4:1)
  513. merge16 = IsY8 ? output=="Y8" ? merge16.ConvertToY8() : Eval("merge16.ConvertTo"+oCSP).Dither_lut16(Y=2, U=-32768, V=-32768) : merge16
  514.  
  515. final = IsRGB ? merge16.Dither_convert_yuv_to_rgb(matrix=matrix, tv_range=tv_range, lsb_in=True, mode=dither, output=output)
  516. \ : lsb ? merge16
  517. \ : merge16.edi_resize16_Down8(tv_range, True, !IsY8, !IsY8, dither)
  518. """) : \
  519. Eval("""
  520. shift16 = input16.Dither_resize16(ow, oh, src_left, src_top, src_width, src_height, kernel="point", y=Y, u=U, v=V)
  521. shift16 = IsY8 ? output=="Y8" ? merge16.ConvertToY8() : Eval("shift16.ConvertTo"+oCSP).Dither_lut16(Y=2, U=-32768, V=-32768) : shift16
  522.  
  523. final = IsRGB ? shift16.Dither_convert_yuv_to_rgb(matrix=matrix, tv_range=tv_range, lsb_in=True, mode=dither, output=output)
  524. \ : lsb ? shift16
  525. \ : shift16.edi_resize16_Down8(tv_range, True, !IsY8, !IsY8, dither)
  526. """)
  527.  
  528.  
  529. return final
  530. }
  531.  
  532.  
  533. Function edi_resize16_rpow2(clip input, int "vct", int "hct", int "vfield", int "hfield", bool "Y", bool "U", bool "V",
  534. \ float "alpha", float "beta", float "gamma", int "nrad", int "mdis", bool "honly")
  535. {
  536. vct = Default(vct, 1 )
  537. hct = Default(hct, 1 )
  538. vfield = Default(vfield, 1 )
  539. hfield = Default(hfield, 1 )
  540. Y = Default(Y, True )
  541. U = Default(U, False )
  542. V = Default(V, False )
  543. honly = Default(honly, False )
  544.  
  545.  
  546. input
  547.  
  548. hct >= 1 ? \
  549. Eval("""
  550. (honly) ? last
  551. \ : Eval(" try { fturnright(chroma=U||V) } catch(error_msg) { TurnRight() } ")
  552. eedi3(hfield, True, Y, U, V, alpha, beta, gamma, nrad, mdis, sclip=nnedi3 (field=hfield,dh=true,nsize=3,nns=4,qual=2,etype=1,pscrn=4,Y=Y,U=U,V=V)).repair (nnedi3 (field=hfield,dh=true,nsize=3,nns=4,qual=2,etype=1,pscrn=4,Y=Y,U=U,V=V),mode=16)
  553.  
  554. hct = hct - 1
  555. honly = hct >= 1
  556. hfield = 0
  557.  
  558. (honly) ? last
  559. \ : Eval(" try { fturnleft (chroma=U||V) } catch(error_msg) { TurnLeft () } ")
  560. """) : NOP()
  561.  
  562. vct >= 1 && !honly ? \
  563. Eval("""
  564. eedi3(vfield, True, Y, U, V, alpha, beta, gamma, nrad, mdis, sclip=nnedi3 (field=vfield,dh=true,nsize=3,nns=4,qual=2,etype=1,pscrn=4,Y=Y,U=U,V=V)).repair (nnedi3 (field=vfield,dh=true,nsize=3,nns=4,qual=2,etype=1,pscrn=4,Y=Y,U=U,V=V),mode=16)
  565.  
  566. vct = vct - 1
  567. vfield = 0
  568. """) : NOP()
  569.  
  570.  
  571. return Y||U||V ? vct <= 0 && hct <= 0 ? last
  572. \ : last.edi_resize16_rpow2(vct, hct, vfield, hfield, Y, U, V, alpha, beta, gamma, nrad, mdis, honly)
  573. \ : input
  574. }
  575.  
  576.  
  577. Function edi_resize16_GetCSP(clip c)
  578. {
  579. return c.IsPlanar ? c.IsYV12 ? "YV12" :
  580. \ c.IsYV16 ? "YV16" :
  581. \ c.IsYV24 ? "YV24" : c.GetCSP_Y8_YV411 :
  582. \ c.IsYUY2 ? "YUY2" :
  583. \ c.IsRGB32 ? "RGB32" :
  584. \ c.IsRGB24 ? "RGB24" : "Unknown"
  585.  
  586.  
  587. Function GetCSP_Y8_YV411(clip c) {
  588. try {
  589. c.UtoY
  590. csp = "YV411"
  591. } catch ( error_msg ) {
  592. csp = "Y8"
  593. }
  594. return csp
  595. }
  596. }
  597.  
  598. /*
  599. Function edi_resize16_RemoveGrain(clip input, int "mode", int "modeU", int "modeV")
  600. {
  601. mode = Default(mode, 1 )
  602. modeU = Default(modeU, mode )
  603. modeV = Default(modeV, modeU )
  604.  
  605.  
  606. iCSP = input.edi_resize16_GetCSP()
  607. isYV12 = iCSP == "YV12"
  608. isY8 = iCSP == "Y8"
  609.  
  610. sw = input.Width ()
  611. sh = input.Height()
  612. wmod4 = sw/4*4 == sw
  613. hmod4 = sh/4*4 == sh
  614. mod4 = wmod4 && hmod4
  615. padw = wmod4 ? sw : (sw/4+1)*4
  616. padh = hmod4 ? sh : (sh/4+1)*4
  617.  
  618.  
  619. input_m4 = mod4 ? input : input.PointResize(padw, padh, 0, 0, padw, padh)
  620.  
  621. return isYV12 ? input.RemoveGrain(mode, modeU, modeV)
  622. \ : isY8 ? input_m4.ConvertToYV12().RemoveGrain(mode , -1, -1).ConvertToY8()
  623. \ .Crop(0, 0, sw, sh)
  624. \ : YToUV(input_m4.UToY() .RemoveGrain(modeU, -1, -1),
  625. \ input_m4.VToY() .RemoveGrain(modeV, -1, -1),
  626. \ input_m4.ConvertToYV12().RemoveGrain(mode , -1, -1))
  627. \ .Crop(0, 0, sw, sh)
  628. }
  629. */
  630.  
  631. Function edi_resize16_U16(clip c, bool "tv_range", bool "Y", bool "U", bool "V")
  632. {
  633. tv_range = Default(tv_range, True ) # define if input clip is of tv range(limited range)
  634. Y = Default(Y, True )
  635. U = Default(U, True )
  636. V = Default(V, True )
  637.  
  638.  
  639. yExpr = "0-0;255-65535;65535-65535"
  640. cExpr = "0-0;128-32768;255-65535;65535-65535"
  641. DfExpr = "0-0;65535-65535"
  642. Yexpr = Y ? yExpr : DfExpr
  643. Uexpr = U ? cExpr : DfExpr
  644. Vexpr = V ? cExpr : DfExpr
  645.  
  646. up = tv_range ? c.Dither_convert_8_to_16()
  647. \ : Y||U||V ? StackVertical(c.Dither_gen_null_lsb(), c).SmoothCurve16(Ycurve=Yexpr, Ucurve=Uexpr, Vcurve=Vexpr, mode=0,
  648. \ limiter=False, TVrange=0, interp=0, dither=-1)
  649. \ : StackVertical(c.Dither_gen_null_lsb(), c)
  650.  
  651.  
  652. return up
  653. }
  654.  
  655.  
  656. Function edi_resize16_Down8(clip c, bool "tv_range", bool "Y", bool "U", bool "V", int "dither", int "interp", bool "HQ")
  657. {
  658. tv_range = Default(tv_range, True ) # define if input clip is of tv range(limited range)
  659. interp = Default( interp, 0 ) # use interp or not for SmoothCurve/SmoothCurve16
  660. HQ = Default( HQ, True ) # enable high quality interpolation (slower)
  661. dither = tv_range ? Default(dither, -1) : Default(dither, -1) # dither mode
  662. Y = Default(Y, True )
  663. U = Default(U, True )
  664. V = Default(V, True )
  665.  
  666.  
  667. Assert(dither>=-1 && dither<=100 , """edi_resize16_Down8: "dither" ranges from -1 to 100!""")
  668.  
  669. yExpr = "0-0;65535-255"
  670. cExpr = "0-0;32768-128;65535-255"
  671. DfExpr = "0-0;65535-65535"
  672. Yexpr = Y ? yExpr : DfExpr
  673. Uexpr = U ? cExpr : DfExpr
  674. Vexpr = V ? cExpr : DfExpr
  675.  
  676. sDown = tv_range ? NOP()
  677. \ : c.SmoothCurve16(Ycurve=Yexpr, Ucurve=Uexpr, Vcurve=Vexpr, mode=0, interp=interp, HQ=HQ,
  678. \ dither=dither, limiter=False, TVrange=0)
  679.  
  680. down = tv_range ? c.DitherPost(mode=dither, y=Y?3:1, u=U?3:1, v=V?3:1)
  681. \ : Y||U||V ? sDown.Dither_get_lsb()
  682. \ : c.Dither_get_msb()
  683.  
  684.  
  685. return down
  686. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement