123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338 |
- /*
- * Copyright (c) 2020 Paul B Mahol
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- #include <float.h>
- #include "libavutil/avassert.h"
- #include "libavutil/avstring.h"
- #include "libavutil/opt.h"
- #include "avfilter.h"
- #include "audio.h"
- #include "filters.h"
- #include "formats.h"
- enum WaveletTypes {
- SYM2,
- SYM4,
- RBIOR68,
- DEB10,
- SYM10,
- COIF5,
- BL3,
- NB_WAVELET_TYPES,
- };
- /*
- * All wavelets coefficients are taken from: http://wavelets.pybytes.com/
- */
- static const double bl3_lp[42] = {
- 0.000146098, -0.000232304, -0.000285414, 0.000462093, 0.000559952,
- -0.000927187, -0.001103748, 0.00188212, 0.002186714, -0.003882426,
- -0.00435384, 0.008201477, 0.008685294, -0.017982291, -0.017176331,
- 0.042068328, 0.032080869, -0.110036987, -0.050201753, 0.433923147,
- 0.766130398, 0.433923147, -0.050201753, -0.110036987, 0.032080869,
- 0.042068328, -0.017176331, -0.017982291, 0.008685294, 0.008201477,
- -0.00435384, -0.003882426, 0.002186714, 0.00188212, -0.001103748,
- -0.000927187, 0.000559952, 0.000462093, -0.000285414, -0.000232304,
- 0.000146098, 0.0,
- };
- static const double bl3_hp[42] = {
- 0.0, 0.000146098, 0.000232304, -0.000285414, -0.000462093, 0.000559952,
- 0.000927187, -0.001103748, -0.00188212, 0.002186714, 0.003882426,
- -0.00435384, -0.008201477, 0.008685294, 0.017982291, -0.017176331,
- -0.042068328, 0.032080869, 0.110036987, -0.050201753, -0.433923147,
- 0.766130398, -0.433923147, -0.050201753, 0.110036987, 0.032080869,
- -0.042068328, -0.017176331, 0.017982291, 0.008685294, -0.008201477,
- -0.00435384, 0.003882426, 0.002186714, -0.00188212, -0.001103748,
- 0.000927187, 0.000559952, -0.000462093, -0.000285414, 0.000232304,
- 0.000146098,
- };
- static const double bl3_ilp[42] = {
- 0.0, 0.000146098, -0.000232304, -0.000285414, 0.000462093, 0.000559952,
- -0.000927187, -0.001103748, 0.00188212, 0.002186714, -0.003882426,
- -0.00435384, 0.008201477, 0.008685294, -0.017982291, -0.017176331,
- 0.042068328, 0.032080869, -0.110036987, -0.050201753, 0.433923147,
- 0.766130398, 0.433923147, -0.050201753, -0.110036987, 0.032080869,
- 0.042068328, -0.017176331, -0.017982291, 0.008685294, 0.008201477,
- -0.00435384, -0.003882426, 0.002186714, 0.00188212, -0.001103748,
- -0.000927187, 0.000559952, 0.000462093, -0.000285414, -0.000232304,
- 0.000146098,
- };
- static const double bl3_ihp[42] = {
- 0.000146098, 0.000232304, -0.000285414, -0.000462093, 0.000559952,
- 0.000927187, -0.001103748, -0.00188212, 0.002186714, 0.003882426,
- -0.00435384, -0.008201477, 0.008685294, 0.017982291, -0.017176331,
- -0.042068328, 0.032080869, 0.110036987, -0.050201753, -0.433923147,
- 0.766130398, -0.433923147, -0.050201753, 0.110036987, 0.032080869,
- -0.042068328, -0.017176331, 0.017982291, 0.008685294, -0.008201477,
- -0.00435384, 0.003882426, 0.002186714, -0.00188212, -0.001103748,
- 0.000927187, 0.000559952, -0.000462093, -0.000285414, 0.000232304,
- 0.000146098,
- };
- static const double sym10_lp[20] = {
- 0.0007701598091144901, 9.563267072289475e-05,
- -0.008641299277022422, -0.0014653825813050513,
- 0.0459272392310922, 0.011609893903711381,
- -0.15949427888491757, -0.07088053578324385,
- 0.47169066693843925, 0.7695100370211071,
- 0.38382676106708546, -0.03553674047381755,
- -0.0319900568824278, 0.04999497207737669,
- 0.005764912033581909, -0.02035493981231129,
- -0.0008043589320165449, 0.004593173585311828,
- 5.7036083618494284e-05, -0.0004593294210046588,
- };
- static const double sym10_hp[20] = {
- 0.0004593294210046588, 5.7036083618494284e-05,
- -0.004593173585311828, -0.0008043589320165449,
- 0.02035493981231129, 0.005764912033581909,
- -0.04999497207737669, -0.0319900568824278,
- 0.03553674047381755, 0.38382676106708546,
- -0.7695100370211071, 0.47169066693843925,
- 0.07088053578324385, -0.15949427888491757,
- -0.011609893903711381, 0.0459272392310922,
- 0.0014653825813050513, -0.008641299277022422,
- -9.563267072289475e-05, 0.0007701598091144901,
- };
- static const double sym10_ilp[20] = {
- -0.0004593294210046588, 5.7036083618494284e-05,
- 0.004593173585311828, -0.0008043589320165449,
- -0.02035493981231129, 0.005764912033581909,
- 0.04999497207737669, -0.0319900568824278,
- -0.03553674047381755, 0.38382676106708546,
- 0.7695100370211071, 0.47169066693843925,
- -0.07088053578324385, -0.15949427888491757,
- 0.011609893903711381, 0.0459272392310922,
- -0.0014653825813050513, -0.008641299277022422,
- 9.563267072289475e-05, 0.0007701598091144901,
- };
- static const double sym10_ihp[20] = {
- 0.0007701598091144901, -9.563267072289475e-05,
- -0.008641299277022422, 0.0014653825813050513,
- 0.0459272392310922, -0.011609893903711381,
- -0.15949427888491757, 0.07088053578324385,
- 0.47169066693843925, -0.7695100370211071,
- 0.38382676106708546, 0.03553674047381755,
- -0.0319900568824278, -0.04999497207737669,
- 0.005764912033581909, 0.02035493981231129,
- -0.0008043589320165449, -0.004593173585311828,
- 5.7036083618494284e-05, 0.0004593294210046588,
- };
- static const double rbior68_lp[18] = {
- 0.0, 0.0, 0.0, 0.0,
- 0.014426282505624435, 0.014467504896790148,
- -0.07872200106262882, -0.04036797903033992,
- 0.41784910915027457, 0.7589077294536541,
- 0.41784910915027457, -0.04036797903033992,
- -0.07872200106262882, 0.014467504896790148,
- 0.014426282505624435, 0.0, 0.0, 0.0,
- };
- static const double rbior68_hp[18] = {
- -0.0019088317364812906, -0.0019142861290887667,
- 0.016990639867602342, 0.01193456527972926,
- -0.04973290349094079, -0.07726317316720414,
- 0.09405920349573646, 0.4207962846098268,
- -0.8259229974584023, 0.4207962846098268,
- 0.09405920349573646, -0.07726317316720414,
- -0.04973290349094079, 0.01193456527972926,
- 0.016990639867602342, -0.0019142861290887667,
- -0.0019088317364812906, 0.0,
- };
- static const double rbior68_ilp[18] = {
- 0.0019088317364812906, -0.0019142861290887667,
- -0.016990639867602342, 0.01193456527972926,
- 0.04973290349094079, -0.07726317316720414,
- -0.09405920349573646, 0.4207962846098268,
- 0.8259229974584023, 0.4207962846098268,
- -0.09405920349573646, -0.07726317316720414,
- 0.04973290349094079, 0.01193456527972926,
- -0.016990639867602342, -0.0019142861290887667,
- 0.0019088317364812906, 0.0,
- };
- static const double rbior68_ihp[18] = {
- 0.0, 0.0, 0.0, 0.0,
- 0.014426282505624435, -0.014467504896790148,
- -0.07872200106262882, 0.04036797903033992,
- 0.41784910915027457, -0.7589077294536541,
- 0.41784910915027457, 0.04036797903033992,
- -0.07872200106262882, -0.014467504896790148,
- 0.014426282505624435, 0.0, 0.0, 0.0,
- };
- static const double coif5_lp[30] = {
- -9.517657273819165e-08, -1.6744288576823017e-07,
- 2.0637618513646814e-06, 3.7346551751414047e-06,
- -2.1315026809955787e-05, -4.134043227251251e-05,
- 0.00014054114970203437, 0.00030225958181306315,
- -0.0006381313430451114, -0.0016628637020130838,
- 0.0024333732126576722, 0.006764185448053083,
- -0.009164231162481846, -0.01976177894257264,
- 0.03268357426711183, 0.0412892087501817,
- -0.10557420870333893, -0.06203596396290357,
- 0.4379916261718371, 0.7742896036529562,
- 0.4215662066908515, -0.05204316317624377,
- -0.09192001055969624, 0.02816802897093635,
- 0.023408156785839195, -0.010131117519849788,
- -0.004159358781386048, 0.0021782363581090178,
- 0.00035858968789573785, -0.00021208083980379827,
- };
- static const double coif5_hp[30] = {
- 0.00021208083980379827, 0.00035858968789573785,
- -0.0021782363581090178, -0.004159358781386048,
- 0.010131117519849788, 0.023408156785839195,
- -0.02816802897093635, -0.09192001055969624,
- 0.05204316317624377, 0.4215662066908515,
- -0.7742896036529562, 0.4379916261718371,
- 0.06203596396290357, -0.10557420870333893,
- -0.0412892087501817, 0.03268357426711183,
- 0.01976177894257264, -0.009164231162481846,
- -0.006764185448053083, 0.0024333732126576722,
- 0.0016628637020130838, -0.0006381313430451114,
- -0.00030225958181306315, 0.00014054114970203437,
- 4.134043227251251e-05, -2.1315026809955787e-05,
- -3.7346551751414047e-06, 2.0637618513646814e-06,
- 1.6744288576823017e-07, -9.517657273819165e-08,
- };
- static const double coif5_ilp[30] = {
- -0.00021208083980379827, 0.00035858968789573785,
- 0.0021782363581090178, -0.004159358781386048,
- -0.010131117519849788, 0.023408156785839195,
- 0.02816802897093635, -0.09192001055969624,
- -0.05204316317624377, 0.4215662066908515,
- 0.7742896036529562, 0.4379916261718371,
- -0.06203596396290357, -0.10557420870333893,
- 0.0412892087501817, 0.03268357426711183,
- -0.01976177894257264, -0.009164231162481846,
- 0.006764185448053083, 0.0024333732126576722,
- -0.0016628637020130838, -0.0006381313430451114,
- 0.00030225958181306315, 0.00014054114970203437,
- -4.134043227251251e-05, -2.1315026809955787e-05,
- 3.7346551751414047e-06, 2.0637618513646814e-06,
- -1.6744288576823017e-07, -9.517657273819165e-08,
- };
- static const double coif5_ihp[30] = {
- -9.517657273819165e-08, 1.6744288576823017e-07,
- 2.0637618513646814e-06, -3.7346551751414047e-06,
- -2.1315026809955787e-05, 4.134043227251251e-05,
- 0.00014054114970203437, -0.00030225958181306315,
- -0.0006381313430451114, 0.0016628637020130838,
- 0.0024333732126576722, -0.006764185448053083,
- -0.009164231162481846, 0.01976177894257264,
- 0.03268357426711183, -0.0412892087501817,
- -0.10557420870333893, 0.06203596396290357,
- 0.4379916261718371, -0.7742896036529562,
- 0.4215662066908515, 0.05204316317624377,
- -0.09192001055969624, -0.02816802897093635,
- 0.023408156785839195, 0.010131117519849788,
- -0.004159358781386048, -0.0021782363581090178,
- 0.00035858968789573785, 0.00021208083980379827,
- };
- static const double deb10_lp[20] = {
- -1.326420300235487e-05, 9.358867000108985e-05,
- -0.0001164668549943862, -0.0006858566950046825,
- 0.00199240529499085, 0.0013953517469940798,
- -0.010733175482979604, 0.0036065535669883944,
- 0.03321267405893324, -0.02945753682194567,
- -0.07139414716586077, 0.09305736460380659,
- 0.12736934033574265, -0.19594627437659665,
- -0.24984642432648865, 0.2811723436604265,
- 0.6884590394525921, 0.5272011889309198,
- 0.18817680007762133, 0.026670057900950818,
- };
- static const double deb10_hp[20] = {
- -0.026670057900950818, 0.18817680007762133,
- -0.5272011889309198, 0.6884590394525921,
- -0.2811723436604265, -0.24984642432648865,
- 0.19594627437659665, 0.12736934033574265,
- -0.09305736460380659, -0.07139414716586077,
- 0.02945753682194567, 0.03321267405893324,
- -0.0036065535669883944, -0.010733175482979604,
- -0.0013953517469940798, 0.00199240529499085,
- 0.0006858566950046825, -0.0001164668549943862,
- -9.358867000108985e-05, -1.326420300235487e-05,
- };
- static const double deb10_ilp[20] = {
- 0.026670057900950818, 0.18817680007762133,
- 0.5272011889309198, 0.6884590394525921,
- 0.2811723436604265, -0.24984642432648865,
- -0.19594627437659665, 0.12736934033574265,
- 0.09305736460380659, -0.07139414716586077,
- -0.02945753682194567, 0.03321267405893324,
- 0.0036065535669883944, -0.010733175482979604,
- 0.0013953517469940798, 0.00199240529499085,
- -0.0006858566950046825, -0.0001164668549943862,
- 9.358867000108985e-05, -1.326420300235487e-05,
- };
- static const double deb10_ihp[20] = {
- -1.326420300235487e-05, -9.358867000108985e-05,
- -0.0001164668549943862, 0.0006858566950046825,
- 0.00199240529499085, -0.0013953517469940798,
- -0.010733175482979604, -0.0036065535669883944,
- 0.03321267405893324, 0.02945753682194567,
- -0.07139414716586077, -0.09305736460380659,
- 0.12736934033574265, 0.19594627437659665,
- -0.24984642432648865, -0.2811723436604265,
- 0.6884590394525921, -0.5272011889309198,
- 0.18817680007762133, -0.026670057900950818,
- };
- static const double sym4_lp[8] = {
- -0.07576571478927333,
- -0.02963552764599851,
- 0.49761866763201545,
- 0.8037387518059161,
- 0.29785779560527736,
- -0.09921954357684722,
- -0.012603967262037833,
- 0.0322231006040427,
- };
- static const double sym4_hp[8] = {
- -0.0322231006040427,
- -0.012603967262037833,
- 0.09921954357684722,
- 0.29785779560527736,
- -0.8037387518059161,
- 0.49761866763201545,
- 0.02963552764599851,
- -0.07576571478927333,
- };
- static const double sym4_ilp[8] = {
- 0.0322231006040427,
- -0.012603967262037833,
- -0.09921954357684722,
- 0.29785779560527736,
- 0.8037387518059161,
- 0.49761866763201545,
- -0.02963552764599851,
- -0.07576571478927333,
- };
- static const double sym4_ihp[8] = {
- -0.07576571478927333,
- 0.02963552764599851,
- 0.49761866763201545,
- -0.8037387518059161,
- 0.29785779560527736,
- 0.09921954357684722,
- -0.012603967262037833,
- -0.0322231006040427,
- };
- static const double sym2_lp[4] = {
- -0.12940952255092145, 0.22414386804185735,
- 0.836516303737469, 0.48296291314469025,
- };
- static const double sym2_hp[4] = {
- -0.48296291314469025, 0.836516303737469,
- -0.22414386804185735, -0.12940952255092145,
- };
- static const double sym2_ilp[4] = {
- 0.48296291314469025, 0.836516303737469,
- 0.22414386804185735, -0.12940952255092145,
- };
- static const double sym2_ihp[4] = {
- -0.12940952255092145, -0.22414386804185735,
- 0.836516303737469, -0.48296291314469025,
- };
- #define MAX_LEVELS 13
- typedef struct ChannelParams {
- int *output_length;
- int *filter_length;
- double **output_coefs;
- double **subbands_to_free;
- double **filter_coefs;
- int tempa_length;
- int tempa_len_max;
- int temp_in_length;
- int temp_in_max_length;
- int buffer_length;
- int min_left_ext;
- int max_left_ext;
- double *tempa;
- double *tempd;
- double *temp_in;
- double *buffer;
- double *buffer2;
- double *prev;
- double *overlap;
- } ChannelParams;
- typedef struct AudioFWTDNContext {
- const AVClass *class;
- double sigma;
- double percent;
- double softness;
- uint64_t sn;
- int64_t eof_pts;
- int wavelet_type;
- int channels;
- int nb_samples;
- int levels;
- int wavelet_length;
- int need_profile;
- int got_profile;
- int adaptive;
- int delay;
- int drop_samples;
- int padd_samples;
- int overlap_length;
- int prev_length;
- ChannelParams *cp;
- const double *lp, *hp;
- const double *ilp, *ihp;
- AVFrame *stddev, *absmean, *filter;
- AVFrame *new_stddev, *new_absmean;
- int (*filter_channel)(AVFilterContext *ctx, void *arg, int ch, int nb_jobs);
- } AudioFWTDNContext;
- #define OFFSET(x) offsetof(AudioFWTDNContext, x)
- #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
- #define AFR AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
- static const AVOption afwtdn_options[] = {
- { "sigma", "set noise sigma", OFFSET(sigma), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, AFR },
- { "levels", "set number of wavelet levels", OFFSET(levels), AV_OPT_TYPE_INT, {.i64=10}, 1, MAX_LEVELS-1, AF },
- { "wavet", "set wavelet type", OFFSET(wavelet_type), AV_OPT_TYPE_INT, {.i64=SYM10}, 0, NB_WAVELET_TYPES - 1, AF, "wavet" },
- { "sym2", "sym2", 0, AV_OPT_TYPE_CONST, {.i64=SYM2}, 0, 0, AF, "wavet" },
- { "sym4", "sym4", 0, AV_OPT_TYPE_CONST, {.i64=SYM4}, 0, 0, AF, "wavet" },
- { "rbior68", "rbior68", 0, AV_OPT_TYPE_CONST, {.i64=RBIOR68}, 0, 0, AF, "wavet" },
- { "deb10", "deb10", 0, AV_OPT_TYPE_CONST, {.i64=DEB10}, 0, 0, AF, "wavet" },
- { "sym10", "sym10", 0, AV_OPT_TYPE_CONST, {.i64=SYM10}, 0, 0, AF, "wavet" },
- { "coif5", "coif5", 0, AV_OPT_TYPE_CONST, {.i64=COIF5}, 0, 0, AF, "wavet" },
- { "bl3", "bl3", 0, AV_OPT_TYPE_CONST, {.i64=BL3}, 0, 0, AF, "wavet" },
- { "percent", "set percent of full denoising", OFFSET(percent),AV_OPT_TYPE_DOUBLE, {.dbl=85}, 0, 100, AFR },
- { "profile", "profile noise", OFFSET(need_profile), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AFR },
- { "adaptive", "adaptive profiling of noise", OFFSET(adaptive), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AFR },
- { "samples", "set frame size in number of samples", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=8192}, 512, 65536, AF },
- { "softness", "set thresholding softness", OFFSET(softness), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 10, AFR },
- { NULL }
- };
- AVFILTER_DEFINE_CLASS(afwtdn);
- static int query_formats(AVFilterContext *ctx)
- {
- static const enum AVSampleFormat sample_fmts[] = {
- AV_SAMPLE_FMT_DBLP,
- AV_SAMPLE_FMT_NONE
- };
- int ret = ff_set_common_formats_from_list(ctx, sample_fmts);
- if (ret < 0)
- return ret;
- ret = ff_set_common_all_channel_counts(ctx);
- if (ret < 0)
- return ret;
- return ff_set_common_all_samplerates(ctx);
- }
- #define pow2(x) (1U << (x))
- #define mod_pow2(x, power_of_two) ((x) & ((power_of_two) - 1))
- static void conv_down(double *in, int in_length, double *low, double *high,
- int out_length, const double *lp, const double *hp,
- int wavelet_length, int skip,
- double *buffer, int buffer_length)
- {
- double thigh = 0.0, tlow = 0.0;
- int buff_idx = 1 + skip;
- memcpy(buffer, in, buff_idx * sizeof(*buffer));
- memset(buffer + buff_idx, 0, (buffer_length - buff_idx) * sizeof(*buffer));
- for (int i = 0; i < out_length - 1; i++) {
- double thigh = 0.0, tlow = 0.0;
- for (int j = 0; j < wavelet_length; j++) {
- const int idx = mod_pow2(-j + buff_idx - 1, buffer_length);
- const double btemp = buffer[idx];
- thigh += btemp * hp[j];
- tlow += btemp * lp[j];
- }
- high[i] = thigh;
- low[i] = tlow;
- buffer[buff_idx++] = in[2 * i + 1 + skip];
- buffer[buff_idx++] = in[2 * i + 2 + skip];
- buff_idx = mod_pow2(buff_idx, buffer_length);
- }
- for (int i = 0; i < wavelet_length; i++) {
- const int idx = mod_pow2(-i + buff_idx - 1, buffer_length);
- const double btemp = buffer[idx];
- thigh += btemp * hp[i];
- tlow += btemp * lp[i];
- }
- high[out_length - 1] = thigh;
- low[out_length - 1] = tlow;
- }
- static int left_ext(int wavelet_length, int levels, uint64_t sn)
- {
- if (!sn)
- return 0;
- return (pow2(levels) - 1) * (wavelet_length - 2) + mod_pow2(sn, pow2(levels));
- }
- static int nb_coefs(int length, int level, uint64_t sn)
- {
- const int pow2_level = pow2(level);
- return (sn + length) / pow2_level - sn / pow2_level;
- }
- static int reallocate_inputs(double **out, int *out_length,
- int in_length, int levels, int ch, uint64_t sn)
- {
- const int temp_length = nb_coefs(in_length, levels, sn);
- for (int level = 0; level < levels; level++) {
- const int temp_length = nb_coefs(in_length, level + 1, sn);
- if (temp_length > out_length[level]) {
- av_freep(&out[level]);
- out_length[level] = 0;
- out[level] = av_calloc(temp_length + 1, sizeof(**out));
- if (!out[level])
- return AVERROR(ENOMEM);
- out_length[level] = temp_length + 1;
- }
- memset(out[level] + temp_length, 0,
- (out_length[level] - temp_length) * sizeof(**out));
- out_length[level] = temp_length;
- }
- if (temp_length > out_length[levels]) {
- av_freep(&out[levels]);
- out_length[levels] = 0;
- out[levels] = av_calloc(temp_length + 1, sizeof(**out));
- if (!out[levels])
- return AVERROR(ENOMEM);
- out_length[levels] = temp_length + 1;
- }
- memset(out[levels] + temp_length, 0,
- (out_length[levels] - temp_length) * sizeof(**out));
- out_length[levels] = temp_length;
- return 0;
- }
- static int max_left_zeros_inverse(int levels, int level, int wavelet_length)
- {
- return (pow2(levels - level) - 1) * (wavelet_length - 1);
- }
- static int reallocate_outputs(AudioFWTDNContext *s,
- double **out, int *out_length,
- int in_length, int levels, int ch, uint64_t sn)
- {
- ChannelParams *cp = &s->cp[ch];
- int temp_length = 0;
- int add = 0;
- for (int level = 0; level < levels; level++) {
- temp_length = nb_coefs(in_length, level + 1, sn);
- if (temp_length > out_length[level]) {
- av_freep(&cp->subbands_to_free[level]);
- out_length[level] = 0;
- add = max_left_zeros_inverse(levels, level + 1, s->wavelet_length);
- cp->subbands_to_free[level] = av_calloc(add + temp_length + 1, sizeof(**out));
- if (!cp->subbands_to_free[level])
- return AVERROR(ENOMEM);
- out_length[level] = add + temp_length + 1;
- out[level] = cp->subbands_to_free[level] + add;
- }
- memset(out[level] + temp_length, 0,
- FFMAX(out_length[level] - temp_length - add, 0) * sizeof(**out));
- out_length[level] = temp_length;
- }
- temp_length = nb_coefs(in_length, levels, sn);
- if (temp_length > out_length[levels]) {
- av_freep(&cp->subbands_to_free[levels]);
- out_length[levels] = 0;
- cp->subbands_to_free[levels] = av_calloc(temp_length + 1, sizeof(**out));
- if (!cp->subbands_to_free[levels])
- return AVERROR(ENOMEM);
- out_length[levels] = temp_length + 1;
- out[levels] = cp->subbands_to_free[levels];
- }
- memset(out[levels] + temp_length, 0,
- (out_length[levels] - temp_length) * sizeof(**out));
- out_length[levels] = temp_length;
- return 0;
- }
- static int discard_left_ext(int wavelet_length, int levels, int level, uint64_t sn)
- {
- if (levels == level || sn == 0)
- return 0;
- return (pow2(levels - level) - 1) * (wavelet_length - 2) + mod_pow2(sn, pow2(levels)) / pow2(level);
- }
- static int forward(AudioFWTDNContext *s,
- const double *in, int in_length,
- double **out, int *out_length, int ch, uint64_t sn)
- {
- ChannelParams *cp = &s->cp[ch];
- int levels = s->levels;
- int skip = sn ? s->wavelet_length - 1 : 1;
- int leftext, ret;
- ret = reallocate_inputs(out, out_length, in_length, levels, ch, sn);
- if (ret < 0)
- return ret;
- ret = reallocate_outputs(s, cp->filter_coefs, cp->filter_length,
- in_length, levels, ch, sn);
- if (ret < 0)
- return ret;
- leftext = left_ext(s->wavelet_length, levels, sn);
- if (cp->temp_in_max_length < in_length + cp->max_left_ext + skip) {
- av_freep(&cp->temp_in);
- cp->temp_in_max_length = in_length + cp->max_left_ext + skip;
- cp->temp_in = av_calloc(cp->temp_in_max_length, sizeof(*cp->temp_in));
- if (!cp->temp_in) {
- cp->temp_in_max_length = 0;
- return AVERROR(ENOMEM);
- }
- }
- memset(cp->temp_in, 0, cp->temp_in_max_length * sizeof(*cp->temp_in));
- cp->temp_in_length = in_length + leftext;
- if (leftext)
- memcpy(cp->temp_in, cp->prev + s->prev_length - leftext, leftext * sizeof(*cp->temp_in));
- memcpy(cp->temp_in + leftext, in, in_length * sizeof(*in));
- if (levels == 1) {
- conv_down(cp->temp_in, cp->temp_in_length, out[1], out[0], out_length[1],
- s->lp, s->hp, s->wavelet_length, skip,
- cp->buffer, cp->buffer_length);
- } else {
- int discard = discard_left_ext(s->wavelet_length, levels, 1, sn);
- int tempa_length_prev;
- if (cp->tempa_len_max < (in_length + cp->max_left_ext + s->wavelet_length - 1) / 2) {
- av_freep(&cp->tempa);
- av_freep(&cp->tempd);
- cp->tempa_len_max = (in_length + cp->max_left_ext + s->wavelet_length - 1) / 2;
- cp->tempa = av_calloc(cp->tempa_len_max, sizeof(*cp->tempa));
- cp->tempd = av_calloc(cp->tempa_len_max, sizeof(*cp->tempd));
- if (!cp->tempa || !cp->tempd) {
- cp->tempa_len_max = 0;
- return AVERROR(ENOMEM);
- }
- }
- memset(cp->tempa, 0, cp->tempa_len_max * sizeof(*cp->tempa));
- memset(cp->tempd, 0, cp->tempa_len_max * sizeof(*cp->tempd));
- cp->tempa_length = out_length[0] + discard;
- conv_down(cp->temp_in, cp->temp_in_length,
- cp->tempa, cp->tempd, cp->tempa_length,
- s->lp, s->hp, s->wavelet_length, skip,
- cp->buffer, cp->buffer_length);
- memcpy(out[0], cp->tempd + discard, out_length[0] * sizeof(**out));
- tempa_length_prev = cp->tempa_length;
- for (int level = 1; level < levels - 1; level++) {
- if (out_length[level] == 0)
- return 0;
- discard = discard_left_ext(s->wavelet_length, levels, level + 1, sn);
- cp->tempa_length = out_length[level] + discard;
- conv_down(cp->tempa, tempa_length_prev,
- cp->tempa, cp->tempd, cp->tempa_length,
- s->lp, s->hp, s->wavelet_length, skip,
- cp->buffer, cp->buffer_length);
- memcpy(out[level], cp->tempd + discard, out_length[level] * sizeof(**out));
- tempa_length_prev = cp->tempa_length;
- }
- if (out_length[levels] == 0)
- return 0;
- conv_down(cp->tempa, cp->tempa_length, out[levels], out[levels - 1], out_length[levels],
- s->lp, s->hp, s->wavelet_length, skip,
- cp->buffer, cp->buffer_length);
- }
- if (s->prev_length < in_length) {
- memcpy(cp->prev, in + in_length - cp->max_left_ext, cp->max_left_ext * sizeof(*cp->prev));
- } else {
- memmove(cp->prev, cp->prev + in_length, (s->prev_length - in_length) * sizeof(*cp->prev));
- memcpy(cp->prev + s->prev_length - in_length, in, in_length * sizeof(*cp->prev));
- }
- return 0;
- }
- static void conv_up(double *low, double *high, int in_length, double *out, int out_length,
- const double *lp, const double *hp, int filter_length,
- double *buffer, double *buffer2, int buffer_length)
- {
- int shift = 0, buff_idx = 0, in_idx = 0;
- memset(buffer, 0, buffer_length * sizeof(*buffer));
- memset(buffer2, 0, buffer_length * sizeof(*buffer2));
- for (int i = 0; i < out_length; i++) {
- double sum = 0.0;
- if ((i & 1) == 0) {
- if (in_idx < in_length) {
- buffer[buff_idx] = low[in_idx];
- buffer2[buff_idx] = high[in_idx++];
- } else {
- buffer[buff_idx] = 0;
- buffer2[buff_idx] = 0;
- }
- buff_idx++;
- if (buff_idx >= buffer_length)
- buff_idx = 0;
- shift = 0;
- }
- for (int j = 0; j < (filter_length - shift + 1) / 2; j++) {
- const int idx = mod_pow2(-j + buff_idx - 1, buffer_length);
- sum += buffer[idx] * lp[j * 2 + shift] + buffer2[idx] * hp[j * 2 + shift];
- }
- out[i] = sum;
- shift = 1;
- }
- }
- static int append_left_ext(int wavelet_length, int levels, int level, uint64_t sn)
- {
- if (levels == level)
- return 0;
- return (pow2(levels - level) - 1) * (wavelet_length - 2) +
- mod_pow2(sn, pow2(levels)) / pow2(level);
- }
- static int inverse(AudioFWTDNContext *s,
- double **in, int *in_length,
- double *out, int out_length, int ch, uint64_t sn)
- {
- ChannelParams *cp = &s->cp[ch];
- const int levels = s->levels;
- int leftext = left_ext(s->wavelet_length, levels, sn);
- int temp_skip = 0;
- if (sn == 0)
- temp_skip = cp->min_left_ext;
- memset(out, 0, out_length * sizeof(*out));
- if (cp->temp_in_max_length < out_length + cp->max_left_ext + s->wavelet_length - 1) {
- av_freep(&cp->temp_in);
- cp->temp_in_max_length = out_length + cp->max_left_ext + s->wavelet_length - 1;
- cp->temp_in = av_calloc(cp->temp_in_max_length, sizeof(*cp->temp_in));
- if (!cp->temp_in) {
- cp->temp_in_max_length = 0;
- return AVERROR(ENOMEM);
- }
- }
- memset(cp->temp_in, 0, cp->temp_in_max_length * sizeof(*cp->temp_in));
- cp->temp_in_length = out_length + cp->max_left_ext;
- if (levels == 1) {
- conv_up(in[1], in[0], in_length[1], cp->temp_in, cp->temp_in_length,
- s->ilp, s->ihp, s->wavelet_length,
- cp->buffer, cp->buffer2, cp->buffer_length);
- memcpy(out + cp->max_left_ext - leftext, cp->temp_in + temp_skip,
- FFMAX(0, out_length - (cp->max_left_ext - leftext)) * sizeof(*out));
- } else {
- double *hp1, *hp2;
- int add, add2;
- if (cp->tempa_len_max < (out_length + cp->max_left_ext + s->wavelet_length - 1) / 2) {
- av_freep(&cp->tempa);
- cp->tempa_len_max = (out_length + cp->max_left_ext + s->wavelet_length - 1) / 2;
- cp->tempa = av_calloc(cp->tempa_len_max, sizeof(*cp->tempa));
- if (!cp->tempa) {
- cp->tempa_len_max = 0;
- return AVERROR(ENOMEM);
- }
- }
- memset(cp->tempa, 0, cp->tempa_len_max * sizeof(*cp->tempa));
- hp1 = levels & 1 ? cp->temp_in : cp->tempa;
- hp2 = levels & 1 ? cp->tempa : cp->temp_in;
- add = append_left_ext(s->wavelet_length, levels, levels - 1, sn);
- conv_up(in[levels], in[levels - 1], in_length[levels], hp1, in_length[levels - 2] + add,
- s->ilp, s->ihp, s->wavelet_length, cp->buffer, cp->buffer2, cp->buffer_length);
- for (int level = levels - 1; level > 1; level--) {
- add2 = append_left_ext(s->wavelet_length, levels, level - 1, sn);
- add = append_left_ext(s->wavelet_length, levels, level, sn);
- conv_up(hp1, in[level - 1] - add, in_length[level - 1] + add,
- hp2, in_length[level - 2] + add2,
- s->ilp, s->ihp, s->wavelet_length,
- cp->buffer, cp->buffer2, cp->buffer_length);
- FFSWAP(double *, hp1, hp2);
- }
- add = append_left_ext(s->wavelet_length, levels, 1, sn);
- conv_up(hp1, in[0] - add, in_length[0] + add, cp->temp_in, cp->temp_in_length,
- s->ilp, s->ihp, s->wavelet_length,
- cp->buffer, cp->buffer2, cp->buffer_length);
- }
- memset(cp->temp_in, 0, temp_skip * sizeof(*cp->temp_in));
- if (s->overlap_length <= out_length) {
- memcpy(out + cp->max_left_ext - leftext, cp->temp_in + temp_skip,
- FFMAX(0, out_length - (cp->max_left_ext - leftext)) * sizeof(*out));
- for (int i = 0;i < FFMIN(s->overlap_length, out_length); i++)
- out[i] += cp->overlap[i];
- memcpy(cp->overlap, cp->temp_in + out_length - (cp->max_left_ext - leftext),
- s->overlap_length * sizeof(*cp->overlap));
- } else {
- for (int i = 0;i < s->overlap_length - (cp->max_left_ext - leftext); i++)
- cp->overlap[i + cp->max_left_ext - leftext] += cp->temp_in[i];
- memcpy(out, cp->overlap, out_length * sizeof(*out));
- memmove(cp->overlap, cp->overlap + out_length,
- (s->overlap_length - out_length) * sizeof(*cp->overlap));
- memcpy(cp->overlap + s->overlap_length - out_length, cp->temp_in + leftext,
- out_length * sizeof(*cp->overlap));
- }
- return 0;
- }
- static int next_pow2(int in)
- {
- return 1 << (av_log2(in) + 1);
- }
- static void denoise_level(double *out, const double *in,
- const double *filter,
- double percent, int length)
- {
- const double x = percent * 0.01;
- const double y = 1.0 - x;
- for (int i = 0; i < length; i++)
- out[i] = x * filter[i] + in[i] * y;
- }
- static double sqr(double in)
- {
- return in * in;
- }
- static double measure_mean(const double *in, int length)
- {
- double sum = 0.0;
- for (int i = 0; i < length; i++)
- sum += in[i];
- return sum / length;
- }
- static double measure_absmean(const double *in, int length)
- {
- double sum = 0.0;
- for (int i = 0; i < length; i++)
- sum += fabs(in[i]);
- return sum / length;
- }
- static double measure_stddev(const double *in, int length, double mean)
- {
- double sum = 0.;
- for (int i = 0; i < length; i++) {
- sum += sqr(in[i] - mean);
- }
- return sqrt(sum / length);
- }
- static void noise_filter(const double stddev, const double *in,
- double *out, double absmean, double softness,
- double new_stddev, int length)
- {
- for (int i = 0; i < length; i++) {
- if (new_stddev <= stddev)
- out[i] = 0.0;
- else if (fabs(in[i]) <= absmean)
- out[i] = 0.0;
- else
- out[i] = in[i] - FFSIGN(in[i]) * absmean / exp(3.0 * softness * (fabs(in[i]) - absmean) / absmean);
- }
- }
- typedef struct ThreadData {
- AVFrame *in, *out;
- } ThreadData;
- static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
- {
- AudioFWTDNContext *s = ctx->priv;
- ThreadData *td = arg;
- AVFrame *in = td->in;
- AVFrame *out = td->out;
- ChannelParams *cp = &s->cp[ch];
- const double *src = (const double *)(in->extended_data[ch]);
- double *dst = (double *)out->extended_data[ch];
- double *absmean = (double *)s->absmean->extended_data[ch];
- double *new_absmean = (double *)s->new_absmean->extended_data[ch];
- double *stddev = (double *)s->stddev->extended_data[ch];
- double *new_stddev = (double *)s->new_stddev->extended_data[ch];
- double *filter = (double *)s->filter->extended_data[ch];
- double is_noise = 0.0;
- int ret;
- ret = forward(s, src, in->nb_samples, cp->output_coefs, cp->output_length, ch, s->sn);
- if (ret < 0)
- return ret;
- if (!s->got_profile && s->need_profile) {
- for (int level = 0; level <= s->levels; level++) {
- const int length = cp->output_length[level];
- const double scale = sqrt(2.0 * log(length));
- stddev[level] = measure_stddev(cp->output_coefs[level], length,
- measure_mean(cp->output_coefs[level], length)) * scale;
- absmean[level] = measure_absmean(cp->output_coefs[level], length) * scale;
- }
- } else if (!s->got_profile && !s->need_profile && !s->adaptive) {
- for (int level = 0; level <= s->levels; level++) {
- const int length = cp->output_length[level];
- const double scale = sqrt(2.0 * log(length));
- stddev[level] = 0.5 * s->sigma * scale;
- absmean[level] = 0.5 * s->sigma * scale;
- }
- }
- for (int level = 0; level <= s->levels; level++) {
- const int length = cp->output_length[level];
- double vad;
- new_stddev[level] = measure_stddev(cp->output_coefs[level], length,
- measure_mean(cp->output_coefs[level], length));
- new_absmean[level] = measure_absmean(cp->output_coefs[level], length);
- if (new_absmean[level] <= FLT_EPSILON)
- vad = 1.0;
- else
- vad = new_stddev[level] / new_absmean[level];
- if (level < s->levels)
- is_noise += sqr(vad - 1.232);
- }
- is_noise *= in->sample_rate;
- is_noise /= s->nb_samples;
- for (int level = 0; level <= s->levels; level++) {
- const double percent = ctx->is_disabled ? 0. : s->percent;
- const int length = cp->output_length[level];
- const double scale = sqrt(2.0 * log(length));
- if (is_noise < 0.05 && s->adaptive) {
- stddev[level] = new_stddev[level] * scale;
- absmean[level] = new_absmean[level] * scale;
- }
- noise_filter(stddev[level], cp->output_coefs[level], filter, absmean[level],
- s->softness, new_stddev[level], length);
- denoise_level(cp->filter_coefs[level], cp->output_coefs[level], filter, percent, length);
- }
- ret = inverse(s, cp->filter_coefs, cp->filter_length, dst, out->nb_samples, ch, s->sn);
- if (ret < 0)
- return ret;
- return 0;
- }
- static int filter_frame(AVFilterLink *inlink, AVFrame *in)
- {
- AVFilterContext *ctx = inlink->dst;
- AudioFWTDNContext *s = ctx->priv;
- AVFilterLink *outlink = ctx->outputs[0];
- ThreadData td;
- AVFrame *out;
- int eof = in == NULL;
- out = ff_get_audio_buffer(outlink, s->nb_samples);
- if (!out) {
- av_frame_free(&in);
- return AVERROR(ENOMEM);
- }
- if (in) {
- av_frame_copy_props(out, in);
- s->eof_pts = in->pts + in->nb_samples;
- }
- if (eof)
- out->pts = s->eof_pts - s->padd_samples;
- if (!in || in->nb_samples < s->nb_samples) {
- AVFrame *new_in = ff_get_audio_buffer(outlink, s->nb_samples);
- if (!new_in) {
- av_frame_free(&in);
- av_frame_free(&out);
- return AVERROR(ENOMEM);
- }
- if (in)
- av_frame_copy_props(new_in, in);
- s->padd_samples -= s->nb_samples - (in ? in->nb_samples: 0);
- if (in)
- av_samples_copy(new_in->extended_data, in->extended_data, 0, 0,
- in->nb_samples, in->channels, in->format);
- av_frame_free(&in);
- in = new_in;
- }
- td.in = in;
- td.out = out;
- ff_filter_execute(ctx, s->filter_channel, &td, NULL, inlink->channels);
- if (s->need_profile)
- s->got_profile = 1;
- s->sn += s->nb_samples;
- if (s->drop_samples >= in->nb_samples) {
- s->drop_samples -= in->nb_samples;
- s->delay += in->nb_samples;
- av_frame_free(&in);
- av_frame_free(&out);
- FF_FILTER_FORWARD_STATUS(inlink, outlink);
- FF_FILTER_FORWARD_WANTED(outlink, inlink);
- return 0;
- } else if (s->drop_samples > 0) {
- for (int ch = 0; ch < out->channels; ch++) {
- memmove(out->extended_data[ch],
- out->extended_data[ch] + s->drop_samples * sizeof(double),
- (in->nb_samples - s->drop_samples) * sizeof(double));
- }
- out->nb_samples = in->nb_samples - s->drop_samples;
- out->pts = in->pts - av_rescale_q(s->delay, (AVRational){1, outlink->sample_rate}, outlink->time_base);
- s->delay += s->drop_samples;
- s->drop_samples = 0;
- } else {
- if (s->padd_samples < 0 && eof) {
- out->nb_samples += s->padd_samples;
- s->padd_samples = 0;
- }
- if (!eof)
- out->pts = in->pts - av_rescale_q(s->delay, (AVRational){1, outlink->sample_rate}, outlink->time_base);
- }
- av_frame_free(&in);
- return ff_filter_frame(outlink, out);
- }
- static int max_left_ext(int wavelet_length, int levels)
- {
- return (pow2(levels) - 1) * (wavelet_length - 1);
- }
- static int min_left_ext(int wavelet_length, int levels)
- {
- return (pow2(levels) - 1) * (wavelet_length - 2);
- }
- static int config_output(AVFilterLink *outlink)
- {
- AVFilterContext *ctx = outlink->src;
- AudioFWTDNContext *s = ctx->priv;
- switch (s->wavelet_type) {
- case SYM2:
- s->wavelet_length = 4;
- s->lp = sym2_lp;
- s->hp = sym2_hp;
- s->ilp = sym2_ilp;
- s->ihp = sym2_ihp;
- break;
- case SYM4:
- s->wavelet_length = 8;
- s->lp = sym4_lp;
- s->hp = sym4_hp;
- s->ilp = sym4_ilp;
- s->ihp = sym4_ihp;
- break;
- case RBIOR68:
- s->wavelet_length = 18;
- s->lp = rbior68_lp;
- s->hp = rbior68_hp;
- s->ilp = rbior68_ilp;
- s->ihp = rbior68_ihp;
- break;
- case DEB10:
- s->wavelet_length = 20;
- s->lp = deb10_lp;
- s->hp = deb10_hp;
- s->ilp = deb10_ilp;
- s->ihp = deb10_ihp;
- break;
- case SYM10:
- s->wavelet_length = 20;
- s->lp = sym10_lp;
- s->hp = sym10_hp;
- s->ilp = sym10_ilp;
- s->ihp = sym10_ihp;
- break;
- case COIF5:
- s->wavelet_length = 30;
- s->lp = coif5_lp;
- s->hp = coif5_hp;
- s->ilp = coif5_ilp;
- s->ihp = coif5_ihp;
- break;
- case BL3:
- s->wavelet_length = 42;
- s->lp = bl3_lp;
- s->hp = bl3_hp;
- s->ilp = bl3_ilp;
- s->ihp = bl3_ihp;
- break;
- default:
- av_assert0(0);
- }
- s->levels = FFMIN(s->levels, lrint(log(s->nb_samples / (s->wavelet_length - 1.0)) / M_LN2));
- av_log(ctx, AV_LOG_VERBOSE, "levels: %d\n", s->levels);
- s->filter_channel = filter_channel;
- s->stddev = ff_get_audio_buffer(outlink, MAX_LEVELS);
- s->new_stddev = ff_get_audio_buffer(outlink, MAX_LEVELS);
- s->filter = ff_get_audio_buffer(outlink, s->nb_samples);
- s->absmean = ff_get_audio_buffer(outlink, MAX_LEVELS);
- s->new_absmean = ff_get_audio_buffer(outlink, MAX_LEVELS);
- if (!s->stddev || !s->absmean || !s->filter ||
- !s->new_stddev || !s->new_absmean)
- return AVERROR(ENOMEM);
- s->channels = outlink->channels;
- s->overlap_length = max_left_ext(s->wavelet_length, s->levels);
- s->prev_length = s->overlap_length;
- s->drop_samples = s->overlap_length;
- s->padd_samples = s->overlap_length;
- s->sn = 1;
- s->cp = av_calloc(s->channels, sizeof(*s->cp));
- if (!s->cp)
- return AVERROR(ENOMEM);
- for (int ch = 0; ch < s->channels; ch++) {
- ChannelParams *cp = &s->cp[ch];
- cp->output_coefs = av_calloc(s->levels + 1, sizeof(*cp->output_coefs));
- cp->filter_coefs = av_calloc(s->levels + 1, sizeof(*cp->filter_coefs));
- cp->output_length = av_calloc(s->levels + 1, sizeof(*cp->output_length));
- cp->filter_length = av_calloc(s->levels + 1, sizeof(*cp->filter_length));
- cp->buffer_length = next_pow2(s->wavelet_length);
- cp->buffer = av_calloc(cp->buffer_length, sizeof(*cp->buffer));
- cp->buffer2 = av_calloc(cp->buffer_length, sizeof(*cp->buffer2));
- cp->subbands_to_free = av_calloc(s->levels + 1, sizeof(*cp->subbands_to_free));
- cp->prev = av_calloc(s->prev_length, sizeof(*cp->prev));
- cp->overlap = av_calloc(s->overlap_length, sizeof(*cp->overlap));
- cp->max_left_ext = max_left_ext(s->wavelet_length, s->levels);
- cp->min_left_ext = min_left_ext(s->wavelet_length, s->levels);
- if (!cp->output_coefs || !cp->filter_coefs || !cp->output_length ||
- !cp->filter_length || !cp->subbands_to_free || !cp->prev || !cp->overlap ||
- !cp->buffer || !cp->buffer2)
- return AVERROR(ENOMEM);
- }
- return 0;
- }
- static int activate(AVFilterContext *ctx)
- {
- AVFilterLink *inlink = ctx->inputs[0];
- AVFilterLink *outlink = ctx->outputs[0];
- AudioFWTDNContext *s = ctx->priv;
- AVFrame *in = NULL;
- int ret, status;
- int64_t pts;
- FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
- ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
- if (ret < 0)
- return ret;
- if (ret > 0)
- return filter_frame(inlink, in);
- if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
- if (status == AVERROR_EOF) {
- while (s->padd_samples != 0) {
- ret = filter_frame(inlink, NULL);
- if (ret < 0)
- return ret;
- }
- ff_outlink_set_status(outlink, status, pts);
- return ret;
- }
- }
- FF_FILTER_FORWARD_WANTED(outlink, inlink);
- return FFERROR_NOT_READY;
- }
- static av_cold void uninit(AVFilterContext *ctx)
- {
- AudioFWTDNContext *s = ctx->priv;
- av_frame_free(&s->filter);
- av_frame_free(&s->new_stddev);
- av_frame_free(&s->stddev);
- av_frame_free(&s->new_absmean);
- av_frame_free(&s->absmean);
- for (int ch = 0; s->cp && ch < s->channels; ch++) {
- ChannelParams *cp = &s->cp[ch];
- av_freep(&cp->tempa);
- av_freep(&cp->tempd);
- av_freep(&cp->temp_in);
- av_freep(&cp->buffer);
- av_freep(&cp->buffer2);
- av_freep(&cp->prev);
- av_freep(&cp->overlap);
- av_freep(&cp->output_length);
- av_freep(&cp->filter_length);
- if (cp->output_coefs) {
- for (int level = 0; level <= s->levels; level++)
- av_freep(&cp->output_coefs[level]);
- }
- if (cp->subbands_to_free) {
- for (int level = 0; level <= s->levels; level++)
- av_freep(&cp->subbands_to_free[level]);
- }
- av_freep(&cp->subbands_to_free);
- av_freep(&cp->output_coefs);
- av_freep(&cp->filter_coefs);
- }
- av_freep(&s->cp);
- }
- static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
- char *res, int res_len, int flags)
- {
- AudioFWTDNContext *s = ctx->priv;
- int ret;
- ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
- if (ret < 0)
- return ret;
- if (!strcmp(cmd, "profile") && s->need_profile)
- s->got_profile = 0;
- return 0;
- }
- static const AVFilterPad inputs[] = {
- {
- .name = "default",
- .type = AVMEDIA_TYPE_AUDIO,
- },
- };
- static const AVFilterPad outputs[] = {
- {
- .name = "default",
- .type = AVMEDIA_TYPE_AUDIO,
- .config_props = config_output,
- },
- };
- const AVFilter ff_af_afwtdn = {
- .name = "afwtdn",
- .description = NULL_IF_CONFIG_SMALL("Denoise audio stream using Wavelets."),
- .query_formats = query_formats,
- .priv_size = sizeof(AudioFWTDNContext),
- .priv_class = &afwtdn_class,
- .activate = activate,
- .uninit = uninit,
- FILTER_INPUTS(inputs),
- FILTER_OUTPUTS(outputs),
- .process_command = process_command,
- .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
- AVFILTER_FLAG_SLICE_THREADS,
- };
|