Advertisement
Guest User

ResizeX

a guest
Mar 19th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 15.10 KB | None | 0 0
  1. /* ResizeX v1.0.1 mod 2
  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. chr420  = sispmt ? input.is420() : input.isyv12()
  82. chr422  = sispmt ? input.is422() : input.isYV16()
  83. chr444  = sispmt ? input.is444() : input.isYV24()
  84.  
  85. Assert(csp != "RGB24" && csp != "RGB32" || !lsb_in && !lsb, "ResizeX: lsb_in and lsb must be false for RGB input")
  86.  
  87. # Check for subsampled chroma
  88. hssc12 = chr420 || chr422 || csp == "YUY2"
  89. hssc14 = csp == "YV411"
  90. vssc12 = chr420
  91.  
  92. Assert(!hssc12 || target_width%2  == 0, "ResizeX: target width of "+csp+" must be a multiple of 2")
  93. Assert(!hssc14 || target_width%4  == 0, "ResizeX: target width of "+csp+" must be a multiple of 4")
  94. Assert(!vssc12 || target_height%2 == 0, "ResizeX: target height of "+csp+" must be a multiple of 2")
  95.  
  96. # Set chroma target and src values based on the subsampling ratios
  97. target_width_c  = hssc12 ? target_width/2  : hssc14 ? target_width/4 : target_width
  98. target_height_c = vssc12 ? target_height/2 : target_height
  99. src_left_c      = hssc12 ? src_left/2.0    : hssc14 ? src_left/4.0   : src_left
  100. src_top_c       = vssc12 ? src_top/2.0     : src_top
  101. src_width_c     = hssc12 ? src_width/2.0   : hssc14 ? src_width/4.0  : src_width
  102. src_height_c    = vssc12 ? src_height/2.0  : src_height
  103.  
  104. # Add the MPEG2 chroma shift correction to the src_left_c value
  105. MPEG2shift = hssc12 ? 0.25*(1.0-Float(src_width_c)/Float(target_width_c))
  106. \          : hssc14 ? 0.375*(1.0-Float(src_width_c)/Float(target_width_c))
  107. \                   : 0
  108. src_left_c  = cplace == "MPEG2" && kernel.LeftStr(5) != "Point" ? src_left_c+MPEG2shift
  109. \                                                               : src_left_c
  110.  
  111. # Remove "Resize" from the end of the kernel string if present
  112. kernel = kernel.RightStr(6) == "Resize" ? kernel.LeftStr(kernel.StrLen()-6) : kernel
  113.  
  114. # Support the Dither_resize16 kernel name variants when resizing 8-bit
  115. kernel = kernel == "Linear"   ? "Bilinear"
  116. \      : kernel == "Cubic"    ? "Bicubic"
  117. \      : kernel == "Gaussian" ? "Gauss"
  118. \                             : kernel
  119.  
  120. # Dither_resize16 kernels without an internal equivalent can't be used without lsb_in or lsb being true
  121. Assert(lsb_in || lsb || kernel == "Spline16" || kernel == "Spline36" || kernel == "Spline64" ||
  122. \      kernel != "Rect" && kernel != "Box" && kernel != "Blackmanminlobe" && kernel.LeftStr(6) != "Spline" && kernel != "Impulse",
  123. \      "ResizeX: Rect, Box, Blackmanminlobe, Spline, and Impulse kernels"+chr(10)+
  124. \      "are available only when resizing 16-bit (lsb_in or lsb must be true)")
  125.  
  126. # Get the taps value from the kernel string if present (overrides the parameter)
  127. taps = kernel.LeftStr(6)  == "Spline"          && kernel != "Spline16" && kernel != "Spline36" && kernel != "Spline64" &&
  128. \                                                 kernel.StrLen() >  6 ? kernel.RightStr(kernel.StrLen()-6).Value().Int()
  129. \    : kernel.LeftStr(7)  == "Lanczos"         && kernel.StrLen() >  7 ? kernel.RightStr(kernel.StrLen()-7).Value().Int()
  130. \    : kernel.LeftStr(8)  == "Blackman"        && kernel.LeftStr(15) != "Blackmanminlobe" &&
  131. \                                                 kernel.StrLen() >  8 ? kernel.RightStr(kernel.StrLen()-8).Value().Int()
  132. \    : kernel.LeftStr(15) == "Blackmanminlobe" && kernel.StrLen() > 15 ? kernel.RightStr(kernel.StrLen()-15).Value().Int()
  133. \    : kernel.LeftStr(4)  == "Sinc"            && kernel.StrLen() >  4 ? kernel.RightStr(kernel.StrLen()-4).Value().Int()
  134. \                                                                      : taps
  135.  
  136. # Remove the taps value from the kernel string if present
  137. kernel = kernel.LeftStr(6)  == "Spline"          && kernel != "Spline16" && kernel != "Spline36" && kernel != "Spline64" &&
  138. \                                                   kernel.StrLen() >  6 ? kernel.LeftStr(6)
  139. \      : kernel.LeftStr(7)  == "Lanczos"         && kernel.StrLen() >  7 ? kernel.LeftStr(7)
  140. \      : kernel.LeftStr(8)  == "Blackman"        && kernel.LeftStr(15) != "Blackmanminlobe" &&
  141. \                                                   kernel.StrLen() >  8 ? kernel.LeftStr(8)
  142. \      : kernel.LeftStr(15) == "Blackmanminlobe" && kernel.StrLen() > 15 ? kernel.LeftStr(15)
  143. \      : kernel.LeftStr(4)  == "Sinc"            && kernel.StrLen() >  4 ? kernel.LeftStr(4)
  144. \                                                                        : kernel
  145.  
  146. # Set the a1 and a2 values for bicubic presets (overrides the parameters)
  147.     kernel == "Catmull-Rom" || kernel == "CatRom" ?
  148. \   Eval("""
  149.         a1     = 0.0
  150.         a2     = 0.5
  151.         kernel = "Bicubic"
  152.         """)
  153. \ : kernel == "Hermite" ?
  154. \   Eval("""
  155.         a1     = 0.0
  156.         a2     = 0.0
  157.         kernel = "Bicubic"
  158.         """)
  159. \ : kernel == "Mitchell-Netravali" || kernel == "Mitchell" ?
  160. \   Eval("""
  161.         a1     = 1.0/3.0
  162.         a2     = 1.0/3.0
  163.         kernel = "Bicubic"
  164.         """)
  165. \ : kernel == "Robidoux" ?
  166. \   Eval("""
  167.         a1     = 0.3782
  168.         a2     = 0.3109
  169.         kernel = "Bicubic"
  170.         """)
  171. \ : kernel == "SoftCubic" ?
  172. \   Eval("""
  173.         a1     = 0.75
  174.         a2     = 0.25
  175.         kernel = "Bicubic"
  176.         """)
  177. \ : kernel.LeftStr(9) == "SoftCubic" && kernel.StrLen() > 9 ?
  178. \   Eval("""
  179.         a1     = kernel.RightStr(kernel.StrLen()-9).Value()
  180.         a1     = a1 >= 0 && a1 <= 100 ? a1/100.0
  181.         \      : Assert(false, "ResizeX: SoftCubic value must be in the range 0 through 100")
  182.         a2     = 1.0-a1
  183.         kernel = "Bicubic"
  184.         """)
  185. \ : NOP()
  186.  
  187. # If chroma=false and resizing 8-bit YUV, convert to Y8 to avoid processing chroma
  188. csp2   = !chroma && !lsb_in && !lsb && !(input.isrgb()) ? "Y" : csp
  189. input2 = csp2 == "Y" ? sispmt ? input.ConvertToY() : input.ConvertToY8() : input
  190.  
  191. # Convert YUY2 to YV16 because Dither_resize16 only supports planar formats
  192. input2 = csp2 == "YUY2" ? input2.ConvertToYV16() : input2
  193.  
  194. # Dither_resize16 is used if either lsb_in or lsb is true,
  195. # so the input needs to be converted to stack16 format if lsb_in=false and lsb=true
  196. input2 = !lsb_in && lsb ? input2.Dither_convert_8_to_16() : input2
  197.  
  198. # Blank luma channel for luma=false
  199. noY = input2.BlankClip(width=target_width, height=target_height, pixel_type=sispmt ? "Y"+string(Input.BitsPerComponent()) : "Y8", color_yuv=color_gray)
  200.  
  201. # Perform resizing
  202.     lsb_in || lsb ?
  203. \   Eval("""
  204.         resized = input2.Dither_resize16(target_width,target_height,src_left,src_top,src_width,src_height,kernel,
  205.         \                                taps=taps, a1=a1, a2=a2, cplace=cplace, y=luma?3:1, u=chroma?3:1, v=chroma?3:1)
  206.         """)
  207. \ : input.isRGB() || (sispmt ? (input.isy() || input.is444() && luma) : (csp2 == "Y8" || csp2 == "YV24" && luma)) ?
  208. \   Eval("""
  209.         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)
  210.         resized = luma || input.isRGB() ? r8 : noY
  211.         """)
  212. \ : Eval("""
  213.         r8Y = sispmt ? input2.ConvertToY() : input2.ConvertToY8()
  214.         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)
  215.         r8U = sispmt ? input2.ExtractU() : input2.UToY8()
  216.         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)
  217.         r8V = sispmt ? input2.ExtractV() : input2.VToY8()
  218.         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)
  219.         resized = luma ? YToUV(r8U,r8V,r8Y)
  220.         \              : YToUV(r8U,r8V,noY)
  221.         """)
  222.  
  223. # The resized clip will be in stack16 format if lsb_in=true, so dither down to 8-bit if lsb=false
  224. resized = lsb_in && !lsb ? resized.DitherPost(mode=6) : resized
  225.  
  226. # Make sure the output is the same colorspace as the input
  227. resized = csp == "YV12"  ? resized.ConvertToYV12()  
  228. \       : csp == "YV16"  ? resized.ConvertToYV16()
  229. \       : csp == "YUY2"  ? resized.ConvertToYUY2()
  230. \       : csp == "YV411" ? resized.ConvertToYV411()
  231. \       : csp == "YV24"  ? resized.ConvertToYV24()
  232. \       : chr420         ? resized.ConvertToYUV420()  
  233. \       : chr422         ? resized.ConvertToYUV422()  
  234. \       : chr444         ? resized.ConvertToYUV444()  
  235. \                        : resized
  236.  
  237. resized = sispmt ? input.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
  238.  
  239. return resized
  240. }
  241.  
  242. # Wrapper function for AviSynth's internal resizers
  243. function ResizeX_AvsResize(clip input, int target_width, int target_height, float "src_left", float "src_top",
  244. \                          float "src_width", float "src_height", string "kernel", int "taps", float "a1", float "a2") {
  245.  
  246. kernel = Default(kernel, "Spline36")
  247.  
  248. Eval("""
  249.     kernel == "Spline16" ||
  250. \    kernel == "Spline36" ||
  251. \    kernel == "Spline64" ||
  252. \    kernel == "Bilinear" ||
  253. \    kernel == "Point"     ? input."""+kernel+"""Resize(target_width,target_height,src_left,src_top,src_width,src_height)
  254. \  : kernel == "Lanczos"  ||
  255. \    kernel == "Blackman" ||
  256. \    kernel == "Sinc"      ? input."""+kernel+"""Resize(target_width,target_height,src_left,src_top,src_width,src_height,taps)
  257. \  : kernel == "Bicubic"   ? input."""+kernel+"""Resize(target_width,target_height,a1,a2,src_left,src_top,src_width,src_height)
  258. \  : kernel == "Gauss"     ? input."""+kernel+"""Resize(target_width,target_height,src_left,src_top,src_width,src_height,a1)
  259. \                          : Assert(false, "ResizeX_AvsResize: invalid kernel")
  260.     """)
  261. }
  262.  
  263. # Wrapper function for AviSynth's mt resizers
  264. function ResizeX_AvsmtResize(clip input, int target_width, int target_height, float "src_left", float "src_top",
  265. \                          float "src_width", float "src_height", string "kernel", int "taps", float "a1", float "a2", string "mt_params") {
  266.  
  267. kernel    = Default(kernel, "Spline36")
  268. mt_params = Default(mt_params,      "")
  269.  
  270. try { Eval("""
  271.     kernel == "Spline16" ||
  272. \    kernel == "Spline36" ||
  273. \    kernel == "Spline64" ||
  274. \    kernel == "Bilinear" ||
  275. \    kernel == "Point"     ? input."""+kernel+"""Resizemt(target_width,target_height,src_left,src_top,src_width,src_height"""+mt_params+""")
  276. \  : kernel == "Lanczos"  ||
  277. \    kernel == "Blackman" ||
  278. \    kernel == "Sinc"      ? input."""+kernel+"""Resizemt(target_width,target_height,src_left,src_top,src_width,src_height,taps"""+mt_params+""")
  279. \  : kernel == "Bicubic"   ? input."""+kernel+"""Resizemt(target_width,target_height,a1,a2,src_left,src_top,src_width,src_height"""+mt_params+""")
  280. \  : kernel == "Gauss"     ? input."""+kernel+"""Resizemt(target_width,target_height,src_left,src_top,src_width,src_height,a1"""+mt_params+""")
  281. \                          : Assert(false, "ResizeX_AvsmtResize: invalid kernel")
  282.          """)
  283.       } catch(error_msg) { input.ResizeX_AvsResize(target_width, target_height, src_left, src_top, src_width, src_height, kernel, taps, a1, a2) }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement