vf_owdenoise.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 OWDenoiseContext {
  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. int pixel_depth;
  43. } OWDenoiseContext;
  44. #define OFFSET(x) offsetof(OWDenoiseContext, x)
  45. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  46. static const AVOption owdenoise_options[] = {
  47. { "depth", "set depth", OFFSET(depth), AV_OPT_TYPE_INT, {.i64 = 8}, 8, 16, FLAGS },
  48. { "luma_strength", "set luma strength", OFFSET(luma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
  49. { "ls", "set luma strength", OFFSET(luma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
  50. { "chroma_strength", "set chroma strength", OFFSET(chroma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
  51. { "cs", "set chroma strength", OFFSET(chroma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
  52. { NULL }
  53. };
  54. AVFILTER_DEFINE_CLASS(owdenoise);
  55. DECLARE_ALIGNED(8, static const uint8_t, dither)[8][8] = {
  56. { 0, 48, 12, 60, 3, 51, 15, 63 },
  57. { 32, 16, 44, 28, 35, 19, 47, 31 },
  58. { 8, 56, 4, 52, 11, 59, 7, 55 },
  59. { 40, 24, 36, 20, 43, 27, 39, 23 },
  60. { 2, 50, 14, 62, 1, 49, 13, 61 },
  61. { 34, 18, 46, 30, 33, 17, 45, 29 },
  62. { 10, 58, 6, 54, 9, 57, 5, 53 },
  63. { 42, 26, 38, 22, 41, 25, 37, 21 },
  64. };
  65. static const double coeff[2][5] = {
  66. {
  67. 0.6029490182363579 * M_SQRT2,
  68. 0.2668641184428723 * M_SQRT2,
  69. -0.07822326652898785 * M_SQRT2,
  70. -0.01686411844287495 * M_SQRT2,
  71. 0.02674875741080976 * M_SQRT2,
  72. },{
  73. 1.115087052456994 / M_SQRT2,
  74. -0.5912717631142470 / M_SQRT2,
  75. -0.05754352622849957 / M_SQRT2,
  76. 0.09127176311424948 / M_SQRT2,
  77. }
  78. };
  79. static const double icoeff[2][5] = {
  80. {
  81. 1.115087052456994 / M_SQRT2,
  82. 0.5912717631142470 / M_SQRT2,
  83. -0.05754352622849957 / M_SQRT2,
  84. -0.09127176311424948 / M_SQRT2,
  85. },{
  86. 0.6029490182363579 * M_SQRT2,
  87. -0.2668641184428723 * M_SQRT2,
  88. -0.07822326652898785 * M_SQRT2,
  89. 0.01686411844287495 * M_SQRT2,
  90. 0.02674875741080976 * M_SQRT2,
  91. }
  92. };
  93. static inline void decompose(float *dst_l, float *dst_h, const float *src,
  94. int linesize, int w)
  95. {
  96. int x, i;
  97. for (x = 0; x < w; x++) {
  98. double sum_l = src[x * linesize] * coeff[0][0];
  99. double sum_h = src[x * linesize] * coeff[1][0];
  100. for (i = 1; i <= 4; i++) {
  101. const double s = src[avpriv_mirror(x - i, w - 1) * linesize]
  102. + src[avpriv_mirror(x + i, w - 1) * linesize];
  103. sum_l += coeff[0][i] * s;
  104. sum_h += coeff[1][i] * s;
  105. }
  106. dst_l[x * linesize] = sum_l;
  107. dst_h[x * linesize] = sum_h;
  108. }
  109. }
  110. static inline void compose(float *dst, const float *src_l, const float *src_h,
  111. int linesize, int w)
  112. {
  113. int x, i;
  114. for (x = 0; x < w; x++) {
  115. double sum_l = src_l[x * linesize] * icoeff[0][0];
  116. double sum_h = src_h[x * linesize] * icoeff[1][0];
  117. for (i = 1; i <= 4; i++) {
  118. const int x0 = avpriv_mirror(x - i, w - 1) * linesize;
  119. const int x1 = avpriv_mirror(x + i, w - 1) * linesize;
  120. sum_l += icoeff[0][i] * (src_l[x0] + src_l[x1]);
  121. sum_h += icoeff[1][i] * (src_h[x0] + src_h[x1]);
  122. }
  123. dst[x * linesize] = (sum_l + sum_h) * 0.5;
  124. }
  125. }
  126. static inline void decompose2D(float *dst_l, float *dst_h, const float *src,
  127. int xlinesize, int ylinesize,
  128. int step, int w, int h)
  129. {
  130. int y, x;
  131. for (y = 0; y < h; y++)
  132. for (x = 0; x < step; x++)
  133. decompose(dst_l + ylinesize*y + xlinesize*x,
  134. dst_h + ylinesize*y + xlinesize*x,
  135. src + ylinesize*y + xlinesize*x,
  136. step * xlinesize, (w - x + step - 1) / step);
  137. }
  138. static inline void compose2D(float *dst, const float *src_l, const float *src_h,
  139. int xlinesize, int ylinesize,
  140. int step, int w, int h)
  141. {
  142. int y, x;
  143. for (y = 0; y < h; y++)
  144. for (x = 0; x < step; x++)
  145. compose(dst + ylinesize*y + xlinesize*x,
  146. src_l + ylinesize*y + xlinesize*x,
  147. src_h + ylinesize*y + xlinesize*x,
  148. step * xlinesize, (w - x + step - 1) / step);
  149. }
  150. static void decompose2D2(float *dst[4], float *src, float *temp[2],
  151. int linesize, int step, int w, int h)
  152. {
  153. decompose2D(temp[0], temp[1], src, 1, linesize, step, w, h);
  154. decompose2D( dst[0], dst[1], temp[0], linesize, 1, step, h, w);
  155. decompose2D( dst[2], dst[3], temp[1], linesize, 1, step, h, w);
  156. }
  157. static void compose2D2(float *dst, float *src[4], float *temp[2],
  158. int linesize, int step, int w, int h)
  159. {
  160. compose2D(temp[0], src[0], src[1], linesize, 1, step, h, w);
  161. compose2D(temp[1], src[2], src[3], linesize, 1, step, h, w);
  162. compose2D(dst, temp[0], temp[1], 1, linesize, step, w, h);
  163. }
  164. static void filter(OWDenoiseContext *s,
  165. uint8_t *dst, int dst_linesize,
  166. const uint8_t *src, int src_linesize,
  167. int width, int height, double strength)
  168. {
  169. int x, y, i, j, depth = s->depth;
  170. while (1<<depth > width || 1<<depth > height)
  171. depth--;
  172. if (s->pixel_depth <= 8) {
  173. for (y = 0; y < height; y++)
  174. for(x = 0; x < width; x++)
  175. s->plane[0][0][y*s->linesize + x] = src[y*src_linesize + x];
  176. } else {
  177. const uint16_t *src16 = (const uint16_t *)src;
  178. src_linesize /= 2;
  179. for (y = 0; y < height; y++)
  180. for(x = 0; x < width; x++)
  181. s->plane[0][0][y*s->linesize + x] = src16[y*src_linesize + x];
  182. }
  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. if (s->pixel_depth <= 8) {
  201. for (y = 0; y < height; y++) {
  202. for (x = 0; x < width; x++) {
  203. 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 :)
  204. if ((unsigned)i > 255U) i = ~(i >> 31);
  205. dst[y*dst_linesize + x] = i;
  206. }
  207. }
  208. } else {
  209. uint16_t *dst16 = (uint16_t *)dst;
  210. dst_linesize /= 2;
  211. for (y = 0; y < height; y++) {
  212. for (x = 0; x < width; x++) {
  213. i = s->plane[0][0][y*s->linesize + x];
  214. dst16[y*dst_linesize + x] = i;
  215. }
  216. }
  217. }
  218. }
  219. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  220. {
  221. AVFilterContext *ctx = inlink->dst;
  222. OWDenoiseContext *s = ctx->priv;
  223. AVFilterLink *outlink = ctx->outputs[0];
  224. AVFrame *out;
  225. const int cw = AV_CEIL_RSHIFT(inlink->w, s->hsub);
  226. const int ch = AV_CEIL_RSHIFT(inlink->h, s->vsub);
  227. if (av_frame_is_writable(in)) {
  228. out = in;
  229. if (s->luma_strength > 0)
  230. filter(s, out->data[0], out->linesize[0], in->data[0], in->linesize[0], inlink->w, inlink->h, s->luma_strength);
  231. if (s->chroma_strength > 0) {
  232. filter(s, out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, s->chroma_strength);
  233. filter(s, out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, s->chroma_strength);
  234. }
  235. } else {
  236. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  237. if (!out) {
  238. av_frame_free(&in);
  239. return AVERROR(ENOMEM);
  240. }
  241. av_frame_copy_props(out, in);
  242. if (s->luma_strength > 0) {
  243. filter(s, out->data[0], out->linesize[0], in->data[0], in->linesize[0], inlink->w, inlink->h, s->luma_strength);
  244. } else {
  245. av_image_copy_plane(out->data[0], out->linesize[0], in ->data[0], in ->linesize[0], inlink->w, inlink->h);
  246. }
  247. if (s->chroma_strength > 0) {
  248. filter(s, out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, s->chroma_strength);
  249. filter(s, out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, s->chroma_strength);
  250. } else {
  251. av_image_copy_plane(out->data[1], out->linesize[1], in ->data[1], in ->linesize[1], inlink->w, inlink->h);
  252. av_image_copy_plane(out->data[2], out->linesize[2], in ->data[2], in ->linesize[2], inlink->w, inlink->h);
  253. }
  254. if (in->data[3])
  255. av_image_copy_plane(out->data[3], out->linesize[3],
  256. in ->data[3], in ->linesize[3],
  257. inlink->w, inlink->h);
  258. av_frame_free(&in);
  259. }
  260. return ff_filter_frame(outlink, out);
  261. }
  262. static int query_formats(AVFilterContext *ctx)
  263. {
  264. static const enum AVPixelFormat pix_fmts[] = {
  265. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  266. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  267. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  268. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P,
  269. AV_PIX_FMT_YUVA420P,
  270. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  271. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  272. AV_PIX_FMT_YUV440P10,
  273. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  274. AV_PIX_FMT_YUV440P12,
  275. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  276. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  277. AV_PIX_FMT_NONE
  278. };
  279. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  280. if (!fmts_list)
  281. return AVERROR(ENOMEM);
  282. return ff_set_common_formats(ctx, fmts_list);
  283. }
  284. static int config_input(AVFilterLink *inlink)
  285. {
  286. int i, j;
  287. OWDenoiseContext *s = inlink->dst->priv;
  288. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  289. const int h = FFALIGN(inlink->h, 16);
  290. s->hsub = desc->log2_chroma_w;
  291. s->vsub = desc->log2_chroma_h;
  292. s->pixel_depth = desc->comp[0].depth;
  293. s->linesize = FFALIGN(inlink->w, 16);
  294. for (j = 0; j < 4; j++) {
  295. for (i = 0; i <= s->depth; i++) {
  296. s->plane[i][j] = av_malloc_array(s->linesize, h * sizeof(s->plane[0][0][0]));
  297. if (!s->plane[i][j])
  298. return AVERROR(ENOMEM);
  299. }
  300. }
  301. return 0;
  302. }
  303. static av_cold void uninit(AVFilterContext *ctx)
  304. {
  305. int i, j;
  306. OWDenoiseContext *s = ctx->priv;
  307. for (j = 0; j < 4; j++)
  308. for (i = 0; i <= s->depth; i++)
  309. av_freep(&s->plane[i][j]);
  310. }
  311. static const AVFilterPad owdenoise_inputs[] = {
  312. {
  313. .name = "default",
  314. .type = AVMEDIA_TYPE_VIDEO,
  315. .filter_frame = filter_frame,
  316. .config_props = config_input,
  317. },
  318. { NULL }
  319. };
  320. static const AVFilterPad owdenoise_outputs[] = {
  321. {
  322. .name = "default",
  323. .type = AVMEDIA_TYPE_VIDEO,
  324. },
  325. { NULL }
  326. };
  327. AVFilter ff_vf_owdenoise = {
  328. .name = "owdenoise",
  329. .description = NULL_IF_CONFIG_SMALL("Denoise using wavelets."),
  330. .priv_size = sizeof(OWDenoiseContext),
  331. .uninit = uninit,
  332. .query_formats = query_formats,
  333. .inputs = owdenoise_inputs,
  334. .outputs = owdenoise_outputs,
  335. .priv_class = &owdenoise_class,
  336. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  337. };