Advertisement
Guest User

ResizeX

a guest
Jun 30th, 2017
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ResizeX v1.0.1 mod 4
  2.  
  3. ResizeX is a wrapper function for AviSynth's internal resizers and Dither_resize16
  4. that corrects for the chroma shift caused by the internal resizers when they're
  5. used on horizontally subsampled chroma with MPEG2 placement.
  6. If either the lsb_in or lsb parameters are set to true, Dither_resize16 is used.
  7. If both are false, AviSynth's internal resizers are used.
  8.  
  9. All AviSynth 2.6 colorspaces are supported. However, to use Dither_resize16 on RGB, the input
  10. must first be split into a Y8 clip containing the individual red, green, and blue channels.
  11. This can be done with the following script:
  12.  
  13.  
  14.      Interleave(ShowRed("Y8"),ShowGreen("Y8"),ShowBlue("Y8"))
  15.      ResizeX(target_width,target_height, lsb=true)
  16.      DitherPost()
  17.      MergeRGB(SelectEvery(3,0),SelectEvery(3,1),SelectEvery(3,2))
  18.  
  19.  
  20. Internal and Dither_resize16 kernels: "Bicubic", "Bilinear", "Blackman", "Gauss", "Lanczos",
  21.                                       "Point", "Sinc", "Spline16", "Spline36", "Spline64"
  22.  
  23. Dither_resize16 only kernels:         "Blackmanminlobe", "Impulse", "Rect" or "Box", "Spline"
  24.  
  25. Bicubic kernel presets:               "Catmull-Rom" or "CatRom", "Hermite",
  26.                                       "Mitchell-Netravali" or "Mitchell", "Robidoux", "SoftCubic"
  27.  
  28. The number of taps for the Blackman, Blackmanminlobe, Lanczos, Sinc, and Spline kernels
  29. can be set with the taps parameter or with a number after the kernel name.
  30. Using the latter method overrides the taps parameter.
  31.  
  32. The b and c values for the Bicubic kernel can be set with the a1 and a2 parameters, respectively.
  33. If a Bicubic preset is used, it will override a1 and a2.
  34. The p value for the Gauss kernel can be set with a1.
  35.  
  36. The softness of the SoftCubic preset can be set with a number after it ranging from 0 through 100.
  37. The default is 75.
  38. */
  39.  
  40. function ResizeX(clip input, int target_width, int target_height, float "src_left", float "src_top",
  41. \                float "src_width", float "src_height", string "kernel", int "taps", float "a1", float "a2",
  42. \                string "cplace", bool "luma", bool "chroma", bool "lsb_in", bool "lsb", bool "mt", string "mt_params") {
  43.  
  44. lsb_in     = Default(lsb_in, false)
  45. lsb        = Default(lsb, false)
  46.  
  47. iw = input.Width()
  48. ih = lsb_in ? input.Height()/2 : input.Height()
  49.  
  50. src_left   = Default(src_left, 0)
  51. src_top    = Default(src_top, 0)
  52. src_width  = Default(src_width, iw)
  53. src_height = Default(src_height, ih)
  54. kernel     = Default(kernel, "Spline36")
  55. cplace     = Default(cplace, "MPEG2")
  56. luma       = Default(luma, true)
  57. chroma     = Default(chroma, true)
  58. mt         = Default(mt, true)
  59. mt_params = Default(mt_params,      "")
  60.  
  61. sispmt  = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  62.  
  63. Assert(target_width  > 0, "ResizeX: target width must be greater than 0")
  64. Assert(target_height > 0, "ResizeX: target height must be greater than 0")
  65. Assert(cplace == "MPEG1" || cplace == "MPEG2", "ResizeX: cplace must be MPEG1 or MPEG2")
  66. lsb_native = sispmt ? !(Input.BitsPerComponent() > 8 && (lsb)) : true
  67. sispmt ? Assert(lsb_native, "lsb hack is not Compatible with native high bit depth" ) : nop()
  68. sispmt ? Assert(!(Input.isYUVA() && lsb), "lsb hack is not Compatible with YUVA" ) : nop()
  69.  
  70. # Set correct src_width and src_height values if the input values are zero or negative
  71. src_width  = src_width  == 0 ? iw
  72. \          : src_width  <  0 ? iw-src_left+src_width
  73. \                            : src_width
  74. src_height = src_height == 0 ? ih
  75. \          : src_height <  0 ? ih-src_top+src_height
  76. \                            : src_height
  77.  
  78. # Get the input clip's colorspace
  79. csp = input.PixelType()
  80.  
  81. sislumaonly = sispmt ? input.isy() : input.isy8()
  82.  
  83. chroma = sislumaonly ? false : chroma
  84.  
  85. chr420  = sispmt ? input.is420() : input.isyv12()
  86. chr422  = sispmt ? input.is422() : input.isYV16()
  87. chr444  = sispmt ? input.is444() : input.isYV24()
  88.  
  89. Assert(csp != "RGB24" && csp != "RGB32" || !lsb_in && !lsb, "ResizeX: lsb_in and lsb must be false for RGB input")
  90.  
  91. # Check for subsampled chroma
  92. hssc12 = chr420 || chr422 || csp == "YUY2"
  93. hssc14 = csp == "YV411"
  94. vssc12 = chr420
  95.  
  96. Assert(!hssc12 || target_width%2  == 0, "ResizeX: target width of "+csp+" must be a multiple of 2")
  97. Assert(!hssc14 || target_width%4  == 0, "ResizeX: target width of "+csp+" must be a multiple of 4")
  98. Assert(!vssc12 || target_height%2 == 0, "ResizeX: target height of "+csp+" must be a multiple of 2")
  99.  
  100. # Set chroma target and src values based on the subsampling ratios
  101. target_width_c  = hssc12 ? target_width/2  : hssc14 ? target_width/4 : target_width
  102. target_height_c = vssc12 ? target_height/2 : target_height
  103. src_left_c      = hssc12 ? src_left/2.0    : hssc14 ? src_left/4.0   : src_left
  104. src_top_c       = vssc12 ? src_top/2.0     : src_top
  105. src_width_c     = hssc12 ? src_width/2.0   : hssc14 ? src_width/4.0  : src_width
  106. src_height_c    = vssc12 ? src_height/2.0  : src_height
  107.  
  108. # Add the MPEG2 chroma shift correction to the src_left_c value
  109. MPEG2shift = hssc12 ? 0.25*(1.0-Float(src_width_c)/Float(target_width_c))
  110. \          : hssc14 ? 0.375*(1.0-Float(src_width_c)/Float(target_width_c))
  111. \                   : 0
  112. src_left_c  = cplace == "MPEG2" && kernel.LeftStr(5) != "Point" ? src_left_c+MPEG2shift
  113. \                                                               : src_left_c
  114.  
  115. # Remove "Resize" from the end of the kernel string if present
  116. kernel = kernel.RightStr(6) == "Resize" ? kernel.LeftStr(kernel.StrLen()-6) : kernel
  117.  
  118. # Support the Dither_resize16 kernel name variants when resizing 8-bit
  119. kernel = kernel == "Linear"   ? "Bilinear"
  120. \      : kernel == "Cubic"    ? "Bicubic"
  121. \      : kernel == "Gaussian" ? "Gauss"
  122. \                             : kernel
  123.  
  124. # Dither_resize16 kernels without an internal equivalent can't be used without lsb_in or lsb being true
  125. Assert(lsb_in || lsb || kernel == "Spline16" || kernel == "Spline36" || kernel == "Spline64" ||
  126. \      kernel != "Rect" && kernel != "Box" && kernel != "Blackmanminlobe" && kernel.LeftStr(6) != "Spline" && kernel != "Impulse",
  127. \      "ResizeX: Rect, Box, Blackmanminlobe, Spline, and Impulse kernels"+chr(10)+
  128. \      "are available only when resizing 16-bit (lsb_in or lsb must be true)")
  129.  
  130. # Get the taps value from the kernel string if present (overrides the parameter)
  131. taps = kernel.LeftStr(6)  == "Spline"          && kernel != "Spline16" && kernel != "Spline36" && kernel != "Spline64" &&
  132. \                                                 kernel.StrLen() >  6 ? kernel.RightStr(kernel.StrLen()-6).Value().Int()
  133. \    : kernel.LeftStr(7)  == "Lanczos"         && kernel.StrLen() >  7 ? kernel.RightStr(kernel.StrLen()-7).Value().Int()
  134. \    : kernel.LeftStr(8)  == "Blackman"        && kernel.LeftStr(15) != "Blackmanminlobe" &&
  135. \                                                 kernel.StrLen() >  8 ? kernel.RightStr(kernel.StrLen()-8).Value().Int()
  136. \    : kernel.LeftStr(15) == "Blackmanminlobe" && kernel.StrLen() > 15 ? kernel.RightStr(kernel.StrLen()-15).Value().Int()
  137. \    : kernel.LeftStr(4)  == "Sinc"            && kernel.StrLen() >  4 ? kernel.RightStr(kernel.StrLen()-4).Value().Int()
  138. \                                                                      : taps
  139.  
  140. # Remove the taps value from the kernel string if present
  141. kernel = kernel.LeftStr(6)  == "Spline"          && kernel != "Spline16" && kernel != "Spline36" && kernel != "Spline64" &&
  142. \                                                   kernel.StrLen() >  6 ? kernel.LeftStr(6)
  143. \      : kernel.LeftStr(7)  == "Lanczos"         && kernel.StrLen() >  7 ? kernel.LeftStr(7)
  144. \      : kernel.LeftStr(8)  == "Blackman"        && kernel.LeftStr(15) != "Blackmanminlobe" &&
  145. \                                                   kernel.StrLen() >  8 ? kernel.LeftStr(8)
  146. \      : kernel.LeftStr(15) == "Blackmanminlobe" && kernel.StrLen() > 15 ? kernel.LeftStr(15)
  147. \      : kernel.LeftStr(4)  == "Sinc"            && kernel.StrLen() >  4 ? kernel.LeftStr(4)
  148. \                                                                        : kernel
  149.  
  150. # Set the a1 and a2 values for bicubic presets (overrides the parameters)
  151.     kernel == "Catmull-Rom" || kernel == "CatRom" ?
  152. \   Eval("""
  153.         a1     = 0.0
  154.         a2     = 0.5
  155.         kernel = "Bicubic"
  156.         """)
  157. \ : kernel == "Hermite" ?
  158. \   Eval("""
  159.         a1     = 0.0
  160.         a2     = 0.0
  161.         kernel = "Bicubic"
  162.         """)
  163. \ : kernel == "Mitchell-Netravali" || kernel == "Mitchell" ?
  164. \   Eval("""
  165.         a1     = 1.0/3.0
  166.         a2     = 1.0/3.0
  167.         kernel = "Bicubic"
  168.         """)
  169. \ : kernel == "Robidoux" ?
  170. \   Eval("""
  171.         a1     = 0.3782
  172.         a2     = 0.3109
  173.         kernel = "Bicubic"
  174.         """)
  175. \ : kernel == "SoftCubic" ?
  176. \   Eval("""
  177.         a1     = 0.75
  178.         a2     = 0.25
  179.         kernel = "Bicubic"
  180.         """)
  181. \ : kernel.LeftStr(9) == "SoftCubic" && kernel.StrLen() > 9 ?
  182. \   Eval("""
  183.         a1     = kernel.RightStr(kernel.StrLen()-9).Value()
  184.         a1     = a1 >= 0 && a1 <= 100 ? a1/100.0
  185.         \      : Assert(false, "ResizeX: SoftCubic value must be in the range 0 through 100")
  186.         a2     = 1.0-a1
  187.         kernel = "Bicubic"
  188.         """)
  189. \ : NOP()
  190.  
  191. # If chroma=false and resizing 8-bit YUV, convert to Y8 to avoid processing chroma
  192. csp2   = !chroma && !lsb_in && !lsb && !(input.isrgb()) ? "Y" : csp
  193. input2 = csp2 == "Y" && !sislumaonly ? sispmt ? input.ConvertToY() : input.ConvertToY8() : input
  194.  
  195. # Convert YUY2 to YV16 because Dither_resize16 only supports planar formats
  196. input2 = csp2 == "YUY2" ? input2.ConvertToYV16() : input2
  197.  
  198. # Dither_resize16 is used if either lsb_in or lsb is true,
  199. # so the input needs to be converted to stack16 format if lsb_in=false and lsb=true
  200. input2 = !lsb_in && lsb ? input2.Dither_convert_8_to_16() : input2
  201.  
  202. # Blank luma channel for luma=false
  203. noY = input2.BlankClip(width=target_width, height=target_height, pixel_type=sispmt ? "Y"+string(Input.BitsPerComponent()) : "Y8", color_yuv=color_gray)
  204.  
  205. # Perform resizing
  206.     lsb_in || lsb ?
  207. \   Eval("""
  208.         resized = input2.Dither_resize16(target_width,target_height,src_left,src_top,src_width,src_height,kernel,
  209.         \                                taps=taps, a1=a1, a2=a2, cplace=cplace, y=luma?3:1, u=chroma?3:1, v=chroma?3:1)
  210.         """)
  211. \ : input.isRGB() || (sispmt ? (csp2 == "Y" || input.is444() && luma) : (csp2 == "Y" || csp2 == "YV24" && luma)) ?
  212. \   Eval("""
  213.         r8 = mt ? input2.ResizeX_AvsmtResize(target_width,target_height,src_left,src_top,src_width,src_height,kernel,taps,a1,a2,mt_params) : input2.ResizeX_AvsResize(target_width,target_height,src_left,src_top,src_width,src_height,kernel,taps,a1,a2)
  214.         resized = luma || input.isRGB() ? r8 : noY
  215.         """)
  216. \ : Eval("""
  217.         r8Y = sispmt ? input2.ConvertToY() : input2.ConvertToY8()
  218.         r8Y = mt ? r8Y.ResizeX_AvsmtResize(target_width,target_height,src_left,src_top,src_width,src_height,kernel,taps,a1,a2,mt_params) : r8Y.ResizeX_AvsResize(target_width,target_height,src_left,src_top,src_width,src_height,kernel,taps,a1,a2)
  219.         r8U = sispmt ? input2.ExtractU() : input2.UToY8()
  220.         r8U = mt ? r8U.ResizeX_AvsmtResize(target_width_c,target_height_c,src_left_c,src_top_c,src_width_c,src_height_c,kernel,taps,a1,a2,mt_params) : r8U.ResizeX_AvsResize(target_width_c,target_height_c,src_left_c,src_top_c,src_width_c,src_height_c,kernel,taps,a1,a2)
  221.         r8V = sispmt ? input2.ExtractV() : input2.VToY8()
  222.         r8V = mt ? r8V.ResizeX_AvsmtResize(target_width_c,target_height_c,src_left_c,src_top_c,src_width_c,src_height_c,kernel,taps,a1,a2,mt_params) : r8V.ResizeX_AvsResize(target_width_c,target_height_c,src_left_c,src_top_c,src_width_c,src_height_c,kernel,taps,a1,a2)
  223.         resized = luma ? YToUV(r8U,r8V,r8Y)
  224.         \              : YToUV(r8U,r8V,noY)
  225.         """)
  226.  
  227. # The resized clip will be in stack16 format if lsb_in=true, so dither down to 8-bit if lsb=false
  228. resized = lsb_in && !lsb ? resized.DitherPost(mode=6) : resized
  229.  
  230. # Make sure the output is the same colorspace as the input
  231. resized = csp == "YV12"  ? resized.ConvertToYV12()  
  232. \       : csp == "YV16"  ? resized.ConvertToYV16()
  233. \       : csp == "YUY2"  ? resized.ConvertToYUY2()
  234. \       : csp == "YV411" ? resized.ConvertToYV411()
  235. \       : csp == "YV24"  ? resized.ConvertToYV24()
  236. \       : chr420         ? resized.ConvertToYUV420()  
  237. \       : chr422         ? resized.ConvertToYUV422()  
  238. \       : chr444         ? resized.ConvertToYUV444()  
  239. \                        : resized
  240.  
  241. resized = sispmt ? input.IsYUVA() && !(resized.IsYUVA()) ? resized.AddAlphaPlane(mt ? input.ExtractA().ResizeX_AvsmtResize(target_width,target_height,src_left,src_top,src_width,src_height,kernel,taps,a1,a2,mt_params) : input.ExtractA().ResizeX_AvsResize(target_width,target_height,src_left,src_top,src_width,src_height,kernel,taps,a1,a2)) : resized : resized
  242.  
  243. return resized
  244. }
  245.  
  246. # Wrapper function for AviSynth's internal resizers
  247. function ResizeX_AvsResize(clip input, int target_width, int target_height, float "src_left", float "src_top",
  248. \                          float "src_width", float "src_height", string "kernel", int "taps", float "a1", float "a2") {
  249.  
  250. kernel = Default(kernel, "Spline36")
  251.  
  252. Eval("""
  253.     kernel == "Spline16" ||
  254. \    kernel == "Spline36" ||
  255. \    kernel == "Spline64" ||
  256. \    kernel == "Bilinear" ||
  257. \    kernel == "Point"     ? input."""+kernel+"""Resize(target_width,target_height,src_left,src_top,src_width,src_height)
  258. \  : kernel == "Lanczos"  ||
  259. \    kernel == "Blackman" ||
  260. \    kernel == "Sinc"      ? input."""+kernel+"""Resize(target_width,target_height,src_left,src_top,src_width,src_height,taps)
  261. \  : kernel == "Bicubic"   ? input."""+kernel+"""Resize(target_width,target_height,a1,a2,src_left,src_top,src_width,src_height)
  262. \  : kernel == "Gauss"     ? input."""+kernel+"""Resize(target_width,target_height,src_left,src_top,src_width,src_height,a1)
  263. \                          : Assert(false, "ResizeX_AvsResize: invalid kernel")
  264.     """)
  265. }
  266.  
  267. # Wrapper function for AviSynth's mt resizers
  268. function ResizeX_AvsmtResize(clip input, int target_width, int target_height, float "src_left", float "src_top",
  269. \                          float "src_width", float "src_height", string "kernel", int "taps", float "a1", float "a2", string "mt_params") {
  270.  
  271. kernel    = Default(kernel, "Spline36")
  272. mt_params = Default(mt_params,      "")
  273.  
  274. try { Eval("""
  275.     kernel == "Spline16" ||
  276. \    kernel == "Spline36" ||
  277. \    kernel == "Spline64" ||
  278. \    kernel == "Bilinear" ||
  279. \    kernel == "Point"     ? input."""+kernel+"""Resizemt(target_width,target_height,src_left,src_top,src_width,src_height"""+mt_params+""")
  280. \  : kernel == "Lanczos"  ||
  281. \    kernel == "Blackman" ||
  282. \    kernel == "Sinc"      ? input."""+kernel+"""Resizemt(target_width,target_height,src_left,src_top,src_width,src_height,taps"""+mt_params+""")
  283. \  : kernel == "Bicubic"   ? input."""+kernel+"""Resizemt(target_width,target_height,a1,a2,src_left,src_top,src_width,src_height"""+mt_params+""")
  284. \  : kernel == "Gauss"     ? input."""+kernel+"""Resizemt(target_width,target_height,src_left,src_top,src_width,src_height,a1"""+mt_params+""")
  285. \                          : Assert(false, "ResizeX_AvsmtResize: invalid kernel")
  286.          """)
  287.       } catch(error_msg) { input.ResizeX_AvsResize(target_width, target_height, src_left, src_top, src_width, src_height, kernel, taps, a1, a2) }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement