vf_owdenoise.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 int mirror(int x, int w)
  93. {
  94. while ((unsigned)x > (unsigned)w) {
  95. x = -x;
  96. if (x < 0)
  97. x += 2 * w;
  98. }
  99. return x;
  100. }
  101. static inline void decompose(float *dst_l, float *dst_h, const float *src,
  102. int linesize, int w)
  103. {
  104. int x, i;
  105. for (x = 0; x < w; x++) {
  106. double sum_l = src[x * linesize] * coeff[0][0];
  107. double sum_h = src[x * linesize] * coeff[1][0];
  108. for (i = 1; i <= 4; i++) {
  109. const double s = src[mirror(x - i, w - 1) * linesize]
  110. + src[mirror(x + i, w - 1) * linesize];
  111. sum_l += coeff[0][i] * s;
  112. sum_h += coeff[1][i] * s;
  113. }
  114. dst_l[x * linesize] = sum_l;
  115. dst_h[x * linesize] = sum_h;
  116. }
  117. }
  118. static inline void compose(float *dst, const float *src_l, const float *src_h,
  119. int linesize, int w)
  120. {
  121. int x, i;
  122. for (x = 0; x < w; x++) {
  123. double sum_l = src_l[x * linesize] * icoeff[0][0];
  124. double sum_h = src_h[x * linesize] * icoeff[1][0];
  125. for (i = 1; i <= 4; i++) {
  126. const int x0 = mirror(x - i, w - 1) * linesize;
  127. const int x1 = mirror(x + i, w - 1) * linesize;
  128. sum_l += icoeff[0][i] * (src_l[x0] + src_l[x1]);
  129. sum_h += icoeff[1][i] * (src_h[x0] + src_h[x1]);
  130. }
  131. dst[x * linesize] = (sum_l + sum_h) * 0.5;
  132. }
  133. }
  134. static inline void decompose2D(float *dst_l, float *dst_h, const float *src,
  135. int xlinesize, int ylinesize,
  136. int step, int w, int h)
  137. {
  138. int y, x;
  139. for (y = 0; y < h; y++)
  140. for (x = 0; x < step; x++)
  141. decompose(dst_l + ylinesize*y + xlinesize*x,
  142. dst_h + ylinesize*y + xlinesize*x,
  143. src + ylinesize*y + xlinesize*x,
  144. step * xlinesize, (w - x + step - 1) / step);
  145. }
  146. static inline void compose2D(float *dst, const float *src_l, const float *src_h,
  147. int xlinesize, int ylinesize,
  148. int step, int w, int h)
  149. {
  150. int y, x;
  151. for (y = 0; y < h; y++)
  152. for (x = 0; x < step; x++)
  153. compose(dst + ylinesize*y + xlinesize*x,
  154. src_l + ylinesize*y + xlinesize*x,
  155. src_h + ylinesize*y + xlinesize*x,
  156. step * xlinesize, (w - x + step - 1) / step);
  157. }
  158. static void decompose2D2(float *dst[4], float *src, float *temp[2],
  159. int linesize, int step, int w, int h)
  160. {
  161. decompose2D(temp[0], temp[1], src, 1, linesize, step, w, h);
  162. decompose2D( dst[0], dst[1], temp[0], linesize, 1, step, h, w);
  163. decompose2D( dst[2], dst[3], temp[1], linesize, 1, step, h, w);
  164. }
  165. static void compose2D2(float *dst, float *src[4], float *temp[2],
  166. int linesize, int step, int w, int h)
  167. {
  168. compose2D(temp[0], src[0], src[1], linesize, 1, step, h, w);
  169. compose2D(temp[1], src[2], src[3], linesize, 1, step, h, w);
  170. compose2D(dst, temp[0], temp[1], 1, linesize, step, w, h);
  171. }
  172. static void filter(OWDenoiseContext *s,
  173. uint8_t *dst, int dst_linesize,
  174. const uint8_t *src, int src_linesize,
  175. int width, int height, double strength)
  176. {
  177. int x, y, i, j, depth = s->depth;
  178. while (1<<depth > width || 1<<depth > height)
  179. depth--;
  180. for (y = 0; y < height; y++)
  181. for(x = 0; x < width; x++)
  182. s->plane[0][0][y*s->linesize + x] = src[y*src_linesize + x];
  183. for (i = 0; i < depth; i++)
  184. decompose2D2(s->plane[i + 1], s->plane[i][0], s->plane[0] + 1, s->linesize, 1<<i, width, height);
  185. for (i = 0; i < depth; i++) {
  186. for (j = 1; j < 4; j++) {
  187. for (y = 0; y < height; y++) {
  188. for (x = 0; x < width; x++) {
  189. double v = s->plane[i + 1][j][y*s->linesize + x];
  190. if (v > strength) v -= strength;
  191. else if (v < -strength) v += strength;
  192. else v = 0;
  193. s->plane[i + 1][j][x + y*s->linesize] = v;
  194. }
  195. }
  196. }
  197. }
  198. for (i = depth-1; i >= 0; i--)
  199. compose2D2(s->plane[i][0], s->plane[i + 1], s->plane[0] + 1, s->linesize, 1<<i, width, height);
  200. for (y = 0; y < height; y++) {
  201. for (x = 0; x < width; x++) {
  202. 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 :)
  203. if ((unsigned)i > 255U) i = ~(i >> 31);
  204. dst[y*dst_linesize + x] = i;
  205. }
  206. }
  207. }
  208. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  209. {
  210. int direct = 0;
  211. AVFilterContext *ctx = inlink->dst;
  212. OWDenoiseContext *s = ctx->priv;
  213. AVFilterLink *outlink = ctx->outputs[0];
  214. AVFrame *out;
  215. const int cw = FF_CEIL_RSHIFT(inlink->w, s->hsub);
  216. const int ch = FF_CEIL_RSHIFT(inlink->h, s->vsub);
  217. if (av_frame_is_writable(in)) {
  218. direct = 1;
  219. out = in;
  220. } else {
  221. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  222. if (!out) {
  223. av_frame_free(&in);
  224. return AVERROR(ENOMEM);
  225. }
  226. av_frame_copy_props(out, in);
  227. }
  228. filter(s, out->data[0], out->linesize[0], in->data[0], in->linesize[0], inlink->w, inlink->h, s->luma_strength);
  229. filter(s, out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, s->chroma_strength);
  230. filter(s, out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, s->chroma_strength);
  231. if (!direct) {
  232. if (in->data[3])
  233. av_image_copy_plane(out->data[3], out->linesize[3],
  234. in ->data[3], in ->linesize[3],
  235. inlink->w, inlink->h);
  236. av_frame_free(&in);
  237. }
  238. return ff_filter_frame(outlink, out);
  239. }
  240. static int query_formats(AVFilterContext *ctx)
  241. {
  242. static const enum AVPixelFormat pix_fmts[] = {
  243. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  244. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  245. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  246. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P,
  247. AV_PIX_FMT_YUVA420P,
  248. AV_PIX_FMT_NONE
  249. };
  250. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  251. return 0;
  252. }
  253. static int config_input(AVFilterLink *inlink)
  254. {
  255. int i, j;
  256. OWDenoiseContext *s = inlink->dst->priv;
  257. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  258. const int h = FFALIGN(inlink->h, 16);
  259. s->hsub = desc->log2_chroma_w;
  260. s->vsub = desc->log2_chroma_h;
  261. s->linesize = FFALIGN(inlink->w, 16);
  262. for (j = 0; j < 4; j++) {
  263. for (i = 0; i <= s->depth; i++) {
  264. s->plane[i][j] = av_malloc_array(s->linesize, h * sizeof(s->plane[0][0][0]));
  265. if (!s->plane[i][j])
  266. return AVERROR(ENOMEM);
  267. }
  268. }
  269. return 0;
  270. }
  271. static av_cold void uninit(AVFilterContext *ctx)
  272. {
  273. int i, j;
  274. OWDenoiseContext *s = ctx->priv;
  275. for (j = 0; j < 4; j++)
  276. for (i = 0; i <= s->depth; i++)
  277. av_freep(&s->plane[i][j]);
  278. }
  279. static const AVFilterPad owdenoise_inputs[] = {
  280. {
  281. .name = "default",
  282. .type = AVMEDIA_TYPE_VIDEO,
  283. .filter_frame = filter_frame,
  284. .config_props = config_input,
  285. },
  286. { NULL }
  287. };
  288. static const AVFilterPad owdenoise_outputs[] = {
  289. {
  290. .name = "default",
  291. .type = AVMEDIA_TYPE_VIDEO,
  292. },
  293. { NULL }
  294. };
  295. AVFilter ff_vf_owdenoise = {
  296. .name = "owdenoise",
  297. .description = NULL_IF_CONFIG_SMALL("Denoise using wavelets."),
  298. .priv_size = sizeof(OWDenoiseContext),
  299. .uninit = uninit,
  300. .query_formats = query_formats,
  301. .inputs = owdenoise_inputs,
  302. .outputs = owdenoise_outputs,
  303. .priv_class = &owdenoise_class,
  304. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  305. };