Advertisement
mawen1250

O16 mod v1.6.1

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