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.

584 lines
19KB

  1. /*
  2. * Copyright (c) 2003 LeFunGus, lefungus@altern.org
  3. *
  4. * This file is part of FFmpeg
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <float.h>
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/attributes.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/opt.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. typedef struct VagueDenoiserContext {
  32. const AVClass *class;
  33. float threshold;
  34. float percent;
  35. int method;
  36. int nsteps;
  37. int planes;
  38. int depth;
  39. int peak;
  40. int nb_planes;
  41. int planeheight[4];
  42. int planewidth[4];
  43. float *block;
  44. float *in;
  45. float *out;
  46. float *tmp;
  47. int hlowsize[4][32];
  48. int hhighsize[4][32];
  49. int vlowsize[4][32];
  50. int vhighsize[4][32];
  51. void (*thresholding)(float *block, const int width, const int height,
  52. const int stride, const float threshold,
  53. const float percent, const int nsteps);
  54. } VagueDenoiserContext;
  55. #define OFFSET(x) offsetof(VagueDenoiserContext, x)
  56. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  57. static const AVOption vaguedenoiser_options[] = {
  58. { "threshold", "set filtering strength", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl=2.}, 0,DBL_MAX, FLAGS },
  59. { "method", "set filtering method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=2 }, 0, 2, FLAGS, "method" },
  60. { "hard", "hard thresholding", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "method" },
  61. { "soft", "soft thresholding", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "method" },
  62. { "garrote", "garotte thresholding", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "method" },
  63. { "nsteps", "set number of steps", OFFSET(nsteps), AV_OPT_TYPE_INT, {.i64=6 }, 1, 32, FLAGS },
  64. { "percent", "set percent of full denoising", OFFSET(percent),AV_OPT_TYPE_FLOAT, {.dbl=85}, 0,100, FLAGS },
  65. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15 }, 0, 15, FLAGS },
  66. { NULL }
  67. };
  68. AVFILTER_DEFINE_CLASS(vaguedenoiser);
  69. #define NPAD 10
  70. static const float analysis_low[9] = {
  71. 0.037828455506995f, -0.023849465019380f, -0.110624404418423f, 0.377402855612654f,
  72. 0.852698679009403f, 0.377402855612654f, -0.110624404418423f, -0.023849465019380f, 0.037828455506995f
  73. };
  74. static const float analysis_high[7] = {
  75. -0.064538882628938f, 0.040689417609558f, 0.418092273222212f, -0.788485616405664f,
  76. 0.418092273222212f, 0.040689417609558f, -0.064538882628938f
  77. };
  78. static const float synthesis_low[7] = {
  79. -0.064538882628938f, -0.040689417609558f, 0.418092273222212f, 0.788485616405664f,
  80. 0.418092273222212f, -0.040689417609558f, -0.064538882628938f
  81. };
  82. static const float synthesis_high[9] = {
  83. -0.037828455506995f, -0.023849465019380f, 0.110624404418423f, 0.377402855612654f,
  84. -0.852698679009403f, 0.377402855612654f, 0.110624404418423f, -0.023849465019380f, -0.037828455506995f
  85. };
  86. static int query_formats(AVFilterContext *ctx)
  87. {
  88. static const enum AVPixelFormat pix_fmts[] = {
  89. AV_PIX_FMT_GRAY8,
  90. AV_PIX_FMT_GRAY16,
  91. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  92. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  93. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  94. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  95. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  96. AV_PIX_FMT_YUVJ411P,
  97. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  98. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  99. AV_PIX_FMT_YUV440P10,
  100. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  101. AV_PIX_FMT_YUV440P12,
  102. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  103. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  104. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  105. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  106. AV_PIX_FMT_NONE
  107. };
  108. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  109. if (!fmts_list)
  110. return AVERROR(ENOMEM);
  111. return ff_set_common_formats(ctx, fmts_list);
  112. }
  113. static int config_input(AVFilterLink *inlink)
  114. {
  115. VagueDenoiserContext *s = inlink->dst->priv;
  116. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  117. int p, i, nsteps_width, nsteps_height, nsteps_max;
  118. s->depth = desc->comp[0].depth;
  119. s->nb_planes = desc->nb_components;
  120. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  121. s->planeheight[0] = s->planeheight[3] = inlink->h;
  122. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  123. s->planewidth[0] = s->planewidth[3] = inlink->w;
  124. s->block = av_malloc_array(inlink->w * inlink->h, sizeof(*s->block));
  125. s->in = av_malloc_array(32 + FFMAX(inlink->w, inlink->h), sizeof(*s->in));
  126. s->out = av_malloc_array(32 + FFMAX(inlink->w, inlink->h), sizeof(*s->out));
  127. s->tmp = av_malloc_array(32 + FFMAX(inlink->w, inlink->h), sizeof(*s->tmp));
  128. if (!s->block || !s->in || !s->out || !s->tmp)
  129. return AVERROR(ENOMEM);
  130. s->threshold *= 1 << (s->depth - 8);
  131. s->peak = (1 << s->depth) - 1;
  132. nsteps_width = ((s->planes & 2 || s->planes & 4) && s->nb_planes > 1) ? s->planewidth[1] : s->planewidth[0];
  133. nsteps_height = ((s->planes & 2 || s->planes & 4) && s->nb_planes > 1) ? s->planeheight[1] : s->planeheight[0];
  134. for (nsteps_max = 1; nsteps_max < 15; nsteps_max++) {
  135. if (pow(2, nsteps_max) >= nsteps_width || pow(2, nsteps_max) >= nsteps_height)
  136. break;
  137. }
  138. s->nsteps = FFMIN(s->nsteps, nsteps_max - 2);
  139. for (p = 0; p < 4; p++) {
  140. s->hlowsize[p][0] = (s->planewidth[p] + 1) >> 1;
  141. s->hhighsize[p][0] = s->planewidth[p] >> 1;
  142. s->vlowsize[p][0] = (s->planeheight[p] + 1) >> 1;
  143. s->vhighsize[p][0] = s->planeheight[p] >> 1;
  144. for (i = 1; i < s->nsteps; i++) {
  145. s->hlowsize[p][i] = (s->hlowsize[p][i - 1] + 1) >> 1;
  146. s->hhighsize[p][i] = s->hlowsize[p][i - 1] >> 1;
  147. s->vlowsize[p][i] = (s->vlowsize[p][i - 1] + 1) >> 1;
  148. s->vhighsize[p][i] = s->vlowsize[p][i - 1] >> 1;
  149. }
  150. }
  151. return 0;
  152. }
  153. static inline void copy(const float *p1, float *p2, const int length)
  154. {
  155. memcpy(p2, p1, length * sizeof(float));
  156. }
  157. static inline void copyv(const float *p1, const int stride1, float *p2, const int length)
  158. {
  159. int i;
  160. for (i = 0; i < length; i++) {
  161. p2[i] = *p1;
  162. p1 += stride1;
  163. }
  164. }
  165. static inline void copyh(const float *p1, float *p2, const int stride2, const int length)
  166. {
  167. int i;
  168. for (i = 0; i < length; i++) {
  169. *p2 = p1[i];
  170. p2 += stride2;
  171. }
  172. }
  173. // Do symmetric extension of data using prescribed symmetries
  174. // Original values are in output[npad] through output[npad+size-1]
  175. // New values will be placed in output[0] through output[npad] and in output[npad+size] through output[2*npad+size-1] (note: end values may not be filled in)
  176. // extension at left bdry is ... 3 2 1 0 | 0 1 2 3 ...
  177. // same for right boundary
  178. // if right_ext=1 then ... 3 2 1 0 | 1 2 3
  179. static void symmetric_extension(float *output, const int size, const int left_ext, const int right_ext)
  180. {
  181. int first = NPAD;
  182. int last = NPAD - 1 + size;
  183. const int originalLast = last;
  184. int i, nextend, idx;
  185. if (left_ext == 2)
  186. output[--first] = output[NPAD];
  187. if (right_ext == 2)
  188. output[++last] = output[originalLast];
  189. // extend left end
  190. nextend = first;
  191. for (i = 0; i < nextend; i++)
  192. output[--first] = output[NPAD + 1 + i];
  193. idx = NPAD + NPAD - 1 + size;
  194. // extend right end
  195. nextend = idx - last;
  196. for (i = 0; i < nextend; i++)
  197. output[++last] = output[originalLast - 1 - i];
  198. }
  199. static void transform_step(float *input, float *output, const int size, const int low_size, VagueDenoiserContext *s)
  200. {
  201. int i;
  202. symmetric_extension(input, size, 1, 1);
  203. for (i = NPAD; i < NPAD + low_size; i++) {
  204. const float a = input[2 * i - 14] * analysis_low[0];
  205. const float b = input[2 * i - 13] * analysis_low[1];
  206. const float c = input[2 * i - 12] * analysis_low[2];
  207. const float d = input[2 * i - 11] * analysis_low[3];
  208. const float e = input[2 * i - 10] * analysis_low[4];
  209. const float f = input[2 * i - 9] * analysis_low[3];
  210. const float g = input[2 * i - 8] * analysis_low[2];
  211. const float h = input[2 * i - 7] * analysis_low[1];
  212. const float k = input[2 * i - 6] * analysis_low[0];
  213. output[i] = a + b + c + d + e + f + g + h + k;
  214. }
  215. for (i = NPAD; i < NPAD + low_size; i++) {
  216. const float a = input[2 * i - 12] * analysis_high[0];
  217. const float b = input[2 * i - 11] * analysis_high[1];
  218. const float c = input[2 * i - 10] * analysis_high[2];
  219. const float d = input[2 * i - 9] * analysis_high[3];
  220. const float e = input[2 * i - 8] * analysis_high[2];
  221. const float f = input[2 * i - 7] * analysis_high[1];
  222. const float g = input[2 * i - 6] * analysis_high[0];
  223. output[i + low_size] = a + b + c + d + e + f + g;
  224. }
  225. }
  226. static void invert_step(const float *input, float *output, float *temp, const int size, VagueDenoiserContext *s)
  227. {
  228. const int low_size = (size + 1) >> 1;
  229. const int high_size = size >> 1;
  230. int left_ext = 1, right_ext, i;
  231. int findex;
  232. memcpy(temp + NPAD, input + NPAD, low_size * sizeof(float));
  233. right_ext = (size % 2 == 0) ? 2 : 1;
  234. symmetric_extension(temp, low_size, left_ext, right_ext);
  235. memset(output, 0, (NPAD + NPAD + size) * sizeof(float));
  236. findex = (size + 2) >> 1;
  237. for (i = 9; i < findex + 11; i++) {
  238. const float a = temp[i] * synthesis_low[0];
  239. const float b = temp[i] * synthesis_low[1];
  240. const float c = temp[i] * synthesis_low[2];
  241. const float d = temp[i] * synthesis_low[3];
  242. output[2 * i - 13] += a;
  243. output[2 * i - 12] += b;
  244. output[2 * i - 11] += c;
  245. output[2 * i - 10] += d;
  246. output[2 * i - 9] += c;
  247. output[2 * i - 8] += b;
  248. output[2 * i - 7] += a;
  249. }
  250. memcpy(temp + NPAD, input + NPAD + low_size, high_size * sizeof(float));
  251. left_ext = 2;
  252. right_ext = (size % 2 == 0) ? 1 : 2;
  253. symmetric_extension(temp, high_size, left_ext, right_ext);
  254. for (i = 8; i < findex + 11; i++) {
  255. const float a = temp[i] * synthesis_high[0];
  256. const float b = temp[i] * synthesis_high[1];
  257. const float c = temp[i] * synthesis_high[2];
  258. const float d = temp[i] * synthesis_high[3];
  259. const float e = temp[i] * synthesis_high[4];
  260. output[2 * i - 13] += a;
  261. output[2 * i - 12] += b;
  262. output[2 * i - 11] += c;
  263. output[2 * i - 10] += d;
  264. output[2 * i - 9] += e;
  265. output[2 * i - 8] += d;
  266. output[2 * i - 7] += c;
  267. output[2 * i - 6] += b;
  268. output[2 * i - 5] += a;
  269. }
  270. }
  271. static void hard_thresholding(float *block, const int width, const int height,
  272. const int stride, const float threshold,
  273. const float percent, const int unused)
  274. {
  275. const float frac = 1.f - percent * 0.01f;
  276. int y, x;
  277. for (y = 0; y < height; y++) {
  278. for (x = 0; x < width; x++) {
  279. if (FFABS(block[x]) <= threshold)
  280. block[x] *= frac;
  281. }
  282. block += stride;
  283. }
  284. }
  285. static void soft_thresholding(float *block, const int width, const int height, const int stride,
  286. const float threshold, const float percent, const int nsteps)
  287. {
  288. const float frac = 1.f - percent * 0.01f;
  289. const float shift = threshold * 0.01f * percent;
  290. int w = width;
  291. int h = height;
  292. int y, x, l;
  293. for (l = 0; l < nsteps; l++) {
  294. w = (w + 1) >> 1;
  295. h = (h + 1) >> 1;
  296. }
  297. for (y = 0; y < height; y++) {
  298. const int x0 = (y < h) ? w : 0;
  299. for (x = x0; x < width; x++) {
  300. const float temp = FFABS(block[x]);
  301. if (temp <= threshold)
  302. block[x] *= frac;
  303. else
  304. block[x] = (block[x] < 0.f ? -1.f : (block[x] > 0.f ? 1.f : 0.f)) * (temp - shift);
  305. }
  306. block += stride;
  307. }
  308. }
  309. static void qian_thresholding(float *block, const int width, const int height,
  310. const int stride, const float threshold,
  311. const float percent, const int unused)
  312. {
  313. const float percent01 = percent * 0.01f;
  314. const float tr2 = threshold * threshold * percent01;
  315. const float frac = 1.f - percent01;
  316. int y, x;
  317. for (y = 0; y < height; y++) {
  318. for (x = 0; x < width; x++) {
  319. const float temp = FFABS(block[x]);
  320. if (temp <= threshold) {
  321. block[x] *= frac;
  322. } else {
  323. const float tp2 = temp * temp;
  324. block[x] *= (tp2 - tr2) / tp2;
  325. }
  326. }
  327. block += stride;
  328. }
  329. }
  330. static void filter(VagueDenoiserContext *s, AVFrame *in, AVFrame *out)
  331. {
  332. int p, y, x, i, j;
  333. for (p = 0; p < s->nb_planes; p++) {
  334. const int height = s->planeheight[p];
  335. const int width = s->planewidth[p];
  336. const uint8_t *srcp8 = in->data[p];
  337. const uint16_t *srcp16 = (const uint16_t *)in->data[p];
  338. uint8_t *dstp8 = out->data[p];
  339. uint16_t *dstp16 = (uint16_t *)out->data[p];
  340. float *output = s->block;
  341. int h_low_size0 = width;
  342. int v_low_size0 = height;
  343. int nsteps_transform = s->nsteps;
  344. int nsteps_invert = s->nsteps;
  345. const float *input = s->block;
  346. if (!((1 << p) & s->planes)) {
  347. av_image_copy_plane(out->data[p], out->linesize[p], in->data[p], in->linesize[p],
  348. s->planewidth[p], s->planeheight[p]);
  349. continue;
  350. }
  351. if (s->depth <= 8) {
  352. for (y = 0; y < height; y++) {
  353. for (x = 0; x < width; x++)
  354. output[x] = srcp8[x];
  355. srcp8 += in->linesize[p];
  356. output += width;
  357. }
  358. } else {
  359. for (y = 0; y < height; y++) {
  360. for (x = 0; x < width; x++)
  361. output[x] = srcp16[x];
  362. srcp16 += in->linesize[p] / 2;
  363. output += width;
  364. }
  365. }
  366. while (nsteps_transform--) {
  367. int low_size = (h_low_size0 + 1) >> 1;
  368. float *input = s->block;
  369. for (j = 0; j < v_low_size0; j++) {
  370. copy(input, s->in + NPAD, h_low_size0);
  371. transform_step(s->in, s->out, h_low_size0, low_size, s);
  372. copy(s->out + NPAD, input, h_low_size0);
  373. input += width;
  374. }
  375. low_size = (v_low_size0 + 1) >> 1;
  376. input = s->block;
  377. for (j = 0; j < h_low_size0; j++) {
  378. copyv(input, width, s->in + NPAD, v_low_size0);
  379. transform_step(s->in, s->out, v_low_size0, low_size, s);
  380. copyh(s->out + NPAD, input, width, v_low_size0);
  381. input++;
  382. }
  383. h_low_size0 = (h_low_size0 + 1) >> 1;
  384. v_low_size0 = (v_low_size0 + 1) >> 1;
  385. }
  386. s->thresholding(s->block, width, height, width, s->threshold, s->percent, s->nsteps);
  387. while (nsteps_invert--) {
  388. const int idx = s->vlowsize[p][nsteps_invert] + s->vhighsize[p][nsteps_invert];
  389. const int idx2 = s->hlowsize[p][nsteps_invert] + s->hhighsize[p][nsteps_invert];
  390. float * idx3 = s->block;
  391. for (i = 0; i < idx2; i++) {
  392. copyv(idx3, width, s->in + NPAD, idx);
  393. invert_step(s->in, s->out, s->tmp, idx, s);
  394. copyh(s->out + NPAD, idx3, width, idx);
  395. idx3++;
  396. }
  397. idx3 = s->block;
  398. for (i = 0; i < idx; i++) {
  399. copy(idx3, s->in + NPAD, idx2);
  400. invert_step(s->in, s->out, s->tmp, idx2, s);
  401. copy(s->out + NPAD, idx3, idx2);
  402. idx3 += width;
  403. }
  404. }
  405. if (s->depth <= 8) {
  406. for (y = 0; y < height; y++) {
  407. for (x = 0; x < width; x++)
  408. dstp8[x] = av_clip_uint8(input[x] + 0.5f);
  409. input += width;
  410. dstp8 += out->linesize[p];
  411. }
  412. } else {
  413. for (y = 0; y < height; y++) {
  414. for (x = 0; x < width; x++)
  415. dstp16[x] = av_clip(input[x] + 0.5f, 0, s->peak);
  416. input += width;
  417. dstp16 += out->linesize[p] / 2;
  418. }
  419. }
  420. }
  421. }
  422. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  423. {
  424. AVFilterContext *ctx = inlink->dst;
  425. VagueDenoiserContext *s = ctx->priv;
  426. AVFilterLink *outlink = ctx->outputs[0];
  427. AVFrame *out;
  428. int direct = av_frame_is_writable(in);
  429. if (direct) {
  430. out = in;
  431. } else {
  432. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  433. if (!out) {
  434. av_frame_free(&in);
  435. return AVERROR(ENOMEM);
  436. }
  437. av_frame_copy_props(out, in);
  438. }
  439. filter(s, in, out);
  440. if (!direct)
  441. av_frame_free(&in);
  442. return ff_filter_frame(outlink, out);
  443. }
  444. static av_cold int init(AVFilterContext *ctx)
  445. {
  446. VagueDenoiserContext *s = ctx->priv;
  447. switch (s->method) {
  448. case 0:
  449. s->thresholding = hard_thresholding;
  450. break;
  451. case 1:
  452. s->thresholding = soft_thresholding;
  453. break;
  454. case 2:
  455. s->thresholding = qian_thresholding;
  456. break;
  457. }
  458. return 0;
  459. }
  460. static av_cold void uninit(AVFilterContext *ctx)
  461. {
  462. VagueDenoiserContext *s = ctx->priv;
  463. av_freep(&s->block);
  464. av_freep(&s->in);
  465. av_freep(&s->out);
  466. av_freep(&s->tmp);
  467. }
  468. static const AVFilterPad vaguedenoiser_inputs[] = {
  469. {
  470. .name = "default",
  471. .type = AVMEDIA_TYPE_VIDEO,
  472. .config_props = config_input,
  473. .filter_frame = filter_frame,
  474. },
  475. { NULL }
  476. };
  477. static const AVFilterPad vaguedenoiser_outputs[] = {
  478. {
  479. .name = "default",
  480. .type = AVMEDIA_TYPE_VIDEO
  481. },
  482. { NULL }
  483. };
  484. AVFilter ff_vf_vaguedenoiser = {
  485. .name = "vaguedenoiser",
  486. .description = NULL_IF_CONFIG_SMALL("Apply a Wavelet based Denoiser."),
  487. .priv_size = sizeof(VagueDenoiserContext),
  488. .priv_class = &vaguedenoiser_class,
  489. .init = init,
  490. .uninit = uninit,
  491. .query_formats = query_formats,
  492. .inputs = vaguedenoiser_inputs,
  493. .outputs = vaguedenoiser_outputs,
  494. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  495. };