Advertisement
Guest User

MCTDmod 08 may 2013

a guest
May 8th, 2013
2,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 129.46 KB | None | 0 0
  1. ###########################################################################################################################
  2. ###                                                                                                                     ###
  3. ###                                     Motion-Compensated Temporal Denoise: MCTD()                                     ###
  4. ###                                                                                                                     ###
  5. ###                                      v1.4.20 by "LaTo INV.", mod4.2 by 06_taro  mod4.3 by AmjadSONY                 ###
  6. ###                                                                                                                     ###
  7. ###                                                    08 may 2013                                                      ###
  8. ###                                                                                                                     ###
  9. ###########################################################################################################################
  10. ###
  11. ###
  12. ###
  13. ### /!\ Needed filters: MVTools (v2.5.11.3/v2.6.0.4), MaskTools (v2.0a48),
  14. ### ------------------- LSFmod (v1.9), FFT3Dgpu (v0.8.2) or FFT3Dfilter (v2.1.1),
  15. ###                     TTempsmooth (v0.9.4), RemoveGrain + Repair (v1.0PR),
  16. ###                     Deblock (v1.2), Deblock_QED (29.Nov.2011), DctFilter (v0.0.1.4),
  17. ###                     EEDI2 (v0.9.2), SangNom (v1.0beta),
  18. ###                     GradFun2DBmod (v1.5), GradFun2db (v1.0), AddGrainC (v1.6.1),
  19. ###                     Dither(v1.18.1).
  20. ###
  21. ###
  22. ###
  23. ### USAGE: MCTD(i, radius, sigma, twopass, useTTmpSm, limit, limitC, limit2, limitC2, post, chroma, interlaced, refine, pMode,
  24. ###             sharp, adapt, strength, SHmode, SHmethod, Slimit, Sovershoot, Tlimit, Tovershoot, ssource, shF,
  25. ###             protect, cutoff, threshold, maxdiff,
  26. ###             AA, useEEDI2, reduc, maxd, AAthr, method, aaF,
  27. ###             deblock, useQED, quant1, quant2,
  28. ###             edgeclean, ECrad, ECthr, ECmode,
  29. ###             stabilize, maxr, lthresh, cthresh, TTstr, stF,
  30. ###             enhance, GFthr, AGstr, bias, temp, dbF,
  31. ###             bwbh, owoh, blksize, overlap,
  32. ###             GPU, fixband, bt, ncpu, precision,
  33. ###             thSAD, thSAD2, thSCD1, thSCD2,
  34. ###             truemotion, MVglobal, pel, pelsearch, search, searchparam, MVsharp, DCT,
  35. ###             p, settings, useMMask,
  36. ###             show, screenW, screenH,
  37. ###             p1nr, p2nr, ppnr, pp)
  38. ###
  39. ###
  40. ###
  41. ### PARAMETERS:
  42. ### -----------
  43. ###
  44. ### +---------+
  45. ### | DENOISE |
  46. ### +---------+-------------------------------------------------------------------------------------------+
  47. ### | radius     : Temporal radius [1...6]                                                                |
  48. ### | sigma      : FFT3D sigma for the pre-filtering clip [0=no pre-filtering,1...]                       |
  49. ### | twopass    : Do the denoising job in 2 stages (stronger but very slow)                              |
  50. ### | useTTmpSm  : Use MDegrain (faster) or MCompensate+TTempsmooth (stronger)                            |
  51. ### | limit      : Limit the effect of the first denoising [-2=luma-adaptive-auto,-1=auto,0=off,1...255]  |
  52. ### | limitC     : Limit the effect of the first denoising on chroma planes                    +--same--+ |
  53. ### | limit2     : Limit the effect of the second denoising (if twopass=true)                  |   as   | |
  54. ### | limit2     : Limit the effect of the second denoising on chroma planes (if twopass=true) +-limit--+ |
  55. ### | post       : Sigma value for post-denoising with FFT3D (before all the PP stage) [0=off,...]        |
  56. ### |              /!\ Absolute value is taken, i.e., post=-2 is valid                                    |
  57. ### |              /!\ When post < 0, do not apply gradfun2db after post-denoising even if fixband==true  |
  58. ### | chroma     : Process or not the chroma plane                                                        |
  59. ### | interlaced : Input video interlaced or not                                                          |
  60. ### | refine     : Refines and recalculate the analyzed motion vector for more robust result or not       |
  61. ### | pMode      : pre-filter mode (0 = off                                                               |
  62. ### |                               1 = Luma-adaptive prefilter to bias to protect dark edges             |
  63. ### |                               2 = 1 plus expanding TV range to PC range for easier motion estimate) |
  64. ### | p1nr       : Use your own denoising filter instead of internal MDegrain/TTempsmooth in the 1st pass |
  65. ### | p2nr       : Use your own denoising filter instead of internal MDegrain/TTempsmooth in the 2nd pass |
  66. ### | ppnr       : Use your own denoising filter instead of internal FFT3D in post-denoising              |
  67. ### |              (Set your filter in string, e.g., p1nr="FluxSmoothST(7, 7)")                           |
  68. ### +-----------------------------------------------------------------------------------------------------+
  69. ###
  70. ###
  71. ### +-------+
  72. ### | SHARP |
  73. ### +-------+-----------------------------------------------------------------------------------------------------------------------------+
  74. ### | sharp      : Enable Post-Sharpening (Spatial-Sharpening + Motion-compensated temporal limiting)                                     |
  75. ### | adapt      : Adaptative sharpening --> sharp only static & detailed areas, prevents grain sharpening & save much bitrate (+-10/15%) |
  76. ### | strength   : Sharpening strength ( see LSFmod: strength  )                                                                          |
  77. ### | SHmode     : Sharpening mode     ( see LSFmod: Smode     )                                                                          |
  78. ### | SHmethod   : Sharpening method   ( see LSFmod: Smethod   )                                                                          |
  79. ### | Slimit     : Spatial limiting    ( see LSFmod: Lmode     )                                                                          |
  80. ### | Sovershoot : Spatial overshoot   ( see LSFmod: overshoot )                                                                          |
  81. ### | Tlimit     : Temporal limiting (Motion compensated, clamp output to Tovershoot)                                                     |
  82. ### | Tovershoot : Temporal overshoot, = Maximum sharpening excess allowed (If Tovershoot=0, don't sharp more than the original clip.)    |
  83. ### |              /!\ If you set Tovershoot>0, be sure that Slimit!=0 (to not over-enhance edges... Slimit=2 is a good compromise)       |
  84. ### | ssource    : Can be either clip or bool                                                                                             |
  85. ### |              If ssource is defined as a clip, MCTD doesn't sharp more a denoised clip than this ssource clip ( see LSFmod: source ) |
  86. ### |              If ssource is bool and is True, MCTD doesn't sharp more a denoised clip than this the input clip                       |
  87. ### | shF        : Use your own sharpening filter instead of internal LSFmod (Set your filter in string, e.g., shF="Sharpen(0.3)")        |
  88. ### +-------------------------------------------------------------------------------------------------------------------------------------+
  89. ###
  90. ###
  91. ### +---------------------------------+
  92. ### | STAR & BRIGHT POINTS PROTECTION |
  93. ### +---------------------------------+---------------------------------------------------------------------------------------+
  94. ### | protect   : Enable star & bright points protection                                                                      |
  95. ### | cutoff    : Luma cutoff (process only brighter areas than this value) [0=all,1...255]                                   |
  96. ### | threshold : Luma threshold (minimal change needed to be filtered, the higher = the lesser areas are filtered) [0...127] |
  97. ### | maxdiff   : Maximum change allowed between source and result (the lesser, the brighter) [0=source,1...255]              |
  98. ### +-------------------------------------------------------------------------------------------------------------------------+
  99. ###
  100. ###
  101. ### +--------------+
  102. ### | ANTIALIASING |
  103. ### +--------------+----------------------------------------------------------------------------------------------------------------------+
  104. ### | AA       : Enable antialiasing (only on edges)                                                                                      |
  105. ### | useEEDI2 : If true use EEDI2 filtering, else use SANGNOM filtering                                                                  |
  106. ### | reduc    : MC-soothe reduc parameter, reduc==-1 turn off "MC-antialiasing" [-1=off,0.0...1.0]                                       |
  107. ### | maxd     : If EEDI2 is used it's his maxd setting, if SANGNOM is used it's his AA setting (see docs of these filters)               |
  108. ### | AAthr    : Threshold for mask (the higher, the less "small edges" are processed) [0...255]                                          |
  109. ### | method   : Sharpness for downsize [0=bilinear,1=spline36,2=lanczos4]                                                                |
  110. ### | aaF      : Use your own anti-aliasing filter instead of internal SANGNOM/EEDI2 (Set your filter in string, e.g., aaF="Sangnom(48)") |
  111. ### +-------------------------------------------------------------------------------------------------------------------------------------+
  112. ###
  113. ###
  114. ### +---------+
  115. ### | DEBLOCK |
  116. ### +---------+-----------------------------------------------------------------------------------+
  117. ### | deblock : Enable deblocking before the denoising                                            |
  118. ### | useQED  : If true, use Deblock_QED, else use Deblock (faster & stronger)                    |
  119. ### | quant1  : Deblock_QED "quant1" parameter (Deblock "quant" parameter is "(quant1+quant2)/2") |
  120. ### | quant2  : Deblock_QED "quant2" parameter (Deblock "quant" parameter is "(quant1+quant2)/2") |
  121. ### +---------------------------------------------------------------------------------------------+
  122. ###
  123. ###
  124. ### +------------------------------+
  125. ### | EDGECLEAN: DERING, DEHALO... |
  126. ### +------------------------------+-----------------------------------------------------------------------------------------------------+
  127. ### | edgeclean : Enable safe edgeclean process after the denoising (only on edges which are in non-detailed areas, so less detail loss) |
  128. ### | ECrad     : Radius for mask (the higher, the greater distance from the edge is filtered)                                           |
  129. ### | ECthr     : Threshold for mask (the higher, the less "small edges" are process) [0...255]                                          |
  130. ### | ECmode    : Script/Filter used for cleaning edges (ex: "dfttest().dehalo_alpha()", don't forget quotes "")                         |
  131. ### +------------------------------------------------------------------------------------------------------------------------------------+
  132. ###
  133. ###
  134. ### +-----------+
  135. ### | STABILIZE |
  136. ### +-----------+------------------------------------------------------------------------------------------------+
  137. ### | stabilize : Enable TTempSmooth post processing to stabilize flat areas (background will be less "nervous") |
  138. ### | maxr      : Temporal radius (the higher, the more stable image)                                            |
  139. ### | lthresh   : luma and motion thesholds (see TTempSmooth docs)                                               |
  140. ### | cthresh   : chroma and motion thesholds (see TTempSmooth docs)                                             |
  141. ### | TTstr     : Strength (see TTempSmooth docs)                                                                |
  142. ### | stF       : Use your own stabilizing filter instead of internal TTempSmoothF (Set your filter in string)   |
  143. ### +------------------------------------------------------------------------------------------------------------+
  144. ###
  145. ###
  146. ### +---------+
  147. ### | ENHANCE |
  148. ### +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
  149. ### | enhance : Enable GradFun2DBmod post processing to enhance flat areas (may avoid banding & blocking when encoding too)                                               |
  150. ### | GFthr   : Gradfun2db threshold [1.0=off,...]                                                                                                                        |
  151. ### | AGstr   : AddGrain strength  [0.0=off,...]                                                                                                                          |
  152. ### | bias    : Brightness bias for adaptative grain mask (the higher, the less grain in dark areas & the more grain in bright areas) [-1=off,0=input,1...254,255=invert] |
  153. ### | temp    : Temporal stabilization for grain (the higher, the more calm) [0=nervous,...,100=static]                                                                   |
  154. ### | dbF     : Use your own debanding filter instead of internal GradFun2dbmod (Set your filter in string, e.g., dbF="f3kdb(31, 32, 0, 0)")                              |
  155. ### |             - filter with stacked 16-bit output is allowed, e.g., dbF="f3kdb(output_depth=16, output_mode=1)"                                                       |
  156. ### +---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  157. ###
  158. ###
  159. ### +---------------------+
  160. ### | BLOCKSIZE / OVERLAP |
  161. ### +---------------------+----------------+
  162. ### | bwbh    : FFT3D blocksize            |
  163. ### | owoh    : FFT3D overlap              |
  164. ### |             - for speed:   bwbh/4    |
  165. ### |             - for quality: bwbh/2    |
  166. ### | blksize : MVTools blocksize          |
  167. ### | overlap : MVTools overlap            |
  168. ### |             - for speed:   blksize/4 |
  169. ### |             - for quality: blksize/2 |
  170. ### +--------------------------------------+
  171. ###
  172. ###
  173. ### +-------+
  174. ### | FFT3D |
  175. ### +-------+---------------------------------------+
  176. ### | GPU     : Use FFT3Dgpu instead of FFT3Dfilter |
  177. ### | fixband : Prevent banding issue with FFT3D    |
  178. ### +---------------------------------------+-------+
  179. ### | bt        : FFT3D block temporal size |
  180. ### | ncpu      : FFT3Dfilter ncpu          |
  181. ### | precision : FFT3Dgpu precision        |
  182. ### +---------------------------------------+
  183. ###
  184. ###
  185. ### +---------+
  186. ### | MVTOOLS |
  187. ### +---------+----------------------------------------------------+
  188. ### | thSAD  : MVTools thSAD for the first pass                    |
  189. ### | thSAD2 : MVTools thSAD for the second pass (if twopass=true) |
  190. ### | thSCD1 : MVTools thSCD1                                      |
  191. ### | thSCD2 : MVTools thSCD2                                      |
  192. ### +-----------------------------------+--------------------------+
  193. ### | truemotion  : MVTools truemotion  |
  194. ### | MVglobal    : MVTools global      |
  195. ### | pel         : MVTools pel         |
  196. ### | pelsearch   : MVTools pelsearch   |
  197. ### | search      : MVTools search      |
  198. ### | searchparam : MVTools searchparam |
  199. ### | MVsharp     : MVTools sharp       |
  200. ### | DCT         : MVTools DCT         |
  201. ### +-----------------------------------+
  202. ###
  203. ###
  204. ### +--------+
  205. ### | GLOBAL |
  206. ### +--------+--------------------------------------------------------------------------------------+
  207. ### | p        : Set an external prefilter clip                                                     |
  208. ### | settings : Global MCTD settings [default="low"]                                               |
  209. ### |             - "very low"                                                                      |
  210. ### |             - "low"                                                                           |
  211. ### |             - "medium"                                                                        |
  212. ### |             - "high"                                                                          |
  213. ### |             - "very high"                                                                     |
  214. ### | useMMask : Enable motion mask for sharp/stabilize/enhance along with edge mask [default=true] |
  215. ### | pp       : Enable MCTDmod_PP for post-processing [default=true]                                  |
  216. ### +-----------------------------------------------------------------------------------------------+
  217. ###
  218. ###
  219. ### +-------+
  220. ### | DEBUG |
  221. ### +-------+------------------------------------------------+
  222. ### | show    : Show debug clip & informations               |
  223. ### | screenW : Screen horizontal resolution (for show clip) |
  224. ### | screenH : Screen vertical resolution (for show clip)   |
  225. ### +--------------------------------------------------------+
  226. ###
  227. ###
  228. ###
  229. ### DEFAULTS:
  230. ### ---------
  231. ###
  232. ### +-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
  233. ### | SETTINGS    |      VERY LOW               |      LOW                    |      MEDIUM                 |      HIGH                   |      VERY HIGH              |
  234. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  235. ### | radius      |      1                      |      2                      |      3                      |      2                      |      3                      |
  236. ### | sigma       |      2                      |      4                      |      8                      |      12                     |      16                     |
  237. ### | twopass     |      false                  |      false                  |      false                  |      true                   |      true                   |
  238. ### | useTTmpSm   |      false                  |      false                  |      false                  |      false                  |      false                  |
  239. ### | limit       |      -1                     |      -1                     |      -1                     |      -1                     |      0                      |
  240. ### | limitC      |      same as limit          |      same as limit          |      same as limit          |      same as limit          |      same as limit          |
  241. ### | limit2      |      -1                     |      -1                     |      -1                     |      0                      |      0                      |
  242. ### | limitC2     |      same as limit2         |      same as limit2         |      same as limit2         |      same as limit2         |      same as limit2         |
  243. ### | post        |      0                      |      0                      |      0                      |      0                      |      0                      |
  244. ### | chroma      |      false                  |      false                  |      true                   |      true                   |      true                   |
  245. ### | interlaced  |      false                  |      false                  |      false                  |      false                  |      false                  |
  246. ### | refine      |      false                  |      false                  |      false                  |      false                  |      false                  |
  247. ### | pMode       |      0                      |      0                      |      0                      |      0                      |      0                      |
  248. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  249. ### | sharp       |      true                   |      true                   |      true                   |      true                   |      true                   |
  250. ### | adapt       |      true                   |      true                   |      true                   |      true                   |      true                   |
  251. ### | strength    |      100                    |      100                    |      150                    |      200                    |      200                    |
  252. ### | SHmode      |      3                      |      3                      |      3                      |      3                      |      3                      |
  253. ### | SHmethod    |      3                      |      3                      |      3                      |      3                      |      3                      |
  254. ### | Slimit      |      0                      |      0                      |      0                      |      0                      |      0                      |
  255. ### | Sovershoot  |      0                      |      0                      |      0                      |      0                      |      0                      |
  256. ### | Tlimit      |      true                   |      true                   |      true                   |      true                   |      true                   |
  257. ### | Tovershoot  |      2                      |      2                      |      1                      |      0                      |      0                      |
  258. ### | ssource     |      false                  |      false                  |      false                  |      false                  |      false                  |
  259. ### | shF         |      "internal"             |      "internal"             |      "internal"             |      "internal"             |      "internal"             |
  260. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  261. ### | protect     |      false                  |      false                  |      false                  |      false                  |      false                  |
  262. ### | cutoff      |      64                     |      64                     |      64                     |      64                     |      64                     |
  263. ### | threshold   |      16                     |      16                     |      16                     |      16                     |      16                     |
  264. ### | maxdiff     |      4                      |      4                      |      4                      |      4                      |      4                      |
  265. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  266. ### | AA          |      false                  |      false                  |      false                  |      false                  |      false                  |
  267. ### | useEEDI2    |      true                   |      true                   |      false                  |      false                  |      false                  |
  268. ### | reduc       |      0.5                    |      0.5                    |      0.5                    |      0.5                    |      0.5                    |
  269. ### | maxd        |      24                     |      24                     |      48                     |      48                     |      48                     |
  270. ### | AAthr       |      64                     |      32                     |      32                     |      16                     |      16                     |
  271. ### | method      |      2                      |      1                      |      1                      |      0                      |      0                      |
  272. ### | aaF         |      "internal"             |      "internal"             |      "internal"             |      "internal"             |      "internal"             |
  273. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  274. ### | deblock     |      false                  |      false                  |      false                  |      false                  |      false                  |
  275. ### | useQED      |      true                   |      true                   |      true                   |      false                  |      false                  |
  276. ### | quant1      |      10                     |      20                     |      30                     |      30                     |      40                     |
  277. ### | quant2      |      20                     |      40                     |      60                     |      60                     |      80                     |
  278. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  279. ### | edgeclean   |      false                  |      false                  |      false                  |      false                  |      false                  |
  280. ### | ECrad       |      1                      |      2                      |      3                      |      4                      |      5                      |
  281. ### | ECthr       |      32                     |      32                     |      32                     |      16                     |      16                     |
  282. ### | ECmode      |      "Removegrain(2,0)"     |      "Removegrain(2,0)"     |      "Removegrain(2,2)"     |      "Removegrain(4,4)"     |      "Removegrain(4,4)"     |
  283. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  284. ### | stabilize   |      false                  |      false                  |      false                  |      true                   |      true                   |
  285. ### | maxr        |      1                      |      1                      |      2                      |      2                      |      2                      |
  286. ### | lthresh     |      4                      |      4                      |      4                      |      4                      |      4                      |
  287. ### | cthresh     |      5                      |      5                      |      5                      |      5                      |      5                      |
  288. ### | TTstr       |      1                      |      1                      |      1                      |      2                      |      2                      |
  289. ### | stF         |      "internal"             |      "internal"             |      "internal"             |      "internal"             |      "internal"             |
  290. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  291. ### | enhance     |      true                   |      true                   |      false                  |      false                  |      false                  |
  292. ### | GFthr       |      1.1                    |      1.2                    |      1.4                    |      1.6                    |      1.8                    |
  293. ### | AGstr       |      0.6                    |      0.8                    |      1.2                    |      1.8                    |      2.4                    |
  294. ### | bias        |      64                     |      64                     |      64                     |      64                     |      64                     |
  295. ### | temp        |      50                     |      50                     |      50                     |      50                     |      50                     |
  296. ### | dbF         |      "internal"             |      "internal"             |      "internal"             |      "internal"             |      "internal"             |
  297. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  298. ### | bwbh        |      HD?16:8                |      HD?16:8                |      HD?16:8                |      HD?16:8                |      HD?16:8                |
  299. ### | owoh        |      HD? 8:4                |      HD? 8:4                |      HD? 8:4                |      HD? 8:4                |      HD? 8:4                |
  300. ### | blksize     |      HD?16:8                |      HD?16:8                |      HD?16:8                |      HD?16:8                |      HD?16:8                |
  301. ### | overlap     |      HD? 8:4                |      HD? 8:4                |      HD? 8:4                |      HD? 8:4                |      HD? 8:4                |
  302. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  303. ### | GPU         |      false                  |      false                  |      false                  |      false                  |      false                  |
  304. ### | fixband     |      true                   |      true                   |      true                   |      true                   |      true                   |
  305. ### | bt          |      1                      |      3                      |      3                      |      3                      |      4                      |
  306. ### | ncpu        |      1                      |      1                      |      1                      |      1                      |      1                      |
  307. ### | precision   |      2                      |      2                      |      2                      |      2                      |      2                      |
  308. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  309. ### | thSAD       |      200                    |      300                    |      400                    |      500                    |      600                    |
  310. ### | thSAD2      |      200                    |      300                    |      400                    |      500                    |      600                    |
  311. ### | thSCD1      |      200                    |      300                    |      400                    |      500                    |      600                    |
  312. ### | thSCD2      |      90                     |      100                    |      100                    |      130                    |      130                    |
  313. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  314. ### | truemotion  |      false                  |      false                  |      false                  |      false                  |      false                  |
  315. ### | MVglobal    |      true                   |      true                   |      true                   |      true                   |      true                   |
  316. ### | pel         |      1                      |      2                      |      2                      |      2                      |      2                      |
  317. ### | pelsearch   |      1                      |      2                      |      2                      |      2                      |      2                      |
  318. ### | search      |      2                      |      2                      |      2                      |      2                      |      2                      |
  319. ### | searchparam |      2                      |      2                      |      2                      |      2                      |      2                      |
  320. ### | MVsharp     |      2                      |      2                      |      2                      |      1                      |      0                      |
  321. ### | DCT         |      0                      |      0                      |      0                      |      0                      |      0                      |
  322. ### |-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------|
  323. ### | show        |      false                  |      false                  |      false                  |      false                  |      false                  |
  324. ### | screenW     |      1280                   |      1280                   |      1280                   |      1280                   |      1280                   |
  325. ### | screenH     |      1024                   |      1024                   |      1024                   |      1024                   |      1024                   |
  326. ### +-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
  327. ###
  328. ###
  329. ###
  330. ### MCTD's Post Processor :
  331. ### ------------------------------------
  332. ###
  333. ### USAGE: MCTDmod_PP( denoised, source,
  334. ###                 sharp, adapt, strength, SHmode, SHmethod, Slimit, Sovershoot, Tlimit, Tovershoot, ssource, shF,
  335. ###                 protect, cutoff, threshold, maxdiff,
  336. ###                 AA, useEEDI2, reduc, maxd, AAthr, method, aaF,
  337. ###                 deblock, useQED, quant1, quant2,
  338. ###                 edgeclean, ECrad, ECthr, ECmode,
  339. ###                 stabilize, maxr, lthresh, cthresh, TTstr, stF,
  340. ###                 enhance, GFthr, AGstr, bias, temp, dbF,
  341. ###                 chroma, settings, useMMask )
  342. ###
  343. ### This is the post-processor used internally in MCTD.
  344. ### It can be called externally if you want some specific functionality without denoising.
  345. ###
  346. ### Parameters & defaults are the same as MCTD.
  347. ### By default, all filters are OFF!
  348. ###
  349. ### "Sharp" & "Protect" options need 2 inputs:
  350. ### EX: source=last
  351. ###     denoised=source.FFT3Dfilter()
  352. ###     MCTDmod_PP(denoised,source,...)
  353. ### The others only need "denoised".
  354. ###
  355. ###
  356. ###
  357. ### CHANGELOG:
  358. ### ----------
  359. ###
  360. ### v1.4.20 mod4.3: fix ""I don't know what "refine" means"" When the MCTemporalDenoise and MCTD in auto-load folder
  361. ###
  362. ### v1.4.20 mod4.2: fix documents and code comments of pMode
  363. ###
  364. ### v1.4.20 mod4.1: recalculate motion vectors for the second pass if refine=True
  365. ###                 recalculate motion masks and motion compensated frames of delta=1 for the second pass properly
  366. ###                 try to use native MDegrainN when radius > 3, but still keep backward capability with official mvtools2
  367. ###
  368. ### v1.4.20 mod4: rework on pMode's adaptive pre-filter, use [0:255 16:255 75:0 255:0] gradient mask map instead of [0:x bias:255 255:y](x=0 or y=0)
  369. ###
  370. ### v1.4.20 mod3.8: only use the finest hierarchical level for MSuper when capable
  371. ###                 added "refine" to recalculate super clip for more robust motion estimation
  372. ###                 added "pMode" to use adaptviely pre-filtered or even range-expanded clip for motion search
  373. ###                 added negative "sigma" value to use dfttest as a frequency-based adaptive pre-filter
  374. ###                 separate "limitC" and "limitC2" from "limit" and "limit2" to define different limit value for chroma planes
  375. ###                 added -2 for limit* to use luma-adaptive auto-limit mode
  376. ###
  377. ### v1.4.20 mod3.7: bug fixes
  378. ###                 added "ssource" as lsfmod's "source" clip
  379. ###
  380. ### v1.4.20 mod3.6: adjusted merge16_8 usage to latest version of dither package
  381. ###
  382. ### v1.4.20 mod3.5: now accept negative value of post as sigma ( was only used to define whether fixband is used on post-denoise, and must be used with custom "ppnr" )
  383. ###                 added "useMMask" in MCTD to turn motion mask on/off for sharpen/stabilize/enhance
  384. ###
  385. ### v1.4.20 mod3.4: fully supported Y/U/V in -65535~5 of MCPP_merge16_8, although MCTDmod_PP only needs value of 1/3
  386. ###
  387. ### v1.4.20 mod3.3: added "stF" to allow custom stabilizing filter
  388. ###
  389. ### v1.4.20 mod3.2: made lthresh/cthresh changeable
  390. ###
  391. ### v1.4.20 mod3.1: added "useMMask" in MCTDmod_PP to turn motion mask on/off for sharpen/stabilize/enhance
  392. ###                 changed the type of "sigma" from int to float
  393. ###
  394. ### v1.4.20 mod3: fixed scene change frames not being filtered correctly with sharp/stabilize/enhance
  395. ###               added "pp" in MCTD to turn all post-processing in MCTDmod_PP off
  396. ###               supported stacked 16-bit output for custom "dbF" ( mainly implemented by SAPikachu )
  397. ###
  398. ### v1.4.20 mod2: removed deblock mod16 warnings when deblock=true, pad and crop internally
  399. ###               updated Deblock_QED with custom bug fix
  400. ###
  401. ### v1.4.20 mod : added "p1nr", "p2nr", "ppnr", "shF", "aaF", "dbF" to allow custom filters
  402. ###
  403. ### v1.4.20 : tweaked some settings
  404. ###           updated QED_Deblock version
  405. ###
  406. ### v1.4.18 : fixed anti-aliasing PP (was broken since v1.4.07)
  407. ###           updated MVTools-MaskTools-AddgrainC version
  408. ###
  409. ### v1.4.16 : added switch to avoid loading of unused filters
  410. ###           fixed a bug in anti-aliasing part
  411. ###
  412. ### v1.4.08 : fixed MOD16 warning when deblock=true
  413. ###
  414. ### v1.4.07 : improved masking in PP-stage
  415. ###           cleaned code (cosmetic)
  416. ###
  417. ### v1.4.00 : fixed bug with maxdiff=0
  418. ###           fixed speed issue in MCTemporalDenoisePP (+20%)
  419. ###           added "deblock" to MCTemporalDenoisePP
  420. ###           changed "flat" to "enhance"
  421. ###           cleaned and optimized all the code
  422. ###           changed some settings
  423. ###           updated documentation
  424. ###
  425. ### v1.3.09 : added MOD4 restriction
  426. ###           updated MVTools version
  427. ###
  428. ### v1.3.00 : added a post-denoising stage (see "post")
  429. ###           fixed a bug with fixFFT3D and defined p clip
  430. ###           changed "very high" to be less insane
  431. ###           renamed fixFFT3D to fixband
  432. ###           cleaned some stuff
  433. ###           adapted to LSFmod v1.8
  434. ###
  435. ### v1.2.02 : changed default gpu=true to gpu=false (set gpu=true if you want speed!!!)
  436. ###           adapted to LSFmod v1.7
  437. ###
  438. ### v1.2.00 : adapted for LSFmod v1.6 & GradFun2DBmod v1.3
  439. ###           changed sharp int->bool (deleted sharp=1)
  440. ###
  441. ### v1.1.27 : fixed a bug in MCTemporalDenoisePP with chroma=false
  442. ###           adapted for LSFmod v1.5
  443. ###
  444. ### v1.1.26 : changed "flat" post-processing to use GradFun2DBmod
  445. ###           changed int->float for AGstr
  446. ###           fixed a small bug in AA
  447. ###
  448. ### v1.1.24 : fixed a chroma bug with PP
  449. ###
  450. ### v1.1.23 : adapted script for MvTools v2.3.0 (deleted MT & idx parameters)
  451. ###           changed MCTemporalDenoisePP's defaults (now the same as MCTemporalDenoise)
  452. ###           deleted safe parameter (slow & useless)
  453. ###           changed some minor things
  454. ###
  455. ### v1.0.03 : fixed a bug in MCTemporalDenoisePP
  456. ###
  457. ### v1.0.02 : changed defaults according to new LSFmod v1.2
  458. ###
  459. ### v1.0.01 : fixed a speed issue in MCTemporalDenoisePP (introduced in beta5)
  460. ###           changed documentation about MCTemporalDenoisePP
  461. ###
  462. ### v1.0.00 : first stable version
  463. ###           changed sharp=2, need LSFmod v1.0 !
  464. ###           fixed a bug introduced in beta5
  465. ###
  466. ### 1.0beta5: added new sharpening routine & deleted LSF (SHmode,Slimit,Sovershoot,Tlimit,Tovershoot)
  467. ###           changed some code (cosmetic)
  468. ###
  469. ### 1.0beta4: changed some descriptions & blksize default (submitted by Didée)
  470. ###           added auto blksize setting (16:HD & 8:SD, if you want speed use 16)
  471. ###
  472. ### 1.0beta3: all post-processing filters are now usable as a standalone function (MCTemporalDenoisePP)
  473. ###           added MC-antialiasing (see reduc parameter)
  474. ###
  475. ### 1.0beta2: fixed MOD16 restriction (effective only if deblock=true)
  476. ###           small speedup/fix with limit>0 and/or protect=true&maxdiff>0
  477. ###           added warning about GPU & MT in the documentation
  478. ###
  479. ### 1.0beta1: changed name Udegrain --> MCTemporalDenoise (to be more specific and avoid confusion with chroma)
  480. ###           added the choice to use MDegrain (since the chroma bug in MDegrain is fixed, +50% speed)
  481. ###           added twopass option to do the denoising job in two stage (very effective but slow)
  482. ###           added MT parameter for MVTools (be careful +- unstable)
  483. ###           added fixFFT3D parameter to prevent banding/posterize with limit=-1
  484. ###           added stabilize options (stabilize,maxr,TTstr)
  485. ###           added deblock options (deblock,useQED,quant1,quant2)
  486. ###           added new presets "light"(=low)/"heavy"(=medium) --> 5 presets (very high is 'a bit' extrem)
  487. ###           added check/warning for some settings
  488. ###           added thSAD2 & limit2 parameters for twopass=true
  489. ###           added overrange parameter for controling sharpness (useful for sharpening more than the source)
  490. ###           changed +- much code to implement twopass/MDegrain/MT, should be bug free (I hope)
  491. ###           changed sharp->MVsharp & post->sharp
  492. ###           changed "light Smode4" --> LimitedSharpenFaster for sharp=2 (strength,Smode,overshoot)
  493. ###           reduced max radius to 6
  494. ###           optimized chroma in PP stage (+20% with all PP on)
  495. ###           fixed a bug with external p clip
  496. ###           fixed a bug in Star&Bright Point Protection with maxdiff>0
  497. ###           added YV12&MOD16 restriction
  498. ###           changed some settings due to new presets
  499. ###           restriction when using MT=true: bt=1 in FFT3Dgpu & plane=4 in MDegrainMulti (will be removed when fixed)
  500. ###
  501. ### v0.9.05a: changed version number: vX.XX branch -> v0.X.XX branch
  502. ###           added AA options (AA,useEEDI2,maxd,AAthr,method)
  503. ###           changed dering --> edgeclean: you can now use your own script
  504. ###           fixed bug with show=true
  505. ###           fixed bug with chroma in PP stage
  506. ###           fixed omission with chroma=false in MAnalyse
  507. ###           changed some settings
  508. ###           added changelog in .avsi
  509. ###
  510. ### v0.9.01a: added adapt & SS for sharpening
  511. ###           added dering option (dering, DRGrad, DRGthr, RGmode)
  512. ###           added flat areas processing (flat, AGstr, GFthr, bias)
  513. ###           added modX(=blocksize) checking
  514. ###           changed some default settings
  515. ###           updated documentation in .avsi
  516. ###
  517. ### v0.8.08c: fixed bug with chroma=false & interlaced=true
  518. ###
  519. ### v0.8.08b: added mocomp contra-sharpening (basic contra-sharpening always usable)
  520. ###
  521. ### v0.7.29a: added star & bright points protection
  522. ###           changed some default options
  523. ###           added more options for FFT3D
  524. ###           added YV12 checking
  525. ###
  526. ### v0.7.25a: changed to chroma=false for light settings
  527. ###           added temporal limiter for sharpening
  528. ###
  529. ### v0.7.24a: replaced blankclip by nop
  530. ###
  531. ### v0.7.23a: first public release
  532. ###
  533. ####################################################################################################################################
  534.  
  535.  
  536.  
  537. function MCTD( clip i, int "radius", float "sigma", bool "twopass", bool "useTTmpSm", int "limit", int "limitC", int "limit2", int "limitC2", float "post",
  538. \              bool "chroma", bool "interlaced", bool "refine", int "pMode",
  539. \              bool "sharp", bool "adapt", int "strength", int "SHmode", int "SHmethod", int "Slimit", int "Sovershoot", bool "Tlimit", int "Tovershoot", val "ssource", string "shF",
  540. \              bool "protect", int "cutoff", int "threshold", int "maxdiff",
  541. \              bool "AA", bool "useEEDI2", float "reduc", int "maxd", int "AAthr", int "method", string "aaF",
  542. \              bool "deblock", bool "useQED", int "quant1", int "quant2",
  543. \              bool "edgeclean", int "ECrad", int "ECthr", string "ECmode",
  544. \              bool "stabilize", int "maxr", int "lthresh", int "cthresh", int "TTstr", string "stF",
  545. \              bool "enhance", float "GFthr", float "AGstr", int "bias", int "temp", string "dbF",
  546. \              int "bwbh", int "owoh", int "blksize", int "overlap",
  547. \              bool "GPU", bool "fixband", int "bt", int "ncpu", int "precision",
  548. \              int "thSAD", int "thSAD2", int "thSCD1", int "thSCD2",
  549. \              bool "truemotion", bool "MVglobal", int "pel", int "pelsearch", int "search", int "searchparam", int "MVsharp", int "DCT",
  550. \              clip "p", string "settings", bool "useMMask",
  551. \              bool "show", int "screenW", int "screenH",
  552. \              string "p1nr", string "p2nr", string "ppnr", bool "pp" )
  553. {
  554. ### VERSION
  555. version = "v1.4.20 mod3.8"
  556. Assert(isYV12(i) == True ? true : false, chr(10) + "This is not an YV12 clip ! Please convert color space to YV12 before using MCTD()" + chr(10))
  557.  
  558.  
  559. ### SETTINGS
  560. settings     = Default(settings, "low")
  561. useMMask     = Default(useMMask,  true)
  562. pp           = Default(pp,        true)
  563. settings_num =  -1
  564. settings_num = (settings == "very low")  ? 0 : settings_num
  565. settings_num = (settings == "low")       ? 1 : settings_num
  566. settings_num = (settings == "medium")    ? 2 : settings_num
  567. settings_num = (settings == "high")      ? 3 : settings_num
  568. settings_num = (settings == "very high") ? 4 : settings_num
  569. Assert(settings_num >=0 ? true : false, chr(10) + "These settings do not exist !" + chr(10))
  570.  
  571.    function MCTDmod_Defaults(clip clp, int "settings_num", string "setting")
  572.    {
  573.    HD = (clp.width > 1024 || clp.height > 576) ? true : false
  574.  
  575.    ### DEFAULTS                     #          VERY LOW           #             LOW             #            MEDIUM           #            HIGH             #          VERY HIGH          #
  576.    radius      = Select(settings_num,      1                      ,      2                      ,      3                      ,      2                      ,      3                      )
  577.    sigma       = Select(settings_num,      2                      ,      4                      ,      8                      ,      12                     ,      16                     )
  578.    twopass     = Select(settings_num,      false                  ,      false                  ,      false                  ,      true                   ,      true                   )
  579.    useTTmpSm   = Select(settings_num,      false                  ,      false                  ,      false                  ,      false                  ,      false                  )
  580.    limit       = Select(settings_num,      -1                     ,      -1                     ,      -1                     ,      -1                     ,      0                      )
  581.    limit2      = Select(settings_num,      -1                     ,      -1                     ,      -1                     ,      0                      ,      0                      )
  582.    post        = Select(settings_num,      0                      ,      0                      ,      0                      ,      0                      ,      0                      )
  583.    chroma      = Select(settings_num,      false                  ,      false                  ,      true                   ,      true                   ,      true                   )
  584.    interlaced  = Select(settings_num,      false                  ,      false                  ,      false                  ,      false                  ,      false                  )
  585.    refine      = Select(settings_num,      false                  ,      false                  ,      false                  ,      false                  ,      false                  )
  586.    pMode       = Select(settings_num,      0                      ,      0                      ,      0                      ,      0                      ,      0                      )
  587.  
  588.    sharp       = Select(settings_num,      true                   ,      true                   ,      true                   ,      true                   ,      true                   )
  589.    adapt       = Select(settings_num,      true                   ,      true                   ,      true                   ,      true                   ,      true                   )
  590.    strength    = Select(settings_num,      100                    ,      100                    ,      150                    ,      200                    ,      200                    )
  591.    SHmode      = Select(settings_num,      3                      ,      3                      ,      3                      ,      3                      ,      3                      )
  592.    SHmethod    = Select(settings_num,      3                      ,      3                      ,      3                      ,      3                      ,      3                      )
  593.    Slimit      = Select(settings_num,      0                      ,      0                      ,      0                      ,      0                      ,      0                      )
  594.    Sovershoot  = Select(settings_num,      0                      ,      0                      ,      0                      ,      0                      ,      0                      )
  595.    Tlimit      = Select(settings_num,      true                   ,      true                   ,      true                   ,      true                   ,      true                   )
  596.    Tovershoot  = Select(settings_num,      2                      ,      2                      ,      1                      ,      0                      ,      0                      )
  597.    ssource     = Select(settings_num,      false                  ,      false                  ,      false                  ,      false                  ,      false                  )
  598.    shF         = Select(settings_num,      "internal"             ,      "internal"             ,      "internal"             ,      "internal"             ,      "internal"             )
  599.  
  600.    protect     = Select(settings_num,      false                  ,      false                  ,      false                  ,      false                  ,      false                  )
  601.    cutoff      = Select(settings_num,      64                     ,      64                     ,      64                     ,      64                     ,      64                     )
  602.    threshold   = Select(settings_num,      16                     ,      16                     ,      16                     ,      16                     ,      16                     )
  603.    maxdiff     = Select(settings_num,      4                      ,      4                      ,      4                      ,      4                      ,      4                      )
  604.  
  605.    AA          = Select(settings_num,      false                  ,      false                  ,      false                  ,      false                  ,      false                  )
  606.    useEEDI2    = Select(settings_num,      true                   ,      true                   ,      false                  ,      false                  ,      false                  )
  607.    reduc       = Select(settings_num,      0.5                    ,      0.5                    ,      0.5                    ,      0.5                    ,      0.5                    )
  608.    maxd        = Select(settings_num,      24                     ,      24                     ,      48                     ,      48                     ,      48                     )
  609.    AAthr       = Select(settings_num,      64                     ,      32                     ,      32                     ,      16                     ,      16                     )
  610.    method      = Select(settings_num,      2                      ,      1                      ,      1                      ,      0                      ,      0                      )
  611.    aaF         = Select(settings_num,      "internal"             ,      "internal"             ,      "internal"             ,      "internal"             ,      "internal"             )
  612.  
  613.    deblock     = Select(settings_num,      false                  ,      false                  ,      false                  ,      false                  ,      false                  )
  614.    useQED      = Select(settings_num,      true                   ,      true                   ,      true                   ,      false                  ,      false                  )
  615.    quant1      = Select(settings_num,      10                     ,      20                     ,      30                     ,      30                     ,      40                     )
  616.    quant2      = Select(settings_num,      20                     ,      40                     ,      60                     ,      60                     ,      80                     )
  617.  
  618.    edgeclean   = Select(settings_num,      false                  ,      false                  ,      false                  ,      false                  ,      false                  )
  619.    ECrad       = Select(settings_num,      1                      ,      2                      ,      3                      ,      4                      ,      5                      )
  620.    ECthr       = Select(settings_num,      64                     ,      32                     ,      32                     ,      16                     ,      16                     )
  621.    ECmode      = Select(settings_num,      "Removegrain(2,0)"     ,      "Removegrain(2,0)"     ,      "Removegrain(2,2)"     ,      "Removegrain(4,4)"     ,      "Removegrain(4,4)"     )
  622.  
  623.    stabilize   = Select(settings_num,      false                  ,      false                  ,      false                  ,      true                   ,      true                   )
  624.    maxr        = Select(settings_num,      1                      ,      1                      ,      2                      ,      2                      ,      2                      )
  625.    lthresh     = Select(settings_num,      4                      ,      4                      ,      4                      ,      4                      ,      4                      )
  626.    cthresh     = Select(settings_num,      5                      ,      5                      ,      5                      ,      5                      ,      5                      )
  627.    TTstr       = Select(settings_num,      1                      ,      1                      ,      1                      ,      2                      ,      2                      )
  628.    stF         = Select(settings_num,      "internal"             ,      "internal"             ,      "internal"             ,      "internal"             ,      "internal"             )
  629.  
  630.    enhance     = Select(settings_num,      true                   ,      true                   ,      false                  ,      false                  ,      false                  )
  631.    GFthr       = Select(settings_num,      1.1                    ,      1.2                    ,      1.4                    ,      1.6                    ,      1.8                    )
  632.    AGstr       = Select(settings_num,      0.6                    ,      0.8                    ,      1.2                    ,      1.8                    ,      2.4                    )
  633.    bias        = Select(settings_num,      64                     ,      64                     ,      64                     ,      64                     ,      64                     )
  634.    temp        = Select(settings_num,      50                     ,      50                     ,      50                     ,      50                     ,      50                     )
  635.    dbF         = Select(settings_num,      "internal"             ,      "internal"             ,      "internal"             ,      "internal"             ,      "internal"             )
  636.  
  637.    bwbh        = Select(settings_num,      HD?16:8                ,      HD?16:8                ,      HD?16:8                ,      HD?16:8                ,      HD?16:8                )
  638.    owoh        = Select(settings_num,      HD? 8:4                ,      HD? 8:4                ,      HD? 8:4                ,      HD? 8:4                ,      HD? 8:4                )
  639.    blksize     = Select(settings_num,      HD?16:8                ,      HD?16:8                ,      HD?16:8                ,      HD?16:8                ,      HD?16:8                )
  640.    overlap     = Select(settings_num,      HD? 8:4                ,      HD? 8:4                ,      HD? 8:4                ,      HD? 8:4                ,      HD? 8:4                )
  641.  
  642.    GPU         = Select(settings_num,      false                  ,      false                  ,      false                  ,      false                  ,      false                  )
  643.    fixband     = Select(settings_num,      true                   ,      true                   ,      true                   ,      true                   ,      true                   )
  644.    bt          = Select(settings_num,      1                      ,      3                      ,      3                      ,      3                      ,      4                      )
  645.    ncpu        = Select(settings_num,      1                      ,      1                      ,      1                      ,      1                      ,      1                      )
  646.    precision   = Select(settings_num,      2                      ,      2                      ,      2                      ,      2                      ,      2                      )
  647.  
  648.    thSAD       = Select(settings_num,      200                    ,      300                    ,      400                    ,      500                    ,      600                    )
  649.    thSAD2      = Select(settings_num,      200                    ,      300                    ,      400                    ,      500                    ,      600                    )
  650.    thSCD1      = Select(settings_num,      200                    ,      300                    ,      400                    ,      500                    ,      600                    )
  651.    thSCD2      = Select(settings_num,      90                     ,      100                    ,      100                    ,      130                    ,      130                    )
  652.  
  653.    truemotion  = Select(settings_num,      false                  ,      false                  ,      false                  ,      false                  ,      false                  )
  654.    MVglobal    = Select(settings_num,      true                   ,      true                   ,      true                   ,      true                   ,      true                   )
  655.    pel         = Select(settings_num,      1                      ,      2                      ,      2                      ,      2                      ,      2                      )
  656.    pelsearch   = Select(settings_num,      1                      ,      2                      ,      2                      ,      2                      ,      2                      )
  657.    search      = Select(settings_num,      2                      ,      2                      ,      2                      ,      2                      ,      2                      )
  658.    searchparam = Select(settings_num,      2                      ,      2                      ,      2                      ,      2                      ,      2                      )
  659.    MVsharp     = Select(settings_num,      2                      ,      2                      ,      2                      ,      1                      ,      0                      )
  660.    DCT         = Select(settings_num,      0                      ,      0                      ,      0                      ,      0                      ,      0                      )
  661.  
  662.    show        = Select(settings_num,      false                  ,      false                  ,      false                  ,      false                  ,      false                  )
  663.    screenW     = Select(settings_num,      1280                   ,      1280                   ,      1280                   ,      1280                   ,      1280                   )
  664.    screenH     = Select(settings_num,      1024                   ,      1024                   ,      1024                   ,      1024                   ,      1024                   )
  665.  
  666.    return Eval(setting)
  667.    }
  668.  
  669. radius      = default(radius,       MCTDmod_Defaults( i, settings_num, "radius"      ) )
  670. sigma       = default(sigma,        MCTDmod_Defaults( i, settings_num, "sigma"       ) )
  671. twopass     = default(twopass,      MCTDmod_Defaults( i, settings_num, "twopass"     ) )
  672. useTTmpSm   = default(useTTmpSm,    MCTDmod_Defaults( i, settings_num, "useTTmpSm"   ) )
  673. limit       = default(limit,        MCTDmod_Defaults( i, settings_num, "limit"       ) )
  674. limitC      = default(limitC,       limit                                           )
  675. limit2      = default(limit2,       MCTDmod_Defaults( i, settings_num, "limit2"      ) )
  676. limitC2     = default(limitC2,      limit2                                          )
  677. post        = default(post,         MCTDmod_Defaults( i, settings_num, "post"        ) )
  678. chroma      = default(chroma,       MCTDmod_Defaults( i, settings_num, "chroma"      ) )
  679. interlaced  = default(interlaced,   MCTDmod_Defaults( i, settings_num, "interlaced"  ) )
  680. refine      = default(refine,       MCTDmod_Defaults( i, settings_num, "refine"      ) )
  681. pMode       = default(pMode,        MCTDmod_Defaults( i, settings_num, "pMode"       ) )
  682.  
  683. sharp       = default(sharp,        MCTDmod_Defaults( i, settings_num, "sharp"       ) )
  684. adapt       = default(adapt,        MCTDmod_Defaults( i, settings_num, "adapt"       ) )
  685. strength    = default(strength,     MCTDmod_Defaults( i, settings_num, "strength"    ) )
  686. SHmode      = default(SHmode,       MCTDmod_Defaults( i, settings_num, "SHmode"      ) )
  687. SHmethod    = default(SHmethod,     MCTDmod_Defaults( i, settings_num, "SHmethod"    ) )
  688. Slimit      = default(Slimit,       MCTDmod_Defaults( i, settings_num, "Slimit"      ) )
  689. Sovershoot  = default(Sovershoot,   MCTDmod_Defaults( i, settings_num, "Sovershoot"  ) )
  690. Tlimit      = default(Tlimit,       MCTDmod_Defaults( i, settings_num, "Tlimit"      ) )
  691. Tovershoot  = default(Tovershoot,   MCTDmod_Defaults( i, settings_num, "Tovershoot"  ) )
  692. ssource     = default(ssource,      MCTDmod_Defaults( i, settings_num, "ssource"     ) )
  693. shF         = default(shF,          MCTDmod_Defaults( i, settings_num, "shF"         ) )
  694.  
  695. protect     = default(protect,      MCTDmod_Defaults( i, settings_num, "protect"     ) )
  696. cutoff      = default(cutoff,       MCTDmod_Defaults( i, settings_num, "cutoff"      ) )
  697. threshold   = default(threshold,    MCTDmod_Defaults( i, settings_num, "threshold"   ) )
  698. maxdiff     = default(maxdiff,      MCTDmod_Defaults( i, settings_num, "maxdiff"     ) )
  699.  
  700. AA          = default(AA,           MCTDmod_Defaults( i, settings_num, "AA"          ) )
  701. useEEDI2    = default(useEEDI2,     MCTDmod_Defaults( i, settings_num, "useEEDI2"    ) )
  702. reduc       = default(reduc,        MCTDmod_Defaults( i, settings_num, "reduc"       ) )
  703. maxd        = default(maxd,         MCTDmod_Defaults( i, settings_num, "maxd"        ) )
  704. AAthr       = default(AAthr,        MCTDmod_Defaults( i, settings_num, "AAthr"       ) )
  705. method      = default(method,       MCTDmod_Defaults( i, settings_num, "method"      ) )
  706. aaF         = default(aaF,          MCTDmod_Defaults( i, settings_num, "aaF"         ) )
  707.  
  708. deblock     = default(deblock,      MCTDmod_Defaults( i, settings_num, "deblock"     ) )
  709. useQED      = default(useQED,       MCTDmod_Defaults( i, settings_num, "useQED"      ) )
  710. quant1      = default(quant1,       MCTDmod_Defaults( i, settings_num, "quant1"      ) )
  711. quant2      = default(quant2,       MCTDmod_Defaults( i, settings_num, "quant2"      ) )
  712.  
  713. edgeclean   = default(edgeclean,    MCTDmod_Defaults( i, settings_num, "edgeclean"   ) )
  714. ECrad       = default(ECrad,        MCTDmod_Defaults( i, settings_num, "ECrad"       ) )
  715. ECthr       = default(ECthr,        MCTDmod_Defaults( i, settings_num, "ECthr"       ) )
  716. ECmode      = default(ECmode,       MCTDmod_Defaults( i, settings_num, "ECmode"      ) )
  717.  
  718. stabilize   = default(stabilize,    MCTDmod_Defaults( i, settings_num, "stabilize"   ) )
  719. maxr        = default(maxr,         MCTDmod_Defaults( i, settings_num, "maxr"        ) )
  720. lthresh     = default(lthresh,      MCTDmod_Defaults( i, settings_num, "lthresh"     ) )
  721. cthresh     = default(cthresh,      MCTDmod_Defaults( i, settings_num, "cthresh"     ) )
  722. TTstr       = default(TTstr,        MCTDmod_Defaults( i, settings_num, "TTstr"       ) )
  723. stF         = default(stF,          MCTDmod_Defaults( i, settings_num, "stF"         ) )
  724.  
  725. enhance     = default(enhance,      MCTDmod_Defaults( i, settings_num, "enhance"     ) )
  726. GFthr       = default(GFthr,        MCTDmod_Defaults( i, settings_num, "GFthr"       ) )
  727. AGstr       = default(AGstr,        MCTDmod_Defaults( i, settings_num, "AGstr"       ) )
  728. bias        = default(bias,         MCTDmod_Defaults( i, settings_num, "bias"        ) )
  729. temp        = default(temp,         MCTDmod_Defaults( i, settings_num, "temp"        ) )
  730. dbF         = default(dbF,          MCTDmod_Defaults( i, settings_num, "dbF"         ) )
  731.  
  732. bwbh        = default(bwbh,         MCTDmod_Defaults( i, settings_num, "bwbh"        ) )
  733. owoh        = default(owoh,         MCTDmod_Defaults( i, settings_num, "owoh"        ) )
  734. blksize     = default(blksize,      MCTDmod_Defaults( i, settings_num, "blksize"     ) )
  735. overlap     = default(overlap,      MCTDmod_Defaults( i, settings_num, "overlap"     ) )
  736.  
  737. GPU         = default(GPU,          MCTDmod_Defaults( i, settings_num, "GPU"         ) )
  738. fixband     = default(fixband,      MCTDmod_Defaults( i, settings_num, "fixband"     ) )
  739. bt          = default(bt,           MCTDmod_Defaults( i, settings_num, "bt"          ) )
  740. ncpu        = default(ncpu,         MCTDmod_Defaults( i, settings_num, "ncpu"        ) )
  741. precision   = default(precision,    MCTDmod_Defaults( i, settings_num, "precision"   ) )
  742.  
  743. thSAD       = default(thSAD,        MCTDmod_Defaults( i, settings_num, "thSAD"       ) )
  744. thSAD2      = default(thSAD2,       MCTDmod_Defaults( i, settings_num, "thSAD2"      ) )
  745. thSCD1      = default(thSCD1,       MCTDmod_Defaults( i, settings_num, "thSCD1"      ) )
  746. thSCD2      = default(thSCD2,       MCTDmod_Defaults( i, settings_num, "thSCD2"      ) )
  747.  
  748. truemotion  = default(truemotion,   MCTDmod_Defaults( i, settings_num, "truemotion"  ) )
  749. MVglobal    = default(MVglobal,     MCTDmod_Defaults( i, settings_num, "MVglobal"    ) )
  750. pel         = default(pel,          MCTDmod_Defaults( i, settings_num, "pel"         ) )
  751. pelsearch   = default(pelsearch,    MCTDmod_Defaults( i, settings_num, "pelsearch"   ) )
  752. search      = default(search,       MCTDmod_Defaults( i, settings_num, "search"      ) )
  753. searchparam = default(searchparam,  MCTDmod_Defaults( i, settings_num, "searchparam" ) )
  754. MVsharp     = default(MVsharp,      MCTDmod_Defaults( i, settings_num, "MVsharp"     ) )
  755. DCT         = default(DCT,          MCTDmod_Defaults( i, settings_num, "DCT"         ) )
  756.  
  757. show        = default(show,         MCTDmod_Defaults( i, settings_num, "show"        ) )
  758. screenW     = default(screenW,      MCTDmod_Defaults( i, settings_num, "screenW"     ) )
  759. screenH     = default(screenH,      MCTDmod_Defaults( i, settings_num, "screenH"     ) )
  760.  
  761. show_p      = defined(p) ? "external" : "internal"
  762.  
  763. rfn_blksize = blksize == 4 ? 4 : blksize / 2
  764. rfn_overlap = overlap == 2 ? 2 : overlap / 4 * 2 + overlap % 4
  765. rfn_thSAD   = thSAD / 2
  766.  
  767. chr41 = chroma?4:1
  768. chr31 = chroma?3:1
  769. chr21 = chroma?2:1
  770.  
  771. ### WARNING
  772. Assert((radius>=1&&radius<=6)         ? true : false, chr(10) + "'radius' have not a correct value! [0-6]" + chr(10))
  773. Assert((limit>=-2&&limit<=255)        ? true : false, chr(10) + "'limit' have not a correct value! [-2,-1,0,...255]" + chr(10))
  774. Assert((limit2>=-2&&limit2<=255)      ? true : false, chr(10) + "'limit2' have not a correct value! [-2,-1,0,...255]" + chr(10))
  775. Assert((Tovershoot>=0)                ? true : false, chr(10) + "'Tovershoot' have not a correct value! [>=0]" + chr(10))
  776. Assert((cutoff>=0&&cutoff<=255)       ? true : false, chr(10) + "'cutoff' have not a correct value! [0,...,255]" + chr(10))
  777. Assert((threshold>=0&&threshold<=127) ? true : false, chr(10) + "'threshold' have not a correct value! [0,...,127]" + chr(10))
  778. Assert((maxdiff>=0&&maxdiff<=255)     ? true : false, chr(10) + "'maxdiff' have not a correct value! [0,...,255]" + chr(10))
  779. Assert((reduc>=-1&&reduc<=1.0)        ? true : false, chr(10) + "'reduc' have not a correct value! [-1,0.0,...,1.0]" + chr(10))
  780. Assert((AAthr>=0&&AAthr<=255)         ? true : false, chr(10) + "'AAthr' have not a correct value! [0,...,255]" + chr(10))
  781. Assert((method>=0&&method<=2)         ? true : false, chr(10) + "'method' have not a correct value! [0,1,2]" + chr(10))
  782. Assert((ECrad>=1)                     ? true : false, chr(10) + "'ECrad' have not a correct value! [>=1]" + chr(10))
  783. Assert((ECthr>=0&&ECthr<=255)         ? true : false, chr(10) + "'ECthr' have not a correct value! [0,...,255]" + chr(10))
  784. Assert((pMode>=0&&pMode<=2)           ? true : false, chr(10) + "'pMode' have not a correct value! [0,1,2]" + chr(10))
  785.  
  786.  
  787. ### INPUT
  788. i   = (interlaced==true) ? i.separatefields() : i
  789.  
  790. mod = (bwbh>=blksize) ? bwbh : blksize
  791. xi  = i.width()
  792. xf  = ceil(xi/float(mod))*mod - xi + mod
  793. xn  = int(xi+xf)
  794. yi  = i.height()
  795. yf  = ceil(yi/float(mod))*mod - yi + mod
  796. yn  = int(yi+yf)
  797. idb = i
  798. i   = i.pointresize(xn,yn,-xf/2,-yf/2,xn,yn)
  799.  
  800. m4 = ((int(xi/4.0)*4==xi)&&(int(yi/4.0)*4==yi)) ? true : false
  801. Assert( m4 ? true : false, chr(10) + "This is not a MOD4 clip! Please use crop() or addborders() before using MCTD()" + chr(10))
  802.  
  803. ### PREFILTERING
  804. p = defined(p)                   ? p.pointresize(xn,yn,-xf/2,-yf/2,xn,yn)
  805. \ : (sigma==0)                   ? i
  806. \ : (sigma<0)                    ? i.dfttest(sstring="0.0:"+string(-sigma*4./3.)+" 0.2:"+string(-sigma*9./3.)+" 1.0:"+string(-sigma*15./3.),tbsize=(bt%2==0)?bt+1:bt,u=chroma,v=chroma,quiet=true,dither=fixband?round(-sigma/11.):0)
  807. \ : (GPU==false&&fixband==false) ? i.FFT3Dfilter(sigma=sigma*0.8,sigma2=sigma*0.6,sigma3=sigma*0.4,sigma4=sigma*0.2,bw=bwbh,bh=bwbh,ow=owoh,oh=owoh,plane=chroma?4:0,bt=bt,ncpu=ncpu)
  808. \ : (GPU==true &&fixband==false) ? i.FFT3Dgpu(sigma=sigma*0.8,sigma2=sigma*0.6,sigma3=sigma*0.4,sigma4=sigma*0.2,bw=bwbh,bh=bwbh,ow=owoh,oh=owoh,plane=chroma?4:0,bt=bt,precision=precision)
  809. \ : (GPU==false&&fixband==true ) ? i.FFT3Dfilter(sigma=sigma*0.8,sigma2=sigma*0.6,sigma3=sigma*0.4,sigma4=sigma*0.2,bw=bwbh,bh=bwbh,ow=owoh,oh=owoh,plane=chroma?4:0,bt=bt,ncpu=ncpu).gradfun2db(thr=1.0+sigma*0.05)
  810. \ :                                i.FFT3Dgpu(sigma=sigma*0.8,sigma2=sigma*0.6,sigma3=sigma*0.4,sigma4=sigma*0.2,bw=bwbh,bh=bwbh,ow=owoh,oh=owoh,plane=chroma?4:0,bt=bt,precision=precision).gradfun2db(thr=1.0+sigma*0.05)
  811.  
  812.  
  813. ### DEBLOCKING
  814. padX = xi%8 == 0 ? 0 : (8 - xi%8)
  815. padY = yi%8 == 0 ? 0 : (8 - yi%8)
  816.  
  817. d  = (deblock==false) ? i
  818. \  : (useQED==true)   ? idb.deblock_QED(quant1=quant1,quant2=quant2,uv=chroma?3:2).pointresize(xn,yn,-xf/2,-yf/2,xn,yn)
  819. \  : ( padX==0 && padY==0 ) ? idb.deblock(quant=int(quant1*0.5+quant2*0.5)).pointresize(xn,yn,-xf/2,-yf/2,xn,yn)
  820. \  : idb.pointresize(xi+padX, yi+padY, 0, 0, xi+padX, xi+padY).deblock(quant=int(quant1*0.5+quant2*0.5)).pointresize(xn,yn,-xf/2,-yf/2,xn-padX,yn-padX)
  821.  
  822. ### PREPARING
  823. nullclip = Blankclip()
  824.  
  825. lmsk = p.mt_lut("x " + string(bias) + " - abs " + string( bias >= 128 ? 255. / bias : 255. / ( 255. - bias ) ) + " *", U=1, V=1)
  826. pAdp = mt_merge(i, p, lmsk, luma=chroma, U=chr31, V=chr31)
  827. mAdp = mt_merge(p, i, i.mt_lut("x 16 < 255 x 75 > 0 255 x 16 - 255 75 16 - / * - ? ?", U=1, V=1), luma=chroma, U=chr31, V=chr31)
  828.  
  829. pReb = pMode == 2 ? mAdp.MCTD_Luma_Rebuild(S0=2., c=0.0625, uv=chr31) /* 2: mReb for MC */
  830. \    : pMode == 1 ? mAdp                                              /* 1: mAdp for MC */
  831. \    :              p                                                 /* 0: p    for MC */
  832.  
  833. pMVS = pReb.MSuper(hpad=0,vpad=0,pel=pel,sharp=MVsharp,rfilter=refine?4:2,chroma=chroma)
  834. rMVS = refine ?
  835. \      pReb.MSuper(hpad=0,vpad=0,pel=pel,sharp=MVsharp,rfilter=2,levels=1,chroma=chroma) : nullclip
  836.  
  837. f1v = (radius<1) ? nullclip
  838. \   :              pMVS.MAnalyse(isb=false, delta=1, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  839. f2v = (radius<2) ? nullclip
  840. \   :              pMVS.MAnalyse(isb=false, delta=2, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  841. f3v = (radius<3) ? nullclip
  842. \   :              pMVS.MAnalyse(isb=false, delta=3, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  843. f4v = (radius<4) ? nullclip
  844. \   :              pMVS.MAnalyse(isb=false, delta=4, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  845. f5v = (radius<5) ? nullclip
  846. \   :              pMVS.MAnalyse(isb=false, delta=5, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  847. f6v = (radius<6) ? nullclip
  848. \   :              pMVS.MAnalyse(isb=false, delta=6, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  849. b1v = (radius<1) ? nullclip
  850. \   :              pMVS.MAnalyse(isb=true, delta=1, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  851. b2v = (radius<2) ? nullclip
  852. \   :              pMVS.MAnalyse(isb=true, delta=2, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  853. b3v = (radius<3) ? nullclip
  854. \   :              pMVS.MAnalyse(isb=true, delta=3, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  855. b4v = (radius<4) ? nullclip
  856. \   :              pMVS.MAnalyse(isb=true, delta=4, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  857. b5v = (radius<5) ? nullclip
  858. \   :              pMVS.MAnalyse(isb=true, delta=5, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  859. b6v = (radius<6) ? nullclip
  860. \   :              pMVS.MAnalyse(isb=true, delta=6, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chroma)
  861.  
  862. f1v = !refine || (radius<1) ? f1v
  863. \   :                         rMVS.MRecalculate(f1v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  864. f2v = !refine || (radius<2) ? f2v
  865. \   :                         rMVS.MRecalculate(f2v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  866. f3v = !refine || (radius<3) ? f3v
  867. \   :                         rMVS.MRecalculate(f3v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  868. f4v = !refine || (radius<4) ? f4v
  869. \   :                         rMVS.MRecalculate(f4v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  870. f5v = !refine || (radius<5) ? f5v
  871. \   :                         rMVS.MRecalculate(f5v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  872. f6v = !refine || (radius<6) ? f6v
  873. \   :                         rMVS.MRecalculate(f6v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  874. b1v = !refine || (radius<1) ? b1v
  875. \   :                         rMVS.MRecalculate(b1v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  876. b2v = !refine || (radius<2) ? b2v
  877. \   :                         rMVS.MRecalculate(b2v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  878. b3v = !refine || (radius<3) ? b3v
  879. \   :                         rMVS.MRecalculate(b3v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  880. b4v = !refine || (radius<4) ? b4v
  881. \   :                         rMVS.MRecalculate(b4v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  882. b5v = !refine || (radius<5) ? b5v
  883. \   :                         rMVS.MRecalculate(b5v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  884. b6v = !refine || (radius<6) ? b6v
  885. \   :                         rMVS.MRecalculate(b6v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  886.  
  887.    function MCTDmod_CTM(clip "i", clip "iMVS", clip "SAD_f1m", clip "SAD_b1m", clip "f1c", clip "b1c",
  888.    \                 clip "f1v", clip "f2v", clip "f3v", clip "f4v", clip "f5v", clip "f6v", clip "b1v", clip "b2v", clip "b3v", clip "b4v", clip "b5v", clip "b6v",
  889.    \                 int "radius", bool "chroma", int "thSAD", int "thSCD1", int "thSCD2", string "cnr")
  890.    {
  891.    nullclip = BlankClip()
  892.  
  893.    SAD_f2m = (radius>=2) ? i.MMask(f2v, kind=1, ml=thSAD, gamma=0.999, Ysc=255) : nullclip
  894.    SAD_f3m = (radius>=3) ? i.MMask(f3v, kind=1, ml=thSAD, gamma=0.999, Ysc=255) : nullclip
  895.    SAD_f4m = (radius>=4) ? i.MMask(f4v, kind=1, ml=thSAD, gamma=0.999, Ysc=255) : nullclip
  896.    SAD_f5m = (radius>=5) ? i.MMask(f5v, kind=1, ml=thSAD, gamma=0.999, Ysc=255) : nullclip
  897.    SAD_f6m = (radius>=6) ? i.MMask(f6v, kind=1, ml=thSAD, gamma=0.999, Ysc=255) : nullclip
  898.    SAD_b2m = (radius>=2) ? i.MMask(b2v, kind=1, ml=thSAD, gamma=0.999, Ysc=255) : nullclip
  899.    SAD_b3m = (radius>=3) ? i.MMask(b3v, kind=1, ml=thSAD, gamma=0.999, Ysc=255) : nullclip
  900.    SAD_b4m = (radius>=4) ? i.MMask(b4v, kind=1, ml=thSAD, gamma=0.999, Ysc=255) : nullclip
  901.    SAD_b5m = (radius>=5) ? i.MMask(b5v, kind=1, ml=thSAD, gamma=0.999, Ysc=255) : nullclip
  902.    SAD_b6m = (radius>=6) ? i.MMask(b6v, kind=1, ml=thSAD, gamma=0.999, Ysc=255) : nullclip
  903.  
  904.    b     = BlankClip(i,color_yuv=$008080)
  905.    SAD_m = (radius==6) ? interleave(SAD_f6m, SAD_f5m, SAD_f4m, SAD_f3m, SAD_f2m, SAD_f1m, b, SAD_b1m, SAD_b2m, SAD_b3m, SAD_b4m, SAD_b5m, SAD_b6m)
  906.    \     : (radius==5) ? interleave(         SAD_f5m, SAD_f4m, SAD_f3m, SAD_f2m, SAD_f1m, b, SAD_b1m, SAD_b2m, SAD_b3m, SAD_b4m, SAD_b5m         )
  907.    \     : (radius==4) ? interleave(                  SAD_f4m, SAD_f3m, SAD_f2m, SAD_f1m, b, SAD_b1m, SAD_b2m, SAD_b3m, SAD_b4m                  )
  908.    \     : (radius==3) ? interleave(                           SAD_f3m, SAD_f2m, SAD_f1m, b, SAD_b1m, SAD_b2m, SAD_b3m                           )
  909.    \     : (radius==2) ? interleave(                                    SAD_f2m, SAD_f1m, b, SAD_b1m, SAD_b2m                                    )
  910.    \     : (radius==1) ? interleave(                                             SAD_f1m, b, SAD_b1m                                             )
  911.    \     : nullclip
  912.  
  913.    b2c = (radius>=2)  ? i.MCompensate(iMVS, b2v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : nullclip
  914.    b3c = (radius>=3)  ? i.MCompensate(iMVS, b3v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : nullclip
  915.    b4c = (radius>=4)  ? i.MCompensate(iMVS, b4v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : nullclip
  916.    b5c = (radius>=5)  ? i.MCompensate(iMVS, b5v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : nullclip
  917.    b6c = (radius>=6)  ? i.MCompensate(iMVS, b6v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : nullclip
  918.    f2c = (radius>=2)  ? i.MCompensate(iMVS, f2v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : nullclip
  919.    f3c = (radius>=3)  ? i.MCompensate(iMVS, f3v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : nullclip
  920.    f4c = (radius>=4)  ? i.MCompensate(iMVS, f4v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : nullclip
  921.    f5c = (radius>=5)  ? i.MCompensate(iMVS, f5v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : nullclip
  922.    f6c = (radius>=6)  ? i.MCompensate(iMVS, f6v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2) : nullclip
  923.    
  924.    c = (radius==6) ? interleave(f6c, f5c, f4c, f3c, f2c, f1c, i, b1c, b2c, b3c, b4c, b5c, b6c)
  925.    \ : (radius==5) ? interleave(     f5c, f4c, f3c, f2c, f1c, i, b1c, b2c, b3c, b4c, b5c     )
  926.    \ : (radius==4) ? interleave(          f4c, f3c, f2c, f1c, i, b1c, b2c, b3c, b4c          )
  927.    \ : (radius==3) ? interleave(               f3c, f2c, f1c, i, b1c, b2c, b3c               )
  928.    \ : (radius==2) ? interleave(                    f2c, f1c, i, b1c, b2c                    )
  929.    \ : (radius==1) ? interleave(                         f1c, i, b1c                         )
  930.    \ : nullclip
  931.  
  932.    sm = Defined(cnr) ? Eval("c."+cnr)
  933.    \  : c.TTempSmooth(radius,255,255,1,1,strength=radius+1,pfclip=SAD_m,fp=false,scthresh=99.9)
  934.    sm = sm.SelectEvery(1+radius*2,radius)
  935.    
  936.    return sm
  937.    }
  938.  
  939.    function MCTDmod_MVD(clip "i", clip "iMVS",
  940.    \                 clip "f1v", clip "f2v", clip "f3v", clip "f4v", clip "f5v", clip "f6v", clip "b1v", clip "b2v", clip "b3v", clip "b4v", clip "b5v", clip "b6v",
  941.    \                 int "radius", bool "chroma", int "thSAD", int "thSCD1", int "thSCD2")
  942.    {
  943.    nullclip = BlankClip()
  944.  
  945.       Try {
  946.  
  947.          vMulti = (radius==4) ? Interleave(b1v, f1v, b2v, f2v, b3v, f3v, b4v, f4v)
  948.          \      : (radius==5) ? Interleave(b1v, f1v, b2v, f2v, b3v, f3v, b4v, f4v, b5v, f5v)
  949.          \      : (radius==6) ? Interleave(b1v, f1v, b2v, f2v, b3v, f3v, b4v, f4v, b5v, f5v, b6v, f6v)
  950.          \      :               nullclip
  951.  
  952.          sm = (radius==1) ? i.MDegrain1(iMVS, b1v, f1v,                     thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0)
  953.          \  : (radius==2) ? i.MDegrain2(iMVS, b1v, f1v, b2v, f2v,           thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0)
  954.          \  : (radius==3) ? i.MDegrain3(iMVS, b1v, f1v, b2v, f2v, b3v, f3v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0)
  955.          \  :               i.MDegrainN(iMVS, vMulti, radius,               thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0)
  956.  
  957.       } Catch ( e ) {
  958.  
  959.          mv12  = (radius==4) ? i.MDegrain2(iMVS, b1v, f1v, b2v, f2v,           thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0) : nullclip
  960.          mv34  = (radius==4) ? i.MDegrain2(iMVS, b3v, f3v, b4v, f4v,           thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0) : nullclip
  961.          mv45  = (radius==5) ? i.MDegrain2(iMVS, b4v, f4v, b5v, f5v,           thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0) : nullclip
  962.          mv123 = (radius>=5) ? i.MDegrain3(iMVS, b1v, f1v, b2v, f2v, b3v, f3v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0) : nullclip
  963.          mv456 = (radius==6) ? i.MDegrain3(iMVS, b4v, f4v, b5v, f5v, b6v, f6v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0) : nullclip
  964.    
  965.          sm = (radius==1) ? i.MDegrain1(iMVS, b1v, f1v,                     thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0)
  966.          \  : (radius==2) ? i.MDegrain2(iMVS, b1v, f1v, b2v, f2v,           thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0)
  967.          \  : (radius==3) ? i.MDegrain3(iMVS, b1v, f1v, b2v, f2v, b3v, f3v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=chroma?4:0)
  968.          \  : (radius==4) ? Merge(mv12,  mv34,  0.4444)
  969.          \  : (radius==5) ? Merge(mv123, mv45,  0.4545)
  970.          \  :               Merge(mv123, mv456, 0.4615)
  971.  
  972.       }
  973.  
  974.    return sm
  975.    }
  976.  
  977. dMVS    = d.MSuper(hpad=0,vpad=0,pel=pel,sharp=MVsharp,levels=1,chroma=chroma)
  978. SAD_f1m = d.MMask(f1v, kind=1, ml=thSAD, gamma=0.999, Ysc=255)
  979. SAD_b1m = d.MMask(b1v, kind=1, ml=thSAD, gamma=0.999, Ysc=255)
  980. f1c     = d.MCompensate(dMVS, f1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  981. b1c     = d.MCompensate(dMVS, b1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  982.  
  983.  
  984. ### DENOISING: FIRST PASS
  985. sm = ( Defined(p1nr) ) ? MCTDmod_CTM(i=d, iMVS=dMVS, SAD_f1m=SAD_f1m, SAD_b1m=SAD_b1m, f1c=f1c, b1c=b1c,
  986. \                                 f1v=f1v, f2v=f2v, f3v=f3v, f4v=f4v, f5v=f5v, f6v=f6v, b1v=b1v, b2v=b2v, b3v=b3v, b4v=b4v, b5v=b5v, b6v=b6v,
  987. \                                 radius=radius, chroma=chroma, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, cnr=p1nr)
  988. \  : (useTTmpSm==true) ? MCTDmod_CTM(i=d, iMVS=dMVS, SAD_f1m=SAD_f1m, SAD_b1m=SAD_b1m, f1c=f1c, b1c=b1c,
  989. \                                 f1v=f1v, f2v=f2v, f3v=f3v, f4v=f4v, f5v=f5v, f6v=f6v, b1v=b1v, b2v=b2v, b3v=b3v, b4v=b4v, b5v=b5v, b6v=b6v,
  990. \                                 radius=radius, chroma=chroma, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  991. \  :                     MCTDmod_MVD(i=d, iMVS=dMVS,
  992. \                                 f1v=f1v, f2v=f2v, f3v=f3v, f4v=f4v, f5v=f5v, f6v=f6v, b1v=b1v, b2v=b2v, b3v=b3v, b4v=b4v, b5v=b5v, b6v=b6v,
  993. \                                 radius=radius, chroma=chroma, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  994.  
  995. ### LIMIT: FIRST PASS
  996. /*
  997.  *  limit >  0 && limitC >  0       makediff            3 3 3
  998.  *  limit >  0 && limitC == 0       makediff            3 c c
  999.  *  limit >  0 && limitC <  0       makediff + pre      3 1 1   +   1 3 3
  1000.  *
  1001.  *  limit == 0 && limitC >  0       makediff            2 3 3
  1002.  *  limit == 0 && limitC == 0       -                   -----
  1003.  *  limit == 0 && limitC <  0       pre                 2 3 3
  1004.  *
  1005.  *  limit <  0 && limitC >  0       makediff + pre      3 1 1   +   1 3 3
  1006.  *  limit <  0 && limitC == 0       pre                 3 c c
  1007.  *  limit == limitC < 0             pre                 3 3 3
  1008.  *  0 > limit != limitC < 0         pre + pre           3 1 1   +   1 3 3
  1009.  */
  1010.  
  1011. pDy31   =             limit  < 0 || ( twopass && limit2  < 0 )   ? 3 : 1
  1012. pDuv31  = chroma && ( limitC < 0 || ( twopass && limitC2 < 0 ) ) ? 3 : 1
  1013.  
  1014. pD  = mt_makediff(i,p,   Y=pDy31,U=pDuv31,V=pDuv31)
  1015. pDa = mt_makediff(i,pAdp,Y=pDy31,U=pDuv31,V=pDuv31)
  1016.  
  1017. pDc  = mt_makediff(i,p,   Y=1,U=3,V=3)
  1018. pDac = mt_makediff(i,pAdp,Y=1,U=3,V=3)
  1019.  
  1020. l_lmr31 = limit == 0 ? 1 : 3
  1021. lchr    = chroma && limitC != 0
  1022. d_chr31 = lchr ? chr31 : 1
  1023. l_chr31 = lchr && ( limit == 0 || ( limit > 0 && limitC > 0 ) || limit == limitC ) ? 3 : 1
  1024.  
  1025. smD = mt_makediff(i,sm,Y=l_lmr31,U=d_chr31,V=d_chr31)             # Only skip when !chroma or limitC == 0
  1026. DD  = (limit<0) ? mt_lutxy(limit==-1?pD:pDa,smD,"x 128 - abs y 128 - abs < x y ?",Y=l_lmr31,U=l_chr31,V=l_chr31)
  1027. \   : (limit>0) ? mt_lutxy(sm,i,yexpr="x y - abs "+string(limit)+" <= x x y - 0 < y "+string(limit)+" - y "+string(limit)+" + ? ?",
  1028. \                                expr=(limitC>0)?("x y - abs "+string(limitC)+" <= x x y - 0 < y "+string(limitC)+" - y "+string(limitC)+" + ? ?"):("x"),
  1029. \                                Y=l_lmr31,U=l_chr31,V=l_chr31)
  1030. \   :             sm
  1031. smL = (limit<0) ? i.mt_makediff(DD,Y=l_lmr31,U=l_chr31,V=l_chr31) : DD
  1032.  
  1033. DDc  = (limitC<0) ? mt_lutxy(limitC==-1?pDc:pDac,smD,"x 128 - abs y 128 - abs < x y ?",Y=1,U=3,V=3)
  1034. \    : (limitC>0) ? mt_lutxy(sm,i,"x y - abs "+string(limitC)+" <= x x y - 0 < y "+string(limitC)+" - y "+string(limitC)+" + ? ?",Y=1,U=3,V=3)
  1035. \    :              sm
  1036. smLc = (limitC<0) ? i.mt_makediff(DDc,Y=1,U=3,V=3) : DDc
  1037.  
  1038. smL  = limit == 0             ? lchr ? MergeChroma(sm, smLc) : /* limit == 0, lchr     */
  1039. \                                      sm                    : /* limit == 0, !lchr    */
  1040. \      chroma && l_chr31 == 1 ? MergeChroma(smL, smLc)       : /* limit != 0, separate process */
  1041. \                               smL                            /* limit != 0, same process */
  1042.  
  1043. ### DENOISING: SECOND PASS
  1044. smLMVS = (twopass==true) ? smL.MSuper(hpad=0,vpad=0,pel=pel,sharp=MVsharp,levels=1,chroma=chroma) : nullclip
  1045.  
  1046. f1v = !twopass || !refine || (radius<1) ? f1v
  1047. \   :                                     smLMVS.MRecalculate(f1v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1048. f2v = !twopass || !refine || (radius<2) ? f2v
  1049. \   :                                     smLMVS.MRecalculate(f2v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1050. f3v = !twopass || !refine || (radius<3) ? f3v
  1051. \   :                                     smLMVS.MRecalculate(f3v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1052. f4v = !twopass || !refine || (radius<4) ? f4v
  1053. \   :                                     smLMVS.MRecalculate(f4v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1054. f5v = !twopass || !refine || (radius<5) ? f5v
  1055. \   :                                     smLMVS.MRecalculate(f5v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1056. f6v = !twopass || !refine || (radius<6) ? f6v
  1057. \   :                                     smLMVS.MRecalculate(f6v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1058. b1v = !twopass || !refine || (radius<1) ? b1v
  1059. \   :                                     smLMVS.MRecalculate(b1v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1060. b2v = !twopass || !refine || (radius<2) ? b2v
  1061. \   :                                     smLMVS.MRecalculate(b2v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1062. b3v = !twopass || !refine || (radius<3) ? b3v
  1063. \   :                                     smLMVS.MRecalculate(b3v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1064. b4v = !twopass || !refine || (radius<4) ? b4v
  1065. \   :                                     smLMVS.MRecalculate(b4v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1066. b5v = !twopass || !refine || (radius<5) ? b5v
  1067. \   :                                     smLMVS.MRecalculate(b5v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1068. b6v = !twopass || !refine || (radius<6) ? b6v
  1069. \   :                                     smLMVS.MRecalculate(b6v, truemotion=truemotion, blksize=rfn_blksize, overlap=rfn_overlap, search=search, searchparam=searchparam, DCT=DCT, thSAD=rfn_thSAD, chroma=chroma)
  1070.  
  1071. SAD_f1m = !twopass ? SAD_f1m : smL.MMask(f1v, kind=1, ml=thSAD, gamma=0.999, Ysc=255)
  1072. SAD_b1m = !twopass ? SAD_b1m : smL.MMask(b1v, kind=1, ml=thSAD, gamma=0.999, Ysc=255)
  1073. f1c     = !twopass ? f1c     : smL.MCompensate(smLMVS, f1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  1074. b1c     = !twopass ? b1c     : smL.MCompensate(smLMVS, b1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  1075.  
  1076. sm = (twopass==false)  ? sm
  1077. \  : ( Defined(p2nr) ) ? MCTDmod_CTM(i=smL, iMVS=smLMVS, SAD_f1m=SAD_f1m, SAD_b1m=SAD_b1m, f1c=f1c, b1c=b1c,
  1078. \                                 f1v=f1v, f2v=f2v, f3v=f3v, f4v=f4v, f5v=f5v, f6v=f6v, b1v=b1v, b2v=b2v, b3v=b3v, b4v=b4v, b5v=b5v, b6v=b6v,
  1079. \                                 radius=radius, chroma=chroma, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, cnr=p2nr)
  1080. \  : (useTTmpSm==true) ? MCTDmod_CTM(i=smL, iMVS=smLMVS, SAD_f1m=SAD_f1m, SAD_b1m=SAD_b1m, f1c=f1c, b1c=b1c,
  1081. \                                 f1v=f1v, f2v=f2v, f3v=f3v, f4v=f4v, f5v=f5v, f6v=f6v, b1v=b1v, b2v=b2v, b3v=b3v, b4v=b4v, b5v=b5v, b6v=b6v,
  1082. \                                 radius=radius, chroma=chroma, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  1083. \  :                     MCTDmod_MVD(i=smL, iMVS=smLMVS,
  1084. \                                 f1v=f1v, f2v=f2v, f3v=f3v, f4v=f4v, f5v=f5v, f6v=f6v, b1v=b1v, b2v=b2v, b3v=b3v, b4v=b4v, b5v=b5v, b6v=b6v,
  1085. \                                 radius=radius, chroma=chroma, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  1086.  
  1087. l_lmr31 = limit2 == 0 ? 1 : 3
  1088. lchr    = chroma && limitC2 != 0
  1089. d_chr31 = lchr ? chr31 : 1
  1090. l_chr31 = lchr && ( limit2 == 0 || ( limit2 > 0 && limitC2 > 0 ) || limit2 == limitC2 ) ? 3 : 1
  1091.  
  1092. smD = twopass ? mt_makediff(i,sm,Y=l_lmr31,U=d_chr31,V=d_chr31) : nullclip
  1093. DD  = (twopass && limit2<0) ? mt_lutxy(limit2==-1?pD:pDa,smD,"x 128 - abs y 128 - abs < x y ?",Y=l_lmr31,U=l_chr31,V=l_chr31)
  1094. \   : (twopass && limit2>0) ? mt_lutxy(sm,i,yexpr="x y - abs "+string(limit2)+" <= x x y - 0 < y "+string(limit2)+" - y "+string(limit2)+" + ? ?",
  1095. \                                           expr=(limitC2>0)?("x y - abs "+string(limitC2)+" <= x x y - 0 < y "+string(limitC2)+" - y "+string(limitC2)+" + ? ?"):("x"),
  1096. \                                           Y=l_lmr31,U=l_chr31,V=l_chr31)
  1097. \   :                         sm
  1098.  
  1099. smL = (!twopass) ? smL
  1100. \   : (limit2<0) ? i.mt_makediff(DD,Y=l_lmr31,U=l_chr31,V=l_chr31) : DD
  1101.  
  1102. DDc  = (twopass && limitC2<0) ? mt_lutxy(limitC2==-1?pDc:pDac,smD,"x 128 - abs y 128 - abs < x y ?",Y=1,U=3,V=3)
  1103. \    : (twopass && limitC2>0) ? mt_lutxy(sm,i,"x y - abs "+string(limitC2)+" <= x x y - 0 < y "+string(limitC2)+" - y "+string(limitC2)+" + ? ?",Y=1,U=3,V=3)
  1104. \    :                          sm
  1105. smLc = (twopass && limitC2<0) ? i.mt_makediff(DDc,Y=1,U=3,V=3) : DDc
  1106.  
  1107. smL  = !twopass               ? smL                          :
  1108. \      limit2 == 0            ? lchr ? MergeChroma(sm, smLc) : /* limit2 == 0, lchr     */
  1109. \                                      sm                    : /* limit2 == 0, !lchr    */
  1110. \      chroma && l_chr31 == 1 ? MergeChroma(smL, smLc)       : /* limit2 != 0, separate process */
  1111. \                               smL                            /* limit2 != 0, same process */
  1112.  
  1113.  
  1114. ### POST-DENOISING: FFT3D
  1115. post_abs = Abs(post)
  1116. smP = (post==0)    ? smL
  1117. \   : Defined(ppnr)? Eval("smL."+ppnr)
  1118. \   : (GPU==false) ? smL.FFT3Dfilter(sigma=post_abs*0.8,sigma2=post_abs*0.6,sigma3=post_abs*0.4,sigma4=post_abs*0.2,bw=bwbh,bh=bwbh,ow=owoh,oh=owoh,plane=chroma?4:0,bt=bt,ncpu=ncpu)
  1119. \   :                smL.FFT3Dgpu(sigma=post_abs*0.8,sigma2=post_abs*0.6,sigma3=post_abs*0.4,sigma4=post_abs*0.2,bw=bwbh,bh=bwbh,ow=owoh,oh=owoh,plane=chroma?4:0,bt=bt,precision=precision)
  1120. smP = (post==0)        ? smP
  1121. \   : ( fixband==false || post<0 ) ? smP
  1122. \   :                                mt_makediff(smL,mt_makediff(smL,smP,U=chr31,V=chr31).gradfun2DB(thr=1.0+post*0.5),U=chr31,V=chr31)
  1123.  
  1124.  
  1125. ### POST-PROCESSING
  1126.    function MCTDmod_PP( clip denoised, clip "source",
  1127.    \                 bool "sharp", bool "adapt", int "strength", int "SHmode", int "SHmethod", int "Slimit", int "Sovershoot", bool "Tlimit", int "Tovershoot", val "ssource", string "shF",
  1128.    \                 bool "protect", int "cutoff", int "threshold", int "maxdiff",
  1129.    \                 bool "AA", bool "useEEDI2", float "reduc", int "maxd", int "AAthr", int "method", string "aaF",
  1130.    \                 bool "deblock", bool "useQED", int "quant1", int "quant2",          
  1131.    \                 bool "edgeclean", int "ECrad", int "ECthr", string "ECmode",
  1132.    \                 bool "stabilize", int "maxr", int "lthresh", int "cthresh", int "TTstr", string "stF",
  1133.    \                 bool "enhance", float "GFthr", float "AGstr", int "bias", int "temp", string "dbF",
  1134.    \                 bool "chroma", string "settings", bool "useMMask",
  1135.    \                 clip "f1v", clip "b1v", clip "f1c", clip "b1c", bool "internal" )
  1136.    {
  1137.    ### SETTINGS
  1138.    settings     = Default(settings, "low")
  1139.    settings_num =  -1
  1140.    settings_num = (settings == "very low")  ? 0 : settings_num
  1141.    settings_num = (settings == "low")       ? 1 : settings_num
  1142.    settings_num = (settings == "medium")    ? 2 : settings_num
  1143.    settings_num = (settings == "high")      ? 3 : settings_num
  1144.    settings_num = (settings == "very high") ? 4 : settings_num
  1145.    Assert(settings_num >=0 ? true : false, chr(10) + "These settings do not exist !" + chr(10))
  1146.  
  1147.    sharp       = default(sharp,        false                                                  )
  1148.    adapt       = default(adapt,        MCTDmod_Defaults( denoised, settings_num, "adapt"       ) )
  1149.    strength    = default(strength,     MCTDmod_Defaults( denoised, settings_num, "strength"    ) )
  1150.    SHmode      = default(SHmode,       MCTDmod_Defaults( denoised, settings_num, "SHmode"      ) )
  1151.    SHmethod    = default(SHmethod,     MCTDmod_Defaults( denoised, settings_num, "SHmethod"    ) )
  1152.    Slimit      = default(Slimit,       MCTDmod_Defaults( denoised, settings_num, "Slimit"      ) )
  1153.    Sovershoot  = default(Sovershoot,   MCTDmod_Defaults( denoised, settings_num, "Sovershoot"  ) )
  1154.    Tlimit      = default(Tlimit,       MCTDmod_Defaults( denoised, settings_num, "Tlimit"      ) )
  1155.    Tovershoot  = default(Tovershoot,   MCTDmod_Defaults( denoised, settings_num, "Tovershoot"  ) )
  1156.    ssource     = default(ssource,      MCTDmod_Defaults( denoised, settings_num, "ssource"     ) )
  1157.    shF         = default(shF,          MCTDmod_Defaults( denoised, settings_num, "shF"         ) )
  1158.  
  1159.    protect     = default(protect,      false                                                  )
  1160.    cutoff      = default(cutoff,       MCTDmod_Defaults( denoised, settings_num, "cutoff"      ) )
  1161.    threshold   = default(threshold,    MCTDmod_Defaults( denoised, settings_num, "threshold"   ) )
  1162.    maxdiff     = default(maxdiff,      MCTDmod_Defaults( denoised, settings_num, "maxdiff"     ) )
  1163.  
  1164.    AA          = default(AA,           false                                                  )
  1165.    useEEDI2    = default(useEEDI2,     MCTDmod_Defaults( denoised, settings_num, "useEEDI2"    ) )
  1166.    reduc       = default(reduc,        MCTDmod_Defaults( denoised, settings_num, "reduc"       ) )
  1167.    maxd        = default(maxd,         MCTDmod_Defaults( denoised, settings_num, "maxd"        ) )
  1168.    AAthr       = default(AAthr,        MCTDmod_Defaults( denoised, settings_num, "AAthr"       ) )
  1169.    method      = default(method,       MCTDmod_Defaults( denoised, settings_num, "method"      ) )
  1170.    aaF         = default(aaF,          MCTDmod_Defaults( denoised, settings_num, "aaF"         ) )
  1171.  
  1172.    deblock     = default(deblock,      false                                                  )
  1173.    useQED      = default(useQED,       MCTDmod_Defaults( denoised, settings_num, "useQED"      ) )
  1174.    quant1      = default(quant1,       MCTDmod_Defaults( denoised, settings_num, "quant1"      ) )
  1175.    quant2      = default(quant2,       MCTDmod_Defaults( denoised, settings_num, "quant2"      ) )
  1176.  
  1177.    edgeclean   = default(edgeclean,    false                                                  )
  1178.    ECrad       = default(ECrad,        MCTDmod_Defaults( denoised, settings_num, "ECrad"       ) )
  1179.    ECthr       = default(ECthr,        MCTDmod_Defaults( denoised, settings_num, "ECthr"       ) )
  1180.    ECmode      = default(ECmode,       MCTDmod_Defaults( denoised, settings_num, "ECmode"      ) )
  1181.  
  1182.    stabilize   = default(stabilize,    false                                                  )
  1183.    maxr        = default(maxr,         MCTDmod_Defaults( denoised, settings_num, "maxr"        ) )
  1184.    lthresh     = default(lthresh,      MCTDmod_Defaults( denoised, settings_num, "lthresh"     ) )
  1185.    cthresh     = default(cthresh,      MCTDmod_Defaults( denoised, settings_num, "cthresh"     ) )
  1186.    TTstr       = default(TTstr,        MCTDmod_Defaults( denoised, settings_num, "TTstr"       ) )
  1187.    stF         = default(stF,          MCTDmod_Defaults( denoised, settings_num, "stF"         ) )
  1188.  
  1189.    enhance     = default(enhance,      false                                                  )
  1190.    GFthr       = default(GFthr,        MCTDmod_Defaults( denoised, settings_num, "GFthr"       ) )
  1191.    AGstr       = default(AGstr,        MCTDmod_Defaults( denoised, settings_num, "AGstr"       ) )
  1192.    bias        = default(bias,         MCTDmod_Defaults( denoised, settings_num, "bias"        ) )
  1193.    temp        = default(temp,         MCTDmod_Defaults( denoised, settings_num, "temp"        ) )
  1194.    dbF         = default(dbF,          MCTDmod_Defaults( denoised, settings_num, "dbF"         ) )
  1195.  
  1196.    chroma      = default(chroma,       MCTDmod_Defaults( denoised, settings_num, "chroma"      ) )
  1197.    useMMask    = default(useMMask,     true                                                   )
  1198.    internal    = default(internal,     false                                                  )
  1199.  
  1200.    Assert((Tovershoot>=0)                ? true : false, chr(10) + "'Tovershoot' have not a correct value! [>=0]" + chr(10))
  1201.    Assert((cutoff>=0&&cutoff<=255)       ? true : false, chr(10) + "'cutoff' have not a correct value! [0,...,255]" + chr(10))
  1202.    Assert((threshold>=0&&threshold<=127) ? true : false, chr(10) + "'threshold' have not a correct value! [0,...,127]" + chr(10))
  1203.    Assert((maxdiff>=0&&maxdiff<=255)     ? true : false, chr(10) + "'maxdiff' have not a correct value! [0,...,255]" + chr(10))
  1204.    Assert((reduc>=-1&&reduc<=1.0)        ? true : false, chr(10) + "'reduc' have not a correct value! [-1,0.0,...,1.0]" + chr(10))
  1205.    Assert((AAthr>=0&&AAthr<=255)         ? true : false, chr(10) + "'AAthr' have not a correct value! [0,...,255]" + chr(10))
  1206.    Assert((method>=0&&method<=2)         ? true : false, chr(10) + "'method' have not a correct value! [0,1,2]" + chr(10))
  1207.    Assert((ECrad>=1)                     ? true : false, chr(10) + "'ECrad' have not a correct value! [>=1]" + chr(10))
  1208.    Assert((ECthr>=0&&ECthr<=255)         ? true : false, chr(10) + "'ECthr' have not a correct value! [0,...,255]" + chr(10))
  1209.  
  1210.    Assert((sharp==true||protect==true)&&defined(source)==false ? false : true, chr(10) + "Sharp & Protect need 'source' input in MCTDmod_PP !" + chr(10))
  1211.  
  1212.  
  1213.    ### WARNING
  1214.    Assert(isYV12(denoised) == True ? true : false, chr(10) + "This is not an YV12 clip ! Please convert color space to YV12 before using MCTDmod_PP()" + chr(10))
  1215.  
  1216.    w = denoised.width()
  1217.    h = denoised.height()
  1218.  
  1219.    m4 = (internal==true) ? true : ((int(w/4.0)*4==w)&&(int(h/4.0)*4==h)) ? true : false
  1220.    Assert( m4 ? true : false, chr(10) + "This is not a MOD4 clip! Please use crop() or addborders() before using MCTDmod_PP()" + chr(10))
  1221.  
  1222.    ### EXTERNAL
  1223.    blksize     = MCTDmod_Defaults( denoised, settings_num, "blksize"     )
  1224.    overlap     = MCTDmod_Defaults( denoised, settings_num, "overlap"     )
  1225.    truemotion  = MCTDmod_Defaults( denoised, settings_num, "truemotion"  )
  1226.    MVglobal    = MCTDmod_Defaults( denoised, settings_num, "MVglobal"    )
  1227.    pel         = MCTDmod_Defaults( denoised, settings_num, "pel"         )
  1228.    pelsearch   = MCTDmod_Defaults( denoised, settings_num, "pelsearch"   )
  1229.    search      = MCTDmod_Defaults( denoised, settings_num, "search"      )
  1230.    searchparam = MCTDmod_Defaults( denoised, settings_num, "searchparam" )
  1231.    MVsharp     = MCTDmod_Defaults( denoised, settings_num, "MVsharp"     )
  1232.    DCT         = MCTDmod_Defaults( denoised, settings_num, "DCT"         )
  1233.    thSAD       = MCTDmod_Defaults( denoised, settings_num, "thSAD"       )
  1234.    thSCD1      = MCTDmod_Defaults( denoised, settings_num, "thSCD1"      )
  1235.    thSCD2      = MCTDmod_Defaults( denoised, settings_num, "thSCD2"      )
  1236.  
  1237.    denoisedMVS = (internal==true) ? denoised : denoised.MSuper(hpad=0,vpad=0,pel=pel,sharp=MVsharp,chroma=false)
  1238.    f1v         = (internal==true) ? f1v : MAnalyse(denoisedMVS, isb=false, delta=1, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=false)
  1239.    b1v         = (internal==true) ? b1v : MAnalyse(denoisedMVS, isb=true,  delta=1, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=false)
  1240.    f1c         = (internal==true) ? f1c
  1241.    \           : defined(source)  ? MCompensate(source  , denoisedMVS, f1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  1242.    \           :                    MCompensate(denoised, denoisedMVS, f1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  1243.    b1c         = (internal==true) ? b1c
  1244.    \           : defined(source)  ? MCompensate(source  , denoisedMVS, b1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  1245.    \           :                    MCompensate(denoised, denoisedMVS, b1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  1246.    SAD_f1m     = defined(source)  ? MMask(source,   f1v, kind=1, ml=thSAD, gamma=0.999, Ysc=0)
  1247.    \           :                    MMask(denoised, f1v, kind=1, ml=thSAD, gamma=0.999, Ysc=0)
  1248.    SAD_b1m     = defined(source)  ? MMask(source,   b1v, kind=1, ml=thSAD, gamma=0.999, Ysc=0)
  1249.    \           :                    MMask(denoised, b1v, kind=1, ml=thSAD, gamma=0.999, Ysc=0)
  1250.  
  1251.  
  1252.    ### DEBLOCKING
  1253.    padX = w%8 == 0 ? 0 : (8 - w%8)
  1254.    padY = h%8 == 0 ? 0 : (8 - h%8)
  1255.  
  1256.    denoised = (internal==true) ? denoised
  1257.    \        : (deblock==false) ? denoised
  1258.    \        : (useQED==true)   ? denoised.deblock_QED(quant1=quant1,quant2=quant2,uv=chroma?3:2)
  1259.    \        : (padX==0 && padY==0) ? denoised.deblock(quant=int(quant1*0.5+quant2*0.5))
  1260.    \        : denoised.pointresize(w+padX, h+padY, 0, 0, w+padX, h+padY).deblock(quant=int(quant1*0.5+quant2*0.5)).Crop(0, 0, -padX, -padY)
  1261.  
  1262.  
  1263.    ### INITIATING
  1264.    nullclip = BlankClip()
  1265.  
  1266.    i   = defined(source) ? source : denoised
  1267.    smP = denoised
  1268.  
  1269.    xn = smP.width()
  1270.    yn = smP.height()
  1271.  
  1272.    chr41 = chroma?4:1
  1273.    chr31 = chroma?3:1
  1274.    chr21 = chroma?2:1
  1275.  
  1276.  
  1277.    ### PROTECTING
  1278.    rg17 = (protect==true) ? smP.removegrain(17,-1) : NOP()
  1279.    iOB  = (protect==true) ? i.mt_lut("x "+string(cutoff)+" >= x 0 ?",U=1,V=1) : NOP()
  1280.    mB   = (protect==true) ? mt_makediff(iOB,rg17,U=1,V=1).mt_binarize(128+threshold,upper=false,U=1,V=1).removegrain(5,-1) : NOP()
  1281.    lB   = (protect==true) ? mt_lutxy(smP,i,"x y - abs "+string(maxdiff)+" <= x x y - 0 < y "+string(maxdiff)+" - x ? ?",U=1,V=1) : NOP()
  1282.    smB  = (protect==true) ? mt_merge(smP,lB,mB,U=chr21,V=chr21) : smP
  1283.  
  1284.  
  1285.    ### ANTI-ALIASING
  1286.    cAA = (AA==false)      ? nullclip
  1287.    \   : (useEEDI2==true) ? smB.Turnleft().EEDI2(field=1,maxd=maxd).Turnright().EEDI2(field=1,maxd=maxd)
  1288.    \   :                    smB.Pointresize(xn*2,yn*2).Turnleft().Sangnom(order=1,aa=maxd).Turnright().Sangnom(order=1,aa=maxd)
  1289.    cAA = (AA==false)      ? nullclip
  1290.    \   : (method==0)      ? cAA.Bilinearresize(xn,yn,0.5,-0.5,xn*2,yn*2)
  1291.    \   : (method==1)      ? cAA.Spline36resize(xn,yn,0.5,-0.5,xn*2,yn*2)
  1292.    \   :                    cAA.Lanczos4resize(xn,yn,0.5,-0.5,xn*2,yn*2)
  1293.    cAA = (aaF!="internal") ? Eval("smB."+aaF) : cAA
  1294.    mA  = (AA==true) ? smB.mt_edge("prewitt",0,255,0,0,V=1,U=1).mt_lut("x "+string(AAthr)+" <= 0 x 1.4 ^ ?",U=1,V=1).removegrain(4,-1).mt_inflate(U=1,V=1) : NOP()
  1295.    smA = (AA==true) ? mt_merge(smB,cAA,mA,luma=chroma?true:false,U=chr31,V=chr31) : NOP()
  1296.  
  1297.    aaD1    = (AA==true&&reduc!=-1) ? mt_makediff(smB, smA, U=1, V=1) : NOP()
  1298.    aaD1MVS = (AA==true&&reduc!=-1) ? aaD1.MSuper(hpad=0,vpad=0,pel=pel,sharp=MVsharp,levels=1,chroma=false) : NOP()              
  1299.    aaD2    = (AA==true&&reduc!=-1) ? aaD1.MDegrain1(aaD1MVS, b1v, f1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2, plane=0) : NOP()
  1300.    aaD2    = (AA==true&&reduc!=-1) ? mt_lutxy(aaD1, aaD2, "x 128 - abs y 128 - abs < x y ?", U=1, V=1).mergeluma(aaD2, 1.0-reduc) : NOP()
  1301.  
  1302.    smA     = (AA==false) ? smB
  1303.    \       : (reduc==-1) ? smA
  1304.    \       :               mt_makediff(smB, aaD2, U=2, V=2)
  1305.  
  1306.  
  1307.    ### EDGECLEANING
  1308.    mP  = (edgeclean==true) ? mt_edge(smA,"prewitt",0,255,0,0,V=1,U=1) : NOP()
  1309.    mS  = (edgeclean==true) ? mP.mt_expand(mode=mt_square(radius=ECrad),U=1,V=1).mt_inflate(U=1,V=1) : NOP()
  1310.    mD  = (edgeclean==true) ? mt_lutxy(mS,mP.mt_inflate(U=1,V=1),"x y - "+string(ECthr)+" <= 0 x y - ?",U=1,V=1).mt_inflate(U=1,V=1).removegrain(20,-1) : NOP()
  1311.    smE = (edgeclean==true) ? mt_merge(smA,Eval("smA." + ECmode),mD,luma=chroma?true:false,U=chr31,V=chr31) : smA
  1312.  
  1313.  
  1314.    ### MASKING
  1315.    mM = (sharp==true||enhance==true||stabilize==true) ? mt_average(SAD_f1m,SAD_b1m,U=1,V=1).mt_lut(expr="x 1.6 ^",U=1,V=1) : NOP()
  1316.    mE = (sharp==true||enhance==true||stabilize==true) ? mt_edge(smE,"prewitt",0,255,0,0,V=1,U=1).mt_lut(expr="x 1.8 ^",U=1,V=1).removegrain(4,-1).mt_inflate(U=1,V=1) : NOP()
  1317.    mL = (sharp==true||enhance==true||stabilize==true) ? mt_logic(mM.invert(),mE,"min",U=1,V=1).removegrain(20,-1) : NOP()
  1318.    mF = (sharp==true||enhance==true||stabilize==true) ? mt_logic(mM,mE,"max",U=1,V=1).removegrain(20,-1) : NOP()
  1319.    mR = (sharp==true||enhance==true||stabilize==true) ? mE.removegrain(20,-1) : NOP()
  1320.  
  1321.  
  1322.    ### SHARPENING
  1323.    Sclp = (sharp == true) ? (shF!="internal") ? Eval("smE."+shF) :
  1324.    \                        IsBool(ssource)   ? ssource ? smE.LSFmod(strength=strength,Smode=SHmode,Smethod=SHmethod,Lmode=0,overshoot=Sovershoot,preblur="ON",secure=true,edgemode=0,soft=0,soothe=false,ss_x=1.00,ss_y=1.00,source=i)
  1325.    \                                                    : smE.LSFmod(strength=strength,Smode=SHmode,Smethod=SHmethod,Lmode=Slimit,overshoot=Sovershoot,preblur="ON",secure=true,edgemode=0,soft=0,soothe=false,ss_x=1.00,ss_y=1.00) :
  1326.    \                        IsClip(ssource)   ? smE.LSFmod(strength=strength,Smode=SHmode,Smethod=SHmethod,Lmode=0,overshoot=Sovershoot,preblur="ON",secure=true,edgemode=0,soft=0,soothe=false,ss_x=1.00,ss_y=1.00,source=ssource) :
  1327.    \                        Assert(False, "MCTD: ssource should be bool or clip!")
  1328.    \                      : NOP()
  1329.    Tmax = (sharp == true) ? i.mt_logic(f1c,"max",U=1,V=1).mt_logic(b1c,"max",U=1,V=1) : NOP()
  1330.    Tmin = (sharp == true) ? i.mt_logic(f1c,"min",U=1,V=1).mt_logic(b1c,"min",U=1,V=1) : NOP()
  1331.  
  1332.    shrp = (sharp==false) ? smE
  1333.    \    : (Tlimit==true) ? Sclp.mt_clamp(Tmax, Tmin, Tovershoot, Tovershoot, U=1, V=1)
  1334.    \    :                  Sclp
  1335.    sL   = (sharp==true&&adapt==true) ? useMMask ? mt_merge(smE,shrp,mL,U=chr21,V=chr21)
  1336.    \                                            : mt_merge(smE,shrp,mR,U=chr21,V=chr21)
  1337.    \                                 : shrp.mergechroma(smE)
  1338.  
  1339.  
  1340.    ### STABILIZING & ENHANCING
  1341.    TTc = (stabilize==true) ? (stF!="internal") ? Eval("sL."+stF)
  1342.    \   :                     sL.TTempSmoothF(maxr=maxr, lthresh=lthresh, cthresh=cthresh, strength=TTstr)
  1343.    \   :                     sL
  1344.    GFc = (enhance==true) ? (dbF!="internal") ? Eval("TTc."+dbF)
  1345.    \   :                   TTc.GradFun2DBmod(thr=GFthr,thrC=chroma?GFthr:1.0,mode=0,str=AGstr,strC=0.0,temp=temp,adapt=bias,mask=false,show=false)
  1346.    \   :                   TTc
  1347.    FSc = (enhance==true||stabilize==true) ? useMMask ? MCPP_final_mt_merge(GFc,sL,mF,luma=chroma?true:false,U=chr31,V=chr31)
  1348.    \                                                 : MCPP_final_mt_merge(GFc,sL,mR,luma=chroma?true:false,U=chr31,V=chr31)
  1349.    \                                      : GFc
  1350.    PP  = (chroma==false) ? FSc.MCPP_final_mergechroma(internal?i:smP)
  1351.    \   :                   FSc
  1352.  
  1353.    return PP
  1354.    }
  1355.  
  1356. PP = pp ? MCTDmod_PP( smP, i,
  1357.    \               sharp, adapt, strength, SHmode, SHmethod, Slimit, Sovershoot, Tlimit, Tovershoot, ssource, shF,
  1358.    \               protect, cutoff, threshold, maxdiff,
  1359.    \               AA, useEEDI2, reduc, maxd, AAthr, method, aaF,
  1360.    \               deblock, useQED, quant1, quant2,
  1361.    \               edgeclean, ECrad, ECthr, ECmode,
  1362.    \               stabilize, maxr, lthresh, cthresh, TTstr, stF,
  1363.    \               enhance, GFthr, AGstr, bias, temp, dbF,
  1364.    \               chroma, settings, useMMask,
  1365.    \               f1v, b1v, f1c, b1c, true )
  1366.    \    : chroma ? smP
  1367.    \             : smP.MCPP_final_MergeChroma(i)
  1368.  
  1369.  
  1370. ### OUTPUT
  1371. o = ((i.height) * 2 == PP.height) ? PP.MCPP_crop16(xf/2,yf/2,-xf/2,-yf/2) : PP.crop(xf/2,yf/2,-xf/2,-yf/2)
  1372. o = (interlaced==true) ? o.weave() : o
  1373.  
  1374. p1nr = Defined(p1nr) ? p1nr : "internal"
  1375. p2nr = Defined(p2nr) ? p2nr : "internal"
  1376. ppnr = Defined(ppnr) ? ppnr : "internal"
  1377.  
  1378. ### SHOW
  1379.    function MCTDmod_Show( clip i, clip p, clip o, int "xf", int "yf", string "version", string "settings", string "show_p",
  1380.    \                   int "radius", float "sigma", bool "twopass", bool "useTTmpSm", int "limit", int "limitC", int "limit2", int "limitC2", int "post", bool "chroma", bool "interlaced", bool "refine", int "pMode",
  1381.    \                   bool "sharp", bool "adapt", int "strength", int "SHmode", int "SHmethod", int "Slimit", int "Sovershoot", bool "Tlimit", int "Tovershoot", val "ssource", string "shF",
  1382.    \                   bool "protect", int "cutoff", int "threshold", int "maxdiff",
  1383.    \                   bool "AA", bool "useEEDI2", float "reduc", int "maxd", int "AAthr", int "method", string "aaF",
  1384.    \                   bool "deblock", bool "useQED", int "quant1", int "quant2",
  1385.    \                   bool "edgeclean", int "ECrad", int "ECthr", string "ECmode",
  1386.    \                   bool "stabilize", int "maxr", int "lthresh", int "cthresh", int "TTstr", string "stF",
  1387.    \                   bool "enhance", float "GFthr", float "AGstr", int "bias", int "temp", string "dbF",
  1388.    \                   int "bwbh", int "owoh", int "blksize", int "overlap",
  1389.    \                   bool "GPU", bool "fixband", int "bt", int "ncpu", int "precision",
  1390.    \                   int "thSAD", int "thSAD2", int "thSCD1", int "thSCD2",
  1391.    \                   bool "truemotion", bool "MVglobal", int "pel", int "pelsearch", int "search", int "searchparam", int "MVsharp", int "DCT",
  1392.    \                   bool "useMMask",
  1393.    \                   int "screenW", int "screenH",
  1394.    \                   string "p1nr", string "p2nr", string "ppnr" )
  1395.    {
  1396.    ssource = IsBool(ssource) ? ssource :
  1397.    \         IsClip(ssource) ? True    :
  1398.    \                           False
  1399.  
  1400.    i  = i.crop(xf/2,yf/2,-xf/2,-yf/2)
  1401.    p  = p.crop(xf/2,yf/2,-xf/2,-yf/2)
  1402.    cW = screenW/2
  1403.    cH = screenH/2
  1404.    iW = i.width()
  1405.    iH = i.height()
  1406.  
  1407.    iR = round((iW*cH)/(4*iH))*4
  1408.    iC = (iR-cW)/2
  1409.  
  1410.    i = i.spline36resize(iR,cH)
  1411.    p = p.spline36resize(iR,cH)
  1412.    o = o.spline36resize(iR,cH)
  1413.  
  1414.    i = (iC>0) ? i.crop(iC,0,-iC,0) : i
  1415.    p = (iC>0) ? p.crop(iC,0,-iC,0) : p
  1416.    o = (iC>0) ? o.crop(iC,0,-iC,0) : o
  1417.  
  1418.    i = (iC<0) ? i.addborders(-iC,0,-iC,0) : i
  1419.    p = (iC<0) ? p.addborders(-iC,0,-iC,0) : p
  1420.    o = (iC<0) ? o.addborders(-iC,0,-iC,0) : o
  1421.  
  1422.    b = i.blankclip()
  1423.  
  1424.    b = b.SubTitle("MCTD    "              + string(version)             ,text_color=$FFFFFF,font="COURIER NEW",size=24,x=40,y=20)
  1425.    b = b.SubTitle("SETTINGS           = " + string(settings)            ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=60)
  1426.    b = b.SubTitle("'p' clip           = " + string(show_p)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=70)
  1427.  
  1428.    b = b.SubTitle("radius             = " + string(radius)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=90)
  1429.    b = b.SubTitle("sigma              = " + string(sigma)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=100)
  1430.    b = b.SubTitle("twopass            = " + string(twopass)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=110)
  1431.    b = b.SubTitle("useTTmpSm          = " + string(useTTmpSm)           ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=120)
  1432.    b = b.SubTitle("limit              = " + string(limit)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=130)
  1433.    b = b.SubTitle("limitC             = " + string(limitC)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=140)
  1434.    b = b.SubTitle("limit2             = " + string(limit2)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=150)
  1435.    b = b.SubTitle("limitC2            = " + string(limitC2)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=160)
  1436.    b = b.SubTitle("post               = " + string(post)                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=170)
  1437.    b = b.SubTitle("chroma             = " + string(chroma)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=180)
  1438.    b = b.SubTitle("interlaced         = " + string(interlaced)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=190)
  1439.    b = b.SubTitle("refine             = " + string(refine)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=200)
  1440.    b = b.SubTitle("pMode              = " + string(pMode)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=210)
  1441.  
  1442.    b = b.SubTitle("sharp              = " + string(sharp)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=230)
  1443.    b = b.SubTitle("adapt              = " + string(adapt)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=240)
  1444.    b = b.SubTitle("strength           = " + string(strength)            ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=250)
  1445.    b = b.SubTitle("SHmode             = " + string(SHmode)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=260)
  1446.    b = b.SubTitle("SHmethod           = " + string(SHmethod)            ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=270)
  1447.    b = b.SubTitle("Slimit             = " + string(Slimit)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=280)
  1448.    b = b.SubTitle("Sovershoot         = " + string(Sovershoot)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=290)
  1449.    b = b.SubTitle("Tlimit             = " + string(Tlimit)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=300)
  1450.    b = b.SubTitle("Tovershoot         = " + string(Tovershoot)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=310)
  1451.    b = b.SubTitle("ssource            = " + string(ssource)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=320)
  1452.    b = b.SubTitle("shF                = " + string(shF)                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=330)
  1453.  
  1454.    b = b.SubTitle("protect            = " + string(protect)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=350)
  1455.    b = b.SubTitle("cutoff             = " + string(cutoff)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=360)
  1456.    b = b.SubTitle("threshold          = " + string(threshold)           ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=370)
  1457.    b = b.SubTitle("maxdiff            = " + string(maxdiff)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=380)
  1458.  
  1459.    b = b.SubTitle("AA                 = " + string(AA)                  ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=400)
  1460.    b = b.SubTitle("useEEDI2           = " + string(useEEDI2)            ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=410)
  1461.    b = b.SubTitle("reduc              = " + string(reduc)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=420)
  1462.    b = b.SubTitle("maxd               = " + string(maxd)                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=430)
  1463.    b = b.SubTitle("AAthr              = " + string(AAthr)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=440)
  1464.    b = b.SubTitle("method             = " + string(method)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=450)
  1465.    b = b.SubTitle("aaF                = " + string(aaF)                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=460)
  1466.  
  1467.    b = b.SubTitle("deblock            = " + string(deblock)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=480)
  1468.    b = b.SubTitle("useQED             = " + string(useQED)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=490)
  1469.    b = b.SubTitle("quant1             = " + string(quant1)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=500)
  1470.    b = b.SubTitle("quant2             = " + string(quant2)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=510)
  1471.  
  1472.    b = b.SubTitle("p1nr               = " + string(p1nr)                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=530)
  1473.    b = b.SubTitle("p2nr               = " + string(p2nr)                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=540)
  1474.    b = b.SubTitle("ppnr               = " + string(ppnr)                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=60,y=550)
  1475.  
  1476.    b = b.SubTitle("edgeclean          = " + string(edgeclean)           ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=60)
  1477.    b = b.SubTitle("ECrad              = " + string(ECrad)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=70)
  1478.    b = b.SubTitle("ECthr              = " + string(ECthr)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=80)
  1479.    b = b.SubTitle("ECmode             = " + string(ECmode)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=90)
  1480.  
  1481.    b = b.SubTitle("stabilize          = " + string(stabilize)           ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=110)
  1482.    b = b.SubTitle("maxr               = " + string(maxr)                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=120)
  1483.    b = b.SubTitle("lthresh            = " + string(lthresh)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=130)
  1484.    b = b.SubTitle("cthresh            = " + string(cthresh)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=140)
  1485.    b = b.SubTitle("TTstr              = " + string(TTstr)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=150)
  1486.    b = b.SubTitle("stF                = " + string(stF)                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=160)
  1487.  
  1488.    b = b.SubTitle("enhance            = " + string(enhance)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=180)
  1489.    b = b.SubTitle("GFthr              = " + string(GFthr)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=190)
  1490.    b = b.SubTitle("AGstr              = " + string(AGstr)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=200)
  1491.    b = b.SubTitle("bias               = " + string(bias)                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=210)
  1492.    b = b.SubTitle("temp               = " + string(temp)                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=220)
  1493.    b = b.SubTitle("dbF                = " + string(dbF)                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=230)
  1494.  
  1495.    b = b.SubTitle("bwbh               = " + string(bwbh)                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=250)
  1496.    b = b.SubTitle("owoh               = " + string(owoh)                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=260)
  1497.    b = b.SubTitle("blksize            = " + string(blksize)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=270)
  1498.    b = b.SubTitle("overlap            = " + string(overlap)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=280)
  1499.  
  1500.    b = b.SubTitle("GPU                = " + string(GPU)                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=300)
  1501.    b = b.SubTitle("fixband            = " + string(fixband)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=310)
  1502.    b = b.SubTitle("bt                 = " + string(bt)                  ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=320)
  1503.    b = b.SubTitle("ncpu               = " + string(ncpu)                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=330)
  1504.    b = b.SubTitle("precision          = " + string(precision)           ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=340)
  1505.  
  1506.    b = b.SubTitle("thSAD              = " + string(thSAD)               ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=360)
  1507.    b = b.SubTitle("thSAD2             = " + string(thSAD2)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=370)
  1508.    b = b.SubTitle("thSCD1             = " + string(thSCD1)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=380)
  1509.    b = b.SubTitle("thSCD2             = " + string(thSCD2)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=390)
  1510.  
  1511.    b = b.SubTitle("truemotion         = " + string(truemotion)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=410)
  1512.    b = b.SubTitle("MVglobal           = " + string(MVglobal)            ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=420)
  1513.    b = b.SubTitle("pel                = " + string(pel)                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=430)
  1514.    b = b.SubTitle("pelsearch          = " + string(pelsearch)           ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=440)
  1515.    b = b.SubTitle("search             = " + string(search)              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=450)
  1516.    b = b.SubTitle("searchparam        = " + string(searchparam)         ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=460)
  1517.    b = b.SubTitle("MVsharp            = " + string(MVsharp)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=470)
  1518.    b = b.SubTitle("DCT                = " + string(DCT)                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=480)
  1519.    b = b.SubTitle("useMMask           = " + string(useMMask)            ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=490)
  1520.  
  1521.    #b = b.SubTitle("screenW            = " + string(screenW)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=500)
  1522.    #b = b.SubTitle("screenH            = " + string(screenH)             ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=360,y=510)
  1523.  
  1524.    i = i.subtitle("Input",text_color=$FFFFFF,size=20,x=20,y=20)
  1525.    p = p.subtitle("Prefilter",text_color=$FFFFFF,size=20,x=20,y=20)
  1526.    o = o.subtitle("Output",text_color=$FFFFFF,size=20,x=20,y=20)
  1527.  
  1528.    TOP = stackhorizontal(b,p)
  1529.    BOT = stackhorizontal(i,o)
  1530.  
  1531.    return stackvertical(TOP,BOT)
  1532.    }
  1533.  
  1534. o = (show==false) ? o : MCTDmod_Show(i,p,o,xf,yf,version,settings,show_p,
  1535. \                                 radius,sigma,twopass,useTTmpSm,limit,limitC,limit2,limitC2,post,chroma,interlaced,refine,pMode,
  1536. \                                 sharp,adapt,strength,SHmode,SHmethod,Slimit,Sovershoot,Tlimit,Tovershoot,ssource,shF,
  1537. \                                 protect,cutoff,threshold,maxdiff,
  1538. \                                 AA,useEEDI2,reduc,maxd,AAthr,method,aaF,
  1539. \                                 deblock,useQED,quant1,quant2,
  1540. \                                 edgeclean,ECrad,ECthr,ECmode,
  1541. \                                 stabilize,maxr,lthresh,cthresh,TTstr,stF,
  1542. \                                 enhance,GFthr,AGstr,bias,temp,dbF,
  1543. \                                 bwbh,owoh,blksize,overlap,
  1544. \                                 GPU,fixband,bt,ncpu,precision,
  1545. \                                 thSAD,thSAD2,thSCD1,thSCD2,
  1546. \                                 truemotion,MVglobal,pel,pelsearch,search,searchparam,MVsharp,DCT,
  1547. \                                 useMMask,
  1548. \                                 screenW,screenH,
  1549. \                                 p1nr,p2nr,ppnr)
  1550.  
  1551. return o
  1552. }
  1553.  
  1554. # ------- Support functions for stacked high bit depth -------
  1555.  
  1556. # ------- Copied from Dither package -------
  1557. Function MCPP_get_msb(clip src){  return src.Crop(0, 0, src.Width, src.Height/2) }
  1558. Function MCPP_get_lsb(clip src){  return src.Crop(0, src.Height/2, src.Width, src.Height/2) }
  1559. Function MCPP_convert_8_to_16(clip src){ return StackVertical(src, BlankClip(src, pixel_type="YV12", color_yuv=0)) }
  1560.  
  1561. Function MCPP_merge16_8(clip src1, clip src2, clip mask, bool "luma", int "y", int "u", int "v")
  1562. {
  1563.     luma = Default(luma, false)
  1564.  
  1565.     y = Default(y, 3)
  1566.     u = Default(u, 2)
  1567.     v = Default(v, 2)
  1568.  
  1569.     mask16 = StackVertical(mask, mask)
  1570.     Dither_merge16(src1, src2, mask16, luma=luma, y=y, u=u, v=v)
  1571. }
  1572.  
  1573. Function MCPP_crop16(clip src, int "left", int "top",
  1574. \   int "width", int "height", bool "align")
  1575. {
  1576.     left   = Default(left,   0)
  1577.     top    = Default(top,    0)
  1578.     width  = Default(width,  0)
  1579.     height = Default(height, 0)
  1580.  
  1581.     src
  1582.     w = Width()
  1583.     h = Height() / 2
  1584.  
  1585.     width  = (width  <= 0) ? w - left + width  : width
  1586.     height = (height <= 0) ? h - top  + height : height
  1587.     Assert (width  > 0)
  1588.     Assert (height > 0)
  1589.  
  1590.     msb = Crop(left, top,     width, height, align)
  1591.     lsb = Crop(left, top + h, width, height, align)
  1592.  
  1593.     return (top == 0 && height == h) ? src.Crop(left, 0, width, 0)
  1594.   \                                : StackVertical(msb, lsb)
  1595. }
  1596.  
  1597. Function MCPP_final_mt_merge(clip src1, clip src2, clip mask, bool "luma", int "y", int "u", int "v")
  1598. {
  1599.     luma = Default(luma, false)
  1600.  
  1601.     y = Default(y, 3)
  1602.     u = Default(u, 2)
  1603.     v = Default(v, 2)
  1604.  
  1605.     src1 = (src1.height * 2 == src2.height    ) ? src1.MCPP_convert_8_to_16() : src1
  1606.     src2 = (src1.height     == src2.height * 2) ? src2.MCPP_convert_8_to_16() : src2
  1607.     return Eval( ((mask.height * 2 == src1.height) ? "MCPP_merge16_8" : "mt_merge") + "(src1, src2, mask, luma, y, u, v)")
  1608. }
  1609.  
  1610. Function MCPP_final_MergeChroma(clip src1, clip src2)
  1611. {
  1612.     src1 = (src1.height * 2 == src2.height    ) ? src1.MCPP_convert_8_to_16() : src1
  1613.     src2 = (src1.height     == src2.height * 2) ? src2.MCPP_convert_8_to_16() : src2
  1614.     return src1.MergeChroma(src2)
  1615. }
  1616.  
  1617. function MCTD_Luma_Rebuild(clip src, float "s0", float "c",int "uv", bool "lsb", bool "lsb_in", bool "lsb_out", int "mode"){
  1618.     lsb_in  = Default( lsb_in  ,false)
  1619.     lsb_out = Default( lsb_out ,false)
  1620.     lsb     = Default( lsb     ,lsb_in || lsb_out)
  1621.     mode    = Default( mode ,6)
  1622.     uv      = Default(uv,    3)
  1623.     s0      = Default(s0,  2.0)
  1624.     c       = Default(c,  1.0/16)
  1625.  
  1626.     k = (s0 - 1) * c
  1627.     t = lsb_in ? "x 4096 - 56064 / 0 1 clip" : "x 16 - 219 / 0 1 clip"
  1628.     e = String(k)+" "+String(1+c)+" "+String((1+c)*c)+" "+t+" "+String(c)
  1629.     \   +" + / - * "+t+" 1 "+String(k)+" - * + "+String(lsb?65536:255)+" *"
  1630.  
  1631.     src
  1632.     lsb ? (lsb_in ? Dither_lut16 (yexpr=e, expr="x 32768 - 32768 * 28672 / 32768 +", y=3, u=uv, v=uv)  : \
  1633.                     Dither_lut8  (yexpr=e, expr="x 128 - 32768 * 112 / 32768 +"    , y=3, u=uv, v=uv)) : \
  1634.                     mt_lut       (yexpr=e, expr="x 128 - 128 * 112 / 128 +"        , y=3, u=uv, v=uv)
  1635.     lsb_out ? last : (lsb ? Ditherpost(mode=mode,u=uv,v=uv) : last)
  1636. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement