af_afwtdn.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. /*
  2. * Copyright (c) 2020 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <float.h>
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/opt.h"
  24. #include "avfilter.h"
  25. #include "audio.h"
  26. #include "filters.h"
  27. #include "formats.h"
  28. enum WaveletTypes {
  29. SYM2,
  30. SYM4,
  31. RBIOR68,
  32. DEB10,
  33. SYM10,
  34. COIF5,
  35. BL3,
  36. NB_WAVELET_TYPES,
  37. };
  38. /*
  39. * All wavelets coefficients are taken from: http://wavelets.pybytes.com/
  40. */
  41. static const double bl3_lp[42] = {
  42. 0.000146098, -0.000232304, -0.000285414, 0.000462093, 0.000559952,
  43. -0.000927187, -0.001103748, 0.00188212, 0.002186714, -0.003882426,
  44. -0.00435384, 0.008201477, 0.008685294, -0.017982291, -0.017176331,
  45. 0.042068328, 0.032080869, -0.110036987, -0.050201753, 0.433923147,
  46. 0.766130398, 0.433923147, -0.050201753, -0.110036987, 0.032080869,
  47. 0.042068328, -0.017176331, -0.017982291, 0.008685294, 0.008201477,
  48. -0.00435384, -0.003882426, 0.002186714, 0.00188212, -0.001103748,
  49. -0.000927187, 0.000559952, 0.000462093, -0.000285414, -0.000232304,
  50. 0.000146098, 0.0,
  51. };
  52. static const double bl3_hp[42] = {
  53. 0.0, 0.000146098, 0.000232304, -0.000285414, -0.000462093, 0.000559952,
  54. 0.000927187, -0.001103748, -0.00188212, 0.002186714, 0.003882426,
  55. -0.00435384, -0.008201477, 0.008685294, 0.017982291, -0.017176331,
  56. -0.042068328, 0.032080869, 0.110036987, -0.050201753, -0.433923147,
  57. 0.766130398, -0.433923147, -0.050201753, 0.110036987, 0.032080869,
  58. -0.042068328, -0.017176331, 0.017982291, 0.008685294, -0.008201477,
  59. -0.00435384, 0.003882426, 0.002186714, -0.00188212, -0.001103748,
  60. 0.000927187, 0.000559952, -0.000462093, -0.000285414, 0.000232304,
  61. 0.000146098,
  62. };
  63. static const double bl3_ilp[42] = {
  64. 0.0, 0.000146098, -0.000232304, -0.000285414, 0.000462093, 0.000559952,
  65. -0.000927187, -0.001103748, 0.00188212, 0.002186714, -0.003882426,
  66. -0.00435384, 0.008201477, 0.008685294, -0.017982291, -0.017176331,
  67. 0.042068328, 0.032080869, -0.110036987, -0.050201753, 0.433923147,
  68. 0.766130398, 0.433923147, -0.050201753, -0.110036987, 0.032080869,
  69. 0.042068328, -0.017176331, -0.017982291, 0.008685294, 0.008201477,
  70. -0.00435384, -0.003882426, 0.002186714, 0.00188212, -0.001103748,
  71. -0.000927187, 0.000559952, 0.000462093, -0.000285414, -0.000232304,
  72. 0.000146098,
  73. };
  74. static const double bl3_ihp[42] = {
  75. 0.000146098, 0.000232304, -0.000285414, -0.000462093, 0.000559952,
  76. 0.000927187, -0.001103748, -0.00188212, 0.002186714, 0.003882426,
  77. -0.00435384, -0.008201477, 0.008685294, 0.017982291, -0.017176331,
  78. -0.042068328, 0.032080869, 0.110036987, -0.050201753, -0.433923147,
  79. 0.766130398, -0.433923147, -0.050201753, 0.110036987, 0.032080869,
  80. -0.042068328, -0.017176331, 0.017982291, 0.008685294, -0.008201477,
  81. -0.00435384, 0.003882426, 0.002186714, -0.00188212, -0.001103748,
  82. 0.000927187, 0.000559952, -0.000462093, -0.000285414, 0.000232304,
  83. 0.000146098,
  84. };
  85. static const double sym10_lp[20] = {
  86. 0.0007701598091144901, 9.563267072289475e-05,
  87. -0.008641299277022422, -0.0014653825813050513,
  88. 0.0459272392310922, 0.011609893903711381,
  89. -0.15949427888491757, -0.07088053578324385,
  90. 0.47169066693843925, 0.7695100370211071,
  91. 0.38382676106708546, -0.03553674047381755,
  92. -0.0319900568824278, 0.04999497207737669,
  93. 0.005764912033581909, -0.02035493981231129,
  94. -0.0008043589320165449, 0.004593173585311828,
  95. 5.7036083618494284e-05, -0.0004593294210046588,
  96. };
  97. static const double sym10_hp[20] = {
  98. 0.0004593294210046588, 5.7036083618494284e-05,
  99. -0.004593173585311828, -0.0008043589320165449,
  100. 0.02035493981231129, 0.005764912033581909,
  101. -0.04999497207737669, -0.0319900568824278,
  102. 0.03553674047381755, 0.38382676106708546,
  103. -0.7695100370211071, 0.47169066693843925,
  104. 0.07088053578324385, -0.15949427888491757,
  105. -0.011609893903711381, 0.0459272392310922,
  106. 0.0014653825813050513, -0.008641299277022422,
  107. -9.563267072289475e-05, 0.0007701598091144901,
  108. };
  109. static const double sym10_ilp[20] = {
  110. -0.0004593294210046588, 5.7036083618494284e-05,
  111. 0.004593173585311828, -0.0008043589320165449,
  112. -0.02035493981231129, 0.005764912033581909,
  113. 0.04999497207737669, -0.0319900568824278,
  114. -0.03553674047381755, 0.38382676106708546,
  115. 0.7695100370211071, 0.47169066693843925,
  116. -0.07088053578324385, -0.15949427888491757,
  117. 0.011609893903711381, 0.0459272392310922,
  118. -0.0014653825813050513, -0.008641299277022422,
  119. 9.563267072289475e-05, 0.0007701598091144901,
  120. };
  121. static const double sym10_ihp[20] = {
  122. 0.0007701598091144901, -9.563267072289475e-05,
  123. -0.008641299277022422, 0.0014653825813050513,
  124. 0.0459272392310922, -0.011609893903711381,
  125. -0.15949427888491757, 0.07088053578324385,
  126. 0.47169066693843925, -0.7695100370211071,
  127. 0.38382676106708546, 0.03553674047381755,
  128. -0.0319900568824278, -0.04999497207737669,
  129. 0.005764912033581909, 0.02035493981231129,
  130. -0.0008043589320165449, -0.004593173585311828,
  131. 5.7036083618494284e-05, 0.0004593294210046588,
  132. };
  133. static const double rbior68_lp[18] = {
  134. 0.0, 0.0, 0.0, 0.0,
  135. 0.014426282505624435, 0.014467504896790148,
  136. -0.07872200106262882, -0.04036797903033992,
  137. 0.41784910915027457, 0.7589077294536541,
  138. 0.41784910915027457, -0.04036797903033992,
  139. -0.07872200106262882, 0.014467504896790148,
  140. 0.014426282505624435, 0.0, 0.0, 0.0,
  141. };
  142. static const double rbior68_hp[18] = {
  143. -0.0019088317364812906, -0.0019142861290887667,
  144. 0.016990639867602342, 0.01193456527972926,
  145. -0.04973290349094079, -0.07726317316720414,
  146. 0.09405920349573646, 0.4207962846098268,
  147. -0.8259229974584023, 0.4207962846098268,
  148. 0.09405920349573646, -0.07726317316720414,
  149. -0.04973290349094079, 0.01193456527972926,
  150. 0.016990639867602342, -0.0019142861290887667,
  151. -0.0019088317364812906, 0.0,
  152. };
  153. static const double rbior68_ilp[18] = {
  154. 0.0019088317364812906, -0.0019142861290887667,
  155. -0.016990639867602342, 0.01193456527972926,
  156. 0.04973290349094079, -0.07726317316720414,
  157. -0.09405920349573646, 0.4207962846098268,
  158. 0.8259229974584023, 0.4207962846098268,
  159. -0.09405920349573646, -0.07726317316720414,
  160. 0.04973290349094079, 0.01193456527972926,
  161. -0.016990639867602342, -0.0019142861290887667,
  162. 0.0019088317364812906, 0.0,
  163. };
  164. static const double rbior68_ihp[18] = {
  165. 0.0, 0.0, 0.0, 0.0,
  166. 0.014426282505624435, -0.014467504896790148,
  167. -0.07872200106262882, 0.04036797903033992,
  168. 0.41784910915027457, -0.7589077294536541,
  169. 0.41784910915027457, 0.04036797903033992,
  170. -0.07872200106262882, -0.014467504896790148,
  171. 0.014426282505624435, 0.0, 0.0, 0.0,
  172. };
  173. static const double coif5_lp[30] = {
  174. -9.517657273819165e-08, -1.6744288576823017e-07,
  175. 2.0637618513646814e-06, 3.7346551751414047e-06,
  176. -2.1315026809955787e-05, -4.134043227251251e-05,
  177. 0.00014054114970203437, 0.00030225958181306315,
  178. -0.0006381313430451114, -0.0016628637020130838,
  179. 0.0024333732126576722, 0.006764185448053083,
  180. -0.009164231162481846, -0.01976177894257264,
  181. 0.03268357426711183, 0.0412892087501817,
  182. -0.10557420870333893, -0.06203596396290357,
  183. 0.4379916261718371, 0.7742896036529562,
  184. 0.4215662066908515, -0.05204316317624377,
  185. -0.09192001055969624, 0.02816802897093635,
  186. 0.023408156785839195, -0.010131117519849788,
  187. -0.004159358781386048, 0.0021782363581090178,
  188. 0.00035858968789573785, -0.00021208083980379827,
  189. };
  190. static const double coif5_hp[30] = {
  191. 0.00021208083980379827, 0.00035858968789573785,
  192. -0.0021782363581090178, -0.004159358781386048,
  193. 0.010131117519849788, 0.023408156785839195,
  194. -0.02816802897093635, -0.09192001055969624,
  195. 0.05204316317624377, 0.4215662066908515,
  196. -0.7742896036529562, 0.4379916261718371,
  197. 0.06203596396290357, -0.10557420870333893,
  198. -0.0412892087501817, 0.03268357426711183,
  199. 0.01976177894257264, -0.009164231162481846,
  200. -0.006764185448053083, 0.0024333732126576722,
  201. 0.0016628637020130838, -0.0006381313430451114,
  202. -0.00030225958181306315, 0.00014054114970203437,
  203. 4.134043227251251e-05, -2.1315026809955787e-05,
  204. -3.7346551751414047e-06, 2.0637618513646814e-06,
  205. 1.6744288576823017e-07, -9.517657273819165e-08,
  206. };
  207. static const double coif5_ilp[30] = {
  208. -0.00021208083980379827, 0.00035858968789573785,
  209. 0.0021782363581090178, -0.004159358781386048,
  210. -0.010131117519849788, 0.023408156785839195,
  211. 0.02816802897093635, -0.09192001055969624,
  212. -0.05204316317624377, 0.4215662066908515,
  213. 0.7742896036529562, 0.4379916261718371,
  214. -0.06203596396290357, -0.10557420870333893,
  215. 0.0412892087501817, 0.03268357426711183,
  216. -0.01976177894257264, -0.009164231162481846,
  217. 0.006764185448053083, 0.0024333732126576722,
  218. -0.0016628637020130838, -0.0006381313430451114,
  219. 0.00030225958181306315, 0.00014054114970203437,
  220. -4.134043227251251e-05, -2.1315026809955787e-05,
  221. 3.7346551751414047e-06, 2.0637618513646814e-06,
  222. -1.6744288576823017e-07, -9.517657273819165e-08,
  223. };
  224. static const double coif5_ihp[30] = {
  225. -9.517657273819165e-08, 1.6744288576823017e-07,
  226. 2.0637618513646814e-06, -3.7346551751414047e-06,
  227. -2.1315026809955787e-05, 4.134043227251251e-05,
  228. 0.00014054114970203437, -0.00030225958181306315,
  229. -0.0006381313430451114, 0.0016628637020130838,
  230. 0.0024333732126576722, -0.006764185448053083,
  231. -0.009164231162481846, 0.01976177894257264,
  232. 0.03268357426711183, -0.0412892087501817,
  233. -0.10557420870333893, 0.06203596396290357,
  234. 0.4379916261718371, -0.7742896036529562,
  235. 0.4215662066908515, 0.05204316317624377,
  236. -0.09192001055969624, -0.02816802897093635,
  237. 0.023408156785839195, 0.010131117519849788,
  238. -0.004159358781386048, -0.0021782363581090178,
  239. 0.00035858968789573785, 0.00021208083980379827,
  240. };
  241. static const double deb10_lp[20] = {
  242. -1.326420300235487e-05, 9.358867000108985e-05,
  243. -0.0001164668549943862, -0.0006858566950046825,
  244. 0.00199240529499085, 0.0013953517469940798,
  245. -0.010733175482979604, 0.0036065535669883944,
  246. 0.03321267405893324, -0.02945753682194567,
  247. -0.07139414716586077, 0.09305736460380659,
  248. 0.12736934033574265, -0.19594627437659665,
  249. -0.24984642432648865, 0.2811723436604265,
  250. 0.6884590394525921, 0.5272011889309198,
  251. 0.18817680007762133, 0.026670057900950818,
  252. };
  253. static const double deb10_hp[20] = {
  254. -0.026670057900950818, 0.18817680007762133,
  255. -0.5272011889309198, 0.6884590394525921,
  256. -0.2811723436604265, -0.24984642432648865,
  257. 0.19594627437659665, 0.12736934033574265,
  258. -0.09305736460380659, -0.07139414716586077,
  259. 0.02945753682194567, 0.03321267405893324,
  260. -0.0036065535669883944, -0.010733175482979604,
  261. -0.0013953517469940798, 0.00199240529499085,
  262. 0.0006858566950046825, -0.0001164668549943862,
  263. -9.358867000108985e-05, -1.326420300235487e-05,
  264. };
  265. static const double deb10_ilp[20] = {
  266. 0.026670057900950818, 0.18817680007762133,
  267. 0.5272011889309198, 0.6884590394525921,
  268. 0.2811723436604265, -0.24984642432648865,
  269. -0.19594627437659665, 0.12736934033574265,
  270. 0.09305736460380659, -0.07139414716586077,
  271. -0.02945753682194567, 0.03321267405893324,
  272. 0.0036065535669883944, -0.010733175482979604,
  273. 0.0013953517469940798, 0.00199240529499085,
  274. -0.0006858566950046825, -0.0001164668549943862,
  275. 9.358867000108985e-05, -1.326420300235487e-05,
  276. };
  277. static const double deb10_ihp[20] = {
  278. -1.326420300235487e-05, -9.358867000108985e-05,
  279. -0.0001164668549943862, 0.0006858566950046825,
  280. 0.00199240529499085, -0.0013953517469940798,
  281. -0.010733175482979604, -0.0036065535669883944,
  282. 0.03321267405893324, 0.02945753682194567,
  283. -0.07139414716586077, -0.09305736460380659,
  284. 0.12736934033574265, 0.19594627437659665,
  285. -0.24984642432648865, -0.2811723436604265,
  286. 0.6884590394525921, -0.5272011889309198,
  287. 0.18817680007762133, -0.026670057900950818,
  288. };
  289. static const double sym4_lp[8] = {
  290. -0.07576571478927333,
  291. -0.02963552764599851,
  292. 0.49761866763201545,
  293. 0.8037387518059161,
  294. 0.29785779560527736,
  295. -0.09921954357684722,
  296. -0.012603967262037833,
  297. 0.0322231006040427,
  298. };
  299. static const double sym4_hp[8] = {
  300. -0.0322231006040427,
  301. -0.012603967262037833,
  302. 0.09921954357684722,
  303. 0.29785779560527736,
  304. -0.8037387518059161,
  305. 0.49761866763201545,
  306. 0.02963552764599851,
  307. -0.07576571478927333,
  308. };
  309. static const double sym4_ilp[8] = {
  310. 0.0322231006040427,
  311. -0.012603967262037833,
  312. -0.09921954357684722,
  313. 0.29785779560527736,
  314. 0.8037387518059161,
  315. 0.49761866763201545,
  316. -0.02963552764599851,
  317. -0.07576571478927333,
  318. };
  319. static const double sym4_ihp[8] = {
  320. -0.07576571478927333,
  321. 0.02963552764599851,
  322. 0.49761866763201545,
  323. -0.8037387518059161,
  324. 0.29785779560527736,
  325. 0.09921954357684722,
  326. -0.012603967262037833,
  327. -0.0322231006040427,
  328. };
  329. static const double sym2_lp[4] = {
  330. -0.12940952255092145, 0.22414386804185735,
  331. 0.836516303737469, 0.48296291314469025,
  332. };
  333. static const double sym2_hp[4] = {
  334. -0.48296291314469025, 0.836516303737469,
  335. -0.22414386804185735, -0.12940952255092145,
  336. };
  337. static const double sym2_ilp[4] = {
  338. 0.48296291314469025, 0.836516303737469,
  339. 0.22414386804185735, -0.12940952255092145,
  340. };
  341. static const double sym2_ihp[4] = {
  342. -0.12940952255092145, -0.22414386804185735,
  343. 0.836516303737469, -0.48296291314469025,
  344. };
  345. #define MAX_LEVELS 13
  346. typedef struct ChannelParams {
  347. int *output_length;
  348. int *filter_length;
  349. double **output_coefs;
  350. double **subbands_to_free;
  351. double **filter_coefs;
  352. int tempa_length;
  353. int tempa_len_max;
  354. int temp_in_length;
  355. int temp_in_max_length;
  356. int buffer_length;
  357. int min_left_ext;
  358. int max_left_ext;
  359. double *tempa;
  360. double *tempd;
  361. double *temp_in;
  362. double *buffer;
  363. double *buffer2;
  364. double *prev;
  365. double *overlap;
  366. } ChannelParams;
  367. typedef struct AudioFWTDNContext {
  368. const AVClass *class;
  369. double sigma;
  370. double percent;
  371. double softness;
  372. uint64_t sn;
  373. int64_t eof_pts;
  374. int wavelet_type;
  375. int channels;
  376. int nb_samples;
  377. int levels;
  378. int wavelet_length;
  379. int need_profile;
  380. int got_profile;
  381. int adaptive;
  382. int delay;
  383. int drop_samples;
  384. int padd_samples;
  385. int overlap_length;
  386. int prev_length;
  387. ChannelParams *cp;
  388. const double *lp, *hp;
  389. const double *ilp, *ihp;
  390. AVFrame *stddev, *absmean, *filter;
  391. AVFrame *new_stddev, *new_absmean;
  392. int (*filter_channel)(AVFilterContext *ctx, void *arg, int ch, int nb_jobs);
  393. } AudioFWTDNContext;
  394. #define OFFSET(x) offsetof(AudioFWTDNContext, x)
  395. #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  396. #define AFR AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  397. static const AVOption afwtdn_options[] = {
  398. { "sigma", "set noise sigma", OFFSET(sigma), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, AFR },
  399. { "levels", "set number of wavelet levels", OFFSET(levels), AV_OPT_TYPE_INT, {.i64=10}, 1, MAX_LEVELS-1, AF },
  400. { "wavet", "set wavelet type", OFFSET(wavelet_type), AV_OPT_TYPE_INT, {.i64=SYM10}, 0, NB_WAVELET_TYPES - 1, AF, "wavet" },
  401. { "sym2", "sym2", 0, AV_OPT_TYPE_CONST, {.i64=SYM2}, 0, 0, AF, "wavet" },
  402. { "sym4", "sym4", 0, AV_OPT_TYPE_CONST, {.i64=SYM4}, 0, 0, AF, "wavet" },
  403. { "rbior68", "rbior68", 0, AV_OPT_TYPE_CONST, {.i64=RBIOR68}, 0, 0, AF, "wavet" },
  404. { "deb10", "deb10", 0, AV_OPT_TYPE_CONST, {.i64=DEB10}, 0, 0, AF, "wavet" },
  405. { "sym10", "sym10", 0, AV_OPT_TYPE_CONST, {.i64=SYM10}, 0, 0, AF, "wavet" },
  406. { "coif5", "coif5", 0, AV_OPT_TYPE_CONST, {.i64=COIF5}, 0, 0, AF, "wavet" },
  407. { "bl3", "bl3", 0, AV_OPT_TYPE_CONST, {.i64=BL3}, 0, 0, AF, "wavet" },
  408. { "percent", "set percent of full denoising", OFFSET(percent),AV_OPT_TYPE_DOUBLE, {.dbl=85}, 0, 100, AFR },
  409. { "profile", "profile noise", OFFSET(need_profile), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AFR },
  410. { "adaptive", "adaptive profiling of noise", OFFSET(adaptive), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AFR },
  411. { "samples", "set frame size in number of samples", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=8192}, 512, 65536, AF },
  412. { "softness", "set thresholding softness", OFFSET(softness), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 10, AFR },
  413. { NULL }
  414. };
  415. AVFILTER_DEFINE_CLASS(afwtdn);
  416. static int query_formats(AVFilterContext *ctx)
  417. {
  418. static const enum AVSampleFormat sample_fmts[] = {
  419. AV_SAMPLE_FMT_DBLP,
  420. AV_SAMPLE_FMT_NONE
  421. };
  422. int ret = ff_set_common_formats_from_list(ctx, sample_fmts);
  423. if (ret < 0)
  424. return ret;
  425. ret = ff_set_common_all_channel_counts(ctx);
  426. if (ret < 0)
  427. return ret;
  428. return ff_set_common_all_samplerates(ctx);
  429. }
  430. #define pow2(x) (1U << (x))
  431. #define mod_pow2(x, power_of_two) ((x) & ((power_of_two) - 1))
  432. static void conv_down(double *in, int in_length, double *low, double *high,
  433. int out_length, const double *lp, const double *hp,
  434. int wavelet_length, int skip,
  435. double *buffer, int buffer_length)
  436. {
  437. double thigh = 0.0, tlow = 0.0;
  438. int buff_idx = 1 + skip;
  439. memcpy(buffer, in, buff_idx * sizeof(*buffer));
  440. memset(buffer + buff_idx, 0, (buffer_length - buff_idx) * sizeof(*buffer));
  441. for (int i = 0; i < out_length - 1; i++) {
  442. double thigh = 0.0, tlow = 0.0;
  443. for (int j = 0; j < wavelet_length; j++) {
  444. const int idx = mod_pow2(-j + buff_idx - 1, buffer_length);
  445. const double btemp = buffer[idx];
  446. thigh += btemp * hp[j];
  447. tlow += btemp * lp[j];
  448. }
  449. high[i] = thigh;
  450. low[i] = tlow;
  451. buffer[buff_idx++] = in[2 * i + 1 + skip];
  452. buffer[buff_idx++] = in[2 * i + 2 + skip];
  453. buff_idx = mod_pow2(buff_idx, buffer_length);
  454. }
  455. for (int i = 0; i < wavelet_length; i++) {
  456. const int idx = mod_pow2(-i + buff_idx - 1, buffer_length);
  457. const double btemp = buffer[idx];
  458. thigh += btemp * hp[i];
  459. tlow += btemp * lp[i];
  460. }
  461. high[out_length - 1] = thigh;
  462. low[out_length - 1] = tlow;
  463. }
  464. static int left_ext(int wavelet_length, int levels, uint64_t sn)
  465. {
  466. if (!sn)
  467. return 0;
  468. return (pow2(levels) - 1) * (wavelet_length - 2) + mod_pow2(sn, pow2(levels));
  469. }
  470. static int nb_coefs(int length, int level, uint64_t sn)
  471. {
  472. const int pow2_level = pow2(level);
  473. return (sn + length) / pow2_level - sn / pow2_level;
  474. }
  475. static int reallocate_inputs(double **out, int *out_length,
  476. int in_length, int levels, int ch, uint64_t sn)
  477. {
  478. const int temp_length = nb_coefs(in_length, levels, sn);
  479. for (int level = 0; level < levels; level++) {
  480. const int temp_length = nb_coefs(in_length, level + 1, sn);
  481. if (temp_length > out_length[level]) {
  482. av_freep(&out[level]);
  483. out_length[level] = 0;
  484. out[level] = av_calloc(temp_length + 1, sizeof(**out));
  485. if (!out[level])
  486. return AVERROR(ENOMEM);
  487. out_length[level] = temp_length + 1;
  488. }
  489. memset(out[level] + temp_length, 0,
  490. (out_length[level] - temp_length) * sizeof(**out));
  491. out_length[level] = temp_length;
  492. }
  493. if (temp_length > out_length[levels]) {
  494. av_freep(&out[levels]);
  495. out_length[levels] = 0;
  496. out[levels] = av_calloc(temp_length + 1, sizeof(**out));
  497. if (!out[levels])
  498. return AVERROR(ENOMEM);
  499. out_length[levels] = temp_length + 1;
  500. }
  501. memset(out[levels] + temp_length, 0,
  502. (out_length[levels] - temp_length) * sizeof(**out));
  503. out_length[levels] = temp_length;
  504. return 0;
  505. }
  506. static int max_left_zeros_inverse(int levels, int level, int wavelet_length)
  507. {
  508. return (pow2(levels - level) - 1) * (wavelet_length - 1);
  509. }
  510. static int reallocate_outputs(AudioFWTDNContext *s,
  511. double **out, int *out_length,
  512. int in_length, int levels, int ch, uint64_t sn)
  513. {
  514. ChannelParams *cp = &s->cp[ch];
  515. int temp_length = 0;
  516. int add = 0;
  517. for (int level = 0; level < levels; level++) {
  518. temp_length = nb_coefs(in_length, level + 1, sn);
  519. if (temp_length > out_length[level]) {
  520. av_freep(&cp->subbands_to_free[level]);
  521. out_length[level] = 0;
  522. add = max_left_zeros_inverse(levels, level + 1, s->wavelet_length);
  523. cp->subbands_to_free[level] = av_calloc(add + temp_length + 1, sizeof(**out));
  524. if (!cp->subbands_to_free[level])
  525. return AVERROR(ENOMEM);
  526. out_length[level] = add + temp_length + 1;
  527. out[level] = cp->subbands_to_free[level] + add;
  528. }
  529. memset(out[level] + temp_length, 0,
  530. FFMAX(out_length[level] - temp_length - add, 0) * sizeof(**out));
  531. out_length[level] = temp_length;
  532. }
  533. temp_length = nb_coefs(in_length, levels, sn);
  534. if (temp_length > out_length[levels]) {
  535. av_freep(&cp->subbands_to_free[levels]);
  536. out_length[levels] = 0;
  537. cp->subbands_to_free[levels] = av_calloc(temp_length + 1, sizeof(**out));
  538. if (!cp->subbands_to_free[levels])
  539. return AVERROR(ENOMEM);
  540. out_length[levels] = temp_length + 1;
  541. out[levels] = cp->subbands_to_free[levels];
  542. }
  543. memset(out[levels] + temp_length, 0,
  544. (out_length[levels] - temp_length) * sizeof(**out));
  545. out_length[levels] = temp_length;
  546. return 0;
  547. }
  548. static int discard_left_ext(int wavelet_length, int levels, int level, uint64_t sn)
  549. {
  550. if (levels == level || sn == 0)
  551. return 0;
  552. return (pow2(levels - level) - 1) * (wavelet_length - 2) + mod_pow2(sn, pow2(levels)) / pow2(level);
  553. }
  554. static int forward(AudioFWTDNContext *s,
  555. const double *in, int in_length,
  556. double **out, int *out_length, int ch, uint64_t sn)
  557. {
  558. ChannelParams *cp = &s->cp[ch];
  559. int levels = s->levels;
  560. int skip = sn ? s->wavelet_length - 1 : 1;
  561. int leftext, ret;
  562. ret = reallocate_inputs(out, out_length, in_length, levels, ch, sn);
  563. if (ret < 0)
  564. return ret;
  565. ret = reallocate_outputs(s, cp->filter_coefs, cp->filter_length,
  566. in_length, levels, ch, sn);
  567. if (ret < 0)
  568. return ret;
  569. leftext = left_ext(s->wavelet_length, levels, sn);
  570. if (cp->temp_in_max_length < in_length + cp->max_left_ext + skip) {
  571. av_freep(&cp->temp_in);
  572. cp->temp_in_max_length = in_length + cp->max_left_ext + skip;
  573. cp->temp_in = av_calloc(cp->temp_in_max_length, sizeof(*cp->temp_in));
  574. if (!cp->temp_in) {
  575. cp->temp_in_max_length = 0;
  576. return AVERROR(ENOMEM);
  577. }
  578. }
  579. memset(cp->temp_in, 0, cp->temp_in_max_length * sizeof(*cp->temp_in));
  580. cp->temp_in_length = in_length + leftext;
  581. if (leftext)
  582. memcpy(cp->temp_in, cp->prev + s->prev_length - leftext, leftext * sizeof(*cp->temp_in));
  583. memcpy(cp->temp_in + leftext, in, in_length * sizeof(*in));
  584. if (levels == 1) {
  585. conv_down(cp->temp_in, cp->temp_in_length, out[1], out[0], out_length[1],
  586. s->lp, s->hp, s->wavelet_length, skip,
  587. cp->buffer, cp->buffer_length);
  588. } else {
  589. int discard = discard_left_ext(s->wavelet_length, levels, 1, sn);
  590. int tempa_length_prev;
  591. if (cp->tempa_len_max < (in_length + cp->max_left_ext + s->wavelet_length - 1) / 2) {
  592. av_freep(&cp->tempa);
  593. av_freep(&cp->tempd);
  594. cp->tempa_len_max = (in_length + cp->max_left_ext + s->wavelet_length - 1) / 2;
  595. cp->tempa = av_calloc(cp->tempa_len_max, sizeof(*cp->tempa));
  596. cp->tempd = av_calloc(cp->tempa_len_max, sizeof(*cp->tempd));
  597. if (!cp->tempa || !cp->tempd) {
  598. cp->tempa_len_max = 0;
  599. return AVERROR(ENOMEM);
  600. }
  601. }
  602. memset(cp->tempa, 0, cp->tempa_len_max * sizeof(*cp->tempa));
  603. memset(cp->tempd, 0, cp->tempa_len_max * sizeof(*cp->tempd));
  604. cp->tempa_length = out_length[0] + discard;
  605. conv_down(cp->temp_in, cp->temp_in_length,
  606. cp->tempa, cp->tempd, cp->tempa_length,
  607. s->lp, s->hp, s->wavelet_length, skip,
  608. cp->buffer, cp->buffer_length);
  609. memcpy(out[0], cp->tempd + discard, out_length[0] * sizeof(**out));
  610. tempa_length_prev = cp->tempa_length;
  611. for (int level = 1; level < levels - 1; level++) {
  612. if (out_length[level] == 0)
  613. return 0;
  614. discard = discard_left_ext(s->wavelet_length, levels, level + 1, sn);
  615. cp->tempa_length = out_length[level] + discard;
  616. conv_down(cp->tempa, tempa_length_prev,
  617. cp->tempa, cp->tempd, cp->tempa_length,
  618. s->lp, s->hp, s->wavelet_length, skip,
  619. cp->buffer, cp->buffer_length);
  620. memcpy(out[level], cp->tempd + discard, out_length[level] * sizeof(**out));
  621. tempa_length_prev = cp->tempa_length;
  622. }
  623. if (out_length[levels] == 0)
  624. return 0;
  625. conv_down(cp->tempa, cp->tempa_length, out[levels], out[levels - 1], out_length[levels],
  626. s->lp, s->hp, s->wavelet_length, skip,
  627. cp->buffer, cp->buffer_length);
  628. }
  629. if (s->prev_length < in_length) {
  630. memcpy(cp->prev, in + in_length - cp->max_left_ext, cp->max_left_ext * sizeof(*cp->prev));
  631. } else {
  632. memmove(cp->prev, cp->prev + in_length, (s->prev_length - in_length) * sizeof(*cp->prev));
  633. memcpy(cp->prev + s->prev_length - in_length, in, in_length * sizeof(*cp->prev));
  634. }
  635. return 0;
  636. }
  637. static void conv_up(double *low, double *high, int in_length, double *out, int out_length,
  638. const double *lp, const double *hp, int filter_length,
  639. double *buffer, double *buffer2, int buffer_length)
  640. {
  641. int shift = 0, buff_idx = 0, in_idx = 0;
  642. memset(buffer, 0, buffer_length * sizeof(*buffer));
  643. memset(buffer2, 0, buffer_length * sizeof(*buffer2));
  644. for (int i = 0; i < out_length; i++) {
  645. double sum = 0.0;
  646. if ((i & 1) == 0) {
  647. if (in_idx < in_length) {
  648. buffer[buff_idx] = low[in_idx];
  649. buffer2[buff_idx] = high[in_idx++];
  650. } else {
  651. buffer[buff_idx] = 0;
  652. buffer2[buff_idx] = 0;
  653. }
  654. buff_idx++;
  655. if (buff_idx >= buffer_length)
  656. buff_idx = 0;
  657. shift = 0;
  658. }
  659. for (int j = 0; j < (filter_length - shift + 1) / 2; j++) {
  660. const int idx = mod_pow2(-j + buff_idx - 1, buffer_length);
  661. sum += buffer[idx] * lp[j * 2 + shift] + buffer2[idx] * hp[j * 2 + shift];
  662. }
  663. out[i] = sum;
  664. shift = 1;
  665. }
  666. }
  667. static int append_left_ext(int wavelet_length, int levels, int level, uint64_t sn)
  668. {
  669. if (levels == level)
  670. return 0;
  671. return (pow2(levels - level) - 1) * (wavelet_length - 2) +
  672. mod_pow2(sn, pow2(levels)) / pow2(level);
  673. }
  674. static int inverse(AudioFWTDNContext *s,
  675. double **in, int *in_length,
  676. double *out, int out_length, int ch, uint64_t sn)
  677. {
  678. ChannelParams *cp = &s->cp[ch];
  679. const int levels = s->levels;
  680. int leftext = left_ext(s->wavelet_length, levels, sn);
  681. int temp_skip = 0;
  682. if (sn == 0)
  683. temp_skip = cp->min_left_ext;
  684. memset(out, 0, out_length * sizeof(*out));
  685. if (cp->temp_in_max_length < out_length + cp->max_left_ext + s->wavelet_length - 1) {
  686. av_freep(&cp->temp_in);
  687. cp->temp_in_max_length = out_length + cp->max_left_ext + s->wavelet_length - 1;
  688. cp->temp_in = av_calloc(cp->temp_in_max_length, sizeof(*cp->temp_in));
  689. if (!cp->temp_in) {
  690. cp->temp_in_max_length = 0;
  691. return AVERROR(ENOMEM);
  692. }
  693. }
  694. memset(cp->temp_in, 0, cp->temp_in_max_length * sizeof(*cp->temp_in));
  695. cp->temp_in_length = out_length + cp->max_left_ext;
  696. if (levels == 1) {
  697. conv_up(in[1], in[0], in_length[1], cp->temp_in, cp->temp_in_length,
  698. s->ilp, s->ihp, s->wavelet_length,
  699. cp->buffer, cp->buffer2, cp->buffer_length);
  700. memcpy(out + cp->max_left_ext - leftext, cp->temp_in + temp_skip,
  701. FFMAX(0, out_length - (cp->max_left_ext - leftext)) * sizeof(*out));
  702. } else {
  703. double *hp1, *hp2;
  704. int add, add2;
  705. if (cp->tempa_len_max < (out_length + cp->max_left_ext + s->wavelet_length - 1) / 2) {
  706. av_freep(&cp->tempa);
  707. cp->tempa_len_max = (out_length + cp->max_left_ext + s->wavelet_length - 1) / 2;
  708. cp->tempa = av_calloc(cp->tempa_len_max, sizeof(*cp->tempa));
  709. if (!cp->tempa) {
  710. cp->tempa_len_max = 0;
  711. return AVERROR(ENOMEM);
  712. }
  713. }
  714. memset(cp->tempa, 0, cp->tempa_len_max * sizeof(*cp->tempa));
  715. hp1 = levels & 1 ? cp->temp_in : cp->tempa;
  716. hp2 = levels & 1 ? cp->tempa : cp->temp_in;
  717. add = append_left_ext(s->wavelet_length, levels, levels - 1, sn);
  718. conv_up(in[levels], in[levels - 1], in_length[levels], hp1, in_length[levels - 2] + add,
  719. s->ilp, s->ihp, s->wavelet_length, cp->buffer, cp->buffer2, cp->buffer_length);
  720. for (int level = levels - 1; level > 1; level--) {
  721. add2 = append_left_ext(s->wavelet_length, levels, level - 1, sn);
  722. add = append_left_ext(s->wavelet_length, levels, level, sn);
  723. conv_up(hp1, in[level - 1] - add, in_length[level - 1] + add,
  724. hp2, in_length[level - 2] + add2,
  725. s->ilp, s->ihp, s->wavelet_length,
  726. cp->buffer, cp->buffer2, cp->buffer_length);
  727. FFSWAP(double *, hp1, hp2);
  728. }
  729. add = append_left_ext(s->wavelet_length, levels, 1, sn);
  730. conv_up(hp1, in[0] - add, in_length[0] + add, cp->temp_in, cp->temp_in_length,
  731. s->ilp, s->ihp, s->wavelet_length,
  732. cp->buffer, cp->buffer2, cp->buffer_length);
  733. }
  734. memset(cp->temp_in, 0, temp_skip * sizeof(*cp->temp_in));
  735. if (s->overlap_length <= out_length) {
  736. memcpy(out + cp->max_left_ext - leftext, cp->temp_in + temp_skip,
  737. FFMAX(0, out_length - (cp->max_left_ext - leftext)) * sizeof(*out));
  738. for (int i = 0;i < FFMIN(s->overlap_length, out_length); i++)
  739. out[i] += cp->overlap[i];
  740. memcpy(cp->overlap, cp->temp_in + out_length - (cp->max_left_ext - leftext),
  741. s->overlap_length * sizeof(*cp->overlap));
  742. } else {
  743. for (int i = 0;i < s->overlap_length - (cp->max_left_ext - leftext); i++)
  744. cp->overlap[i + cp->max_left_ext - leftext] += cp->temp_in[i];
  745. memcpy(out, cp->overlap, out_length * sizeof(*out));
  746. memmove(cp->overlap, cp->overlap + out_length,
  747. (s->overlap_length - out_length) * sizeof(*cp->overlap));
  748. memcpy(cp->overlap + s->overlap_length - out_length, cp->temp_in + leftext,
  749. out_length * sizeof(*cp->overlap));
  750. }
  751. return 0;
  752. }
  753. static int next_pow2(int in)
  754. {
  755. return 1 << (av_log2(in) + 1);
  756. }
  757. static void denoise_level(double *out, const double *in,
  758. const double *filter,
  759. double percent, int length)
  760. {
  761. const double x = percent * 0.01;
  762. const double y = 1.0 - x;
  763. for (int i = 0; i < length; i++)
  764. out[i] = x * filter[i] + in[i] * y;
  765. }
  766. static double sqr(double in)
  767. {
  768. return in * in;
  769. }
  770. static double measure_mean(const double *in, int length)
  771. {
  772. double sum = 0.0;
  773. for (int i = 0; i < length; i++)
  774. sum += in[i];
  775. return sum / length;
  776. }
  777. static double measure_absmean(const double *in, int length)
  778. {
  779. double sum = 0.0;
  780. for (int i = 0; i < length; i++)
  781. sum += fabs(in[i]);
  782. return sum / length;
  783. }
  784. static double measure_stddev(const double *in, int length, double mean)
  785. {
  786. double sum = 0.;
  787. for (int i = 0; i < length; i++) {
  788. sum += sqr(in[i] - mean);
  789. }
  790. return sqrt(sum / length);
  791. }
  792. static void noise_filter(const double stddev, const double *in,
  793. double *out, double absmean, double softness,
  794. double new_stddev, int length)
  795. {
  796. for (int i = 0; i < length; i++) {
  797. if (new_stddev <= stddev)
  798. out[i] = 0.0;
  799. else if (fabs(in[i]) <= absmean)
  800. out[i] = 0.0;
  801. else
  802. out[i] = in[i] - FFSIGN(in[i]) * absmean / exp(3.0 * softness * (fabs(in[i]) - absmean) / absmean);
  803. }
  804. }
  805. typedef struct ThreadData {
  806. AVFrame *in, *out;
  807. } ThreadData;
  808. static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
  809. {
  810. AudioFWTDNContext *s = ctx->priv;
  811. ThreadData *td = arg;
  812. AVFrame *in = td->in;
  813. AVFrame *out = td->out;
  814. ChannelParams *cp = &s->cp[ch];
  815. const double *src = (const double *)(in->extended_data[ch]);
  816. double *dst = (double *)out->extended_data[ch];
  817. double *absmean = (double *)s->absmean->extended_data[ch];
  818. double *new_absmean = (double *)s->new_absmean->extended_data[ch];
  819. double *stddev = (double *)s->stddev->extended_data[ch];
  820. double *new_stddev = (double *)s->new_stddev->extended_data[ch];
  821. double *filter = (double *)s->filter->extended_data[ch];
  822. double is_noise = 0.0;
  823. int ret;
  824. ret = forward(s, src, in->nb_samples, cp->output_coefs, cp->output_length, ch, s->sn);
  825. if (ret < 0)
  826. return ret;
  827. if (!s->got_profile && s->need_profile) {
  828. for (int level = 0; level <= s->levels; level++) {
  829. const int length = cp->output_length[level];
  830. const double scale = sqrt(2.0 * log(length));
  831. stddev[level] = measure_stddev(cp->output_coefs[level], length,
  832. measure_mean(cp->output_coefs[level], length)) * scale;
  833. absmean[level] = measure_absmean(cp->output_coefs[level], length) * scale;
  834. }
  835. } else if (!s->got_profile && !s->need_profile && !s->adaptive) {
  836. for (int level = 0; level <= s->levels; level++) {
  837. const int length = cp->output_length[level];
  838. const double scale = sqrt(2.0 * log(length));
  839. stddev[level] = 0.5 * s->sigma * scale;
  840. absmean[level] = 0.5 * s->sigma * scale;
  841. }
  842. }
  843. for (int level = 0; level <= s->levels; level++) {
  844. const int length = cp->output_length[level];
  845. double vad;
  846. new_stddev[level] = measure_stddev(cp->output_coefs[level], length,
  847. measure_mean(cp->output_coefs[level], length));
  848. new_absmean[level] = measure_absmean(cp->output_coefs[level], length);
  849. if (new_absmean[level] <= FLT_EPSILON)
  850. vad = 1.0;
  851. else
  852. vad = new_stddev[level] / new_absmean[level];
  853. if (level < s->levels)
  854. is_noise += sqr(vad - 1.232);
  855. }
  856. is_noise *= in->sample_rate;
  857. is_noise /= s->nb_samples;
  858. for (int level = 0; level <= s->levels; level++) {
  859. const double percent = ctx->is_disabled ? 0. : s->percent;
  860. const int length = cp->output_length[level];
  861. const double scale = sqrt(2.0 * log(length));
  862. if (is_noise < 0.05 && s->adaptive) {
  863. stddev[level] = new_stddev[level] * scale;
  864. absmean[level] = new_absmean[level] * scale;
  865. }
  866. noise_filter(stddev[level], cp->output_coefs[level], filter, absmean[level],
  867. s->softness, new_stddev[level], length);
  868. denoise_level(cp->filter_coefs[level], cp->output_coefs[level], filter, percent, length);
  869. }
  870. ret = inverse(s, cp->filter_coefs, cp->filter_length, dst, out->nb_samples, ch, s->sn);
  871. if (ret < 0)
  872. return ret;
  873. return 0;
  874. }
  875. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  876. {
  877. AVFilterContext *ctx = inlink->dst;
  878. AudioFWTDNContext *s = ctx->priv;
  879. AVFilterLink *outlink = ctx->outputs[0];
  880. ThreadData td;
  881. AVFrame *out;
  882. int eof = in == NULL;
  883. out = ff_get_audio_buffer(outlink, s->nb_samples);
  884. if (!out) {
  885. av_frame_free(&in);
  886. return AVERROR(ENOMEM);
  887. }
  888. if (in) {
  889. av_frame_copy_props(out, in);
  890. s->eof_pts = in->pts + in->nb_samples;
  891. }
  892. if (eof)
  893. out->pts = s->eof_pts - s->padd_samples;
  894. if (!in || in->nb_samples < s->nb_samples) {
  895. AVFrame *new_in = ff_get_audio_buffer(outlink, s->nb_samples);
  896. if (!new_in) {
  897. av_frame_free(&in);
  898. av_frame_free(&out);
  899. return AVERROR(ENOMEM);
  900. }
  901. if (in)
  902. av_frame_copy_props(new_in, in);
  903. s->padd_samples -= s->nb_samples - (in ? in->nb_samples: 0);
  904. if (in)
  905. av_samples_copy(new_in->extended_data, in->extended_data, 0, 0,
  906. in->nb_samples, in->channels, in->format);
  907. av_frame_free(&in);
  908. in = new_in;
  909. }
  910. td.in = in;
  911. td.out = out;
  912. ff_filter_execute(ctx, s->filter_channel, &td, NULL, inlink->channels);
  913. if (s->need_profile)
  914. s->got_profile = 1;
  915. s->sn += s->nb_samples;
  916. if (s->drop_samples >= in->nb_samples) {
  917. s->drop_samples -= in->nb_samples;
  918. s->delay += in->nb_samples;
  919. av_frame_free(&in);
  920. av_frame_free(&out);
  921. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  922. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  923. return 0;
  924. } else if (s->drop_samples > 0) {
  925. for (int ch = 0; ch < out->channels; ch++) {
  926. memmove(out->extended_data[ch],
  927. out->extended_data[ch] + s->drop_samples * sizeof(double),
  928. (in->nb_samples - s->drop_samples) * sizeof(double));
  929. }
  930. out->nb_samples = in->nb_samples - s->drop_samples;
  931. out->pts = in->pts - av_rescale_q(s->delay, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  932. s->delay += s->drop_samples;
  933. s->drop_samples = 0;
  934. } else {
  935. if (s->padd_samples < 0 && eof) {
  936. out->nb_samples += s->padd_samples;
  937. s->padd_samples = 0;
  938. }
  939. if (!eof)
  940. out->pts = in->pts - av_rescale_q(s->delay, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  941. }
  942. av_frame_free(&in);
  943. return ff_filter_frame(outlink, out);
  944. }
  945. static int max_left_ext(int wavelet_length, int levels)
  946. {
  947. return (pow2(levels) - 1) * (wavelet_length - 1);
  948. }
  949. static int min_left_ext(int wavelet_length, int levels)
  950. {
  951. return (pow2(levels) - 1) * (wavelet_length - 2);
  952. }
  953. static int config_output(AVFilterLink *outlink)
  954. {
  955. AVFilterContext *ctx = outlink->src;
  956. AudioFWTDNContext *s = ctx->priv;
  957. switch (s->wavelet_type) {
  958. case SYM2:
  959. s->wavelet_length = 4;
  960. s->lp = sym2_lp;
  961. s->hp = sym2_hp;
  962. s->ilp = sym2_ilp;
  963. s->ihp = sym2_ihp;
  964. break;
  965. case SYM4:
  966. s->wavelet_length = 8;
  967. s->lp = sym4_lp;
  968. s->hp = sym4_hp;
  969. s->ilp = sym4_ilp;
  970. s->ihp = sym4_ihp;
  971. break;
  972. case RBIOR68:
  973. s->wavelet_length = 18;
  974. s->lp = rbior68_lp;
  975. s->hp = rbior68_hp;
  976. s->ilp = rbior68_ilp;
  977. s->ihp = rbior68_ihp;
  978. break;
  979. case DEB10:
  980. s->wavelet_length = 20;
  981. s->lp = deb10_lp;
  982. s->hp = deb10_hp;
  983. s->ilp = deb10_ilp;
  984. s->ihp = deb10_ihp;
  985. break;
  986. case SYM10:
  987. s->wavelet_length = 20;
  988. s->lp = sym10_lp;
  989. s->hp = sym10_hp;
  990. s->ilp = sym10_ilp;
  991. s->ihp = sym10_ihp;
  992. break;
  993. case COIF5:
  994. s->wavelet_length = 30;
  995. s->lp = coif5_lp;
  996. s->hp = coif5_hp;
  997. s->ilp = coif5_ilp;
  998. s->ihp = coif5_ihp;
  999. break;
  1000. case BL3:
  1001. s->wavelet_length = 42;
  1002. s->lp = bl3_lp;
  1003. s->hp = bl3_hp;
  1004. s->ilp = bl3_ilp;
  1005. s->ihp = bl3_ihp;
  1006. break;
  1007. default:
  1008. av_assert0(0);
  1009. }
  1010. s->levels = FFMIN(s->levels, lrint(log(s->nb_samples / (s->wavelet_length - 1.0)) / M_LN2));
  1011. av_log(ctx, AV_LOG_VERBOSE, "levels: %d\n", s->levels);
  1012. s->filter_channel = filter_channel;
  1013. s->stddev = ff_get_audio_buffer(outlink, MAX_LEVELS);
  1014. s->new_stddev = ff_get_audio_buffer(outlink, MAX_LEVELS);
  1015. s->filter = ff_get_audio_buffer(outlink, s->nb_samples);
  1016. s->absmean = ff_get_audio_buffer(outlink, MAX_LEVELS);
  1017. s->new_absmean = ff_get_audio_buffer(outlink, MAX_LEVELS);
  1018. if (!s->stddev || !s->absmean || !s->filter ||
  1019. !s->new_stddev || !s->new_absmean)
  1020. return AVERROR(ENOMEM);
  1021. s->channels = outlink->channels;
  1022. s->overlap_length = max_left_ext(s->wavelet_length, s->levels);
  1023. s->prev_length = s->overlap_length;
  1024. s->drop_samples = s->overlap_length;
  1025. s->padd_samples = s->overlap_length;
  1026. s->sn = 1;
  1027. s->cp = av_calloc(s->channels, sizeof(*s->cp));
  1028. if (!s->cp)
  1029. return AVERROR(ENOMEM);
  1030. for (int ch = 0; ch < s->channels; ch++) {
  1031. ChannelParams *cp = &s->cp[ch];
  1032. cp->output_coefs = av_calloc(s->levels + 1, sizeof(*cp->output_coefs));
  1033. cp->filter_coefs = av_calloc(s->levels + 1, sizeof(*cp->filter_coefs));
  1034. cp->output_length = av_calloc(s->levels + 1, sizeof(*cp->output_length));
  1035. cp->filter_length = av_calloc(s->levels + 1, sizeof(*cp->filter_length));
  1036. cp->buffer_length = next_pow2(s->wavelet_length);
  1037. cp->buffer = av_calloc(cp->buffer_length, sizeof(*cp->buffer));
  1038. cp->buffer2 = av_calloc(cp->buffer_length, sizeof(*cp->buffer2));
  1039. cp->subbands_to_free = av_calloc(s->levels + 1, sizeof(*cp->subbands_to_free));
  1040. cp->prev = av_calloc(s->prev_length, sizeof(*cp->prev));
  1041. cp->overlap = av_calloc(s->overlap_length, sizeof(*cp->overlap));
  1042. cp->max_left_ext = max_left_ext(s->wavelet_length, s->levels);
  1043. cp->min_left_ext = min_left_ext(s->wavelet_length, s->levels);
  1044. if (!cp->output_coefs || !cp->filter_coefs || !cp->output_length ||
  1045. !cp->filter_length || !cp->subbands_to_free || !cp->prev || !cp->overlap ||
  1046. !cp->buffer || !cp->buffer2)
  1047. return AVERROR(ENOMEM);
  1048. }
  1049. return 0;
  1050. }
  1051. static int activate(AVFilterContext *ctx)
  1052. {
  1053. AVFilterLink *inlink = ctx->inputs[0];
  1054. AVFilterLink *outlink = ctx->outputs[0];
  1055. AudioFWTDNContext *s = ctx->priv;
  1056. AVFrame *in = NULL;
  1057. int ret, status;
  1058. int64_t pts;
  1059. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  1060. ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
  1061. if (ret < 0)
  1062. return ret;
  1063. if (ret > 0)
  1064. return filter_frame(inlink, in);
  1065. if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  1066. if (status == AVERROR_EOF) {
  1067. while (s->padd_samples != 0) {
  1068. ret = filter_frame(inlink, NULL);
  1069. if (ret < 0)
  1070. return ret;
  1071. }
  1072. ff_outlink_set_status(outlink, status, pts);
  1073. return ret;
  1074. }
  1075. }
  1076. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  1077. return FFERROR_NOT_READY;
  1078. }
  1079. static av_cold void uninit(AVFilterContext *ctx)
  1080. {
  1081. AudioFWTDNContext *s = ctx->priv;
  1082. av_frame_free(&s->filter);
  1083. av_frame_free(&s->new_stddev);
  1084. av_frame_free(&s->stddev);
  1085. av_frame_free(&s->new_absmean);
  1086. av_frame_free(&s->absmean);
  1087. for (int ch = 0; s->cp && ch < s->channels; ch++) {
  1088. ChannelParams *cp = &s->cp[ch];
  1089. av_freep(&cp->tempa);
  1090. av_freep(&cp->tempd);
  1091. av_freep(&cp->temp_in);
  1092. av_freep(&cp->buffer);
  1093. av_freep(&cp->buffer2);
  1094. av_freep(&cp->prev);
  1095. av_freep(&cp->overlap);
  1096. av_freep(&cp->output_length);
  1097. av_freep(&cp->filter_length);
  1098. if (cp->output_coefs) {
  1099. for (int level = 0; level <= s->levels; level++)
  1100. av_freep(&cp->output_coefs[level]);
  1101. }
  1102. if (cp->subbands_to_free) {
  1103. for (int level = 0; level <= s->levels; level++)
  1104. av_freep(&cp->subbands_to_free[level]);
  1105. }
  1106. av_freep(&cp->subbands_to_free);
  1107. av_freep(&cp->output_coefs);
  1108. av_freep(&cp->filter_coefs);
  1109. }
  1110. av_freep(&s->cp);
  1111. }
  1112. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  1113. char *res, int res_len, int flags)
  1114. {
  1115. AudioFWTDNContext *s = ctx->priv;
  1116. int ret;
  1117. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  1118. if (ret < 0)
  1119. return ret;
  1120. if (!strcmp(cmd, "profile") && s->need_profile)
  1121. s->got_profile = 0;
  1122. return 0;
  1123. }
  1124. static const AVFilterPad inputs[] = {
  1125. {
  1126. .name = "default",
  1127. .type = AVMEDIA_TYPE_AUDIO,
  1128. },
  1129. };
  1130. static const AVFilterPad outputs[] = {
  1131. {
  1132. .name = "default",
  1133. .type = AVMEDIA_TYPE_AUDIO,
  1134. .config_props = config_output,
  1135. },
  1136. };
  1137. const AVFilter ff_af_afwtdn = {
  1138. .name = "afwtdn",
  1139. .description = NULL_IF_CONFIG_SMALL("Denoise audio stream using Wavelets."),
  1140. .query_formats = query_formats,
  1141. .priv_size = sizeof(AudioFWTDNContext),
  1142. .priv_class = &afwtdn_class,
  1143. .activate = activate,
  1144. .uninit = uninit,
  1145. FILTER_INPUTS(inputs),
  1146. FILTER_OUTPUTS(outputs),
  1147. .process_command = process_command,
  1148. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
  1149. AVFILTER_FLAG_SLICE_THREADS,
  1150. };