vf_dctdnoiz.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright (c) 2013 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 extremely slow DCT image denoiser.
  22. * @see http://www.ipol.im/pub/art/2011/ys-dct/
  23. */
  24. #include "libavcodec/avfft.h"
  25. #include "libavutil/eval.h"
  26. #include "libavutil/opt.h"
  27. #include "drawutils.h"
  28. #include "internal.h"
  29. #define NBITS 4
  30. #define BSIZE (1<<(NBITS))
  31. static const char *const var_names[] = { "c", NULL };
  32. enum { VAR_C, VAR_VARS_NB };
  33. typedef struct {
  34. const AVClass *class;
  35. /* coefficient factor expression */
  36. char *expr_str;
  37. AVExpr *expr;
  38. double var_values[VAR_VARS_NB];
  39. int pr_width, pr_height; // width and height to process
  40. float sigma; // used when no expression are st
  41. float th; // threshold (3*sigma)
  42. float color_dct[3][3]; // 3x3 DCT for color decorrelation
  43. float *cbuf[2][3]; // two planar rgb color buffers
  44. float *weights; // dct coeff are cumulated with overlapping; these values are used for averaging
  45. int p_linesize; // line sizes for color and weights
  46. int overlap; // number of block overlapping pixels
  47. int step; // block step increment (BSIZE - overlap)
  48. DCTContext *dct, *idct; // DCT and inverse DCT contexts
  49. float *block, *tmp_block; // two BSIZE x BSIZE block buffers
  50. } DCTdnoizContext;
  51. #define OFFSET(x) offsetof(DCTdnoizContext, x)
  52. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  53. static const AVOption dctdnoiz_options[] = {
  54. { "sigma", "set noise sigma constant", OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 999, .flags = FLAGS },
  55. { "s", "set noise sigma constant", OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 999, .flags = FLAGS },
  56. { "overlap", "set number of block overlapping pixels", OFFSET(overlap), AV_OPT_TYPE_INT, {.i64=(1<<NBITS)-1}, 0, (1<<NBITS)-1, .flags = FLAGS },
  57. { "expr", "set coefficient factor expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  58. { "e", "set coefficient factor expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(dctdnoiz);
  62. static float *dct_block(DCTdnoizContext *ctx, const float *src, int src_linesize)
  63. {
  64. int x, y;
  65. float *column;
  66. for (y = 0; y < BSIZE; y++) {
  67. float *line = ctx->block;
  68. memcpy(line, src, BSIZE * sizeof(*line));
  69. src += src_linesize;
  70. av_dct_calc(ctx->dct, line);
  71. column = ctx->tmp_block + y;
  72. column[0] = line[0] * (1. / sqrt(BSIZE));
  73. column += BSIZE;
  74. for (x = 1; x < BSIZE; x++) {
  75. *column = line[x] * sqrt(2. / BSIZE);
  76. column += BSIZE;
  77. }
  78. }
  79. column = ctx->tmp_block;
  80. for (x = 0; x < BSIZE; x++) {
  81. av_dct_calc(ctx->dct, column);
  82. column[0] *= 1. / sqrt(BSIZE);
  83. for (y = 1; y < BSIZE; y++)
  84. column[y] *= sqrt(2. / BSIZE);
  85. column += BSIZE;
  86. }
  87. for (y = 0; y < BSIZE; y++)
  88. for (x = 0; x < BSIZE; x++)
  89. ctx->block[y*BSIZE + x] = ctx->tmp_block[x*BSIZE + y];
  90. return ctx->block;
  91. }
  92. static void idct_block(DCTdnoizContext *ctx, float *dst, int dst_linesize)
  93. {
  94. int x, y;
  95. float *block = ctx->block;
  96. float *tmp = ctx->tmp_block;
  97. for (y = 0; y < BSIZE; y++) {
  98. block[0] *= sqrt(BSIZE);
  99. for (x = 1; x < BSIZE; x++)
  100. block[x] *= 1./sqrt(2. / BSIZE);
  101. av_dct_calc(ctx->idct, block);
  102. block += BSIZE;
  103. }
  104. block = ctx->block;
  105. for (y = 0; y < BSIZE; y++) {
  106. tmp[0] = block[y] * sqrt(BSIZE);
  107. for (x = 1; x < BSIZE; x++)
  108. tmp[x] = block[x*BSIZE + y] * (1./sqrt(2. / BSIZE));
  109. av_dct_calc(ctx->idct, tmp);
  110. for (x = 0; x < BSIZE; x++)
  111. dst[x*dst_linesize + y] += tmp[x];
  112. }
  113. }
  114. static int config_input(AVFilterLink *inlink)
  115. {
  116. AVFilterContext *ctx = inlink->dst;
  117. DCTdnoizContext *s = ctx->priv;
  118. int i, x, y, bx, by, linesize, *iweights;
  119. const float dct_3x3[3][3] = {
  120. { 1./sqrt(3), 1./sqrt(3), 1./sqrt(3) },
  121. { 1./sqrt(2), 0, -1./sqrt(2) },
  122. { 1./sqrt(6), -2./sqrt(6), 1./sqrt(6) },
  123. };
  124. uint8_t rgba_map[4];
  125. ff_fill_rgba_map(rgba_map, inlink->format);
  126. for (y = 0; y < 3; y++)
  127. for (x = 0; x < 3; x++)
  128. s->color_dct[y][x] = dct_3x3[rgba_map[y]][rgba_map[x]];
  129. s->pr_width = inlink->w - (inlink->w - BSIZE) % s->step;
  130. s->pr_height = inlink->h - (inlink->h - BSIZE) % s->step;
  131. if (s->pr_width != inlink->w)
  132. av_log(ctx, AV_LOG_WARNING, "The last %d horizontal pixels won't be denoised\n",
  133. inlink->w - s->pr_width);
  134. if (s->pr_height != inlink->h)
  135. av_log(ctx, AV_LOG_WARNING, "The last %d vertical pixels won't be denoised\n",
  136. inlink->h - s->pr_height);
  137. s->p_linesize = linesize = FFALIGN(s->pr_width, 32);
  138. for (i = 0; i < 2; i++) {
  139. s->cbuf[i][0] = av_malloc(linesize * s->pr_height * sizeof(*s->cbuf[i][0]));
  140. s->cbuf[i][1] = av_malloc(linesize * s->pr_height * sizeof(*s->cbuf[i][1]));
  141. s->cbuf[i][2] = av_malloc(linesize * s->pr_height * sizeof(*s->cbuf[i][2]));
  142. if (!s->cbuf[i][0] || !s->cbuf[i][1] || !s->cbuf[i][2])
  143. return AVERROR(ENOMEM);
  144. }
  145. s->weights = av_malloc(s->pr_height * linesize * sizeof(*s->weights));
  146. if (!s->weights)
  147. return AVERROR(ENOMEM);
  148. iweights = av_calloc(s->pr_height, linesize * sizeof(*iweights));
  149. if (!iweights)
  150. return AVERROR(ENOMEM);
  151. for (y = 0; y < s->pr_height - BSIZE + 1; y += s->step)
  152. for (x = 0; x < s->pr_width - BSIZE + 1; x += s->step)
  153. for (by = 0; by < BSIZE; by++)
  154. for (bx = 0; bx < BSIZE; bx++)
  155. iweights[(y + by)*linesize + x + bx]++;
  156. for (y = 0; y < s->pr_height; y++)
  157. for (x = 0; x < s->pr_width; x++)
  158. s->weights[y*linesize + x] = 1. / iweights[y*linesize + x];
  159. av_free(iweights);
  160. return 0;
  161. }
  162. static av_cold int init(AVFilterContext *ctx)
  163. {
  164. DCTdnoizContext *s = ctx->priv;
  165. if (s->expr_str) {
  166. int ret = av_expr_parse(&s->expr, s->expr_str, var_names,
  167. NULL, NULL, NULL, NULL, 0, ctx);
  168. if (ret < 0)
  169. return ret;
  170. }
  171. s->th = s->sigma * 3.;
  172. s->step = BSIZE - s->overlap;
  173. s->dct = av_dct_init(NBITS, DCT_II);
  174. s->idct = av_dct_init(NBITS, DCT_III);
  175. s->block = av_malloc(BSIZE * BSIZE * sizeof(*s->block));
  176. s->tmp_block = av_malloc(BSIZE * BSIZE * sizeof(*s->tmp_block));
  177. if (!s->dct || !s->idct || !s->tmp_block || !s->block)
  178. return AVERROR(ENOMEM);
  179. return 0;
  180. }
  181. static int query_formats(AVFilterContext *ctx)
  182. {
  183. static const enum AVPixelFormat pix_fmts[] = {
  184. AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
  185. AV_PIX_FMT_NONE
  186. };
  187. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  188. return 0;
  189. }
  190. static void color_decorrelation(float dct3ch[3][3], float **dst, int dst_linesize,
  191. const uint8_t *src, int src_linesize, int w, int h)
  192. {
  193. int x, y;
  194. float *dstp_r = dst[0];
  195. float *dstp_g = dst[1];
  196. float *dstp_b = dst[2];
  197. for (y = 0; y < h; y++) {
  198. const uint8_t *srcp = src;
  199. for (x = 0; x < w; x++) {
  200. dstp_r[x] = srcp[0] * dct3ch[0][0] + srcp[1] * dct3ch[0][1] + srcp[2] * dct3ch[0][2];
  201. dstp_g[x] = srcp[0] * dct3ch[1][0] + srcp[1] * dct3ch[1][1] + srcp[2] * dct3ch[1][2];
  202. dstp_b[x] = srcp[0] * dct3ch[2][0] + srcp[1] * dct3ch[2][1] + srcp[2] * dct3ch[2][2];
  203. srcp += 3;
  204. }
  205. src += src_linesize;
  206. dstp_r += dst_linesize;
  207. dstp_g += dst_linesize;
  208. dstp_b += dst_linesize;
  209. }
  210. }
  211. static void color_correlation(float dct3ch[3][3], uint8_t *dst, int dst_linesize,
  212. float **src, int src_linesize, int w, int h)
  213. {
  214. int x, y;
  215. const float *src_r = src[0];
  216. const float *src_g = src[1];
  217. const float *src_b = src[2];
  218. for (y = 0; y < h; y++) {
  219. uint8_t *dstp = dst;
  220. for (x = 0; x < w; x++) {
  221. dstp[0] = av_clip_uint8(src_r[x] * dct3ch[0][0] + src_g[x] * dct3ch[1][0] + src_b[x] * dct3ch[2][0]);
  222. dstp[1] = av_clip_uint8(src_r[x] * dct3ch[0][1] + src_g[x] * dct3ch[1][1] + src_b[x] * dct3ch[2][1]);
  223. dstp[2] = av_clip_uint8(src_r[x] * dct3ch[0][2] + src_g[x] * dct3ch[1][2] + src_b[x] * dct3ch[2][2]);
  224. dstp += 3;
  225. }
  226. dst += dst_linesize;
  227. src_r += src_linesize;
  228. src_g += src_linesize;
  229. src_b += src_linesize;
  230. }
  231. }
  232. static void filter_plane(AVFilterContext *ctx,
  233. float *dst, int dst_linesize,
  234. const float *src, int src_linesize,
  235. int w, int h)
  236. {
  237. int x, y, bx, by;
  238. DCTdnoizContext *s = ctx->priv;
  239. float *dst0 = dst;
  240. const float *weights = s->weights;
  241. // reset block sums
  242. memset(dst, 0, h * dst_linesize * sizeof(*dst));
  243. // block dct sums
  244. for (y = 0; y < h - BSIZE + 1; y += s->step) {
  245. for (x = 0; x < w - BSIZE + 1; x += s->step) {
  246. float *ftb = dct_block(s, src + x, src_linesize);
  247. if (s->expr) {
  248. for (by = 0; by < BSIZE; by++) {
  249. for (bx = 0; bx < BSIZE; bx++) {
  250. s->var_values[VAR_C] = FFABS(*ftb);
  251. *ftb++ *= av_expr_eval(s->expr, s->var_values, s);
  252. }
  253. }
  254. } else {
  255. for (by = 0; by < BSIZE; by++) {
  256. for (bx = 0; bx < BSIZE; bx++) {
  257. if (FFABS(*ftb) < s->th)
  258. *ftb = 0;
  259. ftb++;
  260. }
  261. }
  262. }
  263. idct_block(s, dst + x, dst_linesize);
  264. }
  265. src += s->step * src_linesize;
  266. dst += s->step * dst_linesize;
  267. }
  268. // average blocks
  269. dst = dst0;
  270. for (y = 0; y < h; y++) {
  271. for (x = 0; x < w; x++)
  272. dst[x] *= weights[x];
  273. dst += dst_linesize;
  274. weights += dst_linesize;
  275. }
  276. }
  277. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  278. {
  279. AVFilterContext *ctx = inlink->dst;
  280. DCTdnoizContext *s = ctx->priv;
  281. AVFilterLink *outlink = inlink->dst->outputs[0];
  282. int direct, plane;
  283. AVFrame *out;
  284. if (av_frame_is_writable(in)) {
  285. direct = 1;
  286. out = in;
  287. } else {
  288. direct = 0;
  289. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  290. if (!out) {
  291. av_frame_free(&in);
  292. return AVERROR(ENOMEM);
  293. }
  294. av_frame_copy_props(out, in);
  295. }
  296. color_decorrelation(s->color_dct, s->cbuf[0], s->p_linesize,
  297. in->data[0], in->linesize[0], s->pr_width, s->pr_height);
  298. for (plane = 0; plane < 3; plane++)
  299. filter_plane(ctx, s->cbuf[1][plane], s->p_linesize,
  300. s->cbuf[0][plane], s->p_linesize,
  301. s->pr_width, s->pr_height);
  302. color_correlation(s->color_dct, out->data[0], out->linesize[0],
  303. s->cbuf[1], s->p_linesize, s->pr_width, s->pr_height);
  304. if (!direct) {
  305. int y;
  306. uint8_t *dst = out->data[0];
  307. const uint8_t *src = in->data[0];
  308. const int dst_linesize = out->linesize[0];
  309. const int src_linesize = in->linesize[0];
  310. const int hpad = (inlink->w - s->pr_width) * 3;
  311. const int vpad = (inlink->h - s->pr_height);
  312. if (hpad) {
  313. uint8_t *dstp = dst + s->pr_width * 3;
  314. const uint8_t *srcp = src + s->pr_width * 3;
  315. for (y = 0; y < s->pr_height; y++) {
  316. memcpy(dstp, srcp, hpad);
  317. dstp += dst_linesize;
  318. srcp += src_linesize;
  319. }
  320. }
  321. if (vpad) {
  322. uint8_t *dstp = dst + s->pr_height * dst_linesize;
  323. const uint8_t *srcp = src + s->pr_height * src_linesize;
  324. for (y = 0; y < vpad; y++) {
  325. memcpy(dstp, srcp, inlink->w * 3);
  326. dstp += dst_linesize;
  327. srcp += src_linesize;
  328. }
  329. }
  330. av_frame_free(&in);
  331. }
  332. return ff_filter_frame(outlink, out);
  333. }
  334. static av_cold void uninit(AVFilterContext *ctx)
  335. {
  336. int i;
  337. DCTdnoizContext *s = ctx->priv;
  338. av_dct_end(s->dct);
  339. av_dct_end(s->idct);
  340. av_free(s->block);
  341. av_free(s->tmp_block);
  342. av_free(s->weights);
  343. for (i = 0; i < 2; i++) {
  344. av_free(s->cbuf[i][0]);
  345. av_free(s->cbuf[i][1]);
  346. av_free(s->cbuf[i][2]);
  347. }
  348. av_expr_free(s->expr);
  349. }
  350. static const AVFilterPad dctdnoiz_inputs[] = {
  351. {
  352. .name = "default",
  353. .type = AVMEDIA_TYPE_VIDEO,
  354. .filter_frame = filter_frame,
  355. .config_props = config_input,
  356. },
  357. { NULL }
  358. };
  359. static const AVFilterPad dctdnoiz_outputs[] = {
  360. {
  361. .name = "default",
  362. .type = AVMEDIA_TYPE_VIDEO,
  363. },
  364. { NULL }
  365. };
  366. AVFilter ff_vf_dctdnoiz = {
  367. .name = "dctdnoiz",
  368. .description = NULL_IF_CONFIG_SMALL("Denoise frames using 2D DCT."),
  369. .priv_size = sizeof(DCTdnoizContext),
  370. .init = init,
  371. .uninit = uninit,
  372. .query_formats = query_formats,
  373. .inputs = dctdnoiz_inputs,
  374. .outputs = dctdnoiz_outputs,
  375. .priv_class = &dctdnoiz_class,
  376. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  377. };