vf_dctdnoiz.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. for (y = 0; y < h; y++) {
  397. const uint8_t *srcp = src;
  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. src += src_linesize;
  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. for (y = 0; y < h; y++) {
  420. uint8_t *dstp = dst;
  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. dst += dst_linesize;
  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 int config_input(AVFilterLink *inlink)
  450. {
  451. AVFilterContext *ctx = inlink->dst;
  452. DCTdnoizContext *s = ctx->priv;
  453. int i, x, y, bx, by, linesize, *iweights, max_slice_h, slice_h;
  454. const int bsize = 1 << s->n;
  455. switch (inlink->format) {
  456. case AV_PIX_FMT_BGR24:
  457. s->color_decorrelation = color_decorrelation_bgr;
  458. s->color_correlation = color_correlation_bgr;
  459. break;
  460. case AV_PIX_FMT_RGB24:
  461. s->color_decorrelation = color_decorrelation_rgb;
  462. s->color_correlation = color_correlation_rgb;
  463. break;
  464. default:
  465. av_assert0(0);
  466. }
  467. s->pr_width = inlink->w - (inlink->w - bsize) % s->step;
  468. s->pr_height = inlink->h - (inlink->h - bsize) % s->step;
  469. if (s->pr_width != inlink->w)
  470. av_log(ctx, AV_LOG_WARNING, "The last %d horizontal pixels won't be denoised\n",
  471. inlink->w - s->pr_width);
  472. if (s->pr_height != inlink->h)
  473. av_log(ctx, AV_LOG_WARNING, "The last %d vertical pixels won't be denoised\n",
  474. inlink->h - s->pr_height);
  475. max_slice_h = s->pr_height / ((s->bsize - 1) * 2);
  476. s->nb_threads = FFMIN3(MAX_THREADS, ctx->graph->nb_threads, max_slice_h);
  477. av_log(ctx, AV_LOG_DEBUG, "threads: [max=%d hmax=%d user=%d] => %d\n",
  478. MAX_THREADS, max_slice_h, ctx->graph->nb_threads, s->nb_threads);
  479. s->p_linesize = linesize = FFALIGN(s->pr_width, 32);
  480. for (i = 0; i < 2; i++) {
  481. s->cbuf[i][0] = av_malloc_array(linesize * s->pr_height, sizeof(*s->cbuf[i][0]));
  482. s->cbuf[i][1] = av_malloc_array(linesize * s->pr_height, sizeof(*s->cbuf[i][1]));
  483. s->cbuf[i][2] = av_malloc_array(linesize * s->pr_height, sizeof(*s->cbuf[i][2]));
  484. if (!s->cbuf[i][0] || !s->cbuf[i][1] || !s->cbuf[i][2])
  485. return AVERROR(ENOMEM);
  486. }
  487. /* eval expressions are probably not thread safe when the eval internal
  488. * state can be changed (typically through load & store operations) */
  489. if (s->expr_str) {
  490. for (i = 0; i < s->nb_threads; i++) {
  491. int ret = av_expr_parse(&s->expr[i], s->expr_str, var_names,
  492. NULL, NULL, NULL, NULL, 0, ctx);
  493. if (ret < 0)
  494. return ret;
  495. }
  496. }
  497. /* each slice will need to (pre & re)process the top and bottom block of
  498. * the previous one in in addition to its processing area. This is because
  499. * each pixel is averaged by all the surrounding blocks */
  500. slice_h = (int)ceilf(s->pr_height / (float)s->nb_threads) + (s->bsize - 1) * 2;
  501. for (i = 0; i < s->nb_threads; i++) {
  502. s->slices[i] = av_malloc_array(linesize, slice_h * sizeof(*s->slices[i]));
  503. if (!s->slices[i])
  504. return AVERROR(ENOMEM);
  505. }
  506. s->weights = av_malloc(s->pr_height * linesize * sizeof(*s->weights));
  507. if (!s->weights)
  508. return AVERROR(ENOMEM);
  509. iweights = av_calloc(s->pr_height, linesize * sizeof(*iweights));
  510. if (!iweights)
  511. return AVERROR(ENOMEM);
  512. for (y = 0; y < s->pr_height - bsize + 1; y += s->step)
  513. for (x = 0; x < s->pr_width - bsize + 1; x += s->step)
  514. for (by = 0; by < bsize; by++)
  515. for (bx = 0; bx < bsize; bx++)
  516. iweights[(y + by)*linesize + x + bx]++;
  517. for (y = 0; y < s->pr_height; y++)
  518. for (x = 0; x < s->pr_width; x++)
  519. s->weights[y*linesize + x] = 1. / iweights[y*linesize + x];
  520. av_free(iweights);
  521. return 0;
  522. }
  523. static av_cold int init(AVFilterContext *ctx)
  524. {
  525. DCTdnoizContext *s = ctx->priv;
  526. s->bsize = 1 << s->n;
  527. if (s->overlap == -1)
  528. s->overlap = s->bsize - 1;
  529. if (s->overlap > s->bsize - 1) {
  530. av_log(s, AV_LOG_ERROR, "Overlap value can not except %d "
  531. "with a block size of %dx%d\n",
  532. s->bsize - 1, s->bsize, s->bsize);
  533. return AVERROR(EINVAL);
  534. }
  535. if (s->expr_str) {
  536. switch (s->n) {
  537. case 3: s->filter_freq_func = filter_freq_expr_8; break;
  538. case 4: s->filter_freq_func = filter_freq_expr_16; break;
  539. default: av_assert0(0);
  540. }
  541. } else {
  542. switch (s->n) {
  543. case 3: s->filter_freq_func = filter_freq_sigma_8; break;
  544. case 4: s->filter_freq_func = filter_freq_sigma_16; break;
  545. default: av_assert0(0);
  546. }
  547. }
  548. s->th = s->sigma * 3.;
  549. s->step = s->bsize - s->overlap;
  550. return 0;
  551. }
  552. static int query_formats(AVFilterContext *ctx)
  553. {
  554. static const enum AVPixelFormat pix_fmts[] = {
  555. AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
  556. AV_PIX_FMT_NONE
  557. };
  558. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  559. if (!fmts_list)
  560. return AVERROR(ENOMEM);
  561. return ff_set_common_formats(ctx, fmts_list);
  562. }
  563. typedef struct ThreadData {
  564. float *src, *dst;
  565. } ThreadData;
  566. static int filter_slice(AVFilterContext *ctx,
  567. void *arg, int jobnr, int nb_jobs)
  568. {
  569. int x, y;
  570. DCTdnoizContext *s = ctx->priv;
  571. const ThreadData *td = arg;
  572. const int w = s->pr_width;
  573. const int h = s->pr_height;
  574. const int slice_start = (h * jobnr ) / nb_jobs;
  575. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  576. const int slice_start_ctx = FFMAX(slice_start - s->bsize + 1, 0);
  577. const int slice_end_ctx = FFMIN(slice_end, h - s->bsize + 1);
  578. const int slice_h = slice_end_ctx - slice_start_ctx;
  579. const int src_linesize = s->p_linesize;
  580. const int dst_linesize = s->p_linesize;
  581. const int slice_linesize = s->p_linesize;
  582. float *dst;
  583. const float *src = td->src + slice_start_ctx * src_linesize;
  584. const float *weights = s->weights + slice_start * dst_linesize;
  585. float *slice = s->slices[jobnr];
  586. // reset block sums
  587. memset(slice, 0, (slice_h + s->bsize - 1) * dst_linesize * sizeof(*slice));
  588. // block dct sums
  589. for (y = 0; y < slice_h; y += s->step) {
  590. for (x = 0; x < w - s->bsize + 1; x += s->step)
  591. s->filter_freq_func(s, src + x, src_linesize,
  592. slice + x, slice_linesize,
  593. jobnr);
  594. src += s->step * src_linesize;
  595. slice += s->step * slice_linesize;
  596. }
  597. // average blocks
  598. slice = s->slices[jobnr] + (slice_start - slice_start_ctx) * slice_linesize;
  599. dst = td->dst + slice_start * dst_linesize;
  600. for (y = slice_start; y < slice_end; y++) {
  601. for (x = 0; x < w; x++)
  602. dst[x] = slice[x] * weights[x];
  603. slice += slice_linesize;
  604. dst += dst_linesize;
  605. weights += dst_linesize;
  606. }
  607. return 0;
  608. }
  609. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  610. {
  611. AVFilterContext *ctx = inlink->dst;
  612. DCTdnoizContext *s = ctx->priv;
  613. AVFilterLink *outlink = inlink->dst->outputs[0];
  614. int direct, plane;
  615. AVFrame *out;
  616. if (av_frame_is_writable(in)) {
  617. direct = 1;
  618. out = in;
  619. } else {
  620. direct = 0;
  621. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  622. if (!out) {
  623. av_frame_free(&in);
  624. return AVERROR(ENOMEM);
  625. }
  626. av_frame_copy_props(out, in);
  627. }
  628. s->color_decorrelation(s->cbuf[0], s->p_linesize,
  629. in->data[0], in->linesize[0],
  630. s->pr_width, s->pr_height);
  631. for (plane = 0; plane < 3; plane++) {
  632. ThreadData td = {
  633. .src = s->cbuf[0][plane],
  634. .dst = s->cbuf[1][plane],
  635. };
  636. ctx->internal->execute(ctx, filter_slice, &td, NULL, s->nb_threads);
  637. }
  638. s->color_correlation(out->data[0], out->linesize[0],
  639. s->cbuf[1], s->p_linesize,
  640. s->pr_width, s->pr_height);
  641. if (!direct) {
  642. int y;
  643. uint8_t *dst = out->data[0];
  644. const uint8_t *src = in->data[0];
  645. const int dst_linesize = out->linesize[0];
  646. const int src_linesize = in->linesize[0];
  647. const int hpad = (inlink->w - s->pr_width) * 3;
  648. const int vpad = (inlink->h - s->pr_height);
  649. if (hpad) {
  650. uint8_t *dstp = dst + s->pr_width * 3;
  651. const uint8_t *srcp = src + s->pr_width * 3;
  652. for (y = 0; y < s->pr_height; y++) {
  653. memcpy(dstp, srcp, hpad);
  654. dstp += dst_linesize;
  655. srcp += src_linesize;
  656. }
  657. }
  658. if (vpad) {
  659. uint8_t *dstp = dst + s->pr_height * dst_linesize;
  660. const uint8_t *srcp = src + s->pr_height * src_linesize;
  661. for (y = 0; y < vpad; y++) {
  662. memcpy(dstp, srcp, inlink->w * 3);
  663. dstp += dst_linesize;
  664. srcp += src_linesize;
  665. }
  666. }
  667. av_frame_free(&in);
  668. }
  669. return ff_filter_frame(outlink, out);
  670. }
  671. static av_cold void uninit(AVFilterContext *ctx)
  672. {
  673. int i;
  674. DCTdnoizContext *s = ctx->priv;
  675. av_freep(&s->weights);
  676. for (i = 0; i < 2; i++) {
  677. av_freep(&s->cbuf[i][0]);
  678. av_freep(&s->cbuf[i][1]);
  679. av_freep(&s->cbuf[i][2]);
  680. }
  681. for (i = 0; i < s->nb_threads; i++) {
  682. av_freep(&s->slices[i]);
  683. av_expr_free(s->expr[i]);
  684. }
  685. }
  686. static const AVFilterPad dctdnoiz_inputs[] = {
  687. {
  688. .name = "default",
  689. .type = AVMEDIA_TYPE_VIDEO,
  690. .filter_frame = filter_frame,
  691. .config_props = config_input,
  692. },
  693. { NULL }
  694. };
  695. static const AVFilterPad dctdnoiz_outputs[] = {
  696. {
  697. .name = "default",
  698. .type = AVMEDIA_TYPE_VIDEO,
  699. },
  700. { NULL }
  701. };
  702. AVFilter ff_vf_dctdnoiz = {
  703. .name = "dctdnoiz",
  704. .description = NULL_IF_CONFIG_SMALL("Denoise frames using 2D DCT."),
  705. .priv_size = sizeof(DCTdnoizContext),
  706. .init = init,
  707. .uninit = uninit,
  708. .query_formats = query_formats,
  709. .inputs = dctdnoiz_inputs,
  710. .outputs = dctdnoiz_outputs,
  711. .priv_class = &dctdnoiz_class,
  712. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  713. };