You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
13KB

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