Advertisement
mawen1250

O16 v1.5.2

Jun 19th, 2012
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 14.02 KB | None | 0 0
  1. ###################################################################################
  2. ###                                                                             ###
  3. ### O16 & Down10 v1.5.2: output 16 bit clip using "f3kdb" or "SmoothAdjust.dll" ###
  4. ###                                                                             ###
  5. ###################################################################################
  6. ###
  7. ### Changelog:
  8. ### 2012.04.03 v1.5.2     - Add LowDepth for fun, not fully implemented ( full range and speed optimization )
  9. ### 2012.03.25 v1.5.1     - Down10 now can directly output interleaved msb/lsb high bit depth clip
  10. ### 2012.03.14 v1.5.0     - Synchronize argument names with parameter names of f3kdb v1.5.0
  11. ### 2011.11.08 v1.3.2     - Replace all "dither" fuctions by "f3kdb"
  12. ###                         now it should be faster and uses less memory
  13. ###                         especially directly output high bit clip from TV range 8-bit clips with default settings
  14. ### 2011.11.05 v1.3.1     - Adjust f3kdb arguments to f3kdb version 1.4.1 and later
  15. ### 2011.10.31 v1.3 final - Use f3kdb to dither down, the result is more accurate
  16. ###                       - Separate TV range and PC range algorithm, and ensure that conversion is correct
  17. ###                       - Fix a bug in dithering down
  18. ###                       - Add U16 and C16
  19. ### 2011.09.27 v1.3 alpha - Rename "bitdepth" to "depth"
  20. ###                       - Add "dither" and "smooth", dangerous, no accuracy guaranteed
  21. ###                         Set "dither=-1, smooth=0" if you want old funcion, which does not apply any dithering or smoothing
  22. ###                       - Separate bit-depth downscaling function to "Down10()"
  23. ###                       - Restrict "depth" to [9, 16] in O16 and [8,16] in Down10
  24. ### 2011.08.11 v1.2       - "bitdepth" restricted up to 16
  25. ### 2011.08.09 v1.1       - Add "bitdepth" parameter to output self-defined bit depth
  26. ### 2011.08.05 v1.0       - First Script
  27. ###
  28. ###################################################################################################################################
  29. ###
  30. ### Requirements:
  31. ### "f3kdb"        - [ 1.5.0 ~ ] : http://forum.doom9.org/showthread.php?t=161411
  32. ### "SmoothAdjust" - [ 2.00  ~ ] : http://forum.doom9.org/showthread.php?t=154971
  33. ###
  34. ###################################################################################################################################
  35. ###
  36. ### Usage:
  37. ###
  38. ### O16     - get 8-bit input clip,
  39. ###         - process with 16-bit post-processing filters,
  40. ###         - OUTPUT interleaved 9-16 bit output,
  41. ###         - which can be passed to x264 directly.
  42. ###         - I will not add 8-bit output support in O16,
  43. ###         - since more checking in parameters will slow down O16 funcion and "U16().pp.Down10(8)" can easily do the same thing.
  44. ### ------------
  45. ### input   - input clip, must be 8-bit clip.
  46. ###         - Defualt: not set
  47. ### depth   - output bit depth, range from 9 to 16.
  48. ###         - Defualt: 16
  49. ### pp      - 16-bit precision post-processing defined by yourself.
  50. ###         - Defualt: not set ( no pp )
  51. ### dither  - use dithering if output is a non 16-bit clip and either "pp" is defined or tvRange is false,
  52. ###         - explaination and strength defined in SmoothAdjust,
  53. ###         - when value is -2, use Ordered dithering,
  54. ###         - when value is -3, use Floyd-Steinberg dithering.
  55. ###         - Default: -1 if output 16-bit clip or no pp applied, otherwise -3 for TV range and 50 for PC range
  56. ### smooth  - use gradient smoothing if defined "pp" and output is a non 16-bit clip,
  57. ###         - explaination and strength defined in SmoothAdjust,
  58. ###         - do NOT have effect if dither<-1 or dither==-1 but input is TV range.
  59. ###         - Default: 0
  60. ### tvRange - processe the clip as TV Range or not.
  61. ###         - Default: true
  62. ###
  63. ### ----------------------------------------------------------
  64. ###
  65. ### Down10  - get stacked 16-bit input clip and convert it DOWN to 8-16 bit output.
  66. ### ------------
  67. ### clp     - input clip, must be 16-bit clip.
  68. ###         - Defualt: not set
  69. ### depth   - output bit depth, range from 8 to 16.
  70. ###         - Defualt: 10
  71. ### dither  - use dithering if output is a non 16-bit clip,
  72. ###         - when value is in [-1, 100], explaination and strength defined in SmoothAdjust,
  73. ###         - when value is -2, use Ordered dithering,
  74. ###         - when value is -3, use Floyd-Steinberg dithering.
  75. ###         - Default: -3 for TV range and 50 for PC range
  76. ### smooth  - use gradient smoothing if output is a non 16-bit clip,
  77. ###         - explaination and strength defined in SmoothAdjust,
  78. ###         - do NOT have effect if dither<-1 or dither==-1 but input is TV range.
  79. ###         - Default: 0
  80. ### tvRange - processe the clip as TV Range or not.
  81. ###         - Default: true
  82. ### stack   - output stacked msb/lsb (true) or interleaved msb/lsb (false).
  83. ###         - Default: true
  84. ###
  85. ### ----------------------------------------------------------
  86. ###
  87. ### U16     - get 8-bit input clip and UP-convert it to stacked 16 bit output.
  88. ### ------------
  89. ### c       - input clip, must be 8-bit clip.
  90. ###         - Defualt: not set
  91. ### tvRange - processe the clip as TV Range or not.
  92. ###         - Default: true
  93. ###
  94. ### ----------------------------------------------------------
  95. ###
  96. ### C16     - get stacked input clip and CONVERT it to interleaved output, the same as Dither_convey_yuv4xxp16_on_yvxx.
  97. ### ------------
  98. ### c       - input clip.
  99. ###         - Defualt: not set
  100. ###
  101. ###################################################################################################################################
  102. ###
  103. ### Sample 1 (simply output 16-bit, no pp):
  104. ### AviSource(sample.avi)
  105. ### O16()
  106. ###
  107. ### Sample 2 (simply output 10-bit, no pp):
  108. ### AviSource(sample.avi)
  109. ### O16(10)
  110. ###
  111. ### Sample 3 (output 16-bit after pp):
  112. ### AviSource(sample.avi)
  113. ### O16(pp="Dither_resize16(1280, 720)")
  114. ###
  115. ### Sample 4 (output 16-bit after multi-step pp, with """three quotes""" inside String pp):
  116. ### AviSource(sample.avi)
  117. ### pp16=
  118. ### \ """Dither_resize16(648, 480, 4, 0, -5, 0, kernel="spline64")
  119. ### \ Dither_crop16(4, 0, -4, 0)"""
  120. ### O16(pp=pp16)
  121. ###
  122. ### Sample 5 (another example, exactly the same with Sample 4):
  123. ### AviSource(sample.avi)
  124. ### O16(pp=
  125. ### \ """Dither_resize16(648, 480, 4, 0, -5, 0, kernel="spline64")
  126. ### \ Dither_crop16(4, 0, -4, 0)""")
  127. ###
  128. ### Sample 6 (another example, exactly the same with Sample 4, using custom-defined function):
  129. ### AviSource(sample.avi)
  130. ### O16(pp="CustomPP()")
  131. ### FUNCTION CustomPP(clip input){
  132. ###   tmp=input.Dither_resize16(648, 480, 4, 0, -5, 0, kernel="spline64")
  133. ###   pped=tmp.Dither_crop16(4, 0, -4, 0)
  134. ###   return pped
  135. ### }
  136. ###
  137. ### Sample 7 (output 10-bit after pp, using dithering):
  138. ### AviSource(sample.avi)
  139. ### O16(10, pp="Dither_resize16(1280, 720)", dither=0)
  140. ###
  141. ### Sample 8 (output 8-bit after pp in 16-bit precision):
  142. ### AviSource(sample.avi)
  143. ### U16()
  144. ### Dither_resize16(1280, 720)
  145. ### Down10(8)
  146. ###
  147. ###################################################################################################################################
  148.  
  149. FUNCTION O16( clip input, int "depth", string "pp", int "dither", int "smooth", bool "tvRange" ){
  150.  
  151.   Defined(depth) ? Assert( depth>8 && depth<=16 , """O16: "depth" ranges from 9 to 16!""") : nop()
  152.  
  153.   depth   = Default( depth,     16 )
  154.   smooth  = Default( smooth,     0 )
  155.   tvRange = Default( tvRange, true )
  156.   dither  = tvRange ? Default(dither, -3) : Default(dither, 50) # use dither or not
  157.  
  158.   Assert( depth>8 && depth<=16      , """O16: "depth" ranges from 9 to 16!"""       )
  159.   Assert( dither>=-3 && dither<=100 , """Down10: "dither" ranges from -3 to 100!""" )
  160.  
  161.   last = input.U16( tvRange=tvRange )
  162.   pp16 = Defined(pp) ? Eval(pp) : last
  163.  
  164. ###  
  165. ### The following conversion gives the same result as pp16.Down10.C16,
  166. ### but may be a little bit faster when using f3kdb to dither down,
  167. ### which is the default dithering method.
  168. ###
  169.  
  170.   return ( !Defined(pp) && tvRange ) ? input.f3kdb( Y=0, Cb=0, Cr=0, grainY=0, grainC=0,
  171.   \                                                 dither_algo=1,
  172.   \                                                 keep_tv_range=false,
  173.   \                                                 input_mode=0, input_depth=8,
  174.   \                                                 output_depth=depth, output_mode=2 ) :
  175.   \      ( depth==16 ) ? pp16.C16() :
  176.   \      ( dither<-1 || ( dither==-1 && smooth==0 && tvRange ) ) ? pp16.f3kdb( Y=0, Cb=0, Cr=0, grainY=0, grainC=0,
  177.   \                                                                            dither_algo=(-dither),
  178.   \                                                                            keep_tv_range=false,
  179.   \                                                                            input_mode=1, input_depth=16,
  180.   \                                                                            output_depth=depth, output_mode=2 ) :
  181.   \      pp16.Down10(depth=depth, dither=dither, smooth=smooth, tvRange=tvRange).C16()
  182. }
  183.  
  184. ###################################################################################################################################
  185.  
  186. Function Down10( clip clp, int "depth", int "dither", int "smooth", bool "tvRange", bool "stack" ){
  187.  
  188.   tvRange = Default( tvRange, true )
  189.   depth   = Default( depth,     10 ) # bit-depth of output clip
  190.   smooth  = Default( smooth,     0 ) # use smooth or not
  191.   dither  = tvRange ? Default(dither, -3) : Default(dither, 50) # use dither or not
  192.   stack   = Default( stack,   true ) # output stacked msb/lsb or interleaved msb/lsb
  193.  
  194.   Assert( dither>=-3 && dither<=100 , """Down10: "dither" ranges from -3 to 100!""" )
  195.   Assert( depth>=8   && depth<=16   , """Down10: "depth" ranges from 8 to 16!"""    )
  196.  
  197.   shift   = int( Pow( 2, depth - 8 ) )
  198.   ycFloor = int( 16  * shift )
  199.   neutral = int( 128 * shift )
  200.   yCeil   = int( 235 * shift )
  201.   cCeil   = int( 240 * shift )
  202.   ycRge   = int( Pow( 2, depth ) - 1 )
  203.  
  204.   yExpr   = tvRange ? ( "0-0;4096-"+String(ycFloor)+";32768-"+String(neutral)+";60160-"+String(yCeil)+";65535-"+String(ycRge) )
  205.   \                 : ( "0-0;65535-"+String(ycRge) )
  206.   cExpr   = tvRange ? ( "0-0;4096-"+String(ycFloor)+";32768-"+String(neutral)+";61440-"+String(cCeil)+";65535-"+String(ycRge) )
  207.   \                 : ( "0-0;65535-"+String(ycRge) )
  208.  
  209.   smoothDown = clp.SmoothCurve16( Ycurve=yExpr,
  210.   \                               Ucurve=cExpr,
  211.   \                               Vcurve=cExpr,
  212.   \                               interp=0, smooth=smooth, dither=( (dither>-1) ? dither : -1 ),
  213.   \                               limiter=false, TVrange=0 )
  214.  
  215.   down    = ( dither<-1 || ( dither==-1 && smooth==0 && tvRange ) ) ? clp.f3kdb( Y=0, Cb=0, Cr=0, grainY=0, grainC=0,
  216.   \                                                                              dither_algo=(-dither),
  217.   \                                                                              keep_tv_range=false,
  218.   \                                                                              input_mode=1, input_depth=16,
  219.   \                                                                              output_depth=depth, output_mode=( (depth>8) ? stack ? 1 : 2 : 0 ) )
  220.   \                                                                 : ( depth==8 ) ? smoothDown.Get_lsb()
  221.   \                                                                 :   stack      ? smoothDown
  222.   \                                                                                : smoothDown.C16
  223.  
  224.   return ( depth==16 ) ? stack ? clp : clp.C16 : down
  225. }
  226.  
  227. ###################################################################################################################################
  228.  
  229. Function U16( clip c, bool "tvRange" ){
  230.  
  231.   tvRange = Default( tvRange, true )
  232.  
  233.   return tvRange ? c.f3kdb( Y=0, Cb=0, Cr=0, grainY=0, grainC=0,
  234.        \                    keep_tv_range=false,
  235.        \                    input_mode=0, input_depth=8,
  236.        \                    output_depth=16, output_mode=1 )
  237.        \         : StackVertical(c.Gen_null_lsb(), c).SmoothCurve16(Ycurve="0-0;128-32768;255-65535;65535-65535",
  238.        \                                                                           Ucurve="0-0;128-32768;255-65535;65535-65535",
  239.        \                                                                           Vcurve="0-0;128-32768;255-65535;65535-65535",
  240.        \                                                                           interp=0, limiter=false, TVrange=0, smooth=0, dither=-1)
  241. }
  242.  
  243. ###################################################################################################################################
  244.  
  245. Function C16( clip c ){
  246.   return c.f3kdb( Y=0, Cb=0, Cr=0, grainY=0, grainC=0,
  247.   \               keep_tv_range=false,
  248.   \               input_mode=1, output_mode=2 )
  249.  }
  250.  
  251. ###################################################################################################################################
  252.  
  253. Function LowDepth(clip input, int "depth", int "dither", int "smooth", bool "tvRange"){
  254.  
  255.   tvRange = Default( tvRange, true )
  256.   depth   = Default( depth,      8 ) # bit-depth of output clip
  257.   smooth  = Default( smooth,     0 ) # use smooth or not
  258.   dither  = tvRange ? Default(dither, -3) : Default(dither, 50) # use dither or not
  259.  
  260.   Assert( dither>=-3 && dither<=100, """LowDepth: "dither" ranges from -3 to 100!""" )
  261.   Assert( depth<=8   && depth>=1   , """LowDepth: "depth" ranges from 1 to 8!"""     )
  262.  
  263.   StackVertical(input.Gen_null_lsb, input)
  264.   Down10(depth=depth+8, dither=dither, smooth=smooth, tvRange=tvRange)
  265.   Dither_lut16("x "+string(8-depth)+" <<", Y=3, U=3, V=3)
  266.   Get_lsb
  267. }
  268.  
  269. ###################################################################################################################################
  270.  
  271. Function Get_lsb      (clip src){  return src.Crop(0, src.Height/2, src.Width, src.Height/2) }
  272. Function Gen_null_lsb (clip src){  return BlankClip(src, color_yuv=0)                        }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement