vf_owdenoise.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (c) 2007 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2013 Clément Bœsch <u pkh me>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. /**
  22. * @todo try to change to int
  23. * @todo try lifting based implementation
  24. * @todo optimize optimize optimize
  25. * @todo hard thresholding
  26. * @todo use QP to decide filter strength
  27. * @todo wavelet normalization / least squares optimal signal vs. noise thresholds
  28. */
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "avfilter.h"
  33. #include "internal.h"
  34. typedef struct {
  35. const AVClass *class;
  36. double luma_strength;
  37. double chroma_strength;
  38. int depth;
  39. float *plane[16+1][4];
  40. int linesize;
  41. int hsub, vsub;
  42. } OWDenoiseContext;
  43. #define OFFSET(x) offsetof(OWDenoiseContext, x)
  44. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  45. static const AVOption owdenoise_options[] = {
  46. { "depth", "set depth", OFFSET(depth), AV_OPT_TYPE_INT, {.i64 = 8}, 8, 16, FLAGS },
  47. { "luma_strength", "set luma strength", OFFSET(luma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
  48. { "ls", "set luma strength", OFFSET(luma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
  49. { "chroma_strength", "set chroma strength", OFFSET(chroma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
  50. { "cs", "set chroma strength", OFFSET(chroma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
  51. { NULL }
  52. };
  53. AVFILTER_DEFINE_CLASS(owdenoise);
  54. DECLARE_ALIGNED(8, static const uint8_t, dither)[8][8] = {
  55. { 0, 48, 12, 60, 3, 51, 15, 63 },
  56. { 32, 16, 44, 28, 35, 19, 47, 31 },
  57. { 8, 56, 4, 52, 11, 59, 7, 55 },
  58. { 40, 24, 36, 20, 43, 27, 39, 23 },
  59. { 2, 50, 14, 62, 1, 49, 13, 61 },
  60. { 34, 18, 46, 30, 33, 17, 45, 29 },
  61. { 10, 58, 6, 54, 9, 57, 5, 53 },
  62. { 42, 26, 38, 22, 41, 25, 37, 21 },
  63. };
  64. static const double coeff[2][5] = {
  65. {
  66. 0.6029490182363579 * M_SQRT2,
  67. 0.2668641184428723 * M_SQRT2,
  68. -0.07822326652898785 * M_SQRT2,
  69. -0.01686411844287495 * M_SQRT2,
  70. 0.02674875741080976 * M_SQRT2,
  71. },{
  72. 1.115087052456994 / M_SQRT2,
  73. -0.5912717631142470 / M_SQRT2,
  74. -0.05754352622849957 / M_SQRT2,
  75. 0.09127176311424948 / M_SQRT2,
  76. }
  77. };
  78. static const double icoeff[2][5] = {
  79. {
  80. 1.115087052456994 / M_SQRT2,
  81. 0.5912717631142470 / M_SQRT2,
  82. -0.05754352622849957 / M_SQRT2,
  83. -0.09127176311424948 / M_SQRT2,
  84. },{
  85. 0.6029490182363579 * M_SQRT2,
  86. -0.2668641184428723 * M_SQRT2,
  87. -0.07822326652898785 * M_SQRT2,
  88. 0.01686411844287495 * M_SQRT2,
  89. 0.02674875741080976 * M_SQRT2,
  90. }
  91. };
  92. static inline void decompose(float *dst_l, float *dst_h, const float *src,
  93. int linesize, int w)
  94. {
  95. int x, i;
  96. for (x = 0; x < w; x++) {
  97. double sum_l = src[x * linesize] * coeff[0][0];
  98. double sum_h = src[x * linesize] * coeff[1][0];
  99. for (i = 1; i <= 4; i++) {
  100. const double s = src[avpriv_mirror(x - i, w - 1) * linesize]
  101. + src[avpriv_mirror(x + i, w - 1) * linesize];
  102. sum_l += coeff[0][i] * s;
  103. sum_h += coeff[1][i] * s;
  104. }
  105. dst_l[x * linesize] = sum_l;
  106. dst_h[x * linesize] = sum_h;
  107. }
  108. }
  109. static inline void compose(float *dst, const float *src_l, const float *src_h,
  110. int linesize, int w)
  111. {
  112. int x, i;
  113. for (x = 0; x < w; x++) {
  114. double sum_l = src_l[x * linesize] * icoeff[0][0];
  115. double sum_h = src_h[x * linesize] * icoeff[1][0];
  116. for (i = 1; i <= 4; i++) {
  117. const int x0 = avpriv_mirror(x - i, w - 1) * linesize;
  118. const int x1 = avpriv_mirror(x + i, w - 1) * linesize;
  119. sum_l += icoeff[0][i] * (src_l[x0] + src_l[x1]);
  120. sum_h += icoeff[1][i] * (src_h[x0] + src_h[x1]);
  121. }
  122. dst[x * linesize] = (sum_l + sum_h) * 0.5;
  123. }
  124. }
  125. static inline void decompose2D(float *dst_l, float *dst_h, const float *src,
  126. int xlinesize, int ylinesize,
  127. int step, int w, int h)
  128. {
  129. int y, x;
  130. for (y = 0; y < h; y++)
  131. for (x = 0; x < step; x++)
  132. decompose(dst_l + ylinesize*y + xlinesize*x,
  133. dst_h + ylinesize*y + xlinesize*x,
  134. src + ylinesize*y + xlinesize*x,
  135. step * xlinesize, (w - x + step - 1) / step);
  136. }
  137. static inline void compose2D(float *dst, const float *src_l, const float *src_h,
  138. int xlinesize, int ylinesize,
  139. int step, int w, int h)
  140. {
  141. int y, x;
  142. for (y = 0; y < h; y++)
  143. for (x = 0; x < step; x++)
  144. compose(dst + ylinesize*y + xlinesize*x,
  145. src_l + ylinesize*y + xlinesize*x,
  146. src_h + ylinesize*y + xlinesize*x,
  147. step * xlinesize, (w - x + step - 1) / step);
  148. }
  149. static void decompose2D2(float *dst[4], float *src, float *temp[2],
  150. int linesize, int step, int w, int h)
  151. {
  152. decompose2D(temp[0], temp[1], src, 1, linesize, step, w, h);
  153. decompose2D( dst[0], dst[1], temp[0], linesize, 1, step, h, w);
  154. decompose2D( dst[2], dst[3], temp[1], linesize, 1, step, h, w);
  155. }
  156. static void compose2D2(float *dst, float *src[4], float *temp[2],
  157. int linesize, int step, int w, int h)
  158. {
  159. compose2D(temp[0], src[0], src[1], linesize, 1, step, h, w);
  160. compose2D(temp[1], src[2], src[3], linesize, 1, step, h, w);
  161. compose2D(dst, temp[0], temp[1], 1, linesize, step, w, h);
  162. }
  163. static void filter(OWDenoiseContext *s,
  164. uint8_t *dst, int dst_linesize,
  165. const uint8_t *src, int src_linesize,
  166. int width, int height, double strength)
  167. {
  168. int x, y, i, j, depth = s->depth;
  169. while (1<<depth > width || 1<<depth > height)
  170. depth--;
  171. for (y = 0; y < height; y++)
  172. for(x = 0; x < width; x++)
  173. s->plane[0][0][y*s->linesize + x] = src[y*src_linesize + x];
  174. for (i = 0; i < depth; i++)
  175. decompose2D2(s->plane[i + 1], s->plane[i][0], s->plane[0] + 1, s->linesize, 1<<i, width, height);
  176. for (i = 0; i < depth; i++) {
  177. for (j = 1; j < 4; j++) {
  178. for (y = 0; y < height; y++) {
  179. for (x = 0; x < width; x++) {
  180. double v = s->plane[i + 1][j][y*s->linesize + x];
  181. if (v > strength) v -= strength;
  182. else if (v < -strength) v += strength;
  183. else v = 0;
  184. s->plane[i + 1][j][x + y*s->linesize] = v;
  185. }
  186. }
  187. }
  188. }
  189. for (i = depth-1; i >= 0; i--)
  190. compose2D2(s->plane[i][0], s->plane[i + 1], s->plane[0] + 1, s->linesize, 1<<i, width, height);
  191. for (y = 0; y < height; y++) {
  192. for (x = 0; x < width; x++) {
  193. i = s->plane[0][0][y*s->linesize + x] + dither[x&7][y&7]*(1.0/64) + 1.0/128; // yes the rounding is insane but optimal :)
  194. if ((unsigned)i > 255U) i = ~(i >> 31);
  195. dst[y*dst_linesize + x] = i;
  196. }
  197. }
  198. }
  199. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  200. {
  201. int direct = 0;
  202. AVFilterContext *ctx = inlink->dst;
  203. OWDenoiseContext *s = ctx->priv;
  204. AVFilterLink *outlink = ctx->outputs[0];
  205. AVFrame *out;
  206. const int cw = FF_CEIL_RSHIFT(inlink->w, s->hsub);
  207. const int ch = FF_CEIL_RSHIFT(inlink->h, s->vsub);
  208. if (av_frame_is_writable(in)) {
  209. direct = 1;
  210. out = in;
  211. } else {
  212. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  213. if (!out) {
  214. av_frame_free(&in);
  215. return AVERROR(ENOMEM);
  216. }
  217. av_frame_copy_props(out, in);
  218. }
  219. filter(s, out->data[0], out->linesize[0], in->data[0], in->linesize[0], inlink->w, inlink->h, s->luma_strength);
  220. filter(s, out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, s->chroma_strength);
  221. filter(s, out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, s->chroma_strength);
  222. if (!direct) {
  223. if (in->data[3])
  224. av_image_copy_plane(out->data[3], out->linesize[3],
  225. in ->data[3], in ->linesize[3],
  226. inlink->w, inlink->h);
  227. av_frame_free(&in);
  228. }
  229. return ff_filter_frame(outlink, out);
  230. }
  231. static int query_formats(AVFilterContext *ctx)
  232. {
  233. static const enum AVPixelFormat pix_fmts[] = {
  234. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  235. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  236. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  237. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P,
  238. AV_PIX_FMT_YUVA420P,
  239. AV_PIX_FMT_NONE
  240. };
  241. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  242. return 0;
  243. }
  244. static int config_input(AVFilterLink *inlink)
  245. {
  246. int i, j;
  247. OWDenoiseContext *s = inlink->dst->priv;
  248. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  249. const int h = FFALIGN(inlink->h, 16);
  250. s->hsub = desc->log2_chroma_w;
  251. s->vsub = desc->log2_chroma_h;
  252. s->linesize = FFALIGN(inlink->w, 16);
  253. for (j = 0; j < 4; j++) {
  254. for (i = 0; i <= s->depth; i++) {
  255. s->plane[i][j] = av_malloc_array(s->linesize, h * sizeof(s->plane[0][0][0]));
  256. if (!s->plane[i][j])
  257. return AVERROR(ENOMEM);
  258. }
  259. }
  260. return 0;
  261. }
  262. static av_cold void uninit(AVFilterContext *ctx)
  263. {
  264. int i, j;
  265. OWDenoiseContext *s = ctx->priv;
  266. for (j = 0; j < 4; j++)
  267. for (i = 0; i <= s->depth; i++)
  268. av_freep(&s->plane[i][j]);
  269. }
  270. static const AVFilterPad owdenoise_inputs[] = {
  271. {
  272. .name = "default",
  273. .type = AVMEDIA_TYPE_VIDEO,
  274. .filter_frame = filter_frame,
  275. .config_props = config_input,
  276. },
  277. { NULL }
  278. };
  279. static const AVFilterPad owdenoise_outputs[] = {
  280. {
  281. .name = "default",
  282. .type = AVMEDIA_TYPE_VIDEO,
  283. },
  284. { NULL }
  285. };
  286. AVFilter ff_vf_owdenoise = {
  287. .name = "owdenoise",
  288. .description = NULL_IF_CONFIG_SMALL("Denoise using wavelets."),
  289. .priv_size = sizeof(OWDenoiseContext),
  290. .uninit = uninit,
  291. .query_formats = query_formats,
  292. .inputs = owdenoise_inputs,
  293. .outputs = owdenoise_outputs,
  294. .priv_class = &owdenoise_class,
  295. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  296. };