Advertisement
mawen1250

O16 mod v1.6.0

Jan 5th, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 18.10 KB | None | 0 0
  1. #############################################################################################
  2. ###                                                                                       ###
  3. ### O16 & Down10 v1.6.0: output 16 bit clip using "dither", "f3kdb" or "SmoothAdjust.dll" ###
  4. ### original script by 06_taro, modified by mawen1250                                     ###
  5. ###                                                                                       ###
  6. #############################################################################################
  7. ###
  8. ### Changelog:
  9. ### 2013.01.05 mod v1.6.0 - Add U10 and Round10, speed optimization, using some functions from "dither" tool
  10. ### 2012.04.03 v1.5.2     - Add LowDepth for fun, not fully implemented ( full range and speed optimization )
  11. ### 2012.03.25 v1.5.1     - Down10 now can directly output interleaved msb/lsb high bit depth clip
  12. ### 2012.03.14 v1.5.0     - Synchronize argument names with parameter names of f3kdb v1.5.0
  13. ### 2011.11.08 v1.3.2     - Replace all "dither" fuctions by "f3kdb"
  14. ###                         now it should be faster and uses less memory
  15. ###                         especially directly output high bit clip from TV range 8-bit clips with default settings
  16. ### 2011.11.05 v1.3.1     - Adjust f3kdb arguments to f3kdb version 1.4.1 and later
  17. ### 2011.10.31 v1.3 final - Use f3kdb to dither down, the result is more accurate
  18. ###                       - Separate TV range and PC range algorithm, and ensure that conversion is correct
  19. ###                       - Fix a bug in dithering down
  20. ###                       - Add U16 and C16
  21. ### 2011.09.27 v1.3 alpha - Rename "bitdepth" to "depth"
  22. ###                       - Add "dither" and "smooth", dangerous, no accuracy guaranteed
  23. ###                         Set "dither=-1, smooth=0" if you want old funcion, which does not apply any dithering or smoothing
  24. ###                       - Separate bit-depth downscaling function to "Down10()"
  25. ###                       - Restrict "depth" to [9, 16] in O16 and [8,16] in Down10
  26. ### 2011.08.11 v1.2       - "bitdepth" restricted up to 16
  27. ### 2011.08.09 v1.1       - Add "bitdepth" parameter to output self-defined bit depth
  28. ### 2011.08.05 v1.0       - First Script
  29. ###
  30. ###################################################################################################################################
  31. ###
  32. ### Requirements:
  33. ### "dither"       - [ 1.22.0 ~ ] : http://forum.doom9.org/showthread.php?p=1386559#post1386559
  34. ### "f3kdb"        - [ 1.5.0 ~  ] : http://forum.doom9.org/showthread.php?t=161411
  35. ### "SmoothAdjust" - [ 2.00  ~  ] : http://forum.doom9.org/showthread.php?t=154971
  36. ###
  37. ###################################################################################################################################
  38. ###
  39. ### Usage:
  40. ###
  41. ### O16     - get 8-bit input clip,
  42. ###         - process with 16-bit post-processing filters,
  43. ###         - OUTPUT interleaved 9-16 bit output,
  44. ###         - which can be passed to x264 directly.
  45. ###         - I will not add 8-bit output support in O16,
  46. ###         - since more checking in parameters will slow down O16 funcion and "U16().pp.Down10(8)" can easily do the same thing.
  47. ### ------------
  48. ### input   - input clip, must be 8-bit clip.
  49. ###         - Defualt: not set
  50. ### depth   - output bit depth, range from 9 to 16.
  51. ###         - Defualt: 16
  52. ### pp      - 16-bit precision post-processing defined by yourself.
  53. ###         - Defualt: not set ( no pp )
  54. ### dither  - use dithering if output is a non 16-bit clip and either "pp" is defined or tvRange is false,
  55. ###         - explaination and strength defined in SmoothAdjust,
  56. ###         - when value is -2, use Ordered dithering,
  57. ###         - when value is -3, use Floyd-Steinberg dithering.
  58. ###         - when value is -4, use Dither_quantize instead of f3kdb.
  59. ###         - Default: -1 if output 16-bit clip or no pp applied, otherwise -3 for TV range and 50 for PC range
  60. ### ditherm - Dithering method for DitherPost/Dither_quantize:
  61. ###         - -1    no dither, round to the closest value
  62. ###         - 0     8-bit ordered dither + noise.
  63. ###         - 1     1-bit dither
  64. ###         - 2     2-bit dither, light
  65. ###         - 3     2-bit dither, medium
  66. ###         - 4     2-bit dither, strong
  67. ###         - 5     2-bit dither, stronger
  68. ###         - 6     Serpentine Floyd-Steinberg error diffusion + noise. Well-balanced algorithm.
  69. ###         - 7     Stucki error diffusion + noise. Looks “sharp” and preserve light edges and details well.
  70. ###         - 8     Atkinson error diffusion + noise. Generates distinct patterns but keeps clean the flat areas.
  71. ### smooth  - use gradient smoothing if defined "pp" and output is a non 16-bit clip,
  72. ###         - explaination and strength defined in SmoothAdjust,
  73. ###         - do NOT have effect if dither<-1 or dither==-1 but input is TV range.
  74. ###         - Default: 0
  75. ### tvRange - processe the clip as TV Range or not.
  76. ###         - Default: true
  77. ###
  78. ### ----------------------------------------------------------
  79. ###
  80. ### Down10  - get stacked 16-bit input clip and convert it DOWN to 8-16 bit output.
  81. ### ------------
  82. ### clp     - input clip, must be 16-bit clip.
  83. ###         - Defualt: not set
  84. ### depth   - output bit depth, range from 8 to 16.
  85. ###         - Defualt: 10
  86. ### dither  - use dithering if output is a non 16-bit clip,
  87. ###         - when value is in [-1, 100], explaination and strength defined in SmoothAdjust,
  88. ###         - when value is -2, use Ordered dithering,
  89. ###         - when value is -3, use Floyd-Steinberg dithering.
  90. ###         - when value is -4, use Dither_quantize instead of f3kdb.
  91. ###         - Default: -3 for TV range and 50 for PC range
  92. ### ditherm - Dithering method for DitherPost/Dither_quantize:
  93. ###         - -1    no dither, round to the closest value
  94. ###         - 0     8-bit ordered dither + noise.
  95. ###         - 1     1-bit dither
  96. ###         - 2     2-bit dither, light
  97. ###         - 3     2-bit dither, medium
  98. ###         - 4     2-bit dither, strong
  99. ###         - 5     2-bit dither, stronger
  100. ###         - 6     Serpentine Floyd-Steinberg error diffusion + noise. Well-balanced algorithm.
  101. ###         - 7     Stucki error diffusion + noise. Looks “sharp” and preserve light edges and details well.
  102. ###         - 8     Atkinson error diffusion + noise. Generates distinct patterns but keeps clean the flat areas.
  103. ### smooth  - use gradient smoothing if output is a non 16-bit clip,
  104. ###         - explaination and strength defined in SmoothAdjust,
  105. ###         - do NOT have effect if dither<-1 or dither==-1 but input is TV range.
  106. ###         - Default: 0
  107. ### tvRange - processe the clip as TV Range or not.
  108. ###         - Default: true
  109. ### stack   - output stacked msb/lsb (true) or interleaved msb/lsb (false).
  110. ###         - Default: true
  111. ###
  112. ### ----------------------------------------------------------
  113. ###
  114. ### U16     - get 8-bit input clip and UP-convert it to stacked/interleaved 16 bit output.
  115. ### ------------
  116. ### c       - input clip, must be 8-bit clip.
  117. ###         - Defualt: not set
  118. ### tvRange - processe the clip as TV Range or not.
  119. ###         - Default: true
  120. ### stack   - output stacked msb/lsb (true) or interleaved msb/lsb (false).
  121. ###         - Default: true
  122. ###
  123. ### ----------------------------------------------------------
  124. ###
  125. ### C16     - get stacked input clip and CONVERT it to interleaved output, the same as Dither_convey_yuv4xxp16_on_yvxx.
  126. ### ------------
  127. ### c       - input clip.
  128. ###         - Defualt: not set
  129. ###
  130. ### ----------------------------------------------------------
  131. ###
  132. ### U10     - get 8-bit input clip and UP-convert it to stacked/interleaved 8-16 bit output.
  133. ### ------------
  134. ### c       - input clip, must be 8-bit clip.
  135. ###         - Defualt: not set
  136. ### depth   - output bit depth, range from 8 to 16.
  137. ###         - Defualt: 10
  138. ### tvRange - processe the clip as TV Range or not.
  139. ###         - Default: true
  140. ### stack   - output stacked msb/lsb (true) or interleaved msb/lsb (false).
  141. ###         - Default: true
  142. ###
  143. ### ----------------------------------------------------------
  144. ###
  145. ### Round10 - get stacked 16-bit tvRange input clip and convert it Down to stacked 8-16 bit output with Rounding.
  146. ### ------------
  147. ### c       - input clip, must be stacked 16-bit tvRange clip.
  148. ###         - Defualt: not set
  149. ### depth   - output bit depth, range from 8 to 16.
  150. ###         - Defualt: 10
  151. ###
  152. ###################################################################################################################################
  153. ###
  154. ### Sample 1 (simply output 16-bit, no pp):
  155. ### AviSource(sample.avi)
  156. ### O16()
  157. ###
  158. ### Sample 2 (simply output 10-bit, no pp):
  159. ### AviSource(sample.avi)
  160. ### O16(10)
  161. ###
  162. ### Sample 3 (output 16-bit after pp):
  163. ### AviSource(sample.avi)
  164. ### O16(pp="Dither_resize16(1280, 720)")
  165. ###
  166. ### Sample 4 (output 16-bit after multi-step pp, with """three quotes""" inside String pp):
  167. ### AviSource(sample.avi)
  168. ### pp16=
  169. ### \ """Dither_resize16(648, 480, 4, 0, -5, 0, kernel="spline64")
  170. ### \ Dither_crop16(4, 0, -4, 0)"""
  171. ### O16(pp=pp16)
  172. ###
  173. ### Sample 5 (another example, exactly the same with Sample 4):
  174. ### AviSource(sample.avi)
  175. ### O16(pp=
  176. ### \ """Dither_resize16(648, 480, 4, 0, -5, 0, kernel="spline64")
  177. ### \ Dither_crop16(4, 0, -4, 0)""")
  178. ###
  179. ### Sample 6 (another example, exactly the same with Sample 4, using custom-defined function):
  180. ### AviSource(sample.avi)
  181. ### O16(pp="CustomPP()")
  182. ### FUNCTION CustomPP(clip input){
  183. ###   tmp=input.Dither_resize16(648, 480, 4, 0, -5, 0, kernel="spline64")
  184. ###   pped=tmp.Dither_crop16(4, 0, -4, 0)
  185. ###   return pped
  186. ### }
  187. ###
  188. ### Sample 7 (output 10-bit after pp, using dithering):
  189. ### AviSource(sample.avi)
  190. ### O16(10, pp="Dither_resize16(1280, 720)", dither=0)
  191. ###
  192. ### Sample 8 (output 8-bit after pp in 16-bit precision):
  193. ### AviSource(sample.avi)
  194. ### U16()
  195. ### Dither_resize16(1280, 720)
  196. ### Down10(8)
  197. ###
  198. ###################################################################################################################################
  199.  
  200. FUNCTION O16( clip input, int "depth", string "pp", int "dither", int "ditherm", int "smooth", bool "tvRange" ){
  201.  
  202.   Defined(depth) ? Assert( depth>8 && depth<=16 , """O16: "depth" ranges from 9 to 16!""") : nop()
  203.  
  204.   depth   = Default( depth,     16 )
  205.   smooth  = Default( smooth,     0 )
  206.   tvRange = Default( tvRange, true )
  207.   dither  = tvRange ? Default(dither, -3) : Default(dither, 50) # use dither or not, -4 use Dither_quantize instead of f3kdb
  208.   ditherm = Default( ditherm,    6 ) # dither mode for DitherPost/Dither_quantize
  209.  
  210.   Assert( depth>8 && depth<=16      , """O16: "depth" ranges from 9 to 16!"""    )
  211.   Assert( dither>=-4 && dither<=100 , """O16: "dither" ranges from -4 to 100!""" )
  212.   Assert( ditherm>=-1 && ditherm<=8 , """O16: "ditherm" ranges from -1 to 8!"""  )
  213.   Assert( !(dither==-4 && depth==15), """O16: "depth" can't be 15 when "dither" is -4""" )
  214.  
  215.   last = input.U16( tvRange=tvRange )
  216.   pp16 = Defined(pp) ? Eval(pp) : last
  217.  
  218. ###  
  219. ### The following conversion gives the same result as pp16.Down10.C16,
  220. ### but may be a little bit faster when using f3kdb to dither down,
  221. ### which is the default dithering method.
  222. ###
  223.  
  224.   return  !Defined(pp) ? input.U10( depth=depth, tvRange=tvRange, stack=false ) :
  225.   \      ( depth==16 ) ? pp16.C16() :
  226.   \      pp16.Down10(depth=depth, dither=dither, ditherm=ditherm, smooth=smooth, tvRange=tvRange, stack=false)
  227. }
  228.  
  229. ###################################################################################################################################
  230.  
  231. Function Down10( clip clp, int "depth", int "dither", int "ditherm", int "smooth", bool "tvRange", bool "stack" ){
  232.  
  233.   tvRange = Default( tvRange, true )
  234.   depth   = Default( depth,     10 ) # bit-depth of output clip
  235.   smooth  = Default( smooth,     0 ) # use smooth or not
  236.   dither  = tvRange ? Default(dither, -3) : Default(dither, 50) # use dither or not, -4 use Dither_quantize instead of f3kdb
  237.   ditherm = Default( ditherm,    6 ) # dither mode for DitherPost/Dither_quantize
  238.   stack   = Default( stack,   true ) # output stacked msb/lsb or interleaved msb/lsb
  239.  
  240.   Assert( dither>=-4 && dither<=100 , """Down10: "dither" ranges from -4 to 100!""" )
  241.   Assert( ditherm>=-1 && ditherm<=8 , """Down10: "ditherm" ranges from -1 to 8!"""  )
  242.   Assert( depth>=8   && depth<=16   , """Down10: "depth" ranges from 8 to 16!"""    )
  243.   Assert( !(dither==-4 && depth==15), """Down10: "depth" can't be 15 when "dither" is -4""" )
  244.  
  245.   dither  = dither==-4 && depth==15 ? -3 : dither
  246.  
  247.   shift   = int( Pow( 2, depth - 8 ) )
  248.   ycFloor = int( 16  * shift )
  249.   neutral = int( 128 * shift )
  250.   yCeil   = int( 235 * shift )
  251.   cCeil   = int( 240 * shift )
  252.   ycRge   = int( Pow( 2, depth ) - 1 )
  253.  
  254.   yExpr   = tvRange ? ( "0-0;4096-"+String(ycFloor)+";32768-"+String(neutral)+";60160-"+String(yCeil)+";65535-"+String(ycRge) )
  255.   \                 : ( "0-0;65535-"+String(ycRge) )
  256.   cExpr   = tvRange ? ( "0-0;4096-"+String(ycFloor)+";32768-"+String(neutral)+";61440-"+String(cCeil)+";65535-"+String(ycRge) )
  257.   \                 : ( "0-0;65535-"+String(ycRge) )
  258.  
  259.   smoothDown = clp.SmoothCurve16( Ycurve=yExpr, Ucurve=cExpr, Vcurve=cExpr, interp=0, smooth=smooth,
  260.   \                               dither=( (dither>-1) ? dither : -1 ), limiter=false, TVrange=0 )
  261.  
  262.   ditherDown = dither==-4 ? clp.Dither_quantize( depth, reducerange=true, mode=ditherm )
  263.   \          : dither==-1 ? clp.Round10( depth=depth )
  264.   \                       : clp.f3kdb( Y=0, Cb=0, Cr=0, grainY=0, grainC=0, dither_algo=(-dither), input_mode=1,
  265.   \                                    output_depth=depth, output_mode=( (depth>8) ? stack ? 1 : 2 : 0 ) )
  266.  
  267.   down    = ( dither<-1 || ( dither==-1 && smooth==0 && tvRange ) ) ? ( depth==8 ) ? clp.DitherPost( mode=ditherm )
  268.   \                                                                 : (dither>=-3) ? ditherDown
  269.   \                                                                 :   stack      ? ditherDown
  270.   \                                                                                : ditherDown.C16
  271.   \                                                                 : ( depth==8 ) ? smoothDown.Get_lsb()
  272.   \                                                                 :   stack      ? smoothDown
  273.   \                                                                                : smoothDown.C16
  274.  
  275.   return ( depth==16 ) ? stack ? clp : clp.C16 : down
  276. }
  277.  
  278. ###################################################################################################################################
  279.  
  280. Function U16( clip c, bool "tvRange", bool "stack" ){
  281.  
  282.   tvRange = Default( tvRange, true )
  283.   stack   = Default( stack,   true )
  284.  
  285.   Expr    = "0-0;128-32768;255-65535;65535-65535"
  286.  
  287.   up      =  tvRange ? c.Dither_convert_8_to_16()
  288.   \                  : StackVertical(c.Gen_null_lsb(), c).SmoothCurve16(Ycurve=Expr, Ucurve=Expr, Vcurve=Expr, interp=0,
  289.   \                                                                     limiter=false, TVrange=0, smooth=0, dither=-1)
  290.  
  291.   return stack ? up : up.Dither_out()
  292. }
  293.  
  294. ###################################################################################################################################
  295.  
  296. Function C16( clip c ){
  297.   return c.Dither_out()
  298.  }
  299.  
  300. ###################################################################################################################################
  301.  
  302. Function LowDepth(clip input, int "depth", int "dither", int "ditherm", int "smooth", bool "tvRange"){
  303.  
  304.   tvRange = Default( tvRange, true )
  305.   depth   = Default( depth,      8 ) # bit-depth of output clip
  306.   smooth  = Default( smooth,     0 ) # use smooth or not
  307.   dither  = tvRange ? Default(dither, -3) : Default(dither, 50) # use dither or not, -4 use Dither_quantize instead of f3kdb
  308.   ditherm = Default( ditherm,    6 ) # dither mode for DitherPost/Dither_quantize
  309.  
  310.   Assert( dither>=-4 && dither<=100, """LowDepth: "dither" ranges from -4 to 100!""" )
  311.   Assert( depth<=8   && depth>=1   , """LowDepth: "depth" ranges from 1 to 8!"""     )
  312.  
  313.   StackVertical(input.Gen_null_lsb, input)
  314.   Down10(depth=depth+8, dither=dither, ditherm=ditherm, smooth=smooth, tvRange=tvRange)
  315.   Dither_lut16("x "+string(8-depth)+" <<", Y=3, U=3, V=3)
  316.   Get_lsb
  317. }
  318.  
  319. ###################################################################################################################################
  320.  
  321. Function U10( clip c, int "depth", bool "tvRange", bool "stack" ){
  322.  
  323.   depth   = Default( depth,     10 )
  324.   tvRange = Default( tvRange, true )
  325.   stack   = Default( stack,   true )
  326.  
  327.   Assert( depth>=8   && depth<=16   , """U10: "depth" ranges from 8 to 16!"""    )
  328.  
  329.   shift   = int( Pow( 2, depth - 8 ) )
  330.   neutral = int( 128 * shift )
  331.   ycRge   = int( Pow( 2, depth ) - 1 )
  332.   Expr    = "0-0;128-"+String(neutral)+";255-"+String(ycRge)+";65535-65535"
  333.  
  334.   upmsb   = c.mt_lut("x " +string(16-depth)+ " >>", U=3, V=3)
  335.   uplsb   = c.mt_lut("x " +string(depth-8 )+ " << x " +string(16-depth)+ " >> 8 << -", U=3, V=3)
  336.  
  337.   tvup    = depth==16 ? c.Dither_convert_8_to_16() : StackVertical(upmsb, uplsb)
  338.   pcup    = StackVertical(c.Gen_null_lsb(), c).SmoothCurve16(Ycurve=Expr, Ucurve=Expr, Vcurve=Expr, interp=0,
  339.   \                                                          limiter=false, TVrange=0, smooth=0, dither=-1)
  340.  
  341.   up      = tvRange ? tvup : pcup
  342.  
  343.   return depth==8 ? c : stack ? up : up.Dither_out()
  344. }
  345.  
  346. ###################################################################################################################################
  347.  
  348. Function Round10( clip c, int "depth" ){
  349.  
  350.   depth   = Default( depth,     10 )
  351.  
  352.   Assert( depth>=8   && depth<=16   , """Round10: "depth" ranges from 8 to 16!"""    )
  353.  
  354.   divisor = int( Pow( 2, 16-depth ) )
  355.  
  356.   return depth==16 ? c : depth==8 ? c.DitherPost(mode=-1) : c.Dither_lut16("x " +String(divisor)+ " /", U=3, V=3)
  357. }
  358.  
  359. ###################################################################################################################################
  360.  
  361. Function Get_lsb      (clip src){  return src.Crop(0, src.Height/2, src.Width, src.Height/2) }
  362. Function Gen_null_lsb (clip src){  return BlankClip(src, color_yuv=0)                        }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement