vf_dctdnoiz.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * Copyright (c) 2013-2014 Clément Bœsch
  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. /**
  21. * A simple, relatively efficient and slow DCT image denoiser.
  22. *
  23. * @see http://www.ipol.im/pub/art/2011/ys-dct/
  24. *
  25. * The DCT factorization used is based on "Fast and numerically stable
  26. * algorithms for discrete cosine transforms" from Gerlind Plonkaa & Manfred
  27. * Tasche (DOI: 10.1016/j.laa.2004.07.015).
  28. */
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/eval.h"
  31. #include "libavutil/opt.h"
  32. #include "internal.h"
  33. static const char *const var_names[] = { "c", NULL };
  34. enum { VAR_C, VAR_VARS_NB };
  35. #define MAX_THREADS 8
  36. typedef struct DCTdnoizContext {
  37. const AVClass *class;
  38. /* coefficient factor expression */
  39. char *expr_str;
  40. AVExpr *expr[MAX_THREADS];
  41. double var_values[MAX_THREADS][VAR_VARS_NB];
  42. int nb_threads;
  43. int pr_width, pr_height; // width and height to process
  44. float sigma; // used when no expression are st
  45. float th; // threshold (3*sigma)
  46. float *cbuf[2][3]; // two planar rgb color buffers
  47. float *slices[MAX_THREADS]; // slices buffers (1 slice buffer per thread)
  48. float *weights; // dct coeff are cumulated with overlapping; these values are used for averaging
  49. int p_linesize; // line sizes for color and weights
  50. int overlap; // number of block overlapping pixels
  51. int step; // block step increment (blocksize - overlap)
  52. int n; // 1<<n is the block size
  53. int bsize; // block size, 1<<n
  54. void (*filter_freq_func)(struct DCTdnoizContext *s,
  55. const float *src, int src_linesize,
  56. float *dst, int dst_linesize,
  57. int thread_id);
  58. void (*color_decorrelation)(float **dst, int dst_linesize,
  59. const uint8_t **src, int src_linesize,
  60. int w, int h);
  61. void (*color_correlation)(uint8_t **dst, int dst_linesize,
  62. float **src, int src_linesize,
  63. int w, int h);
  64. } DCTdnoizContext;
  65. #define MIN_NBITS 3 /* blocksize = 1<<3 = 8 */
  66. #define MAX_NBITS 4 /* blocksize = 1<<4 = 16 */
  67. #define DEFAULT_NBITS 3
  68. #define OFFSET(x) offsetof(DCTdnoizContext, x)
  69. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  70. static const AVOption dctdnoiz_options[] = {
  71. { "sigma", "set noise sigma constant", OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 999, .flags = FLAGS },
  72. { "s", "set noise sigma constant", OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 999, .flags = FLAGS },
  73. { "overlap", "set number of block overlapping pixels", OFFSET(overlap), AV_OPT_TYPE_INT, {.i64=-1}, -1, (1<<MAX_NBITS)-1, .flags = FLAGS },
  74. { "expr", "set coefficient factor expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  75. { "e", "set coefficient factor expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  76. { "n", "set the block size, expressed in bits", OFFSET(n), AV_OPT_TYPE_INT, {.i64=DEFAULT_NBITS}, MIN_NBITS, MAX_NBITS, .flags = FLAGS },
  77. { NULL }
  78. };
  79. AVFILTER_DEFINE_CLASS(dctdnoiz);
  80. static void av_always_inline fdct8_1d(float *dst, const float *src,
  81. int dst_stridea, int dst_strideb,
  82. int src_stridea, int src_strideb)
  83. {
  84. int i;
  85. for (i = 0; i < 8; i++) {
  86. const float x00 = src[0*src_stridea] + src[7*src_stridea];
  87. const float x01 = src[1*src_stridea] + src[6*src_stridea];
  88. const float x02 = src[2*src_stridea] + src[5*src_stridea];
  89. const float x03 = src[3*src_stridea] + src[4*src_stridea];
  90. const float x04 = src[0*src_stridea] - src[7*src_stridea];
  91. const float x05 = src[1*src_stridea] - src[6*src_stridea];
  92. const float x06 = src[2*src_stridea] - src[5*src_stridea];
  93. const float x07 = src[3*src_stridea] - src[4*src_stridea];
  94. const float x08 = x00 + x03;
  95. const float x09 = x01 + x02;
  96. const float x0a = x00 - x03;
  97. const float x0b = x01 - x02;
  98. const float x0c = 1.38703984532215f*x04 + 0.275899379282943f*x07;
  99. const float x0d = 1.17587560241936f*x05 + 0.785694958387102f*x06;
  100. const float x0e = -0.785694958387102f*x05 + 1.17587560241936f*x06;
  101. const float x0f = 0.275899379282943f*x04 - 1.38703984532215f*x07;
  102. const float x10 = 0.353553390593274f * (x0c - x0d);
  103. const float x11 = 0.353553390593274f * (x0e - x0f);
  104. dst[0*dst_stridea] = 0.353553390593274f * (x08 + x09);
  105. dst[1*dst_stridea] = 0.353553390593274f * (x0c + x0d);
  106. dst[2*dst_stridea] = 0.461939766255643f*x0a + 0.191341716182545f*x0b;
  107. dst[3*dst_stridea] = 0.707106781186547f * (x10 - x11);
  108. dst[4*dst_stridea] = 0.353553390593274f * (x08 - x09);
  109. dst[5*dst_stridea] = 0.707106781186547f * (x10 + x11);
  110. dst[6*dst_stridea] = 0.191341716182545f*x0a - 0.461939766255643f*x0b;
  111. dst[7*dst_stridea] = 0.353553390593274f * (x0e + x0f);
  112. dst += dst_strideb;
  113. src += src_strideb;
  114. }
  115. }
  116. static void av_always_inline idct8_1d(float *dst, const float *src,
  117. int dst_stridea, int dst_strideb,
  118. int src_stridea, int src_strideb,
  119. int add)
  120. {
  121. int i;
  122. for (i = 0; i < 8; i++) {
  123. const float x00 = 1.4142135623731f *src[0*src_stridea];
  124. const float x01 = 1.38703984532215f *src[1*src_stridea] + 0.275899379282943f*src[7*src_stridea];
  125. const float x02 = 1.30656296487638f *src[2*src_stridea] + 0.541196100146197f*src[6*src_stridea];
  126. const float x03 = 1.17587560241936f *src[3*src_stridea] + 0.785694958387102f*src[5*src_stridea];
  127. const float x04 = 1.4142135623731f *src[4*src_stridea];
  128. const float x05 = -0.785694958387102f*src[3*src_stridea] + 1.17587560241936f*src[5*src_stridea];
  129. const float x06 = 0.541196100146197f*src[2*src_stridea] - 1.30656296487638f*src[6*src_stridea];
  130. const float x07 = -0.275899379282943f*src[1*src_stridea] + 1.38703984532215f*src[7*src_stridea];
  131. const float x09 = x00 + x04;
  132. const float x0a = x01 + x03;
  133. const float x0b = 1.4142135623731f*x02;
  134. const float x0c = x00 - x04;
  135. const float x0d = x01 - x03;
  136. const float x0e = 0.353553390593274f * (x09 - x0b);
  137. const float x0f = 0.353553390593274f * (x0c + x0d);
  138. const float x10 = 0.353553390593274f * (x0c - x0d);
  139. const float x11 = 1.4142135623731f*x06;
  140. const float x12 = x05 + x07;
  141. const float x13 = x05 - x07;
  142. const float x14 = 0.353553390593274f * (x11 + x12);
  143. const float x15 = 0.353553390593274f * (x11 - x12);
  144. const float x16 = 0.5f*x13;
  145. dst[0*dst_stridea] = (add ? dst[ 0*dst_stridea] : 0) + 0.25f * (x09 + x0b) + 0.353553390593274f*x0a;
  146. dst[1*dst_stridea] = (add ? dst[ 1*dst_stridea] : 0) + 0.707106781186547f * (x0f + x15);
  147. dst[2*dst_stridea] = (add ? dst[ 2*dst_stridea] : 0) + 0.707106781186547f * (x0f - x15);
  148. dst[3*dst_stridea] = (add ? dst[ 3*dst_stridea] : 0) + 0.707106781186547f * (x0e + x16);
  149. dst[4*dst_stridea] = (add ? dst[ 4*dst_stridea] : 0) + 0.707106781186547f * (x0e - x16);
  150. dst[5*dst_stridea] = (add ? dst[ 5*dst_stridea] : 0) + 0.707106781186547f * (x10 - x14);
  151. dst[6*dst_stridea] = (add ? dst[ 6*dst_stridea] : 0) + 0.707106781186547f * (x10 + x14);
  152. dst[7*dst_stridea] = (add ? dst[ 7*dst_stridea] : 0) + 0.25f * (x09 + x0b) - 0.353553390593274f*x0a;
  153. dst += dst_strideb;
  154. src += src_strideb;
  155. }
  156. }
  157. static void av_always_inline fdct16_1d(float *dst, const float *src,
  158. int dst_stridea, int dst_strideb,
  159. int src_stridea, int src_strideb)
  160. {
  161. int i;
  162. for (i = 0; i < 16; i++) {
  163. const float x00 = src[ 0*src_stridea] + src[15*src_stridea];
  164. const float x01 = src[ 1*src_stridea] + src[14*src_stridea];
  165. const float x02 = src[ 2*src_stridea] + src[13*src_stridea];
  166. const float x03 = src[ 3*src_stridea] + src[12*src_stridea];
  167. const float x04 = src[ 4*src_stridea] + src[11*src_stridea];
  168. const float x05 = src[ 5*src_stridea] + src[10*src_stridea];
  169. const float x06 = src[ 6*src_stridea] + src[ 9*src_stridea];
  170. const float x07 = src[ 7*src_stridea] + src[ 8*src_stridea];
  171. const float x08 = src[ 0*src_stridea] - src[15*src_stridea];
  172. const float x09 = src[ 1*src_stridea] - src[14*src_stridea];
  173. const float x0a = src[ 2*src_stridea] - src[13*src_stridea];
  174. const float x0b = src[ 3*src_stridea] - src[12*src_stridea];
  175. const float x0c = src[ 4*src_stridea] - src[11*src_stridea];
  176. const float x0d = src[ 5*src_stridea] - src[10*src_stridea];
  177. const float x0e = src[ 6*src_stridea] - src[ 9*src_stridea];
  178. const float x0f = src[ 7*src_stridea] - src[ 8*src_stridea];
  179. const float x10 = x00 + x07;
  180. const float x11 = x01 + x06;
  181. const float x12 = x02 + x05;
  182. const float x13 = x03 + x04;
  183. const float x14 = x00 - x07;
  184. const float x15 = x01 - x06;
  185. const float x16 = x02 - x05;
  186. const float x17 = x03 - x04;
  187. const float x18 = x10 + x13;
  188. const float x19 = x11 + x12;
  189. const float x1a = x10 - x13;
  190. const float x1b = x11 - x12;
  191. const float x1c = 1.38703984532215f*x14 + 0.275899379282943f*x17;
  192. const float x1d = 1.17587560241936f*x15 + 0.785694958387102f*x16;
  193. const float x1e = -0.785694958387102f*x15 + 1.17587560241936f *x16;
  194. const float x1f = 0.275899379282943f*x14 - 1.38703984532215f *x17;
  195. const float x20 = 0.25f * (x1c - x1d);
  196. const float x21 = 0.25f * (x1e - x1f);
  197. const float x22 = 1.40740373752638f *x08 + 0.138617169199091f*x0f;
  198. const float x23 = 1.35331800117435f *x09 + 0.410524527522357f*x0e;
  199. const float x24 = 1.24722501298667f *x0a + 0.666655658477747f*x0d;
  200. const float x25 = 1.09320186700176f *x0b + 0.897167586342636f*x0c;
  201. const float x26 = -0.897167586342636f*x0b + 1.09320186700176f *x0c;
  202. const float x27 = 0.666655658477747f*x0a - 1.24722501298667f *x0d;
  203. const float x28 = -0.410524527522357f*x09 + 1.35331800117435f *x0e;
  204. const float x29 = 0.138617169199091f*x08 - 1.40740373752638f *x0f;
  205. const float x2a = x22 + x25;
  206. const float x2b = x23 + x24;
  207. const float x2c = x22 - x25;
  208. const float x2d = x23 - x24;
  209. const float x2e = 0.25f * (x2a - x2b);
  210. const float x2f = 0.326640741219094f*x2c + 0.135299025036549f*x2d;
  211. const float x30 = 0.135299025036549f*x2c - 0.326640741219094f*x2d;
  212. const float x31 = x26 + x29;
  213. const float x32 = x27 + x28;
  214. const float x33 = x26 - x29;
  215. const float x34 = x27 - x28;
  216. const float x35 = 0.25f * (x31 - x32);
  217. const float x36 = 0.326640741219094f*x33 + 0.135299025036549f*x34;
  218. const float x37 = 0.135299025036549f*x33 - 0.326640741219094f*x34;
  219. dst[ 0*dst_stridea] = 0.25f * (x18 + x19);
  220. dst[ 1*dst_stridea] = 0.25f * (x2a + x2b);
  221. dst[ 2*dst_stridea] = 0.25f * (x1c + x1d);
  222. dst[ 3*dst_stridea] = 0.707106781186547f * (x2f - x37);
  223. dst[ 4*dst_stridea] = 0.326640741219094f*x1a + 0.135299025036549f*x1b;
  224. dst[ 5*dst_stridea] = 0.707106781186547f * (x2f + x37);
  225. dst[ 6*dst_stridea] = 0.707106781186547f * (x20 - x21);
  226. dst[ 7*dst_stridea] = 0.707106781186547f * (x2e + x35);
  227. dst[ 8*dst_stridea] = 0.25f * (x18 - x19);
  228. dst[ 9*dst_stridea] = 0.707106781186547f * (x2e - x35);
  229. dst[10*dst_stridea] = 0.707106781186547f * (x20 + x21);
  230. dst[11*dst_stridea] = 0.707106781186547f * (x30 - x36);
  231. dst[12*dst_stridea] = 0.135299025036549f*x1a - 0.326640741219094f*x1b;
  232. dst[13*dst_stridea] = 0.707106781186547f * (x30 + x36);
  233. dst[14*dst_stridea] = 0.25f * (x1e + x1f);
  234. dst[15*dst_stridea] = 0.25f * (x31 + x32);
  235. dst += dst_strideb;
  236. src += src_strideb;
  237. }
  238. }
  239. static void av_always_inline idct16_1d(float *dst, const float *src,
  240. int dst_stridea, int dst_strideb,
  241. int src_stridea, int src_strideb,
  242. int add)
  243. {
  244. int i;
  245. for (i = 0; i < 16; i++) {
  246. const float x00 = 1.4142135623731f *src[ 0*src_stridea];
  247. const float x01 = 1.40740373752638f *src[ 1*src_stridea] + 0.138617169199091f*src[15*src_stridea];
  248. const float x02 = 1.38703984532215f *src[ 2*src_stridea] + 0.275899379282943f*src[14*src_stridea];
  249. const float x03 = 1.35331800117435f *src[ 3*src_stridea] + 0.410524527522357f*src[13*src_stridea];
  250. const float x04 = 1.30656296487638f *src[ 4*src_stridea] + 0.541196100146197f*src[12*src_stridea];
  251. const float x05 = 1.24722501298667f *src[ 5*src_stridea] + 0.666655658477747f*src[11*src_stridea];
  252. const float x06 = 1.17587560241936f *src[ 6*src_stridea] + 0.785694958387102f*src[10*src_stridea];
  253. const float x07 = 1.09320186700176f *src[ 7*src_stridea] + 0.897167586342636f*src[ 9*src_stridea];
  254. const float x08 = 1.4142135623731f *src[ 8*src_stridea];
  255. const float x09 = -0.897167586342636f*src[ 7*src_stridea] + 1.09320186700176f*src[ 9*src_stridea];
  256. const float x0a = 0.785694958387102f*src[ 6*src_stridea] - 1.17587560241936f*src[10*src_stridea];
  257. const float x0b = -0.666655658477747f*src[ 5*src_stridea] + 1.24722501298667f*src[11*src_stridea];
  258. const float x0c = 0.541196100146197f*src[ 4*src_stridea] - 1.30656296487638f*src[12*src_stridea];
  259. const float x0d = -0.410524527522357f*src[ 3*src_stridea] + 1.35331800117435f*src[13*src_stridea];
  260. const float x0e = 0.275899379282943f*src[ 2*src_stridea] - 1.38703984532215f*src[14*src_stridea];
  261. const float x0f = -0.138617169199091f*src[ 1*src_stridea] + 1.40740373752638f*src[15*src_stridea];
  262. const float x12 = x00 + x08;
  263. const float x13 = x01 + x07;
  264. const float x14 = x02 + x06;
  265. const float x15 = x03 + x05;
  266. const float x16 = 1.4142135623731f*x04;
  267. const float x17 = x00 - x08;
  268. const float x18 = x01 - x07;
  269. const float x19 = x02 - x06;
  270. const float x1a = x03 - x05;
  271. const float x1d = x12 + x16;
  272. const float x1e = x13 + x15;
  273. const float x1f = 1.4142135623731f*x14;
  274. const float x20 = x12 - x16;
  275. const float x21 = x13 - x15;
  276. const float x22 = 0.25f * (x1d - x1f);
  277. const float x23 = 0.25f * (x20 + x21);
  278. const float x24 = 0.25f * (x20 - x21);
  279. const float x25 = 1.4142135623731f*x17;
  280. const float x26 = 1.30656296487638f*x18 + 0.541196100146197f*x1a;
  281. const float x27 = 1.4142135623731f*x19;
  282. const float x28 = -0.541196100146197f*x18 + 1.30656296487638f*x1a;
  283. const float x29 = 0.176776695296637f * (x25 + x27) + 0.25f*x26;
  284. const float x2a = 0.25f * (x25 - x27);
  285. const float x2b = 0.176776695296637f * (x25 + x27) - 0.25f*x26;
  286. const float x2c = 0.353553390593274f*x28;
  287. const float x1b = 0.707106781186547f * (x2a - x2c);
  288. const float x1c = 0.707106781186547f * (x2a + x2c);
  289. const float x2d = 1.4142135623731f*x0c;
  290. const float x2e = x0b + x0d;
  291. const float x2f = x0a + x0e;
  292. const float x30 = x09 + x0f;
  293. const float x31 = x09 - x0f;
  294. const float x32 = x0a - x0e;
  295. const float x33 = x0b - x0d;
  296. const float x37 = 1.4142135623731f*x2d;
  297. const float x38 = 1.30656296487638f*x2e + 0.541196100146197f*x30;
  298. const float x39 = 1.4142135623731f*x2f;
  299. const float x3a = -0.541196100146197f*x2e + 1.30656296487638f*x30;
  300. const float x3b = 0.176776695296637f * (x37 + x39) + 0.25f*x38;
  301. const float x3c = 0.25f * (x37 - x39);
  302. const float x3d = 0.176776695296637f * (x37 + x39) - 0.25f*x38;
  303. const float x3e = 0.353553390593274f*x3a;
  304. const float x34 = 0.707106781186547f * (x3c - x3e);
  305. const float x35 = 0.707106781186547f * (x3c + x3e);
  306. const float x3f = 1.4142135623731f*x32;
  307. const float x40 = x31 + x33;
  308. const float x41 = x31 - x33;
  309. const float x42 = 0.25f * (x3f + x40);
  310. const float x43 = 0.25f * (x3f - x40);
  311. const float x44 = 0.353553390593274f*x41;
  312. dst[ 0*dst_stridea] = (add ? dst[ 0*dst_stridea] : 0) + 0.176776695296637f * (x1d + x1f) + 0.25f*x1e;
  313. dst[ 1*dst_stridea] = (add ? dst[ 1*dst_stridea] : 0) + 0.707106781186547f * (x29 + x3d);
  314. dst[ 2*dst_stridea] = (add ? dst[ 2*dst_stridea] : 0) + 0.707106781186547f * (x29 - x3d);
  315. dst[ 3*dst_stridea] = (add ? dst[ 3*dst_stridea] : 0) + 0.707106781186547f * (x23 - x43);
  316. dst[ 4*dst_stridea] = (add ? dst[ 4*dst_stridea] : 0) + 0.707106781186547f * (x23 + x43);
  317. dst[ 5*dst_stridea] = (add ? dst[ 5*dst_stridea] : 0) + 0.707106781186547f * (x1b - x35);
  318. dst[ 6*dst_stridea] = (add ? dst[ 6*dst_stridea] : 0) + 0.707106781186547f * (x1b + x35);
  319. dst[ 7*dst_stridea] = (add ? dst[ 7*dst_stridea] : 0) + 0.707106781186547f * (x22 + x44);
  320. dst[ 8*dst_stridea] = (add ? dst[ 8*dst_stridea] : 0) + 0.707106781186547f * (x22 - x44);
  321. dst[ 9*dst_stridea] = (add ? dst[ 9*dst_stridea] : 0) + 0.707106781186547f * (x1c + x34);
  322. dst[10*dst_stridea] = (add ? dst[10*dst_stridea] : 0) + 0.707106781186547f * (x1c - x34);
  323. dst[11*dst_stridea] = (add ? dst[11*dst_stridea] : 0) + 0.707106781186547f * (x24 + x42);
  324. dst[12*dst_stridea] = (add ? dst[12*dst_stridea] : 0) + 0.707106781186547f * (x24 - x42);
  325. dst[13*dst_stridea] = (add ? dst[13*dst_stridea] : 0) + 0.707106781186547f * (x2b - x3b);
  326. dst[14*dst_stridea] = (add ? dst[14*dst_stridea] : 0) + 0.707106781186547f * (x2b + x3b);
  327. dst[15*dst_stridea] = (add ? dst[15*dst_stridea] : 0) + 0.176776695296637f * (x1d + x1f) - 0.25f*x1e;
  328. dst += dst_strideb;
  329. src += src_strideb;
  330. }
  331. }
  332. #define DEF_FILTER_FREQ_FUNCS(bsize) \
  333. static av_always_inline void filter_freq_##bsize(const float *src, int src_linesize, \
  334. float *dst, int dst_linesize, \
  335. AVExpr *expr, double *var_values, \
  336. int sigma_th) \
  337. { \
  338. unsigned i; \
  339. DECLARE_ALIGNED(32, float, tmp_block1)[bsize * bsize]; \
  340. DECLARE_ALIGNED(32, float, tmp_block2)[bsize * bsize]; \
  341. \
  342. /* forward DCT */ \
  343. fdct##bsize##_1d(tmp_block1, src, 1, bsize, 1, src_linesize); \
  344. fdct##bsize##_1d(tmp_block2, tmp_block1, bsize, 1, bsize, 1); \
  345. \
  346. for (i = 0; i < bsize*bsize; i++) { \
  347. float *b = &tmp_block2[i]; \
  348. /* frequency filtering */ \
  349. if (expr) { \
  350. var_values[VAR_C] = fabsf(*b); \
  351. *b *= av_expr_eval(expr, var_values, NULL); \
  352. } else { \
  353. if (fabsf(*b) < sigma_th) \
  354. *b = 0; \
  355. } \
  356. } \
  357. \
  358. /* inverse DCT */ \
  359. idct##bsize##_1d(tmp_block1, tmp_block2, 1, bsize, 1, bsize, 0); \
  360. idct##bsize##_1d(dst, tmp_block1, dst_linesize, 1, bsize, 1, 1); \
  361. } \
  362. \
  363. static void filter_freq_sigma_##bsize(DCTdnoizContext *s, \
  364. const float *src, int src_linesize, \
  365. float *dst, int dst_linesize, int thread_id) \
  366. { \
  367. filter_freq_##bsize(src, src_linesize, dst, dst_linesize, NULL, NULL, s->th); \
  368. } \
  369. \
  370. static void filter_freq_expr_##bsize(DCTdnoizContext *s, \
  371. const float *src, int src_linesize, \
  372. float *dst, int dst_linesize, int thread_id) \
  373. { \
  374. filter_freq_##bsize(src, src_linesize, dst, dst_linesize, \
  375. s->expr[thread_id], s->var_values[thread_id], 0); \
  376. }
  377. DEF_FILTER_FREQ_FUNCS(8)
  378. DEF_FILTER_FREQ_FUNCS(16)
  379. #define DCT3X3_0_0 0.5773502691896258f /* 1/sqrt(3) */
  380. #define DCT3X3_0_1 0.5773502691896258f /* 1/sqrt(3) */
  381. #define DCT3X3_0_2 0.5773502691896258f /* 1/sqrt(3) */
  382. #define DCT3X3_1_0 0.7071067811865475f /* 1/sqrt(2) */
  383. #define DCT3X3_1_2 -0.7071067811865475f /* -1/sqrt(2) */
  384. #define DCT3X3_2_0 0.4082482904638631f /* 1/sqrt(6) */
  385. #define DCT3X3_2_1 -0.8164965809277261f /* -2/sqrt(6) */
  386. #define DCT3X3_2_2 0.4082482904638631f /* 1/sqrt(6) */
  387. static av_always_inline void color_decorrelation(float **dst, int dst_linesize,
  388. const uint8_t **src, int src_linesize,
  389. int w, int h,
  390. int r, int g, int b)
  391. {
  392. int x, y;
  393. float *dstp_r = dst[0];
  394. float *dstp_g = dst[1];
  395. float *dstp_b = dst[2];
  396. const uint8_t *srcp = src[0];
  397. for (y = 0; y < h; y++) {
  398. for (x = 0; x < w; x++) {
  399. dstp_r[x] = srcp[r] * DCT3X3_0_0 + srcp[g] * DCT3X3_0_1 + srcp[b] * DCT3X3_0_2;
  400. dstp_g[x] = srcp[r] * DCT3X3_1_0 + srcp[b] * DCT3X3_1_2;
  401. dstp_b[x] = srcp[r] * DCT3X3_2_0 + srcp[g] * DCT3X3_2_1 + srcp[b] * DCT3X3_2_2;
  402. srcp += 3;
  403. }
  404. srcp += src_linesize - w * 3;
  405. dstp_r += dst_linesize;
  406. dstp_g += dst_linesize;
  407. dstp_b += dst_linesize;
  408. }
  409. }
  410. static av_always_inline void color_correlation(uint8_t **dst, int dst_linesize,
  411. float **src, int src_linesize,
  412. int w, int h,
  413. int r, int g, int b)
  414. {
  415. int x, y;
  416. const float *src_r = src[0];
  417. const float *src_g = src[1];
  418. const float *src_b = src[2];
  419. uint8_t *dstp = dst[0];
  420. for (y = 0; y < h; y++) {
  421. for (x = 0; x < w; x++) {
  422. dstp[r] = av_clip_uint8(src_r[x] * DCT3X3_0_0 + src_g[x] * DCT3X3_1_0 + src_b[x] * DCT3X3_2_0);
  423. dstp[g] = av_clip_uint8(src_r[x] * DCT3X3_0_1 + src_b[x] * DCT3X3_2_1);
  424. dstp[b] = av_clip_uint8(src_r[x] * DCT3X3_0_2 + src_g[x] * DCT3X3_1_2 + src_b[x] * DCT3X3_2_2);
  425. dstp += 3;
  426. }
  427. dstp += dst_linesize - w * 3;
  428. src_r += src_linesize;
  429. src_g += src_linesize;
  430. src_b += src_linesize;
  431. }
  432. }
  433. #define DECLARE_COLOR_FUNCS(name, r, g, b) \
  434. static void color_decorrelation_##name(float **dst, int dst_linesize, \
  435. const uint8_t **src, int src_linesize, \
  436. int w, int h) \
  437. { \
  438. color_decorrelation(dst, dst_linesize, src, src_linesize, w, h, r, g, b); \
  439. } \
  440. \
  441. static void color_correlation_##name(uint8_t **dst, int dst_linesize, \
  442. float **src, int src_linesize, \
  443. int w, int h) \
  444. { \
  445. color_correlation(dst, dst_linesize, src, src_linesize, w, h, r, g, b); \
  446. }
  447. DECLARE_COLOR_FUNCS(rgb, 0, 1, 2)
  448. DECLARE_COLOR_FUNCS(bgr, 2, 1, 0)
  449. static av_always_inline void color_decorrelation_gbrp(float **dst, int dst_linesize,
  450. const uint8_t **src, int src_linesize,
  451. int w, int h)
  452. {
  453. int x, y;
  454. float *dstp_r = dst[0];
  455. float *dstp_g = dst[1];
  456. float *dstp_b = dst[2];
  457. const uint8_t *srcp_r = src[2];
  458. const uint8_t *srcp_g = src[0];
  459. const uint8_t *srcp_b = src[1];
  460. for (y = 0; y < h; y++) {
  461. for (x = 0; x < w; x++) {
  462. dstp_r[x] = srcp_r[x] * DCT3X3_0_0 + srcp_g[x] * DCT3X3_0_1 + srcp_b[x] * DCT3X3_0_2;
  463. dstp_g[x] = srcp_r[x] * DCT3X3_1_0 + srcp_b[x] * DCT3X3_1_2;
  464. dstp_b[x] = srcp_r[x] * DCT3X3_2_0 + srcp_g[x] * DCT3X3_2_1 + srcp_b[x] * DCT3X3_2_2;
  465. }
  466. srcp_r += src_linesize;
  467. srcp_g += src_linesize;
  468. srcp_b += src_linesize;
  469. dstp_r += dst_linesize;
  470. dstp_g += dst_linesize;
  471. dstp_b += dst_linesize;
  472. }
  473. }
  474. static av_always_inline void color_correlation_gbrp(uint8_t **dst, int dst_linesize,
  475. float **src, int src_linesize,
  476. int w, int h)
  477. {
  478. int x, y;
  479. const float *src_r = src[0];
  480. const float *src_g = src[1];
  481. const float *src_b = src[2];
  482. uint8_t *dstp_r = dst[2];
  483. uint8_t *dstp_g = dst[0];
  484. uint8_t *dstp_b = dst[1];
  485. for (y = 0; y < h; y++) {
  486. for (x = 0; x < w; x++) {
  487. dstp_r[x] = av_clip_uint8(src_r[x] * DCT3X3_0_0 + src_g[x] * DCT3X3_1_0 + src_b[x] * DCT3X3_2_0);
  488. dstp_g[x] = av_clip_uint8(src_r[x] * DCT3X3_0_1 + src_b[x] * DCT3X3_2_1);
  489. dstp_b[x] = av_clip_uint8(src_r[x] * DCT3X3_0_2 + src_g[x] * DCT3X3_1_2 + src_b[x] * DCT3X3_2_2);
  490. }
  491. dstp_r += dst_linesize;
  492. dstp_g += dst_linesize;
  493. dstp_b += dst_linesize;
  494. src_r += src_linesize;
  495. src_g += src_linesize;
  496. src_b += src_linesize;
  497. }
  498. }
  499. static int config_input(AVFilterLink *inlink)
  500. {
  501. AVFilterContext *ctx = inlink->dst;
  502. DCTdnoizContext *s = ctx->priv;
  503. int i, x, y, bx, by, linesize, *iweights, max_slice_h, slice_h;
  504. const int bsize = 1 << s->n;
  505. switch (inlink->format) {
  506. case AV_PIX_FMT_BGR24:
  507. s->color_decorrelation = color_decorrelation_bgr;
  508. s->color_correlation = color_correlation_bgr;
  509. break;
  510. case AV_PIX_FMT_RGB24:
  511. s->color_decorrelation = color_decorrelation_rgb;
  512. s->color_correlation = color_correlation_rgb;
  513. break;
  514. case AV_PIX_FMT_GBRP:
  515. s->color_decorrelation = color_decorrelation_gbrp;
  516. s->color_correlation = color_correlation_gbrp;
  517. break;
  518. default:
  519. av_assert0(0);
  520. }
  521. s->pr_width = inlink->w - (inlink->w - bsize) % s->step;
  522. s->pr_height = inlink->h - (inlink->h - bsize) % s->step;
  523. if (s->pr_width != inlink->w)
  524. av_log(ctx, AV_LOG_WARNING, "The last %d horizontal pixels won't be denoised\n",
  525. inlink->w - s->pr_width);
  526. if (s->pr_height != inlink->h)
  527. av_log(ctx, AV_LOG_WARNING, "The last %d vertical pixels won't be denoised\n",
  528. inlink->h - s->pr_height);
  529. max_slice_h = s->pr_height / ((s->bsize - 1) * 2);
  530. s->nb_threads = FFMIN3(MAX_THREADS, ff_filter_get_nb_threads(ctx), max_slice_h);
  531. av_log(ctx, AV_LOG_DEBUG, "threads: [max=%d hmax=%d user=%d] => %d\n",
  532. MAX_THREADS, max_slice_h, ff_filter_get_nb_threads(ctx), s->nb_threads);
  533. s->p_linesize = linesize = FFALIGN(s->pr_width, 32);
  534. for (i = 0; i < 2; i++) {
  535. s->cbuf[i][0] = av_malloc_array(linesize * s->pr_height, sizeof(*s->cbuf[i][0]));
  536. s->cbuf[i][1] = av_malloc_array(linesize * s->pr_height, sizeof(*s->cbuf[i][1]));
  537. s->cbuf[i][2] = av_malloc_array(linesize * s->pr_height, sizeof(*s->cbuf[i][2]));
  538. if (!s->cbuf[i][0] || !s->cbuf[i][1] || !s->cbuf[i][2])
  539. return AVERROR(ENOMEM);
  540. }
  541. /* eval expressions are probably not thread safe when the eval internal
  542. * state can be changed (typically through load & store operations) */
  543. if (s->expr_str) {
  544. for (i = 0; i < s->nb_threads; i++) {
  545. int ret = av_expr_parse(&s->expr[i], s->expr_str, var_names,
  546. NULL, NULL, NULL, NULL, 0, ctx);
  547. if (ret < 0)
  548. return ret;
  549. }
  550. }
  551. /* each slice will need to (pre & re)process the top and bottom block of
  552. * the previous one in in addition to its processing area. This is because
  553. * each pixel is averaged by all the surrounding blocks */
  554. slice_h = (int)ceilf(s->pr_height / (float)s->nb_threads) + (s->bsize - 1) * 2;
  555. for (i = 0; i < s->nb_threads; i++) {
  556. s->slices[i] = av_malloc_array(linesize, slice_h * sizeof(*s->slices[i]));
  557. if (!s->slices[i])
  558. return AVERROR(ENOMEM);
  559. }
  560. s->weights = av_malloc(s->pr_height * linesize * sizeof(*s->weights));
  561. if (!s->weights)
  562. return AVERROR(ENOMEM);
  563. iweights = av_calloc(s->pr_height, linesize * sizeof(*iweights));
  564. if (!iweights)
  565. return AVERROR(ENOMEM);
  566. for (y = 0; y < s->pr_height - bsize + 1; y += s->step)
  567. for (x = 0; x < s->pr_width - bsize + 1; x += s->step)
  568. for (by = 0; by < bsize; by++)
  569. for (bx = 0; bx < bsize; bx++)
  570. iweights[(y + by)*linesize + x + bx]++;
  571. for (y = 0; y < s->pr_height; y++)
  572. for (x = 0; x < s->pr_width; x++)
  573. s->weights[y*linesize + x] = 1. / iweights[y*linesize + x];
  574. av_free(iweights);
  575. return 0;
  576. }
  577. static av_cold int init(AVFilterContext *ctx)
  578. {
  579. DCTdnoizContext *s = ctx->priv;
  580. s->bsize = 1 << s->n;
  581. if (s->overlap == -1)
  582. s->overlap = s->bsize - 1;
  583. if (s->overlap > s->bsize - 1) {
  584. av_log(s, AV_LOG_ERROR, "Overlap value can not except %d "
  585. "with a block size of %dx%d\n",
  586. s->bsize - 1, s->bsize, s->bsize);
  587. return AVERROR(EINVAL);
  588. }
  589. if (s->expr_str) {
  590. switch (s->n) {
  591. case 3: s->filter_freq_func = filter_freq_expr_8; break;
  592. case 4: s->filter_freq_func = filter_freq_expr_16; break;
  593. default: av_assert0(0);
  594. }
  595. } else {
  596. switch (s->n) {
  597. case 3: s->filter_freq_func = filter_freq_sigma_8; break;
  598. case 4: s->filter_freq_func = filter_freq_sigma_16; break;
  599. default: av_assert0(0);
  600. }
  601. }
  602. s->th = s->sigma * 3.;
  603. s->step = s->bsize - s->overlap;
  604. return 0;
  605. }
  606. static int query_formats(AVFilterContext *ctx)
  607. {
  608. static const enum AVPixelFormat pix_fmts[] = {
  609. AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
  610. AV_PIX_FMT_GBRP,
  611. AV_PIX_FMT_NONE
  612. };
  613. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  614. if (!fmts_list)
  615. return AVERROR(ENOMEM);
  616. return ff_set_common_formats(ctx, fmts_list);
  617. }
  618. typedef struct ThreadData {
  619. float *src, *dst;
  620. } ThreadData;
  621. static int filter_slice(AVFilterContext *ctx,
  622. void *arg, int jobnr, int nb_jobs)
  623. {
  624. int x, y;
  625. DCTdnoizContext *s = ctx->priv;
  626. const ThreadData *td = arg;
  627. const int w = s->pr_width;
  628. const int h = s->pr_height;
  629. const int slice_start = (h * jobnr ) / nb_jobs;
  630. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  631. const int slice_start_ctx = FFMAX(slice_start - s->bsize + 1, 0);
  632. const int slice_end_ctx = FFMIN(slice_end, h - s->bsize + 1);
  633. const int slice_h = slice_end_ctx - slice_start_ctx;
  634. const int src_linesize = s->p_linesize;
  635. const int dst_linesize = s->p_linesize;
  636. const int slice_linesize = s->p_linesize;
  637. float *dst;
  638. const float *src = td->src + slice_start_ctx * src_linesize;
  639. const float *weights = s->weights + slice_start * dst_linesize;
  640. float *slice = s->slices[jobnr];
  641. // reset block sums
  642. memset(slice, 0, (slice_h + s->bsize - 1) * dst_linesize * sizeof(*slice));
  643. // block dct sums
  644. for (y = 0; y < slice_h; y += s->step) {
  645. for (x = 0; x < w - s->bsize + 1; x += s->step)
  646. s->filter_freq_func(s, src + x, src_linesize,
  647. slice + x, slice_linesize,
  648. jobnr);
  649. src += s->step * src_linesize;
  650. slice += s->step * slice_linesize;
  651. }
  652. // average blocks
  653. slice = s->slices[jobnr] + (slice_start - slice_start_ctx) * slice_linesize;
  654. dst = td->dst + slice_start * dst_linesize;
  655. for (y = slice_start; y < slice_end; y++) {
  656. for (x = 0; x < w; x++)
  657. dst[x] = slice[x] * weights[x];
  658. slice += slice_linesize;
  659. dst += dst_linesize;
  660. weights += dst_linesize;
  661. }
  662. return 0;
  663. }
  664. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  665. {
  666. AVFilterContext *ctx = inlink->dst;
  667. DCTdnoizContext *s = ctx->priv;
  668. AVFilterLink *outlink = inlink->dst->outputs[0];
  669. int direct, plane;
  670. AVFrame *out;
  671. if (av_frame_is_writable(in)) {
  672. direct = 1;
  673. out = in;
  674. } else {
  675. direct = 0;
  676. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  677. if (!out) {
  678. av_frame_free(&in);
  679. return AVERROR(ENOMEM);
  680. }
  681. av_frame_copy_props(out, in);
  682. }
  683. s->color_decorrelation(s->cbuf[0], s->p_linesize,
  684. (const uint8_t **)in->data, in->linesize[0],
  685. s->pr_width, s->pr_height);
  686. for (plane = 0; plane < 3; plane++) {
  687. ThreadData td = {
  688. .src = s->cbuf[0][plane],
  689. .dst = s->cbuf[1][plane],
  690. };
  691. ctx->internal->execute(ctx, filter_slice, &td, NULL, s->nb_threads);
  692. }
  693. s->color_correlation(out->data, out->linesize[0],
  694. s->cbuf[1], s->p_linesize,
  695. s->pr_width, s->pr_height);
  696. if (!direct) {
  697. int y;
  698. uint8_t *dst = out->data[0];
  699. const uint8_t *src = in->data[0];
  700. const int dst_linesize = out->linesize[0];
  701. const int src_linesize = in->linesize[0];
  702. const int hpad = (inlink->w - s->pr_width) * 3;
  703. const int vpad = (inlink->h - s->pr_height);
  704. if (hpad) {
  705. uint8_t *dstp = dst + s->pr_width * 3;
  706. const uint8_t *srcp = src + s->pr_width * 3;
  707. for (y = 0; y < s->pr_height; y++) {
  708. memcpy(dstp, srcp, hpad);
  709. dstp += dst_linesize;
  710. srcp += src_linesize;
  711. }
  712. }
  713. if (vpad) {
  714. uint8_t *dstp = dst + s->pr_height * dst_linesize;
  715. const uint8_t *srcp = src + s->pr_height * src_linesize;
  716. for (y = 0; y < vpad; y++) {
  717. memcpy(dstp, srcp, inlink->w * 3);
  718. dstp += dst_linesize;
  719. srcp += src_linesize;
  720. }
  721. }
  722. av_frame_free(&in);
  723. }
  724. return ff_filter_frame(outlink, out);
  725. }
  726. static av_cold void uninit(AVFilterContext *ctx)
  727. {
  728. int i;
  729. DCTdnoizContext *s = ctx->priv;
  730. av_freep(&s->weights);
  731. for (i = 0; i < 2; i++) {
  732. av_freep(&s->cbuf[i][0]);
  733. av_freep(&s->cbuf[i][1]);
  734. av_freep(&s->cbuf[i][2]);
  735. }
  736. for (i = 0; i < s->nb_threads; i++) {
  737. av_freep(&s->slices[i]);
  738. av_expr_free(s->expr[i]);
  739. }
  740. }
  741. static const AVFilterPad dctdnoiz_inputs[] = {
  742. {
  743. .name = "default",
  744. .type = AVMEDIA_TYPE_VIDEO,
  745. .filter_frame = filter_frame,
  746. .config_props = config_input,
  747. },
  748. { NULL }
  749. };
  750. static const AVFilterPad dctdnoiz_outputs[] = {
  751. {
  752. .name = "default",
  753. .type = AVMEDIA_TYPE_VIDEO,
  754. },
  755. { NULL }
  756. };
  757. AVFilter ff_vf_dctdnoiz = {
  758. .name = "dctdnoiz",
  759. .description = NULL_IF_CONFIG_SMALL("Denoise frames using 2D DCT."),
  760. .priv_size = sizeof(DCTdnoizContext),
  761. .init = init,
  762. .uninit = uninit,
  763. .query_formats = query_formats,
  764. .inputs = dctdnoiz_inputs,
  765. .outputs = dctdnoiz_outputs,
  766. .priv_class = &dctdnoiz_class,
  767. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  768. };